1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2017 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"
32 #include "gimple-predict.h"
33 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
36 #include "tree-pretty-print.h"
37 #include "diagnostic-core.h"
39 #include "fold-const.h"
44 #include "gimple-fold.h"
47 #include "gimple-iterator.h"
48 #include "stor-layout.h"
49 #include "print-tree.h"
50 #include "tree-iterator.h"
51 #include "tree-inline.h"
52 #include "langhooks.h"
55 #include "omp-general.h"
57 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
64 #include "stringpool.h"
69 /* Hash set of poisoned variables in a bind expr. */
70 static hash_set
<tree
> *asan_poisoned_variables
= NULL
;
72 enum gimplify_omp_var_data
78 GOVD_FIRSTPRIVATE
= 16,
79 GOVD_LASTPRIVATE
= 32,
83 GOVD_DEBUG_PRIVATE
= 512,
84 GOVD_PRIVATE_OUTER_REF
= 1024,
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY
= 8192,
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER
= 16384,
94 GOVD_MAP_0LEN_ARRAY
= 32768,
96 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
97 GOVD_MAP_ALWAYS_TO
= 65536,
99 /* Flag for shared vars that are or might be stored to in the region. */
100 GOVD_WRITTEN
= 131072,
102 /* Flag for GOVD_MAP, if it is a forced mapping. */
103 GOVD_MAP_FORCE
= 262144,
105 /* Flag for GOVD_MAP: must be present already. */
106 GOVD_MAP_FORCE_PRESENT
= 524288,
108 GOVD_DATA_SHARE_CLASS
= (GOVD_SHARED
| GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
109 | GOVD_LASTPRIVATE
| GOVD_REDUCTION
| GOVD_LINEAR
116 ORT_WORKSHARE
= 0x00,
120 ORT_COMBINED_PARALLEL
= 0x03,
123 ORT_UNTIED_TASK
= 0x05,
126 ORT_COMBINED_TEAMS
= 0x09,
129 ORT_TARGET_DATA
= 0x10,
131 /* Data region with offloading. */
133 ORT_COMBINED_TARGET
= 0x21,
135 /* OpenACC variants. */
136 ORT_ACC
= 0x40, /* A generic OpenACC region. */
137 ORT_ACC_DATA
= ORT_ACC
| ORT_TARGET_DATA
, /* Data construct. */
138 ORT_ACC_PARALLEL
= ORT_ACC
| ORT_TARGET
, /* Parallel construct */
139 ORT_ACC_KERNELS
= ORT_ACC
| ORT_TARGET
| 0x80, /* Kernels construct. */
140 ORT_ACC_HOST_DATA
= ORT_ACC
| ORT_TARGET_DATA
| 0x80, /* Host data. */
142 /* Dummy OpenMP region, used to disable expansion of
143 DECL_VALUE_EXPRs in taskloop pre body. */
147 /* Gimplify hashtable helper. */
149 struct gimplify_hasher
: free_ptr_hash
<elt_t
>
151 static inline hashval_t
hash (const elt_t
*);
152 static inline bool equal (const elt_t
*, const elt_t
*);
157 struct gimplify_ctx
*prev_context
;
159 vec
<gbind
*> bind_expr_stack
;
161 gimple_seq conditional_cleanups
;
165 vec
<tree
> case_labels
;
166 hash_set
<tree
> *live_switch_vars
;
167 /* The formal temporary table. Should this be persistent? */
168 hash_table
<gimplify_hasher
> *temp_htab
;
171 unsigned into_ssa
: 1;
172 unsigned allow_rhs_cond_expr
: 1;
173 unsigned in_cleanup_point_expr
: 1;
174 unsigned keep_stack
: 1;
175 unsigned save_stack
: 1;
176 unsigned in_switch_expr
: 1;
179 struct gimplify_omp_ctx
181 struct gimplify_omp_ctx
*outer_context
;
182 splay_tree variables
;
183 hash_set
<tree
> *privatized_types
;
184 /* Iteration variables in an OMP_FOR. */
185 vec
<tree
> loop_iter_var
;
187 enum omp_clause_default_kind default_kind
;
188 enum omp_region_type region_type
;
191 bool target_map_scalars_firstprivate
;
192 bool target_map_pointers_as_0len_arrays
;
193 bool target_firstprivatize_array_bases
;
196 static struct gimplify_ctx
*gimplify_ctxp
;
197 static struct gimplify_omp_ctx
*gimplify_omp_ctxp
;
199 /* Forward declaration. */
200 static enum gimplify_status
gimplify_compound_expr (tree
*, gimple_seq
*, bool);
201 static hash_map
<tree
, tree
> *oacc_declare_returns
;
202 static enum gimplify_status
gimplify_expr (tree
*, gimple_seq
*, gimple_seq
*,
203 bool (*) (tree
), fallback_t
, bool);
205 /* Shorter alias name for the above function for use in gimplify.c
209 gimplify_seq_add_stmt (gimple_seq
*seq_p
, gimple
*gs
)
211 gimple_seq_add_stmt_without_update (seq_p
, gs
);
214 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
215 NULL, a new sequence is allocated. This function is
216 similar to gimple_seq_add_seq, but does not scan the operands.
217 During gimplification, we need to manipulate statement sequences
218 before the def/use vectors have been constructed. */
221 gimplify_seq_add_seq (gimple_seq
*dst_p
, gimple_seq src
)
223 gimple_stmt_iterator si
;
228 si
= gsi_last (*dst_p
);
229 gsi_insert_seq_after_without_update (&si
, src
, GSI_NEW_STMT
);
233 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
234 and popping gimplify contexts. */
236 static struct gimplify_ctx
*ctx_pool
= NULL
;
238 /* Return a gimplify context struct from the pool. */
240 static inline struct gimplify_ctx
*
243 struct gimplify_ctx
* c
= ctx_pool
;
246 ctx_pool
= c
->prev_context
;
248 c
= XNEW (struct gimplify_ctx
);
250 memset (c
, '\0', sizeof (*c
));
254 /* Put gimplify context C back into the pool. */
257 ctx_free (struct gimplify_ctx
*c
)
259 c
->prev_context
= ctx_pool
;
263 /* Free allocated ctx stack memory. */
266 free_gimplify_stack (void)
268 struct gimplify_ctx
*c
;
270 while ((c
= ctx_pool
))
272 ctx_pool
= c
->prev_context
;
278 /* Set up a context for the gimplifier. */
281 push_gimplify_context (bool in_ssa
, bool rhs_cond_ok
)
283 struct gimplify_ctx
*c
= ctx_alloc ();
285 c
->prev_context
= gimplify_ctxp
;
287 gimplify_ctxp
->into_ssa
= in_ssa
;
288 gimplify_ctxp
->allow_rhs_cond_expr
= rhs_cond_ok
;
291 /* Tear down a context for the gimplifier. If BODY is non-null, then
292 put the temporaries into the outer BIND_EXPR. Otherwise, put them
295 BODY is not a sequence, but the first tuple in a sequence. */
298 pop_gimplify_context (gimple
*body
)
300 struct gimplify_ctx
*c
= gimplify_ctxp
;
303 && (!c
->bind_expr_stack
.exists ()
304 || c
->bind_expr_stack
.is_empty ()));
305 c
->bind_expr_stack
.release ();
306 gimplify_ctxp
= c
->prev_context
;
309 declare_vars (c
->temps
, body
, false);
311 record_vars (c
->temps
);
318 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
321 gimple_push_bind_expr (gbind
*bind_stmt
)
323 gimplify_ctxp
->bind_expr_stack
.reserve (8);
324 gimplify_ctxp
->bind_expr_stack
.safe_push (bind_stmt
);
327 /* Pop the first element off the stack of bindings. */
330 gimple_pop_bind_expr (void)
332 gimplify_ctxp
->bind_expr_stack
.pop ();
335 /* Return the first element of the stack of bindings. */
338 gimple_current_bind_expr (void)
340 return gimplify_ctxp
->bind_expr_stack
.last ();
343 /* Return the stack of bindings created during gimplification. */
346 gimple_bind_expr_stack (void)
348 return gimplify_ctxp
->bind_expr_stack
;
351 /* Return true iff there is a COND_EXPR between us and the innermost
352 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
355 gimple_conditional_context (void)
357 return gimplify_ctxp
->conditions
> 0;
360 /* Note that we've entered a COND_EXPR. */
363 gimple_push_condition (void)
365 #ifdef ENABLE_GIMPLE_CHECKING
366 if (gimplify_ctxp
->conditions
== 0)
367 gcc_assert (gimple_seq_empty_p (gimplify_ctxp
->conditional_cleanups
));
369 ++(gimplify_ctxp
->conditions
);
372 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
373 now, add any conditional cleanups we've seen to the prequeue. */
376 gimple_pop_condition (gimple_seq
*pre_p
)
378 int conds
= --(gimplify_ctxp
->conditions
);
380 gcc_assert (conds
>= 0);
383 gimplify_seq_add_seq (pre_p
, gimplify_ctxp
->conditional_cleanups
);
384 gimplify_ctxp
->conditional_cleanups
= NULL
;
388 /* A stable comparison routine for use with splay trees and DECLs. */
391 splay_tree_compare_decl_uid (splay_tree_key xa
, splay_tree_key xb
)
396 return DECL_UID (a
) - DECL_UID (b
);
399 /* Create a new omp construct that deals with variable remapping. */
401 static struct gimplify_omp_ctx
*
402 new_omp_context (enum omp_region_type region_type
)
404 struct gimplify_omp_ctx
*c
;
406 c
= XCNEW (struct gimplify_omp_ctx
);
407 c
->outer_context
= gimplify_omp_ctxp
;
408 c
->variables
= splay_tree_new (splay_tree_compare_decl_uid
, 0, 0);
409 c
->privatized_types
= new hash_set
<tree
>;
410 c
->location
= input_location
;
411 c
->region_type
= region_type
;
412 if ((region_type
& ORT_TASK
) == 0)
413 c
->default_kind
= OMP_CLAUSE_DEFAULT_SHARED
;
415 c
->default_kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
420 /* Destroy an omp construct that deals with variable remapping. */
423 delete_omp_context (struct gimplify_omp_ctx
*c
)
425 splay_tree_delete (c
->variables
);
426 delete c
->privatized_types
;
427 c
->loop_iter_var
.release ();
431 static void omp_add_variable (struct gimplify_omp_ctx
*, tree
, unsigned int);
432 static bool omp_notice_variable (struct gimplify_omp_ctx
*, tree
, bool);
434 /* Both gimplify the statement T and append it to *SEQ_P. This function
435 behaves exactly as gimplify_stmt, but you don't have to pass T as a
439 gimplify_and_add (tree t
, gimple_seq
*seq_p
)
441 gimplify_stmt (&t
, seq_p
);
444 /* Gimplify statement T into sequence *SEQ_P, and return the first
445 tuple in the sequence of generated tuples for this statement.
446 Return NULL if gimplifying T produced no tuples. */
449 gimplify_and_return_first (tree t
, gimple_seq
*seq_p
)
451 gimple_stmt_iterator last
= gsi_last (*seq_p
);
453 gimplify_and_add (t
, seq_p
);
455 if (!gsi_end_p (last
))
458 return gsi_stmt (last
);
461 return gimple_seq_first_stmt (*seq_p
);
464 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
465 LHS, or for a call argument. */
468 is_gimple_mem_rhs (tree t
)
470 /* If we're dealing with a renamable type, either source or dest must be
471 a renamed variable. */
472 if (is_gimple_reg_type (TREE_TYPE (t
)))
473 return is_gimple_val (t
);
475 return is_gimple_val (t
) || is_gimple_lvalue (t
);
478 /* Return true if T is a CALL_EXPR or an expression that can be
479 assigned to a temporary. Note that this predicate should only be
480 used during gimplification. See the rationale for this in
481 gimplify_modify_expr. */
484 is_gimple_reg_rhs_or_call (tree t
)
486 return (get_gimple_rhs_class (TREE_CODE (t
)) != GIMPLE_INVALID_RHS
487 || TREE_CODE (t
) == CALL_EXPR
);
490 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
491 this predicate should only be used during gimplification. See the
492 rationale for this in gimplify_modify_expr. */
495 is_gimple_mem_rhs_or_call (tree t
)
497 /* If we're dealing with a renamable type, either source or dest must be
498 a renamed variable. */
499 if (is_gimple_reg_type (TREE_TYPE (t
)))
500 return is_gimple_val (t
);
502 return (is_gimple_val (t
)
503 || is_gimple_lvalue (t
)
504 || TREE_CLOBBER_P (t
)
505 || TREE_CODE (t
) == CALL_EXPR
);
508 /* Create a temporary with a name derived from VAL. Subroutine of
509 lookup_tmp_var; nobody else should call this function. */
512 create_tmp_from_val (tree val
)
514 /* Drop all qualifiers and address-space information from the value type. */
515 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (val
));
516 tree var
= create_tmp_var (type
, get_name (val
));
517 if (TREE_CODE (TREE_TYPE (var
)) == COMPLEX_TYPE
518 || TREE_CODE (TREE_TYPE (var
)) == VECTOR_TYPE
)
519 DECL_GIMPLE_REG_P (var
) = 1;
523 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
524 an existing expression temporary. */
527 lookup_tmp_var (tree val
, bool is_formal
)
531 /* If not optimizing, never really reuse a temporary. local-alloc
532 won't allocate any variable that is used in more than one basic
533 block, which means it will go into memory, causing much extra
534 work in reload and final and poorer code generation, outweighing
535 the extra memory allocation here. */
536 if (!optimize
|| !is_formal
|| TREE_SIDE_EFFECTS (val
))
537 ret
= create_tmp_from_val (val
);
544 if (!gimplify_ctxp
->temp_htab
)
545 gimplify_ctxp
->temp_htab
= new hash_table
<gimplify_hasher
> (1000);
546 slot
= gimplify_ctxp
->temp_htab
->find_slot (&elt
, INSERT
);
549 elt_p
= XNEW (elt_t
);
551 elt_p
->temp
= ret
= create_tmp_from_val (val
);
564 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
567 internal_get_tmp_var (tree val
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
568 bool is_formal
, bool allow_ssa
)
572 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
573 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
574 gimplify_expr (&val
, pre_p
, post_p
, is_gimple_reg_rhs_or_call
,
578 && gimplify_ctxp
->into_ssa
579 && is_gimple_reg_type (TREE_TYPE (val
)))
581 t
= make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val
)));
582 if (! gimple_in_ssa_p (cfun
))
584 const char *name
= get_name (val
);
586 SET_SSA_NAME_VAR_OR_IDENTIFIER (t
, create_tmp_var_name (name
));
590 t
= lookup_tmp_var (val
, is_formal
);
592 mod
= build2 (INIT_EXPR
, TREE_TYPE (t
), t
, unshare_expr (val
));
594 SET_EXPR_LOCATION (mod
, EXPR_LOC_OR_LOC (val
, input_location
));
596 /* gimplify_modify_expr might want to reduce this further. */
597 gimplify_and_add (mod
, pre_p
);
603 /* Return a formal temporary variable initialized with VAL. PRE_P is as
604 in gimplify_expr. Only use this function if:
606 1) The value of the unfactored expression represented by VAL will not
607 change between the initialization and use of the temporary, and
608 2) The temporary will not be otherwise modified.
610 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
611 and #2 means it is inappropriate for && temps.
613 For other cases, use get_initialized_tmp_var instead. */
616 get_formal_tmp_var (tree val
, gimple_seq
*pre_p
)
618 return internal_get_tmp_var (val
, pre_p
, NULL
, true, true);
621 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
622 are as in gimplify_expr. */
625 get_initialized_tmp_var (tree val
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
628 return internal_get_tmp_var (val
, pre_p
, post_p
, false, allow_ssa
);
631 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
632 generate debug info for them; otherwise don't. */
635 declare_vars (tree vars
, gimple
*gs
, bool debug_info
)
642 gbind
*scope
= as_a
<gbind
*> (gs
);
644 temps
= nreverse (last
);
646 block
= gimple_bind_block (scope
);
647 gcc_assert (!block
|| TREE_CODE (block
) == BLOCK
);
648 if (!block
|| !debug_info
)
650 DECL_CHAIN (last
) = gimple_bind_vars (scope
);
651 gimple_bind_set_vars (scope
, temps
);
655 /* We need to attach the nodes both to the BIND_EXPR and to its
656 associated BLOCK for debugging purposes. The key point here
657 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
658 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
659 if (BLOCK_VARS (block
))
660 BLOCK_VARS (block
) = chainon (BLOCK_VARS (block
), temps
);
663 gimple_bind_set_vars (scope
,
664 chainon (gimple_bind_vars (scope
), temps
));
665 BLOCK_VARS (block
) = temps
;
671 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
672 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
673 no such upper bound can be obtained. */
676 force_constant_size (tree var
)
678 /* The only attempt we make is by querying the maximum size of objects
679 of the variable's type. */
681 HOST_WIDE_INT max_size
;
683 gcc_assert (VAR_P (var
));
685 max_size
= max_int_size_in_bytes (TREE_TYPE (var
));
687 gcc_assert (max_size
>= 0);
690 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var
)), max_size
);
692 = build_int_cst (TREE_TYPE (DECL_SIZE (var
)), max_size
* BITS_PER_UNIT
);
695 /* Push the temporary variable TMP into the current binding. */
698 gimple_add_tmp_var_fn (struct function
*fn
, tree tmp
)
700 gcc_assert (!DECL_CHAIN (tmp
) && !DECL_SEEN_IN_BIND_EXPR_P (tmp
));
702 /* Later processing assumes that the object size is constant, which might
703 not be true at this point. Force the use of a constant upper bound in
705 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp
)))
706 force_constant_size (tmp
);
708 DECL_CONTEXT (tmp
) = fn
->decl
;
709 DECL_SEEN_IN_BIND_EXPR_P (tmp
) = 1;
711 record_vars_into (tmp
, fn
->decl
);
714 /* Push the temporary variable TMP into the current binding. */
717 gimple_add_tmp_var (tree tmp
)
719 gcc_assert (!DECL_CHAIN (tmp
) && !DECL_SEEN_IN_BIND_EXPR_P (tmp
));
721 /* Later processing assumes that the object size is constant, which might
722 not be true at this point. Force the use of a constant upper bound in
724 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp
)))
725 force_constant_size (tmp
);
727 DECL_CONTEXT (tmp
) = current_function_decl
;
728 DECL_SEEN_IN_BIND_EXPR_P (tmp
) = 1;
732 DECL_CHAIN (tmp
) = gimplify_ctxp
->temps
;
733 gimplify_ctxp
->temps
= tmp
;
735 /* Mark temporaries local within the nearest enclosing parallel. */
736 if (gimplify_omp_ctxp
)
738 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
740 && (ctx
->region_type
== ORT_WORKSHARE
741 || ctx
->region_type
== ORT_SIMD
742 || ctx
->region_type
== ORT_ACC
))
743 ctx
= ctx
->outer_context
;
745 omp_add_variable (ctx
, tmp
, GOVD_LOCAL
| GOVD_SEEN
);
754 /* This case is for nested functions. We need to expose the locals
756 body_seq
= gimple_body (current_function_decl
);
757 declare_vars (tmp
, gimple_seq_first_stmt (body_seq
), false);
763 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
764 nodes that are referenced more than once in GENERIC functions. This is
765 necessary because gimplification (translation into GIMPLE) is performed
766 by modifying tree nodes in-place, so gimplication of a shared node in a
767 first context could generate an invalid GIMPLE form in a second context.
769 This is achieved with a simple mark/copy/unmark algorithm that walks the
770 GENERIC representation top-down, marks nodes with TREE_VISITED the first
771 time it encounters them, duplicates them if they already have TREE_VISITED
772 set, and finally removes the TREE_VISITED marks it has set.
774 The algorithm works only at the function level, i.e. it generates a GENERIC
775 representation of a function with no nodes shared within the function when
776 passed a GENERIC function (except for nodes that are allowed to be shared).
778 At the global level, it is also necessary to unshare tree nodes that are
779 referenced in more than one function, for the same aforementioned reason.
780 This requires some cooperation from the front-end. There are 2 strategies:
782 1. Manual unsharing. The front-end needs to call unshare_expr on every
783 expression that might end up being shared across functions.
785 2. Deep unsharing. This is an extension of regular unsharing. Instead
786 of calling unshare_expr on expressions that might be shared across
787 functions, the front-end pre-marks them with TREE_VISITED. This will
788 ensure that they are unshared on the first reference within functions
789 when the regular unsharing algorithm runs. The counterpart is that
790 this algorithm must look deeper than for manual unsharing, which is
791 specified by LANG_HOOKS_DEEP_UNSHARING.
793 If there are only few specific cases of node sharing across functions, it is
794 probably easier for a front-end to unshare the expressions manually. On the
795 contrary, if the expressions generated at the global level are as widespread
796 as expressions generated within functions, deep unsharing is very likely the
799 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800 These nodes model computations that must be done once. If we were to
801 unshare something like SAVE_EXPR(i++), the gimplification process would
802 create wrong code. However, if DATA is non-null, it must hold a pointer
803 set that is used to unshare the subtrees of these nodes. */
806 mostly_copy_tree_r (tree
*tp
, int *walk_subtrees
, void *data
)
809 enum tree_code code
= TREE_CODE (t
);
811 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
812 copy their subtrees if we can make sure to do it only once. */
813 if (code
== SAVE_EXPR
|| code
== TARGET_EXPR
|| code
== BIND_EXPR
)
815 if (data
&& !((hash_set
<tree
> *)data
)->add (t
))
821 /* Stop at types, decls, constants like copy_tree_r. */
822 else if (TREE_CODE_CLASS (code
) == tcc_type
823 || TREE_CODE_CLASS (code
) == tcc_declaration
824 || TREE_CODE_CLASS (code
) == tcc_constant
)
827 /* Cope with the statement expression extension. */
828 else if (code
== STATEMENT_LIST
)
831 /* Leave the bulk of the work to copy_tree_r itself. */
833 copy_tree_r (tp
, walk_subtrees
, NULL
);
838 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
839 If *TP has been visited already, then *TP is deeply copied by calling
840 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
843 copy_if_shared_r (tree
*tp
, int *walk_subtrees
, void *data
)
846 enum tree_code code
= TREE_CODE (t
);
848 /* Skip types, decls, and constants. But we do want to look at their
849 types and the bounds of types. Mark them as visited so we properly
850 unmark their subtrees on the unmark pass. If we've already seen them,
851 don't look down further. */
852 if (TREE_CODE_CLASS (code
) == tcc_type
853 || TREE_CODE_CLASS (code
) == tcc_declaration
854 || TREE_CODE_CLASS (code
) == tcc_constant
)
856 if (TREE_VISITED (t
))
859 TREE_VISITED (t
) = 1;
862 /* If this node has been visited already, unshare it and don't look
864 else if (TREE_VISITED (t
))
866 walk_tree (tp
, mostly_copy_tree_r
, data
, NULL
);
870 /* Otherwise, mark the node as visited and keep looking. */
872 TREE_VISITED (t
) = 1;
877 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
878 copy_if_shared_r callback unmodified. */
881 copy_if_shared (tree
*tp
, void *data
)
883 walk_tree (tp
, copy_if_shared_r
, data
, NULL
);
886 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
887 any nested functions. */
890 unshare_body (tree fndecl
)
892 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
893 /* If the language requires deep unsharing, we need a pointer set to make
894 sure we don't repeatedly unshare subtrees of unshareable nodes. */
895 hash_set
<tree
> *visited
896 = lang_hooks
.deep_unsharing
? new hash_set
<tree
> : NULL
;
898 copy_if_shared (&DECL_SAVED_TREE (fndecl
), visited
);
899 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl
)), visited
);
900 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl
)), visited
);
905 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
906 unshare_body (cgn
->decl
);
909 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
910 Subtrees are walked until the first unvisited node is encountered. */
913 unmark_visited_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
917 /* If this node has been visited, unmark it and keep looking. */
918 if (TREE_VISITED (t
))
919 TREE_VISITED (t
) = 0;
921 /* Otherwise, don't look any deeper. */
928 /* Unmark the visited trees rooted at *TP. */
931 unmark_visited (tree
*tp
)
933 walk_tree (tp
, unmark_visited_r
, NULL
, NULL
);
936 /* Likewise, but mark all trees as not visited. */
939 unvisit_body (tree fndecl
)
941 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
943 unmark_visited (&DECL_SAVED_TREE (fndecl
));
944 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl
)));
945 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl
)));
948 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
949 unvisit_body (cgn
->decl
);
952 /* Unconditionally make an unshared copy of EXPR. This is used when using
953 stored expressions which span multiple functions, such as BINFO_VTABLE,
954 as the normal unsharing process can't tell that they're shared. */
957 unshare_expr (tree expr
)
959 walk_tree (&expr
, mostly_copy_tree_r
, NULL
, NULL
);
963 /* Worker for unshare_expr_without_location. */
966 prune_expr_location (tree
*tp
, int *walk_subtrees
, void *)
969 SET_EXPR_LOCATION (*tp
, UNKNOWN_LOCATION
);
975 /* Similar to unshare_expr but also prune all expression locations
979 unshare_expr_without_location (tree expr
)
981 walk_tree (&expr
, mostly_copy_tree_r
, NULL
, NULL
);
983 walk_tree (&expr
, prune_expr_location
, NULL
, NULL
);
987 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
988 contain statements and have a value. Assign its value to a temporary
989 and give it void_type_node. Return the temporary, or NULL_TREE if
990 WRAPPER was already void. */
993 voidify_wrapper_expr (tree wrapper
, tree temp
)
995 tree type
= TREE_TYPE (wrapper
);
996 if (type
&& !VOID_TYPE_P (type
))
1000 /* Set p to point to the body of the wrapper. Loop until we find
1001 something that isn't a wrapper. */
1002 for (p
= &wrapper
; p
&& *p
; )
1004 switch (TREE_CODE (*p
))
1007 TREE_SIDE_EFFECTS (*p
) = 1;
1008 TREE_TYPE (*p
) = void_type_node
;
1009 /* For a BIND_EXPR, the body is operand 1. */
1010 p
= &BIND_EXPR_BODY (*p
);
1013 case CLEANUP_POINT_EXPR
:
1014 case TRY_FINALLY_EXPR
:
1015 case TRY_CATCH_EXPR
:
1016 TREE_SIDE_EFFECTS (*p
) = 1;
1017 TREE_TYPE (*p
) = void_type_node
;
1018 p
= &TREE_OPERAND (*p
, 0);
1021 case STATEMENT_LIST
:
1023 tree_stmt_iterator i
= tsi_last (*p
);
1024 TREE_SIDE_EFFECTS (*p
) = 1;
1025 TREE_TYPE (*p
) = void_type_node
;
1026 p
= tsi_end_p (i
) ? NULL
: tsi_stmt_ptr (i
);
1031 /* Advance to the last statement. Set all container types to
1033 for (; TREE_CODE (*p
) == COMPOUND_EXPR
; p
= &TREE_OPERAND (*p
, 1))
1035 TREE_SIDE_EFFECTS (*p
) = 1;
1036 TREE_TYPE (*p
) = void_type_node
;
1040 case TRANSACTION_EXPR
:
1041 TREE_SIDE_EFFECTS (*p
) = 1;
1042 TREE_TYPE (*p
) = void_type_node
;
1043 p
= &TRANSACTION_EXPR_BODY (*p
);
1047 /* Assume that any tree upon which voidify_wrapper_expr is
1048 directly called is a wrapper, and that its body is op0. */
1051 TREE_SIDE_EFFECTS (*p
) = 1;
1052 TREE_TYPE (*p
) = void_type_node
;
1053 p
= &TREE_OPERAND (*p
, 0);
1061 if (p
== NULL
|| IS_EMPTY_STMT (*p
))
1065 /* The wrapper is on the RHS of an assignment that we're pushing
1067 gcc_assert (TREE_CODE (temp
) == INIT_EXPR
1068 || TREE_CODE (temp
) == MODIFY_EXPR
);
1069 TREE_OPERAND (temp
, 1) = *p
;
1074 temp
= create_tmp_var (type
, "retval");
1075 *p
= build2 (INIT_EXPR
, type
, temp
, *p
);
1084 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1085 a temporary through which they communicate. */
1088 build_stack_save_restore (gcall
**save
, gcall
**restore
)
1092 *save
= gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE
), 0);
1093 tmp_var
= create_tmp_var (ptr_type_node
, "saved_stack");
1094 gimple_call_set_lhs (*save
, tmp_var
);
1097 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE
),
1101 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1104 build_asan_poison_call_expr (tree decl
)
1106 /* Do not poison variables that have size equal to zero. */
1107 tree unit_size
= DECL_SIZE_UNIT (decl
);
1108 if (zerop (unit_size
))
1111 tree base
= build_fold_addr_expr (decl
);
1113 return build_call_expr_internal_loc (UNKNOWN_LOCATION
, IFN_ASAN_MARK
,
1115 build_int_cst (integer_type_node
,
1120 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1121 on POISON flag, shadow memory of a DECL variable. The call will be
1122 put on location identified by IT iterator, where BEFORE flag drives
1123 position where the stmt will be put. */
1126 asan_poison_variable (tree decl
, bool poison
, gimple_stmt_iterator
*it
,
1129 /* When within an OMP context, do not emit ASAN_MARK internal fns. */
1130 if (gimplify_omp_ctxp
)
1133 tree unit_size
= DECL_SIZE_UNIT (decl
);
1134 tree base
= build_fold_addr_expr (decl
);
1136 /* Do not poison variables that have size equal to zero. */
1137 if (zerop (unit_size
))
1140 /* It's necessary to have all stack variables aligned to ASAN granularity
1142 if (DECL_ALIGN_UNIT (decl
) <= ASAN_SHADOW_GRANULARITY
)
1143 SET_DECL_ALIGN (decl
, BITS_PER_UNIT
* ASAN_SHADOW_GRANULARITY
);
1145 HOST_WIDE_INT flags
= poison
? ASAN_MARK_POISON
: ASAN_MARK_UNPOISON
;
1148 = gimple_build_call_internal (IFN_ASAN_MARK
, 3,
1149 build_int_cst (integer_type_node
, flags
),
1153 gsi_insert_before (it
, g
, GSI_NEW_STMT
);
1155 gsi_insert_after (it
, g
, GSI_NEW_STMT
);
1158 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1159 either poisons or unpoisons a DECL. Created statement is appended
1160 to SEQ_P gimple sequence. */
1163 asan_poison_variable (tree decl
, bool poison
, gimple_seq
*seq_p
)
1165 gimple_stmt_iterator it
= gsi_last (*seq_p
);
1166 bool before
= false;
1171 asan_poison_variable (decl
, poison
, &it
, before
);
1174 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1177 sort_by_decl_uid (const void *a
, const void *b
)
1179 const tree
*t1
= (const tree
*)a
;
1180 const tree
*t2
= (const tree
*)b
;
1182 int uid1
= DECL_UID (*t1
);
1183 int uid2
= DECL_UID (*t2
);
1187 else if (uid1
> uid2
)
1193 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1194 depending on POISON flag. Created statement is appended
1195 to SEQ_P gimple sequence. */
1198 asan_poison_variables (hash_set
<tree
> *variables
, bool poison
, gimple_seq
*seq_p
)
1200 unsigned c
= variables
->elements ();
1204 auto_vec
<tree
> sorted_variables (c
);
1206 for (hash_set
<tree
>::iterator it
= variables
->begin ();
1207 it
!= variables
->end (); ++it
)
1208 sorted_variables
.safe_push (*it
);
1210 sorted_variables
.qsort (sort_by_decl_uid
);
1214 FOR_EACH_VEC_ELT (sorted_variables
, i
, var
)
1216 asan_poison_variable (var
, poison
, seq_p
);
1218 /* Add use_after_scope_memory attribute for the variable in order
1219 to prevent re-written into SSA. */
1220 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE
,
1221 DECL_ATTRIBUTES (var
)))
1222 DECL_ATTRIBUTES (var
)
1223 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE
),
1225 DECL_ATTRIBUTES (var
));
1229 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1231 static enum gimplify_status
1232 gimplify_bind_expr (tree
*expr_p
, gimple_seq
*pre_p
)
1234 tree bind_expr
= *expr_p
;
1235 bool old_keep_stack
= gimplify_ctxp
->keep_stack
;
1236 bool old_save_stack
= gimplify_ctxp
->save_stack
;
1239 gimple_seq body
, cleanup
;
1241 location_t start_locus
= 0, end_locus
= 0;
1242 tree ret_clauses
= NULL
;
1244 tree temp
= voidify_wrapper_expr (bind_expr
, NULL
);
1246 /* Mark variables seen in this bind expr. */
1247 for (t
= BIND_EXPR_VARS (bind_expr
); t
; t
= DECL_CHAIN (t
))
1251 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
1253 /* Mark variable as local. */
1254 if (ctx
&& ctx
->region_type
!= ORT_NONE
&& !DECL_EXTERNAL (t
)
1255 && (! DECL_SEEN_IN_BIND_EXPR_P (t
)
1256 || splay_tree_lookup (ctx
->variables
,
1257 (splay_tree_key
) t
) == NULL
))
1259 if (ctx
->region_type
== ORT_SIMD
1260 && TREE_ADDRESSABLE (t
)
1261 && !TREE_STATIC (t
))
1262 omp_add_variable (ctx
, t
, GOVD_PRIVATE
| GOVD_SEEN
);
1264 omp_add_variable (ctx
, t
, GOVD_LOCAL
| GOVD_SEEN
);
1267 DECL_SEEN_IN_BIND_EXPR_P (t
) = 1;
1269 if (DECL_HARD_REGISTER (t
) && !is_global_var (t
) && cfun
)
1270 cfun
->has_local_explicit_reg_vars
= true;
1273 /* Preliminarily mark non-addressed complex variables as eligible
1274 for promotion to gimple registers. We'll transform their uses
1276 if ((TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
1277 || TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1278 && !TREE_THIS_VOLATILE (t
)
1279 && (VAR_P (t
) && !DECL_HARD_REGISTER (t
))
1280 && !needs_to_live_in_memory (t
))
1281 DECL_GIMPLE_REG_P (t
) = 1;
1284 bind_stmt
= gimple_build_bind (BIND_EXPR_VARS (bind_expr
), NULL
,
1285 BIND_EXPR_BLOCK (bind_expr
));
1286 gimple_push_bind_expr (bind_stmt
);
1288 gimplify_ctxp
->keep_stack
= false;
1289 gimplify_ctxp
->save_stack
= false;
1291 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1293 gimplify_stmt (&BIND_EXPR_BODY (bind_expr
), &body
);
1294 gimple_bind_set_body (bind_stmt
, body
);
1296 /* Source location wise, the cleanup code (stack_restore and clobbers)
1297 belongs to the end of the block, so propagate what we have. The
1298 stack_save operation belongs to the beginning of block, which we can
1299 infer from the bind_expr directly if the block has no explicit
1301 if (BIND_EXPR_BLOCK (bind_expr
))
1303 end_locus
= BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr
));
1304 start_locus
= BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr
));
1306 if (start_locus
== 0)
1307 start_locus
= EXPR_LOCATION (bind_expr
);
1312 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1313 the stack space allocated to the VLAs. */
1314 if (gimplify_ctxp
->save_stack
&& !gimplify_ctxp
->keep_stack
)
1316 gcall
*stack_restore
;
1318 /* Save stack on entry and restore it on exit. Add a try_finally
1319 block to achieve this. */
1320 build_stack_save_restore (&stack_save
, &stack_restore
);
1322 gimple_set_location (stack_save
, start_locus
);
1323 gimple_set_location (stack_restore
, end_locus
);
1325 gimplify_seq_add_stmt (&cleanup
, stack_restore
);
1328 /* Add clobbers for all variables that go out of scope. */
1329 for (t
= BIND_EXPR_VARS (bind_expr
); t
; t
= DECL_CHAIN (t
))
1332 && !is_global_var (t
)
1333 && DECL_CONTEXT (t
) == current_function_decl
)
1335 if (!DECL_HARD_REGISTER (t
)
1336 && !TREE_THIS_VOLATILE (t
)
1337 && !DECL_HAS_VALUE_EXPR_P (t
)
1338 /* Only care for variables that have to be in memory. Others
1339 will be rewritten into SSA names, hence moved to the
1341 && !is_gimple_reg (t
)
1342 && flag_stack_reuse
!= SR_NONE
)
1344 tree clobber
= build_constructor (TREE_TYPE (t
), NULL
);
1345 gimple
*clobber_stmt
;
1346 TREE_THIS_VOLATILE (clobber
) = 1;
1347 clobber_stmt
= gimple_build_assign (t
, clobber
);
1348 gimple_set_location (clobber_stmt
, end_locus
);
1349 gimplify_seq_add_stmt (&cleanup
, clobber_stmt
);
1352 if (flag_openacc
&& oacc_declare_returns
!= NULL
)
1354 tree
*c
= oacc_declare_returns
->get (t
);
1358 OMP_CLAUSE_CHAIN (*c
) = ret_clauses
;
1362 oacc_declare_returns
->remove (t
);
1364 if (oacc_declare_returns
->elements () == 0)
1366 delete oacc_declare_returns
;
1367 oacc_declare_returns
= NULL
;
1373 if (asan_poisoned_variables
!= NULL
1374 && asan_poisoned_variables
->contains (t
))
1376 asan_poisoned_variables
->remove (t
);
1377 asan_poison_variable (t
, true, &cleanup
);
1380 if (gimplify_ctxp
->live_switch_vars
!= NULL
1381 && gimplify_ctxp
->live_switch_vars
->contains (t
))
1382 gimplify_ctxp
->live_switch_vars
->remove (t
);
1388 gimple_stmt_iterator si
= gsi_start (cleanup
);
1390 stmt
= gimple_build_omp_target (NULL
, GF_OMP_TARGET_KIND_OACC_DECLARE
,
1392 gsi_insert_seq_before_without_update (&si
, stmt
, GSI_NEW_STMT
);
1398 gimple_seq new_body
;
1401 gs
= gimple_build_try (gimple_bind_body (bind_stmt
), cleanup
,
1402 GIMPLE_TRY_FINALLY
);
1405 gimplify_seq_add_stmt (&new_body
, stack_save
);
1406 gimplify_seq_add_stmt (&new_body
, gs
);
1407 gimple_bind_set_body (bind_stmt
, new_body
);
1410 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1411 if (!gimplify_ctxp
->keep_stack
)
1412 gimplify_ctxp
->keep_stack
= old_keep_stack
;
1413 gimplify_ctxp
->save_stack
= old_save_stack
;
1415 gimple_pop_bind_expr ();
1417 gimplify_seq_add_stmt (pre_p
, bind_stmt
);
1425 *expr_p
= NULL_TREE
;
1429 /* Maybe add early return predict statement to PRE_P sequence. */
1432 maybe_add_early_return_predict_stmt (gimple_seq
*pre_p
)
1434 /* If we are not in a conditional context, add PREDICT statement. */
1435 if (gimple_conditional_context ())
1437 gimple
*predict
= gimple_build_predict (PRED_TREE_EARLY_RETURN
,
1439 gimplify_seq_add_stmt (pre_p
, predict
);
1443 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1444 GIMPLE value, it is assigned to a new temporary and the statement is
1445 re-written to return the temporary.
1447 PRE_P points to the sequence where side effects that must happen before
1448 STMT should be stored. */
1450 static enum gimplify_status
1451 gimplify_return_expr (tree stmt
, gimple_seq
*pre_p
)
1454 tree ret_expr
= TREE_OPERAND (stmt
, 0);
1455 tree result_decl
, result
;
1457 if (ret_expr
== error_mark_node
)
1460 /* Implicit _Cilk_sync must be inserted right before any return statement
1461 if there is a _Cilk_spawn in the function. If the user has provided a
1462 _Cilk_sync, the optimizer should remove this duplicate one. */
1463 if (fn_contains_cilk_spawn_p (cfun
))
1465 tree impl_sync
= build0 (CILK_SYNC_STMT
, void_type_node
);
1466 gimplify_and_add (impl_sync
, pre_p
);
1470 || TREE_CODE (ret_expr
) == RESULT_DECL
1471 || ret_expr
== error_mark_node
)
1473 maybe_add_early_return_predict_stmt (pre_p
);
1474 greturn
*ret
= gimple_build_return (ret_expr
);
1475 gimple_set_no_warning (ret
, TREE_NO_WARNING (stmt
));
1476 gimplify_seq_add_stmt (pre_p
, ret
);
1480 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1481 result_decl
= NULL_TREE
;
1484 result_decl
= TREE_OPERAND (ret_expr
, 0);
1486 /* See through a return by reference. */
1487 if (TREE_CODE (result_decl
) == INDIRECT_REF
)
1488 result_decl
= TREE_OPERAND (result_decl
, 0);
1490 gcc_assert ((TREE_CODE (ret_expr
) == MODIFY_EXPR
1491 || TREE_CODE (ret_expr
) == INIT_EXPR
)
1492 && TREE_CODE (result_decl
) == RESULT_DECL
);
1495 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1496 Recall that aggregate_value_p is FALSE for any aggregate type that is
1497 returned in registers. If we're returning values in registers, then
1498 we don't want to extend the lifetime of the RESULT_DECL, particularly
1499 across another call. In addition, for those aggregates for which
1500 hard_function_value generates a PARALLEL, we'll die during normal
1501 expansion of structure assignments; there's special code in expand_return
1502 to handle this case that does not exist in expand_expr. */
1505 else if (aggregate_value_p (result_decl
, TREE_TYPE (current_function_decl
)))
1507 if (TREE_CODE (DECL_SIZE (result_decl
)) != INTEGER_CST
)
1509 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl
)))
1510 gimplify_type_sizes (TREE_TYPE (result_decl
), pre_p
);
1511 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1512 should be effectively allocated by the caller, i.e. all calls to
1513 this function must be subject to the Return Slot Optimization. */
1514 gimplify_one_sizepos (&DECL_SIZE (result_decl
), pre_p
);
1515 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl
), pre_p
);
1517 result
= result_decl
;
1519 else if (gimplify_ctxp
->return_temp
)
1520 result
= gimplify_ctxp
->return_temp
;
1523 result
= create_tmp_reg (TREE_TYPE (result_decl
));
1525 /* ??? With complex control flow (usually involving abnormal edges),
1526 we can wind up warning about an uninitialized value for this. Due
1527 to how this variable is constructed and initialized, this is never
1528 true. Give up and never warn. */
1529 TREE_NO_WARNING (result
) = 1;
1531 gimplify_ctxp
->return_temp
= result
;
1534 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1535 Then gimplify the whole thing. */
1536 if (result
!= result_decl
)
1537 TREE_OPERAND (ret_expr
, 0) = result
;
1539 gimplify_and_add (TREE_OPERAND (stmt
, 0), pre_p
);
1541 maybe_add_early_return_predict_stmt (pre_p
);
1542 ret
= gimple_build_return (result
);
1543 gimple_set_no_warning (ret
, TREE_NO_WARNING (stmt
));
1544 gimplify_seq_add_stmt (pre_p
, ret
);
1549 /* Gimplify a variable-length array DECL. */
1552 gimplify_vla_decl (tree decl
, gimple_seq
*seq_p
)
1554 /* This is a variable-sized decl. Simplify its size and mark it
1555 for deferred expansion. */
1556 tree t
, addr
, ptr_type
;
1558 gimplify_one_sizepos (&DECL_SIZE (decl
), seq_p
);
1559 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl
), seq_p
);
1561 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1562 if (DECL_HAS_VALUE_EXPR_P (decl
))
1565 /* All occurrences of this decl in final gimplified code will be
1566 replaced by indirection. Setting DECL_VALUE_EXPR does two
1567 things: First, it lets the rest of the gimplifier know what
1568 replacement to use. Second, it lets the debug info know
1569 where to find the value. */
1570 ptr_type
= build_pointer_type (TREE_TYPE (decl
));
1571 addr
= create_tmp_var (ptr_type
, get_name (decl
));
1572 DECL_IGNORED_P (addr
) = 0;
1573 t
= build_fold_indirect_ref (addr
);
1574 TREE_THIS_NOTRAP (t
) = 1;
1575 SET_DECL_VALUE_EXPR (decl
, t
);
1576 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1578 t
= build_alloca_call_expr (DECL_SIZE_UNIT (decl
), DECL_ALIGN (decl
),
1579 max_int_size_in_bytes (TREE_TYPE (decl
)));
1580 /* The call has been built for a variable-sized object. */
1581 CALL_ALLOCA_FOR_VAR_P (t
) = 1;
1582 t
= fold_convert (ptr_type
, t
);
1583 t
= build2 (MODIFY_EXPR
, TREE_TYPE (addr
), addr
, t
);
1585 gimplify_and_add (t
, seq_p
);
1588 /* A helper function to be called via walk_tree. Mark all labels under *TP
1589 as being forced. To be called for DECL_INITIAL of static variables. */
1592 force_labels_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
1596 if (TREE_CODE (*tp
) == LABEL_DECL
)
1598 FORCED_LABEL (*tp
) = 1;
1599 cfun
->has_forced_label_in_static
= 1;
1605 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1606 and initialization explicit. */
1608 static enum gimplify_status
1609 gimplify_decl_expr (tree
*stmt_p
, gimple_seq
*seq_p
)
1611 tree stmt
= *stmt_p
;
1612 tree decl
= DECL_EXPR_DECL (stmt
);
1614 *stmt_p
= NULL_TREE
;
1616 if (TREE_TYPE (decl
) == error_mark_node
)
1619 if ((TREE_CODE (decl
) == TYPE_DECL
1621 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl
)))
1623 gimplify_type_sizes (TREE_TYPE (decl
), seq_p
);
1624 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
1625 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl
)), seq_p
);
1628 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1629 in case its size expressions contain problematic nodes like CALL_EXPR. */
1630 if (TREE_CODE (decl
) == TYPE_DECL
1631 && DECL_ORIGINAL_TYPE (decl
)
1632 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl
)))
1634 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl
), seq_p
);
1635 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl
)) == REFERENCE_TYPE
)
1636 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl
)), seq_p
);
1639 if (VAR_P (decl
) && !DECL_EXTERNAL (decl
))
1641 tree init
= DECL_INITIAL (decl
);
1642 bool is_vla
= false;
1644 if (TREE_CODE (DECL_SIZE_UNIT (decl
)) != INTEGER_CST
1645 || (!TREE_STATIC (decl
)
1646 && flag_stack_check
== GENERIC_STACK_CHECK
1647 && compare_tree_int (DECL_SIZE_UNIT (decl
),
1648 STACK_CHECK_MAX_VAR_SIZE
) > 0))
1650 gimplify_vla_decl (decl
, seq_p
);
1654 if (asan_poisoned_variables
1656 && TREE_ADDRESSABLE (decl
)
1657 && !TREE_STATIC (decl
)
1658 && !DECL_HAS_VALUE_EXPR_P (decl
)
1659 && DECL_ALIGN (decl
) <= MAX_SUPPORTED_STACK_ALIGNMENT
1660 && dbg_cnt (asan_use_after_scope
))
1662 asan_poisoned_variables
->add (decl
);
1663 asan_poison_variable (decl
, false, seq_p
);
1664 if (!DECL_ARTIFICIAL (decl
) && gimplify_ctxp
->live_switch_vars
)
1665 gimplify_ctxp
->live_switch_vars
->add (decl
);
1668 /* Some front ends do not explicitly declare all anonymous
1669 artificial variables. We compensate here by declaring the
1670 variables, though it would be better if the front ends would
1671 explicitly declare them. */
1672 if (!DECL_SEEN_IN_BIND_EXPR_P (decl
)
1673 && DECL_ARTIFICIAL (decl
) && DECL_NAME (decl
) == NULL_TREE
)
1674 gimple_add_tmp_var (decl
);
1676 if (init
&& init
!= error_mark_node
)
1678 if (!TREE_STATIC (decl
))
1680 DECL_INITIAL (decl
) = NULL_TREE
;
1681 init
= build2 (INIT_EXPR
, void_type_node
, decl
, init
);
1682 gimplify_and_add (init
, seq_p
);
1686 /* We must still examine initializers for static variables
1687 as they may contain a label address. */
1688 walk_tree (&init
, force_labels_r
, NULL
, NULL
);
1695 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1696 and replacing the LOOP_EXPR with goto, but if the loop contains an
1697 EXIT_EXPR, we need to append a label for it to jump to. */
1699 static enum gimplify_status
1700 gimplify_loop_expr (tree
*expr_p
, gimple_seq
*pre_p
)
1702 tree saved_label
= gimplify_ctxp
->exit_label
;
1703 tree start_label
= create_artificial_label (UNKNOWN_LOCATION
);
1705 gimplify_seq_add_stmt (pre_p
, gimple_build_label (start_label
));
1707 gimplify_ctxp
->exit_label
= NULL_TREE
;
1709 gimplify_and_add (LOOP_EXPR_BODY (*expr_p
), pre_p
);
1711 gimplify_seq_add_stmt (pre_p
, gimple_build_goto (start_label
));
1713 if (gimplify_ctxp
->exit_label
)
1714 gimplify_seq_add_stmt (pre_p
,
1715 gimple_build_label (gimplify_ctxp
->exit_label
));
1717 gimplify_ctxp
->exit_label
= saved_label
;
1723 /* Gimplify a statement list onto a sequence. These may be created either
1724 by an enlightened front-end, or by shortcut_cond_expr. */
1726 static enum gimplify_status
1727 gimplify_statement_list (tree
*expr_p
, gimple_seq
*pre_p
)
1729 tree temp
= voidify_wrapper_expr (*expr_p
, NULL
);
1731 tree_stmt_iterator i
= tsi_start (*expr_p
);
1733 while (!tsi_end_p (i
))
1735 gimplify_stmt (tsi_stmt_ptr (i
), pre_p
);
1748 /* Callback for walk_gimple_seq. */
1751 warn_switch_unreachable_r (gimple_stmt_iterator
*gsi_p
, bool *handled_ops_p
,
1752 struct walk_stmt_info
*wi
)
1754 gimple
*stmt
= gsi_stmt (*gsi_p
);
1756 *handled_ops_p
= true;
1757 switch (gimple_code (stmt
))
1760 /* A compiler-generated cleanup or a user-written try block.
1761 If it's empty, don't dive into it--that would result in
1762 worse location info. */
1763 if (gimple_try_eval (stmt
) == NULL
)
1766 return integer_zero_node
;
1771 case GIMPLE_EH_FILTER
:
1772 case GIMPLE_TRANSACTION
:
1773 /* Walk the sub-statements. */
1774 *handled_ops_p
= false;
1777 if (gimple_call_internal_p (stmt
, IFN_ASAN_MARK
))
1779 *handled_ops_p
= false;
1784 /* Save the first "real" statement (not a decl/lexical scope/...). */
1786 return integer_zero_node
;
1791 /* Possibly warn about unreachable statements between switch's controlling
1792 expression and the first case. SEQ is the body of a switch expression. */
1795 maybe_warn_switch_unreachable (gimple_seq seq
)
1797 if (!warn_switch_unreachable
1798 /* This warning doesn't play well with Fortran when optimizations
1800 || lang_GNU_Fortran ()
1804 struct walk_stmt_info wi
;
1805 memset (&wi
, 0, sizeof (wi
));
1806 walk_gimple_seq (seq
, warn_switch_unreachable_r
, NULL
, &wi
);
1807 gimple
*stmt
= (gimple
*) wi
.info
;
1809 if (stmt
&& gimple_code (stmt
) != GIMPLE_LABEL
)
1811 if (gimple_code (stmt
) == GIMPLE_GOTO
1812 && TREE_CODE (gimple_goto_dest (stmt
)) == LABEL_DECL
1813 && DECL_ARTIFICIAL (gimple_goto_dest (stmt
)))
1814 /* Don't warn for compiler-generated gotos. These occur
1815 in Duff's devices, for example. */;
1817 warning_at (gimple_location (stmt
), OPT_Wswitch_unreachable
,
1818 "statement will never be executed");
1823 /* A label entry that pairs label and a location. */
1830 /* Find LABEL in vector of label entries VEC. */
1832 static struct label_entry
*
1833 find_label_entry (const auto_vec
<struct label_entry
> *vec
, tree label
)
1836 struct label_entry
*l
;
1838 FOR_EACH_VEC_ELT (*vec
, i
, l
)
1839 if (l
->label
== label
)
1844 /* Return true if LABEL, a LABEL_DECL, represents a case label
1845 in a vector of labels CASES. */
1848 case_label_p (const vec
<tree
> *cases
, tree label
)
1853 FOR_EACH_VEC_ELT (*cases
, i
, l
)
1854 if (CASE_LABEL (l
) == label
)
1859 /* Find the last statement in a scope STMT. */
1862 last_stmt_in_scope (gimple
*stmt
)
1867 switch (gimple_code (stmt
))
1871 gbind
*bind
= as_a
<gbind
*> (stmt
);
1872 stmt
= gimple_seq_last_stmt (gimple_bind_body (bind
));
1873 return last_stmt_in_scope (stmt
);
1878 gtry
*try_stmt
= as_a
<gtry
*> (stmt
);
1879 stmt
= gimple_seq_last_stmt (gimple_try_eval (try_stmt
));
1880 gimple
*last_eval
= last_stmt_in_scope (stmt
);
1881 if (gimple_stmt_may_fallthru (last_eval
)
1882 && (last_eval
== NULL
1883 || !gimple_call_internal_p (last_eval
, IFN_FALLTHROUGH
))
1884 && gimple_try_kind (try_stmt
) == GIMPLE_TRY_FINALLY
)
1886 stmt
= gimple_seq_last_stmt (gimple_try_cleanup (try_stmt
));
1887 return last_stmt_in_scope (stmt
);
1898 /* Collect interesting labels in LABELS and return the statement preceding
1899 another case label, or a user-defined label. */
1902 collect_fallthrough_labels (gimple_stmt_iterator
*gsi_p
,
1903 auto_vec
<struct label_entry
> *labels
)
1905 gimple
*prev
= NULL
;
1909 if (gimple_code (gsi_stmt (*gsi_p
)) == GIMPLE_BIND
1910 || gimple_code (gsi_stmt (*gsi_p
)) == GIMPLE_TRY
)
1912 /* Nested scope. Only look at the last statement of
1913 the innermost scope. */
1914 location_t bind_loc
= gimple_location (gsi_stmt (*gsi_p
));
1915 gimple
*last
= last_stmt_in_scope (gsi_stmt (*gsi_p
));
1919 /* It might be a label without a location. Use the
1920 location of the scope then. */
1921 if (!gimple_has_location (prev
))
1922 gimple_set_location (prev
, bind_loc
);
1928 /* Ifs are tricky. */
1929 if (gimple_code (gsi_stmt (*gsi_p
)) == GIMPLE_COND
)
1931 gcond
*cond_stmt
= as_a
<gcond
*> (gsi_stmt (*gsi_p
));
1932 tree false_lab
= gimple_cond_false_label (cond_stmt
);
1933 location_t if_loc
= gimple_location (cond_stmt
);
1936 if (i > 1) goto <D.2259>; else goto D;
1937 we can't do much with the else-branch. */
1938 if (!DECL_ARTIFICIAL (false_lab
))
1941 /* Go on until the false label, then one step back. */
1942 for (; !gsi_end_p (*gsi_p
); gsi_next (gsi_p
))
1944 gimple
*stmt
= gsi_stmt (*gsi_p
);
1945 if (gimple_code (stmt
) == GIMPLE_LABEL
1946 && gimple_label_label (as_a
<glabel
*> (stmt
)) == false_lab
)
1950 /* Not found? Oops. */
1951 if (gsi_end_p (*gsi_p
))
1954 struct label_entry l
= { false_lab
, if_loc
};
1955 labels
->safe_push (l
);
1957 /* Go to the last statement of the then branch. */
1960 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
1966 if (gimple_code (gsi_stmt (*gsi_p
)) == GIMPLE_GOTO
1967 && !gimple_has_location (gsi_stmt (*gsi_p
)))
1969 /* Look at the statement before, it might be
1970 attribute fallthrough, in which case don't warn. */
1972 bool fallthru_before_dest
1973 = gimple_call_internal_p (gsi_stmt (*gsi_p
), IFN_FALLTHROUGH
);
1975 tree goto_dest
= gimple_goto_dest (gsi_stmt (*gsi_p
));
1976 if (!fallthru_before_dest
)
1978 struct label_entry l
= { goto_dest
, if_loc
};
1979 labels
->safe_push (l
);
1982 /* And move back. */
1986 /* Remember the last statement. Skip labels that are of no interest
1988 if (gimple_code (gsi_stmt (*gsi_p
)) == GIMPLE_LABEL
)
1990 tree label
= gimple_label_label (as_a
<glabel
*> (gsi_stmt (*gsi_p
)));
1991 if (find_label_entry (labels
, label
))
1992 prev
= gsi_stmt (*gsi_p
);
1994 else if (gimple_call_internal_p (gsi_stmt (*gsi_p
), IFN_ASAN_MARK
))
1997 prev
= gsi_stmt (*gsi_p
);
2000 while (!gsi_end_p (*gsi_p
)
2001 /* Stop if we find a case or a user-defined label. */
2002 && (gimple_code (gsi_stmt (*gsi_p
)) != GIMPLE_LABEL
2003 || !gimple_has_location (gsi_stmt (*gsi_p
))));
2008 /* Return true if the switch fallthough warning should occur. LABEL is
2009 the label statement that we're falling through to. */
2012 should_warn_for_implicit_fallthrough (gimple_stmt_iterator
*gsi_p
, tree label
)
2014 gimple_stmt_iterator gsi
= *gsi_p
;
2016 /* Don't warn if the label is marked with a "falls through" comment. */
2017 if (FALLTHROUGH_LABEL_P (label
))
2020 /* Don't warn for non-case labels followed by a statement:
2025 as these are likely intentional. */
2026 if (!case_label_p (&gimplify_ctxp
->case_labels
, label
))
2029 while (!gsi_end_p (gsi
)
2030 && gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
2031 && (l
= gimple_label_label (as_a
<glabel
*> (gsi_stmt (gsi
))))
2032 && !case_label_p (&gimplify_ctxp
->case_labels
, l
))
2034 if (gsi_end_p (gsi
) || gimple_code (gsi_stmt (gsi
)) != GIMPLE_LABEL
)
2038 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2039 immediately breaks. */
2042 /* Skip all immediately following labels. */
2043 while (!gsi_end_p (gsi
)
2044 && (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
2045 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_PREDICT
))
2048 /* { ... something; default:; } */
2050 /* { ... something; default: break; } or
2051 { ... something; default: goto L; } */
2052 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_GOTO
2053 /* { ... something; default: return; } */
2054 || gimple_code (gsi_stmt (gsi
)) == GIMPLE_RETURN
)
2060 /* Callback for walk_gimple_seq. */
2063 warn_implicit_fallthrough_r (gimple_stmt_iterator
*gsi_p
, bool *handled_ops_p
,
2064 struct walk_stmt_info
*)
2066 gimple
*stmt
= gsi_stmt (*gsi_p
);
2068 *handled_ops_p
= true;
2069 switch (gimple_code (stmt
))
2074 case GIMPLE_EH_FILTER
:
2075 case GIMPLE_TRANSACTION
:
2076 /* Walk the sub-statements. */
2077 *handled_ops_p
= false;
2080 /* Find a sequence of form:
2087 and possibly warn. */
2090 /* Found a label. Skip all immediately following labels. */
2091 while (!gsi_end_p (*gsi_p
)
2092 && gimple_code (gsi_stmt (*gsi_p
)) == GIMPLE_LABEL
)
2095 /* There might be no more statements. */
2096 if (gsi_end_p (*gsi_p
))
2097 return integer_zero_node
;
2099 /* Vector of labels that fall through. */
2100 auto_vec
<struct label_entry
> labels
;
2101 gimple
*prev
= collect_fallthrough_labels (gsi_p
, &labels
);
2103 /* There might be no more statements. */
2104 if (gsi_end_p (*gsi_p
))
2105 return integer_zero_node
;
2107 gimple
*next
= gsi_stmt (*gsi_p
);
2109 /* If what follows is a label, then we may have a fallthrough. */
2110 if (gimple_code (next
) == GIMPLE_LABEL
2111 && gimple_has_location (next
)
2112 && (label
= gimple_label_label (as_a
<glabel
*> (next
)))
2115 struct label_entry
*l
;
2116 bool warned_p
= false;
2117 if (!should_warn_for_implicit_fallthrough (gsi_p
, label
))
2119 else if (gimple_code (prev
) == GIMPLE_LABEL
2120 && (label
= gimple_label_label (as_a
<glabel
*> (prev
)))
2121 && (l
= find_label_entry (&labels
, label
)))
2122 warned_p
= warning_at (l
->loc
, OPT_Wimplicit_fallthrough_
,
2123 "this statement may fall through");
2124 else if (!gimple_call_internal_p (prev
, IFN_FALLTHROUGH
)
2125 /* Try to be clever and don't warn when the statement
2126 can't actually fall through. */
2127 && gimple_stmt_may_fallthru (prev
)
2128 && gimple_has_location (prev
))
2129 warned_p
= warning_at (gimple_location (prev
),
2130 OPT_Wimplicit_fallthrough_
,
2131 "this statement may fall through");
2133 inform (gimple_location (next
), "here");
2135 /* Mark this label as processed so as to prevent multiple
2136 warnings in nested switches. */
2137 FALLTHROUGH_LABEL_P (label
) = true;
2139 /* So that next warn_implicit_fallthrough_r will start looking for
2140 a new sequence starting with this label. */
2151 /* Warn when a switch case falls through. */
2154 maybe_warn_implicit_fallthrough (gimple_seq seq
)
2156 if (!warn_implicit_fallthrough
)
2159 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2162 || lang_GNU_OBJC ()))
2165 struct walk_stmt_info wi
;
2166 memset (&wi
, 0, sizeof (wi
));
2167 walk_gimple_seq (seq
, warn_implicit_fallthrough_r
, NULL
, &wi
);
2170 /* Callback for walk_gimple_seq. */
2173 expand_FALLTHROUGH_r (gimple_stmt_iterator
*gsi_p
, bool *handled_ops_p
,
2174 struct walk_stmt_info
*)
2176 gimple
*stmt
= gsi_stmt (*gsi_p
);
2178 *handled_ops_p
= true;
2179 switch (gimple_code (stmt
))
2184 case GIMPLE_EH_FILTER
:
2185 case GIMPLE_TRANSACTION
:
2186 /* Walk the sub-statements. */
2187 *handled_ops_p
= false;
2190 if (gimple_call_internal_p (stmt
, IFN_FALLTHROUGH
))
2192 gsi_remove (gsi_p
, true);
2193 if (gsi_end_p (*gsi_p
))
2194 return integer_zero_node
;
2197 location_t loc
= gimple_location (stmt
);
2199 gimple_stmt_iterator gsi2
= *gsi_p
;
2200 stmt
= gsi_stmt (gsi2
);
2201 if (gimple_code (stmt
) == GIMPLE_GOTO
&& !gimple_has_location (stmt
))
2203 /* Go on until the artificial label. */
2204 tree goto_dest
= gimple_goto_dest (stmt
);
2205 for (; !gsi_end_p (gsi2
); gsi_next (&gsi2
))
2207 if (gimple_code (gsi_stmt (gsi2
)) == GIMPLE_LABEL
2208 && gimple_label_label (as_a
<glabel
*> (gsi_stmt (gsi2
)))
2213 /* Not found? Stop. */
2214 if (gsi_end_p (gsi2
))
2217 /* Look one past it. */
2221 /* We're looking for a case label or default label here. */
2222 while (!gsi_end_p (gsi2
))
2224 stmt
= gsi_stmt (gsi2
);
2225 if (gimple_code (stmt
) == GIMPLE_LABEL
)
2227 tree label
= gimple_label_label (as_a
<glabel
*> (stmt
));
2228 if (gimple_has_location (stmt
) && DECL_ARTIFICIAL (label
))
2235 /* Something other than a label. That's not expected. */
2240 warning_at (loc
, 0, "attribute %<fallthrough%> not preceding "
2241 "a case label or default label");
2250 /* Expand all FALLTHROUGH () calls in SEQ. */
2253 expand_FALLTHROUGH (gimple_seq
*seq_p
)
2255 struct walk_stmt_info wi
;
2256 memset (&wi
, 0, sizeof (wi
));
2257 walk_gimple_seq_mod (seq_p
, expand_FALLTHROUGH_r
, NULL
, &wi
);
2261 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2264 static enum gimplify_status
2265 gimplify_switch_expr (tree
*expr_p
, gimple_seq
*pre_p
)
2267 tree switch_expr
= *expr_p
;
2268 gimple_seq switch_body_seq
= NULL
;
2269 enum gimplify_status ret
;
2270 tree index_type
= TREE_TYPE (switch_expr
);
2271 if (index_type
== NULL_TREE
)
2272 index_type
= TREE_TYPE (SWITCH_COND (switch_expr
));
2274 ret
= gimplify_expr (&SWITCH_COND (switch_expr
), pre_p
, NULL
, is_gimple_val
,
2276 if (ret
== GS_ERROR
|| ret
== GS_UNHANDLED
)
2279 if (SWITCH_BODY (switch_expr
))
2282 vec
<tree
> saved_labels
;
2283 hash_set
<tree
> *saved_live_switch_vars
= NULL
;
2284 tree default_case
= NULL_TREE
;
2285 gswitch
*switch_stmt
;
2287 /* If someone can be bothered to fill in the labels, they can
2288 be bothered to null out the body too. */
2289 gcc_assert (!SWITCH_LABELS (switch_expr
));
2291 /* Save old labels, get new ones from body, then restore the old
2292 labels. Save all the things from the switch body to append after. */
2293 saved_labels
= gimplify_ctxp
->case_labels
;
2294 gimplify_ctxp
->case_labels
.create (8);
2296 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2297 saved_live_switch_vars
= gimplify_ctxp
->live_switch_vars
;
2298 tree_code body_type
= TREE_CODE (SWITCH_BODY (switch_expr
));
2299 if (body_type
== BIND_EXPR
|| body_type
== STATEMENT_LIST
)
2300 gimplify_ctxp
->live_switch_vars
= new hash_set
<tree
> (4);
2302 gimplify_ctxp
->live_switch_vars
= NULL
;
2304 bool old_in_switch_expr
= gimplify_ctxp
->in_switch_expr
;
2305 gimplify_ctxp
->in_switch_expr
= true;
2307 gimplify_stmt (&SWITCH_BODY (switch_expr
), &switch_body_seq
);
2309 gimplify_ctxp
->in_switch_expr
= old_in_switch_expr
;
2310 maybe_warn_switch_unreachable (switch_body_seq
);
2311 maybe_warn_implicit_fallthrough (switch_body_seq
);
2312 /* Only do this for the outermost GIMPLE_SWITCH. */
2313 if (!gimplify_ctxp
->in_switch_expr
)
2314 expand_FALLTHROUGH (&switch_body_seq
);
2316 labels
= gimplify_ctxp
->case_labels
;
2317 gimplify_ctxp
->case_labels
= saved_labels
;
2319 if (gimplify_ctxp
->live_switch_vars
)
2321 gcc_assert (gimplify_ctxp
->live_switch_vars
->elements () == 0);
2322 delete gimplify_ctxp
->live_switch_vars
;
2324 gimplify_ctxp
->live_switch_vars
= saved_live_switch_vars
;
2326 preprocess_case_label_vec_for_gimple (labels
, index_type
,
2331 glabel
*new_default
;
2334 = build_case_label (NULL_TREE
, NULL_TREE
,
2335 create_artificial_label (UNKNOWN_LOCATION
));
2336 new_default
= gimple_build_label (CASE_LABEL (default_case
));
2337 gimplify_seq_add_stmt (&switch_body_seq
, new_default
);
2340 switch_stmt
= gimple_build_switch (SWITCH_COND (switch_expr
),
2341 default_case
, labels
);
2342 gimplify_seq_add_stmt (pre_p
, switch_stmt
);
2343 gimplify_seq_add_seq (pre_p
, switch_body_seq
);
2347 gcc_assert (SWITCH_LABELS (switch_expr
));
2352 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2354 static enum gimplify_status
2355 gimplify_label_expr (tree
*expr_p
, gimple_seq
*pre_p
)
2357 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p
))
2358 == current_function_decl
);
2360 tree label
= LABEL_EXPR_LABEL (*expr_p
);
2361 glabel
*label_stmt
= gimple_build_label (label
);
2362 gimple_set_location (label_stmt
, EXPR_LOCATION (*expr_p
));
2363 gimplify_seq_add_stmt (pre_p
, label_stmt
);
2365 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label
)))
2366 gimple_seq_add_stmt (pre_p
, gimple_build_predict (PRED_COLD_LABEL
,
2368 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label
)))
2369 gimple_seq_add_stmt (pre_p
, gimple_build_predict (PRED_HOT_LABEL
,
2375 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2377 static enum gimplify_status
2378 gimplify_case_label_expr (tree
*expr_p
, gimple_seq
*pre_p
)
2380 struct gimplify_ctx
*ctxp
;
2383 /* Invalid programs can play Duff's Device type games with, for example,
2384 #pragma omp parallel. At least in the C front end, we don't
2385 detect such invalid branches until after gimplification, in the
2386 diagnose_omp_blocks pass. */
2387 for (ctxp
= gimplify_ctxp
; ; ctxp
= ctxp
->prev_context
)
2388 if (ctxp
->case_labels
.exists ())
2391 label_stmt
= gimple_build_label (CASE_LABEL (*expr_p
));
2392 gimple_set_location (label_stmt
, EXPR_LOCATION (*expr_p
));
2393 ctxp
->case_labels
.safe_push (*expr_p
);
2394 gimplify_seq_add_stmt (pre_p
, label_stmt
);
2399 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2403 build_and_jump (tree
*label_p
)
2405 if (label_p
== NULL
)
2406 /* If there's nowhere to jump, just fall through. */
2409 if (*label_p
== NULL_TREE
)
2411 tree label
= create_artificial_label (UNKNOWN_LOCATION
);
2415 return build1 (GOTO_EXPR
, void_type_node
, *label_p
);
2418 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2419 This also involves building a label to jump to and communicating it to
2420 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2422 static enum gimplify_status
2423 gimplify_exit_expr (tree
*expr_p
)
2425 tree cond
= TREE_OPERAND (*expr_p
, 0);
2428 expr
= build_and_jump (&gimplify_ctxp
->exit_label
);
2429 expr
= build3 (COND_EXPR
, void_type_node
, cond
, expr
, NULL_TREE
);
2435 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2436 different from its canonical type, wrap the whole thing inside a
2437 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2440 The canonical type of a COMPONENT_REF is the type of the field being
2441 referenced--unless the field is a bit-field which can be read directly
2442 in a smaller mode, in which case the canonical type is the
2443 sign-appropriate type corresponding to that mode. */
2446 canonicalize_component_ref (tree
*expr_p
)
2448 tree expr
= *expr_p
;
2451 gcc_assert (TREE_CODE (expr
) == COMPONENT_REF
);
2453 if (INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
2454 type
= TREE_TYPE (get_unwidened (expr
, NULL_TREE
));
2456 type
= TREE_TYPE (TREE_OPERAND (expr
, 1));
2458 /* One could argue that all the stuff below is not necessary for
2459 the non-bitfield case and declare it a FE error if type
2460 adjustment would be needed. */
2461 if (TREE_TYPE (expr
) != type
)
2463 #ifdef ENABLE_TYPES_CHECKING
2464 tree old_type
= TREE_TYPE (expr
);
2468 /* We need to preserve qualifiers and propagate them from
2470 type_quals
= TYPE_QUALS (type
)
2471 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2472 if (TYPE_QUALS (type
) != type_quals
)
2473 type
= build_qualified_type (TYPE_MAIN_VARIANT (type
), type_quals
);
2475 /* Set the type of the COMPONENT_REF to the underlying type. */
2476 TREE_TYPE (expr
) = type
;
2478 #ifdef ENABLE_TYPES_CHECKING
2479 /* It is now a FE error, if the conversion from the canonical
2480 type to the original expression type is not useless. */
2481 gcc_assert (useless_type_conversion_p (old_type
, type
));
2486 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2487 to foo, embed that change in the ADDR_EXPR by converting
2492 where L is the lower bound. For simplicity, only do this for constant
2494 The constraint is that the type of &array[L] is trivially convertible
2498 canonicalize_addr_expr (tree
*expr_p
)
2500 tree expr
= *expr_p
;
2501 tree addr_expr
= TREE_OPERAND (expr
, 0);
2502 tree datype
, ddatype
, pddatype
;
2504 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2505 if (!POINTER_TYPE_P (TREE_TYPE (expr
))
2506 || TREE_CODE (addr_expr
) != ADDR_EXPR
)
2509 /* The addr_expr type should be a pointer to an array. */
2510 datype
= TREE_TYPE (TREE_TYPE (addr_expr
));
2511 if (TREE_CODE (datype
) != ARRAY_TYPE
)
2514 /* The pointer to element type shall be trivially convertible to
2515 the expression pointer type. */
2516 ddatype
= TREE_TYPE (datype
);
2517 pddatype
= build_pointer_type (ddatype
);
2518 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)),
2522 /* The lower bound and element sizes must be constant. */
2523 if (!TYPE_SIZE_UNIT (ddatype
)
2524 || TREE_CODE (TYPE_SIZE_UNIT (ddatype
)) != INTEGER_CST
2525 || !TYPE_DOMAIN (datype
) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype
))
2526 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype
))) != INTEGER_CST
)
2529 /* All checks succeeded. Build a new node to merge the cast. */
2530 *expr_p
= build4 (ARRAY_REF
, ddatype
, TREE_OPERAND (addr_expr
, 0),
2531 TYPE_MIN_VALUE (TYPE_DOMAIN (datype
)),
2532 NULL_TREE
, NULL_TREE
);
2533 *expr_p
= build1 (ADDR_EXPR
, pddatype
, *expr_p
);
2535 /* We can have stripped a required restrict qualifier above. */
2536 if (!useless_type_conversion_p (TREE_TYPE (expr
), TREE_TYPE (*expr_p
)))
2537 *expr_p
= fold_convert (TREE_TYPE (expr
), *expr_p
);
2540 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2541 underneath as appropriate. */
2543 static enum gimplify_status
2544 gimplify_conversion (tree
*expr_p
)
2546 location_t loc
= EXPR_LOCATION (*expr_p
);
2547 gcc_assert (CONVERT_EXPR_P (*expr_p
));
2549 /* Then strip away all but the outermost conversion. */
2550 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p
, 0));
2552 /* And remove the outermost conversion if it's useless. */
2553 if (tree_ssa_useless_type_conversion (*expr_p
))
2554 *expr_p
= TREE_OPERAND (*expr_p
, 0);
2556 /* If we still have a conversion at the toplevel,
2557 then canonicalize some constructs. */
2558 if (CONVERT_EXPR_P (*expr_p
))
2560 tree sub
= TREE_OPERAND (*expr_p
, 0);
2562 /* If a NOP conversion is changing the type of a COMPONENT_REF
2563 expression, then canonicalize its type now in order to expose more
2564 redundant conversions. */
2565 if (TREE_CODE (sub
) == COMPONENT_REF
)
2566 canonicalize_component_ref (&TREE_OPERAND (*expr_p
, 0));
2568 /* If a NOP conversion is changing a pointer to array of foo
2569 to a pointer to foo, embed that change in the ADDR_EXPR. */
2570 else if (TREE_CODE (sub
) == ADDR_EXPR
)
2571 canonicalize_addr_expr (expr_p
);
2574 /* If we have a conversion to a non-register type force the
2575 use of a VIEW_CONVERT_EXPR instead. */
2576 if (CONVERT_EXPR_P (*expr_p
) && !is_gimple_reg_type (TREE_TYPE (*expr_p
)))
2577 *expr_p
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, TREE_TYPE (*expr_p
),
2578 TREE_OPERAND (*expr_p
, 0));
2580 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2581 if (TREE_CODE (*expr_p
) == CONVERT_EXPR
)
2582 TREE_SET_CODE (*expr_p
, NOP_EXPR
);
2587 /* Nonlocal VLAs seen in the current function. */
2588 static hash_set
<tree
> *nonlocal_vlas
;
2590 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2591 static tree nonlocal_vla_vars
;
2593 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2594 DECL_VALUE_EXPR, and it's worth re-examining things. */
2596 static enum gimplify_status
2597 gimplify_var_or_parm_decl (tree
*expr_p
)
2599 tree decl
= *expr_p
;
2601 /* ??? If this is a local variable, and it has not been seen in any
2602 outer BIND_EXPR, then it's probably the result of a duplicate
2603 declaration, for which we've already issued an error. It would
2604 be really nice if the front end wouldn't leak these at all.
2605 Currently the only known culprit is C++ destructors, as seen
2606 in g++.old-deja/g++.jason/binding.C. */
2608 && !DECL_SEEN_IN_BIND_EXPR_P (decl
)
2609 && !TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
)
2610 && decl_function_context (decl
) == current_function_decl
)
2612 gcc_assert (seen_error ());
2616 /* When within an OMP context, notice uses of variables. */
2617 if (gimplify_omp_ctxp
&& omp_notice_variable (gimplify_omp_ctxp
, decl
, true))
2620 /* If the decl is an alias for another expression, substitute it now. */
2621 if (DECL_HAS_VALUE_EXPR_P (decl
))
2623 tree value_expr
= DECL_VALUE_EXPR (decl
);
2625 /* For referenced nonlocal VLAs add a decl for debugging purposes
2626 to the current function. */
2628 && TREE_CODE (DECL_SIZE_UNIT (decl
)) != INTEGER_CST
2629 && nonlocal_vlas
!= NULL
2630 && TREE_CODE (value_expr
) == INDIRECT_REF
2631 && TREE_CODE (TREE_OPERAND (value_expr
, 0)) == VAR_DECL
2632 && decl_function_context (decl
) != current_function_decl
)
2634 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
2636 && (ctx
->region_type
== ORT_WORKSHARE
2637 || ctx
->region_type
== ORT_SIMD
2638 || ctx
->region_type
== ORT_ACC
))
2639 ctx
= ctx
->outer_context
;
2640 if (!ctx
&& !nonlocal_vlas
->add (decl
))
2642 tree copy
= copy_node (decl
);
2644 lang_hooks
.dup_lang_specific_decl (copy
);
2645 SET_DECL_RTL (copy
, 0);
2646 TREE_USED (copy
) = 1;
2647 DECL_CHAIN (copy
) = nonlocal_vla_vars
;
2648 nonlocal_vla_vars
= copy
;
2649 SET_DECL_VALUE_EXPR (copy
, unshare_expr (value_expr
));
2650 DECL_HAS_VALUE_EXPR_P (copy
) = 1;
2654 *expr_p
= unshare_expr (value_expr
);
2661 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2664 recalculate_side_effects (tree t
)
2666 enum tree_code code
= TREE_CODE (t
);
2667 int len
= TREE_OPERAND_LENGTH (t
);
2670 switch (TREE_CODE_CLASS (code
))
2672 case tcc_expression
:
2678 case PREDECREMENT_EXPR
:
2679 case PREINCREMENT_EXPR
:
2680 case POSTDECREMENT_EXPR
:
2681 case POSTINCREMENT_EXPR
:
2682 /* All of these have side-effects, no matter what their
2691 case tcc_comparison
: /* a comparison expression */
2692 case tcc_unary
: /* a unary arithmetic expression */
2693 case tcc_binary
: /* a binary arithmetic expression */
2694 case tcc_reference
: /* a reference */
2695 case tcc_vl_exp
: /* a function call */
2696 TREE_SIDE_EFFECTS (t
) = TREE_THIS_VOLATILE (t
);
2697 for (i
= 0; i
< len
; ++i
)
2699 tree op
= TREE_OPERAND (t
, i
);
2700 if (op
&& TREE_SIDE_EFFECTS (op
))
2701 TREE_SIDE_EFFECTS (t
) = 1;
2706 /* No side-effects. */
2714 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2718 : min_lval '[' val ']'
2720 | compound_lval '[' val ']'
2721 | compound_lval '.' ID
2723 This is not part of the original SIMPLE definition, which separates
2724 array and member references, but it seems reasonable to handle them
2725 together. Also, this way we don't run into problems with union
2726 aliasing; gcc requires that for accesses through a union to alias, the
2727 union reference must be explicit, which was not always the case when we
2728 were splitting up array and member refs.
2730 PRE_P points to the sequence where side effects that must happen before
2731 *EXPR_P should be stored.
2733 POST_P points to the sequence where side effects that must happen after
2734 *EXPR_P should be stored. */
2736 static enum gimplify_status
2737 gimplify_compound_lval (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
2738 fallback_t fallback
)
2741 enum gimplify_status ret
= GS_ALL_DONE
, tret
;
2743 location_t loc
= EXPR_LOCATION (*expr_p
);
2744 tree expr
= *expr_p
;
2746 /* Create a stack of the subexpressions so later we can walk them in
2747 order from inner to outer. */
2748 auto_vec
<tree
, 10> expr_stack
;
2750 /* We can handle anything that get_inner_reference can deal with. */
2751 for (p
= expr_p
; ; p
= &TREE_OPERAND (*p
, 0))
2754 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2755 if (TREE_CODE (*p
) == INDIRECT_REF
)
2756 *p
= fold_indirect_ref_loc (loc
, *p
);
2758 if (handled_component_p (*p
))
2760 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2761 additional COMPONENT_REFs. */
2762 else if ((VAR_P (*p
) || TREE_CODE (*p
) == PARM_DECL
)
2763 && gimplify_var_or_parm_decl (p
) == GS_OK
)
2768 expr_stack
.safe_push (*p
);
2771 gcc_assert (expr_stack
.length ());
2773 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2774 walked through and P points to the innermost expression.
2776 Java requires that we elaborated nodes in source order. That
2777 means we must gimplify the inner expression followed by each of
2778 the indices, in order. But we can't gimplify the inner
2779 expression until we deal with any variable bounds, sizes, or
2780 positions in order to deal with PLACEHOLDER_EXPRs.
2782 So we do this in three steps. First we deal with the annotations
2783 for any variables in the components, then we gimplify the base,
2784 then we gimplify any indices, from left to right. */
2785 for (i
= expr_stack
.length () - 1; i
>= 0; i
--)
2787 tree t
= expr_stack
[i
];
2789 if (TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
2791 /* Gimplify the low bound and element type size and put them into
2792 the ARRAY_REF. If these values are set, they have already been
2794 if (TREE_OPERAND (t
, 2) == NULL_TREE
)
2796 tree low
= unshare_expr (array_ref_low_bound (t
));
2797 if (!is_gimple_min_invariant (low
))
2799 TREE_OPERAND (t
, 2) = low
;
2800 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
,
2801 post_p
, is_gimple_reg
,
2803 ret
= MIN (ret
, tret
);
2808 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
, post_p
,
2809 is_gimple_reg
, fb_rvalue
);
2810 ret
= MIN (ret
, tret
);
2813 if (TREE_OPERAND (t
, 3) == NULL_TREE
)
2815 tree elmt_type
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0)));
2816 tree elmt_size
= unshare_expr (array_ref_element_size (t
));
2817 tree factor
= size_int (TYPE_ALIGN_UNIT (elmt_type
));
2819 /* Divide the element size by the alignment of the element
2822 = size_binop_loc (loc
, EXACT_DIV_EXPR
, elmt_size
, factor
);
2824 if (!is_gimple_min_invariant (elmt_size
))
2826 TREE_OPERAND (t
, 3) = elmt_size
;
2827 tret
= gimplify_expr (&TREE_OPERAND (t
, 3), pre_p
,
2828 post_p
, is_gimple_reg
,
2830 ret
= MIN (ret
, tret
);
2835 tret
= gimplify_expr (&TREE_OPERAND (t
, 3), pre_p
, post_p
,
2836 is_gimple_reg
, fb_rvalue
);
2837 ret
= MIN (ret
, tret
);
2840 else if (TREE_CODE (t
) == COMPONENT_REF
)
2842 /* Set the field offset into T and gimplify it. */
2843 if (TREE_OPERAND (t
, 2) == NULL_TREE
)
2845 tree offset
= unshare_expr (component_ref_field_offset (t
));
2846 tree field
= TREE_OPERAND (t
, 1);
2848 = size_int (DECL_OFFSET_ALIGN (field
) / BITS_PER_UNIT
);
2850 /* Divide the offset by its alignment. */
2851 offset
= size_binop_loc (loc
, EXACT_DIV_EXPR
, offset
, factor
);
2853 if (!is_gimple_min_invariant (offset
))
2855 TREE_OPERAND (t
, 2) = offset
;
2856 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
,
2857 post_p
, is_gimple_reg
,
2859 ret
= MIN (ret
, tret
);
2864 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
, post_p
,
2865 is_gimple_reg
, fb_rvalue
);
2866 ret
= MIN (ret
, tret
);
2871 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2872 so as to match the min_lval predicate. Failure to do so may result
2873 in the creation of large aggregate temporaries. */
2874 tret
= gimplify_expr (p
, pre_p
, post_p
, is_gimple_min_lval
,
2875 fallback
| fb_lvalue
);
2876 ret
= MIN (ret
, tret
);
2878 /* And finally, the indices and operands of ARRAY_REF. During this
2879 loop we also remove any useless conversions. */
2880 for (; expr_stack
.length () > 0; )
2882 tree t
= expr_stack
.pop ();
2884 if (TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
2886 /* Gimplify the dimension. */
2887 if (!is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
2889 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), pre_p
, post_p
,
2890 is_gimple_val
, fb_rvalue
);
2891 ret
= MIN (ret
, tret
);
2895 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t
, 0));
2897 /* The innermost expression P may have originally had
2898 TREE_SIDE_EFFECTS set which would have caused all the outer
2899 expressions in *EXPR_P leading to P to also have had
2900 TREE_SIDE_EFFECTS set. */
2901 recalculate_side_effects (t
);
2904 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2905 if ((fallback
& fb_rvalue
) && TREE_CODE (*expr_p
) == COMPONENT_REF
)
2907 canonicalize_component_ref (expr_p
);
2910 expr_stack
.release ();
2912 gcc_assert (*expr_p
== expr
|| ret
!= GS_ALL_DONE
);
2917 /* Gimplify the self modifying expression pointed to by EXPR_P
2920 PRE_P points to the list where side effects that must happen before
2921 *EXPR_P should be stored.
2923 POST_P points to the list where side effects that must happen after
2924 *EXPR_P should be stored.
2926 WANT_VALUE is nonzero iff we want to use the value of this expression
2927 in another expression.
2929 ARITH_TYPE is the type the computation should be performed in. */
2931 enum gimplify_status
2932 gimplify_self_mod_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
2933 bool want_value
, tree arith_type
)
2935 enum tree_code code
;
2936 tree lhs
, lvalue
, rhs
, t1
;
2937 gimple_seq post
= NULL
, *orig_post_p
= post_p
;
2939 enum tree_code arith_code
;
2940 enum gimplify_status ret
;
2941 location_t loc
= EXPR_LOCATION (*expr_p
);
2943 code
= TREE_CODE (*expr_p
);
2945 gcc_assert (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
2946 || code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
);
2948 /* Prefix or postfix? */
2949 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
2950 /* Faster to treat as prefix if result is not used. */
2951 postfix
= want_value
;
2955 /* For postfix, make sure the inner expression's post side effects
2956 are executed after side effects from this expression. */
2960 /* Add or subtract? */
2961 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2962 arith_code
= PLUS_EXPR
;
2964 arith_code
= MINUS_EXPR
;
2966 /* Gimplify the LHS into a GIMPLE lvalue. */
2967 lvalue
= TREE_OPERAND (*expr_p
, 0);
2968 ret
= gimplify_expr (&lvalue
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
2969 if (ret
== GS_ERROR
)
2972 /* Extract the operands to the arithmetic operation. */
2974 rhs
= TREE_OPERAND (*expr_p
, 1);
2976 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2977 that as the result value and in the postqueue operation. */
2980 ret
= gimplify_expr (&lhs
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
2981 if (ret
== GS_ERROR
)
2984 lhs
= get_initialized_tmp_var (lhs
, pre_p
, NULL
);
2987 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2988 if (POINTER_TYPE_P (TREE_TYPE (lhs
)))
2990 rhs
= convert_to_ptrofftype_loc (loc
, rhs
);
2991 if (arith_code
== MINUS_EXPR
)
2992 rhs
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
2993 t1
= fold_build2 (POINTER_PLUS_EXPR
, TREE_TYPE (*expr_p
), lhs
, rhs
);
2996 t1
= fold_convert (TREE_TYPE (*expr_p
),
2997 fold_build2 (arith_code
, arith_type
,
2998 fold_convert (arith_type
, lhs
),
2999 fold_convert (arith_type
, rhs
)));
3003 gimplify_assign (lvalue
, t1
, pre_p
);
3004 gimplify_seq_add_seq (orig_post_p
, post
);
3010 *expr_p
= build2 (MODIFY_EXPR
, TREE_TYPE (lvalue
), lvalue
, t1
);
3015 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3018 maybe_with_size_expr (tree
*expr_p
)
3020 tree expr
= *expr_p
;
3021 tree type
= TREE_TYPE (expr
);
3024 /* If we've already wrapped this or the type is error_mark_node, we can't do
3026 if (TREE_CODE (expr
) == WITH_SIZE_EXPR
3027 || type
== error_mark_node
)
3030 /* If the size isn't known or is a constant, we have nothing to do. */
3031 size
= TYPE_SIZE_UNIT (type
);
3032 if (!size
|| TREE_CODE (size
) == INTEGER_CST
)
3035 /* Otherwise, make a WITH_SIZE_EXPR. */
3036 size
= unshare_expr (size
);
3037 size
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, expr
);
3038 *expr_p
= build2 (WITH_SIZE_EXPR
, type
, expr
, size
);
3041 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3042 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3043 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3044 gimplified to an SSA name. */
3046 enum gimplify_status
3047 gimplify_arg (tree
*arg_p
, gimple_seq
*pre_p
, location_t call_location
,
3050 bool (*test
) (tree
);
3053 /* In general, we allow lvalues for function arguments to avoid
3054 extra overhead of copying large aggregates out of even larger
3055 aggregates into temporaries only to copy the temporaries to
3056 the argument list. Make optimizers happy by pulling out to
3057 temporaries those types that fit in registers. */
3058 if (is_gimple_reg_type (TREE_TYPE (*arg_p
)))
3059 test
= is_gimple_val
, fb
= fb_rvalue
;
3062 test
= is_gimple_lvalue
, fb
= fb_either
;
3063 /* Also strip a TARGET_EXPR that would force an extra copy. */
3064 if (TREE_CODE (*arg_p
) == TARGET_EXPR
)
3066 tree init
= TARGET_EXPR_INITIAL (*arg_p
);
3068 && !VOID_TYPE_P (TREE_TYPE (init
)))
3073 /* If this is a variable sized type, we must remember the size. */
3074 maybe_with_size_expr (arg_p
);
3076 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3077 /* Make sure arguments have the same location as the function call
3079 protected_set_expr_location (*arg_p
, call_location
);
3081 /* There is a sequence point before a function call. Side effects in
3082 the argument list must occur before the actual call. So, when
3083 gimplifying arguments, force gimplify_expr to use an internal
3084 post queue which is then appended to the end of PRE_P. */
3085 return gimplify_expr (arg_p
, pre_p
, NULL
, test
, fb
, allow_ssa
);
3088 /* Don't fold inside offloading or taskreg regions: it can break code by
3089 adding decl references that weren't in the source. We'll do it during
3090 omplower pass instead. */
3093 maybe_fold_stmt (gimple_stmt_iterator
*gsi
)
3095 struct gimplify_omp_ctx
*ctx
;
3096 for (ctx
= gimplify_omp_ctxp
; ctx
; ctx
= ctx
->outer_context
)
3097 if ((ctx
->region_type
& (ORT_TARGET
| ORT_PARALLEL
| ORT_TASK
)) != 0)
3099 return fold_stmt (gsi
);
3102 /* Add a gimple call to __builtin_cilk_detach to GIMPLE sequence PRE_P,
3103 with the pointer to the proper cilk frame. */
3105 gimplify_cilk_detach (gimple_seq
*pre_p
)
3107 tree frame
= cfun
->cilk_frame_decl
;
3108 tree ptrf
= build1 (ADDR_EXPR
, cilk_frame_ptr_type_decl
,
3110 gcall
*detach
= gimple_build_call (cilk_detach_fndecl
, 1,
3112 gimplify_seq_add_stmt(pre_p
, detach
);
3115 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3116 WANT_VALUE is true if the result of the call is desired. */
3118 static enum gimplify_status
3119 gimplify_call_expr (tree
*expr_p
, gimple_seq
*pre_p
, bool want_value
)
3121 tree fndecl
, parms
, p
, fnptrtype
;
3122 enum gimplify_status ret
;
3125 bool builtin_va_start_p
= false;
3126 location_t loc
= EXPR_LOCATION (*expr_p
);
3128 gcc_assert (TREE_CODE (*expr_p
) == CALL_EXPR
);
3130 /* For reliable diagnostics during inlining, it is necessary that
3131 every call_expr be annotated with file and line. */
3132 if (! EXPR_HAS_LOCATION (*expr_p
))
3133 SET_EXPR_LOCATION (*expr_p
, input_location
);
3135 /* Gimplify internal functions created in the FEs. */
3136 if (CALL_EXPR_FN (*expr_p
) == NULL_TREE
)
3141 nargs
= call_expr_nargs (*expr_p
);
3142 enum internal_fn ifn
= CALL_EXPR_IFN (*expr_p
);
3143 auto_vec
<tree
> vargs (nargs
);
3145 for (i
= 0; i
< nargs
; i
++)
3147 gimplify_arg (&CALL_EXPR_ARG (*expr_p
, i
), pre_p
,
3148 EXPR_LOCATION (*expr_p
));
3149 vargs
.quick_push (CALL_EXPR_ARG (*expr_p
, i
));
3152 if (EXPR_CILK_SPAWN (*expr_p
))
3153 gimplify_cilk_detach (pre_p
);
3154 gcall
*call
= gimple_build_call_internal_vec (ifn
, vargs
);
3155 gimple_call_set_nothrow (call
, TREE_NOTHROW (*expr_p
));
3156 gimplify_seq_add_stmt (pre_p
, call
);
3160 /* This may be a call to a builtin function.
3162 Builtin function calls may be transformed into different
3163 (and more efficient) builtin function calls under certain
3164 circumstances. Unfortunately, gimplification can muck things
3165 up enough that the builtin expanders are not aware that certain
3166 transformations are still valid.
3168 So we attempt transformation/gimplification of the call before
3169 we gimplify the CALL_EXPR. At this time we do not manage to
3170 transform all calls in the same manner as the expanders do, but
3171 we do transform most of them. */
3172 fndecl
= get_callee_fndecl (*expr_p
);
3174 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
3175 switch (DECL_FUNCTION_CODE (fndecl
))
3177 CASE_BUILT_IN_ALLOCA
:
3178 /* If the call has been built for a variable-sized object, then we
3179 want to restore the stack level when the enclosing BIND_EXPR is
3180 exited to reclaim the allocated space; otherwise, we precisely
3181 need to do the opposite and preserve the latest stack level. */
3182 if (CALL_ALLOCA_FOR_VAR_P (*expr_p
))
3183 gimplify_ctxp
->save_stack
= true;
3185 gimplify_ctxp
->keep_stack
= true;
3188 case BUILT_IN_VA_START
:
3190 builtin_va_start_p
= TRUE
;
3191 if (call_expr_nargs (*expr_p
) < 2)
3193 error ("too few arguments to function %<va_start%>");
3194 *expr_p
= build_empty_stmt (EXPR_LOCATION (*expr_p
));
3198 if (fold_builtin_next_arg (*expr_p
, true))
3200 *expr_p
= build_empty_stmt (EXPR_LOCATION (*expr_p
));
3209 if (fndecl
&& DECL_BUILT_IN (fndecl
))
3211 tree new_tree
= fold_call_expr (input_location
, *expr_p
, !want_value
);
3212 if (new_tree
&& new_tree
!= *expr_p
)
3214 /* There was a transformation of this call which computes the
3215 same value, but in a more efficient way. Return and try
3222 /* Remember the original function pointer type. */
3223 fnptrtype
= TREE_TYPE (CALL_EXPR_FN (*expr_p
));
3225 /* There is a sequence point before the call, so any side effects in
3226 the calling expression must occur before the actual call. Force
3227 gimplify_expr to use an internal post queue. */
3228 ret
= gimplify_expr (&CALL_EXPR_FN (*expr_p
), pre_p
, NULL
,
3229 is_gimple_call_addr
, fb_rvalue
);
3231 nargs
= call_expr_nargs (*expr_p
);
3233 /* Get argument types for verification. */
3234 fndecl
= get_callee_fndecl (*expr_p
);
3237 parms
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3239 parms
= TYPE_ARG_TYPES (TREE_TYPE (fnptrtype
));
3241 if (fndecl
&& DECL_ARGUMENTS (fndecl
))
3242 p
= DECL_ARGUMENTS (fndecl
);
3247 for (i
= 0; i
< nargs
&& p
; i
++, p
= TREE_CHAIN (p
))
3250 /* If the last argument is __builtin_va_arg_pack () and it is not
3251 passed as a named argument, decrease the number of CALL_EXPR
3252 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3255 && TREE_CODE (CALL_EXPR_ARG (*expr_p
, nargs
- 1)) == CALL_EXPR
)
3257 tree last_arg
= CALL_EXPR_ARG (*expr_p
, nargs
- 1);
3258 tree last_arg_fndecl
= get_callee_fndecl (last_arg
);
3261 && TREE_CODE (last_arg_fndecl
) == FUNCTION_DECL
3262 && DECL_BUILT_IN_CLASS (last_arg_fndecl
) == BUILT_IN_NORMAL
3263 && DECL_FUNCTION_CODE (last_arg_fndecl
) == BUILT_IN_VA_ARG_PACK
)
3265 tree call
= *expr_p
;
3268 *expr_p
= build_call_array_loc (loc
, TREE_TYPE (call
),
3269 CALL_EXPR_FN (call
),
3270 nargs
, CALL_EXPR_ARGP (call
));
3272 /* Copy all CALL_EXPR flags, location and block, except
3273 CALL_EXPR_VA_ARG_PACK flag. */
3274 CALL_EXPR_STATIC_CHAIN (*expr_p
) = CALL_EXPR_STATIC_CHAIN (call
);
3275 CALL_EXPR_TAILCALL (*expr_p
) = CALL_EXPR_TAILCALL (call
);
3276 CALL_EXPR_RETURN_SLOT_OPT (*expr_p
)
3277 = CALL_EXPR_RETURN_SLOT_OPT (call
);
3278 CALL_FROM_THUNK_P (*expr_p
) = CALL_FROM_THUNK_P (call
);
3279 SET_EXPR_LOCATION (*expr_p
, EXPR_LOCATION (call
));
3281 /* Set CALL_EXPR_VA_ARG_PACK. */
3282 CALL_EXPR_VA_ARG_PACK (*expr_p
) = 1;
3286 /* If the call returns twice then after building the CFG the call
3287 argument computations will no longer dominate the call because
3288 we add an abnormal incoming edge to the call. So do not use SSA
3290 bool returns_twice
= call_expr_flags (*expr_p
) & ECF_RETURNS_TWICE
;
3292 /* Gimplify the function arguments. */
3295 for (i
= (PUSH_ARGS_REVERSED
? nargs
- 1 : 0);
3296 PUSH_ARGS_REVERSED
? i
>= 0 : i
< nargs
;
3297 PUSH_ARGS_REVERSED
? i
-- : i
++)
3299 enum gimplify_status t
;
3301 /* Avoid gimplifying the second argument to va_start, which needs to
3302 be the plain PARM_DECL. */
3303 if ((i
!= 1) || !builtin_va_start_p
)
3305 t
= gimplify_arg (&CALL_EXPR_ARG (*expr_p
, i
), pre_p
,
3306 EXPR_LOCATION (*expr_p
), ! returns_twice
);
3314 /* Gimplify the static chain. */
3315 if (CALL_EXPR_STATIC_CHAIN (*expr_p
))
3317 if (fndecl
&& !DECL_STATIC_CHAIN (fndecl
))
3318 CALL_EXPR_STATIC_CHAIN (*expr_p
) = NULL
;
3321 enum gimplify_status t
;
3322 t
= gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
,
3323 EXPR_LOCATION (*expr_p
), ! returns_twice
);
3329 /* Verify the function result. */
3330 if (want_value
&& fndecl
3331 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype
))))
3333 error_at (loc
, "using result of function returning %<void%>");
3337 /* Try this again in case gimplification exposed something. */
3338 if (ret
!= GS_ERROR
)
3340 tree new_tree
= fold_call_expr (input_location
, *expr_p
, !want_value
);
3342 if (new_tree
&& new_tree
!= *expr_p
)
3344 /* There was a transformation of this call which computes the
3345 same value, but in a more efficient way. Return and try
3353 *expr_p
= error_mark_node
;
3357 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3358 decl. This allows us to eliminate redundant or useless
3359 calls to "const" functions. */
3360 if (TREE_CODE (*expr_p
) == CALL_EXPR
)
3362 int flags
= call_expr_flags (*expr_p
);
3363 if (flags
& (ECF_CONST
| ECF_PURE
)
3364 /* An infinite loop is considered a side effect. */
3365 && !(flags
& (ECF_LOOPING_CONST_OR_PURE
)))
3366 TREE_SIDE_EFFECTS (*expr_p
) = 0;
3369 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3370 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3371 form and delegate the creation of a GIMPLE_CALL to
3372 gimplify_modify_expr. This is always possible because when
3373 WANT_VALUE is true, the caller wants the result of this call into
3374 a temporary, which means that we will emit an INIT_EXPR in
3375 internal_get_tmp_var which will then be handled by
3376 gimplify_modify_expr. */
3379 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3380 have to do is replicate it as a GIMPLE_CALL tuple. */
3381 gimple_stmt_iterator gsi
;
3382 call
= gimple_build_call_from_tree (*expr_p
, fnptrtype
);
3383 notice_special_calls (call
);
3384 if (EXPR_CILK_SPAWN (*expr_p
))
3385 gimplify_cilk_detach (pre_p
);
3386 gimplify_seq_add_stmt (pre_p
, call
);
3387 gsi
= gsi_last (*pre_p
);
3388 maybe_fold_stmt (&gsi
);
3389 *expr_p
= NULL_TREE
;
3392 /* Remember the original function type. */
3393 CALL_EXPR_FN (*expr_p
) = build1 (NOP_EXPR
, fnptrtype
,
3394 CALL_EXPR_FN (*expr_p
));
3399 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3400 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3402 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3403 condition is true or false, respectively. If null, we should generate
3404 our own to skip over the evaluation of this specific expression.
3406 LOCUS is the source location of the COND_EXPR.
3408 This function is the tree equivalent of do_jump.
3410 shortcut_cond_r should only be called by shortcut_cond_expr. */
3413 shortcut_cond_r (tree pred
, tree
*true_label_p
, tree
*false_label_p
,
3416 tree local_label
= NULL_TREE
;
3417 tree t
, expr
= NULL
;
3419 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3420 retain the shortcut semantics. Just insert the gotos here;
3421 shortcut_cond_expr will append the real blocks later. */
3422 if (TREE_CODE (pred
) == TRUTH_ANDIF_EXPR
)
3424 location_t new_locus
;
3426 /* Turn if (a && b) into
3428 if (a); else goto no;
3429 if (b) goto yes; else goto no;
3432 if (false_label_p
== NULL
)
3433 false_label_p
= &local_label
;
3435 /* Keep the original source location on the first 'if'. */
3436 t
= shortcut_cond_r (TREE_OPERAND (pred
, 0), NULL
, false_label_p
, locus
);
3437 append_to_statement_list (t
, &expr
);
3439 /* Set the source location of the && on the second 'if'. */
3440 new_locus
= EXPR_HAS_LOCATION (pred
) ? EXPR_LOCATION (pred
) : locus
;
3441 t
= shortcut_cond_r (TREE_OPERAND (pred
, 1), true_label_p
, false_label_p
,
3443 append_to_statement_list (t
, &expr
);
3445 else if (TREE_CODE (pred
) == TRUTH_ORIF_EXPR
)
3447 location_t new_locus
;
3449 /* Turn if (a || b) into
3452 if (b) goto yes; else goto no;
3455 if (true_label_p
== NULL
)
3456 true_label_p
= &local_label
;
3458 /* Keep the original source location on the first 'if'. */
3459 t
= shortcut_cond_r (TREE_OPERAND (pred
, 0), true_label_p
, NULL
, locus
);
3460 append_to_statement_list (t
, &expr
);
3462 /* Set the source location of the || on the second 'if'. */
3463 new_locus
= EXPR_HAS_LOCATION (pred
) ? EXPR_LOCATION (pred
) : locus
;
3464 t
= shortcut_cond_r (TREE_OPERAND (pred
, 1), true_label_p
, false_label_p
,
3466 append_to_statement_list (t
, &expr
);
3468 else if (TREE_CODE (pred
) == COND_EXPR
3469 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred
, 1)))
3470 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred
, 2))))
3472 location_t new_locus
;
3474 /* As long as we're messing with gotos, turn if (a ? b : c) into
3476 if (b) goto yes; else goto no;
3478 if (c) goto yes; else goto no;
3480 Don't do this if one of the arms has void type, which can happen
3481 in C++ when the arm is throw. */
3483 /* Keep the original source location on the first 'if'. Set the source
3484 location of the ? on the second 'if'. */
3485 new_locus
= EXPR_HAS_LOCATION (pred
) ? EXPR_LOCATION (pred
) : locus
;
3486 expr
= build3 (COND_EXPR
, void_type_node
, TREE_OPERAND (pred
, 0),
3487 shortcut_cond_r (TREE_OPERAND (pred
, 1), true_label_p
,
3488 false_label_p
, locus
),
3489 shortcut_cond_r (TREE_OPERAND (pred
, 2), true_label_p
,
3490 false_label_p
, new_locus
));
3494 expr
= build3 (COND_EXPR
, void_type_node
, pred
,
3495 build_and_jump (true_label_p
),
3496 build_and_jump (false_label_p
));
3497 SET_EXPR_LOCATION (expr
, locus
);
3502 t
= build1 (LABEL_EXPR
, void_type_node
, local_label
);
3503 append_to_statement_list (t
, &expr
);
3509 /* Given a conditional expression EXPR with short-circuit boolean
3510 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3511 predicate apart into the equivalent sequence of conditionals. */
3514 shortcut_cond_expr (tree expr
)
3516 tree pred
= TREE_OPERAND (expr
, 0);
3517 tree then_
= TREE_OPERAND (expr
, 1);
3518 tree else_
= TREE_OPERAND (expr
, 2);
3519 tree true_label
, false_label
, end_label
, t
;
3521 tree
*false_label_p
;
3522 bool emit_end
, emit_false
, jump_over_else
;
3523 bool then_se
= then_
&& TREE_SIDE_EFFECTS (then_
);
3524 bool else_se
= else_
&& TREE_SIDE_EFFECTS (else_
);
3526 /* First do simple transformations. */
3529 /* If there is no 'else', turn
3532 if (a) if (b) then c. */
3533 while (TREE_CODE (pred
) == TRUTH_ANDIF_EXPR
)
3535 /* Keep the original source location on the first 'if'. */
3536 location_t locus
= EXPR_LOC_OR_LOC (expr
, input_location
);
3537 TREE_OPERAND (expr
, 0) = TREE_OPERAND (pred
, 1);
3538 /* Set the source location of the && on the second 'if'. */
3539 if (EXPR_HAS_LOCATION (pred
))
3540 SET_EXPR_LOCATION (expr
, EXPR_LOCATION (pred
));
3541 then_
= shortcut_cond_expr (expr
);
3542 then_se
= then_
&& TREE_SIDE_EFFECTS (then_
);
3543 pred
= TREE_OPERAND (pred
, 0);
3544 expr
= build3 (COND_EXPR
, void_type_node
, pred
, then_
, NULL_TREE
);
3545 SET_EXPR_LOCATION (expr
, locus
);
3551 /* If there is no 'then', turn
3554 if (a); else if (b); else d. */
3555 while (TREE_CODE (pred
) == TRUTH_ORIF_EXPR
)
3557 /* Keep the original source location on the first 'if'. */
3558 location_t locus
= EXPR_LOC_OR_LOC (expr
, input_location
);
3559 TREE_OPERAND (expr
, 0) = TREE_OPERAND (pred
, 1);
3560 /* Set the source location of the || on the second 'if'. */
3561 if (EXPR_HAS_LOCATION (pred
))
3562 SET_EXPR_LOCATION (expr
, EXPR_LOCATION (pred
));
3563 else_
= shortcut_cond_expr (expr
);
3564 else_se
= else_
&& TREE_SIDE_EFFECTS (else_
);
3565 pred
= TREE_OPERAND (pred
, 0);
3566 expr
= build3 (COND_EXPR
, void_type_node
, pred
, NULL_TREE
, else_
);
3567 SET_EXPR_LOCATION (expr
, locus
);
3571 /* If we're done, great. */
3572 if (TREE_CODE (pred
) != TRUTH_ANDIF_EXPR
3573 && TREE_CODE (pred
) != TRUTH_ORIF_EXPR
)
3576 /* Otherwise we need to mess with gotos. Change
3579 if (a); else goto no;
3582 and recursively gimplify the condition. */
3584 true_label
= false_label
= end_label
= NULL_TREE
;
3586 /* If our arms just jump somewhere, hijack those labels so we don't
3587 generate jumps to jumps. */
3590 && TREE_CODE (then_
) == GOTO_EXPR
3591 && TREE_CODE (GOTO_DESTINATION (then_
)) == LABEL_DECL
)
3593 true_label
= GOTO_DESTINATION (then_
);
3599 && TREE_CODE (else_
) == GOTO_EXPR
3600 && TREE_CODE (GOTO_DESTINATION (else_
)) == LABEL_DECL
)
3602 false_label
= GOTO_DESTINATION (else_
);
3607 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3609 true_label_p
= &true_label
;
3611 true_label_p
= NULL
;
3613 /* The 'else' branch also needs a label if it contains interesting code. */
3614 if (false_label
|| else_se
)
3615 false_label_p
= &false_label
;
3617 false_label_p
= NULL
;
3619 /* If there was nothing else in our arms, just forward the label(s). */
3620 if (!then_se
&& !else_se
)
3621 return shortcut_cond_r (pred
, true_label_p
, false_label_p
,
3622 EXPR_LOC_OR_LOC (expr
, input_location
));
3624 /* If our last subexpression already has a terminal label, reuse it. */
3626 t
= expr_last (else_
);
3628 t
= expr_last (then_
);
3631 if (t
&& TREE_CODE (t
) == LABEL_EXPR
)
3632 end_label
= LABEL_EXPR_LABEL (t
);
3634 /* If we don't care about jumping to the 'else' branch, jump to the end
3635 if the condition is false. */
3637 false_label_p
= &end_label
;
3639 /* We only want to emit these labels if we aren't hijacking them. */
3640 emit_end
= (end_label
== NULL_TREE
);
3641 emit_false
= (false_label
== NULL_TREE
);
3643 /* We only emit the jump over the else clause if we have to--if the
3644 then clause may fall through. Otherwise we can wind up with a
3645 useless jump and a useless label at the end of gimplified code,
3646 which will cause us to think that this conditional as a whole
3647 falls through even if it doesn't. If we then inline a function
3648 which ends with such a condition, that can cause us to issue an
3649 inappropriate warning about control reaching the end of a
3650 non-void function. */
3651 jump_over_else
= block_may_fallthru (then_
);
3653 pred
= shortcut_cond_r (pred
, true_label_p
, false_label_p
,
3654 EXPR_LOC_OR_LOC (expr
, input_location
));
3657 append_to_statement_list (pred
, &expr
);
3659 append_to_statement_list (then_
, &expr
);
3664 tree last
= expr_last (expr
);
3665 t
= build_and_jump (&end_label
);
3666 if (EXPR_HAS_LOCATION (last
))
3667 SET_EXPR_LOCATION (t
, EXPR_LOCATION (last
));
3668 append_to_statement_list (t
, &expr
);
3672 t
= build1 (LABEL_EXPR
, void_type_node
, false_label
);
3673 append_to_statement_list (t
, &expr
);
3675 append_to_statement_list (else_
, &expr
);
3677 if (emit_end
&& end_label
)
3679 t
= build1 (LABEL_EXPR
, void_type_node
, end_label
);
3680 append_to_statement_list (t
, &expr
);
3686 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3689 gimple_boolify (tree expr
)
3691 tree type
= TREE_TYPE (expr
);
3692 location_t loc
= EXPR_LOCATION (expr
);
3694 if (TREE_CODE (expr
) == NE_EXPR
3695 && TREE_CODE (TREE_OPERAND (expr
, 0)) == CALL_EXPR
3696 && integer_zerop (TREE_OPERAND (expr
, 1)))
3698 tree call
= TREE_OPERAND (expr
, 0);
3699 tree fn
= get_callee_fndecl (call
);
3701 /* For __builtin_expect ((long) (x), y) recurse into x as well
3702 if x is truth_value_p. */
3704 && DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
3705 && DECL_FUNCTION_CODE (fn
) == BUILT_IN_EXPECT
3706 && call_expr_nargs (call
) == 2)
3708 tree arg
= CALL_EXPR_ARG (call
, 0);
3711 if (TREE_CODE (arg
) == NOP_EXPR
3712 && TREE_TYPE (arg
) == TREE_TYPE (call
))
3713 arg
= TREE_OPERAND (arg
, 0);
3714 if (truth_value_p (TREE_CODE (arg
)))
3716 arg
= gimple_boolify (arg
);
3717 CALL_EXPR_ARG (call
, 0)
3718 = fold_convert_loc (loc
, TREE_TYPE (call
), arg
);
3724 switch (TREE_CODE (expr
))
3726 case TRUTH_AND_EXPR
:
3728 case TRUTH_XOR_EXPR
:
3729 case TRUTH_ANDIF_EXPR
:
3730 case TRUTH_ORIF_EXPR
:
3731 /* Also boolify the arguments of truth exprs. */
3732 TREE_OPERAND (expr
, 1) = gimple_boolify (TREE_OPERAND (expr
, 1));
3735 case TRUTH_NOT_EXPR
:
3736 TREE_OPERAND (expr
, 0) = gimple_boolify (TREE_OPERAND (expr
, 0));
3738 /* These expressions always produce boolean results. */
3739 if (TREE_CODE (type
) != BOOLEAN_TYPE
)
3740 TREE_TYPE (expr
) = boolean_type_node
;
3744 switch ((enum annot_expr_kind
) TREE_INT_CST_LOW (TREE_OPERAND (expr
, 1)))
3746 case annot_expr_ivdep_kind
:
3747 case annot_expr_no_vector_kind
:
3748 case annot_expr_vector_kind
:
3749 TREE_OPERAND (expr
, 0) = gimple_boolify (TREE_OPERAND (expr
, 0));
3750 if (TREE_CODE (type
) != BOOLEAN_TYPE
)
3751 TREE_TYPE (expr
) = boolean_type_node
;
3758 if (COMPARISON_CLASS_P (expr
))
3760 /* There expressions always prduce boolean results. */
3761 if (TREE_CODE (type
) != BOOLEAN_TYPE
)
3762 TREE_TYPE (expr
) = boolean_type_node
;
3765 /* Other expressions that get here must have boolean values, but
3766 might need to be converted to the appropriate mode. */
3767 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
3769 return fold_convert_loc (loc
, boolean_type_node
, expr
);
3773 /* Given a conditional expression *EXPR_P without side effects, gimplify
3774 its operands. New statements are inserted to PRE_P. */
3776 static enum gimplify_status
3777 gimplify_pure_cond_expr (tree
*expr_p
, gimple_seq
*pre_p
)
3779 tree expr
= *expr_p
, cond
;
3780 enum gimplify_status ret
, tret
;
3781 enum tree_code code
;
3783 cond
= gimple_boolify (COND_EXPR_COND (expr
));
3785 /* We need to handle && and || specially, as their gimplification
3786 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3787 code
= TREE_CODE (cond
);
3788 if (code
== TRUTH_ANDIF_EXPR
)
3789 TREE_SET_CODE (cond
, TRUTH_AND_EXPR
);
3790 else if (code
== TRUTH_ORIF_EXPR
)
3791 TREE_SET_CODE (cond
, TRUTH_OR_EXPR
);
3792 ret
= gimplify_expr (&cond
, pre_p
, NULL
, is_gimple_condexpr
, fb_rvalue
);
3793 COND_EXPR_COND (*expr_p
) = cond
;
3795 tret
= gimplify_expr (&COND_EXPR_THEN (expr
), pre_p
, NULL
,
3796 is_gimple_val
, fb_rvalue
);
3797 ret
= MIN (ret
, tret
);
3798 tret
= gimplify_expr (&COND_EXPR_ELSE (expr
), pre_p
, NULL
,
3799 is_gimple_val
, fb_rvalue
);
3801 return MIN (ret
, tret
);
3804 /* Return true if evaluating EXPR could trap.
3805 EXPR is GENERIC, while tree_could_trap_p can be called
3809 generic_expr_could_trap_p (tree expr
)
3813 if (!expr
|| is_gimple_val (expr
))
3816 if (!EXPR_P (expr
) || tree_could_trap_p (expr
))
3819 n
= TREE_OPERAND_LENGTH (expr
);
3820 for (i
= 0; i
< n
; i
++)
3821 if (generic_expr_could_trap_p (TREE_OPERAND (expr
, i
)))
3827 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3836 The second form is used when *EXPR_P is of type void.
3838 PRE_P points to the list where side effects that must happen before
3839 *EXPR_P should be stored. */
3841 static enum gimplify_status
3842 gimplify_cond_expr (tree
*expr_p
, gimple_seq
*pre_p
, fallback_t fallback
)
3844 tree expr
= *expr_p
;
3845 tree type
= TREE_TYPE (expr
);
3846 location_t loc
= EXPR_LOCATION (expr
);
3847 tree tmp
, arm1
, arm2
;
3848 enum gimplify_status ret
;
3849 tree label_true
, label_false
, label_cont
;
3850 bool have_then_clause_p
, have_else_clause_p
;
3852 enum tree_code pred_code
;
3853 gimple_seq seq
= NULL
;
3855 /* If this COND_EXPR has a value, copy the values into a temporary within
3857 if (!VOID_TYPE_P (type
))
3859 tree then_
= TREE_OPERAND (expr
, 1), else_
= TREE_OPERAND (expr
, 2);
3862 /* If either an rvalue is ok or we do not require an lvalue, create the
3863 temporary. But we cannot do that if the type is addressable. */
3864 if (((fallback
& fb_rvalue
) || !(fallback
& fb_lvalue
))
3865 && !TREE_ADDRESSABLE (type
))
3867 if (gimplify_ctxp
->allow_rhs_cond_expr
3868 /* If either branch has side effects or could trap, it can't be
3869 evaluated unconditionally. */
3870 && !TREE_SIDE_EFFECTS (then_
)
3871 && !generic_expr_could_trap_p (then_
)
3872 && !TREE_SIDE_EFFECTS (else_
)
3873 && !generic_expr_could_trap_p (else_
))
3874 return gimplify_pure_cond_expr (expr_p
, pre_p
);
3876 tmp
= create_tmp_var (type
, "iftmp");
3880 /* Otherwise, only create and copy references to the values. */
3883 type
= build_pointer_type (type
);
3885 if (!VOID_TYPE_P (TREE_TYPE (then_
)))
3886 then_
= build_fold_addr_expr_loc (loc
, then_
);
3888 if (!VOID_TYPE_P (TREE_TYPE (else_
)))
3889 else_
= build_fold_addr_expr_loc (loc
, else_
);
3892 = build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0), then_
, else_
);
3894 tmp
= create_tmp_var (type
, "iftmp");
3895 result
= build_simple_mem_ref_loc (loc
, tmp
);
3898 /* Build the new then clause, `tmp = then_;'. But don't build the
3899 assignment if the value is void; in C++ it can be if it's a throw. */
3900 if (!VOID_TYPE_P (TREE_TYPE (then_
)))
3901 TREE_OPERAND (expr
, 1) = build2 (MODIFY_EXPR
, type
, tmp
, then_
);
3903 /* Similarly, build the new else clause, `tmp = else_;'. */
3904 if (!VOID_TYPE_P (TREE_TYPE (else_
)))
3905 TREE_OPERAND (expr
, 2) = build2 (MODIFY_EXPR
, type
, tmp
, else_
);
3907 TREE_TYPE (expr
) = void_type_node
;
3908 recalculate_side_effects (expr
);
3910 /* Move the COND_EXPR to the prequeue. */
3911 gimplify_stmt (&expr
, pre_p
);
3917 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3918 STRIP_TYPE_NOPS (TREE_OPERAND (expr
, 0));
3919 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == COMPOUND_EXPR
)
3920 gimplify_compound_expr (&TREE_OPERAND (expr
, 0), pre_p
, true);
3922 /* Make sure the condition has BOOLEAN_TYPE. */
3923 TREE_OPERAND (expr
, 0) = gimple_boolify (TREE_OPERAND (expr
, 0));
3925 /* Break apart && and || conditions. */
3926 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == TRUTH_ANDIF_EXPR
3927 || TREE_CODE (TREE_OPERAND (expr
, 0)) == TRUTH_ORIF_EXPR
)
3929 expr
= shortcut_cond_expr (expr
);
3931 if (expr
!= *expr_p
)
3935 /* We can't rely on gimplify_expr to re-gimplify the expanded
3936 form properly, as cleanups might cause the target labels to be
3937 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3938 set up a conditional context. */
3939 gimple_push_condition ();
3940 gimplify_stmt (expr_p
, &seq
);
3941 gimple_pop_condition (pre_p
);
3942 gimple_seq_add_seq (pre_p
, seq
);
3948 /* Now do the normal gimplification. */
3950 /* Gimplify condition. */
3951 ret
= gimplify_expr (&TREE_OPERAND (expr
, 0), pre_p
, NULL
, is_gimple_condexpr
,
3953 if (ret
== GS_ERROR
)
3955 gcc_assert (TREE_OPERAND (expr
, 0) != NULL_TREE
);
3957 gimple_push_condition ();
3959 have_then_clause_p
= have_else_clause_p
= false;
3960 if (TREE_OPERAND (expr
, 1) != NULL
3961 && TREE_CODE (TREE_OPERAND (expr
, 1)) == GOTO_EXPR
3962 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr
, 1))) == LABEL_DECL
3963 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr
, 1)))
3964 == current_function_decl
)
3965 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3966 have different locations, otherwise we end up with incorrect
3967 location information on the branches. */
3969 || !EXPR_HAS_LOCATION (expr
)
3970 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr
, 1))
3971 || EXPR_LOCATION (expr
) == EXPR_LOCATION (TREE_OPERAND (expr
, 1))))
3973 label_true
= GOTO_DESTINATION (TREE_OPERAND (expr
, 1));
3974 have_then_clause_p
= true;
3977 label_true
= create_artificial_label (UNKNOWN_LOCATION
);
3978 if (TREE_OPERAND (expr
, 2) != NULL
3979 && TREE_CODE (TREE_OPERAND (expr
, 2)) == GOTO_EXPR
3980 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr
, 2))) == LABEL_DECL
3981 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr
, 2)))
3982 == current_function_decl
)
3983 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3984 have different locations, otherwise we end up with incorrect
3985 location information on the branches. */
3987 || !EXPR_HAS_LOCATION (expr
)
3988 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr
, 2))
3989 || EXPR_LOCATION (expr
) == EXPR_LOCATION (TREE_OPERAND (expr
, 2))))
3991 label_false
= GOTO_DESTINATION (TREE_OPERAND (expr
, 2));
3992 have_else_clause_p
= true;
3995 label_false
= create_artificial_label (UNKNOWN_LOCATION
);
3997 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr
), &pred_code
, &arm1
,
3999 cond_stmt
= gimple_build_cond (pred_code
, arm1
, arm2
, label_true
,
4001 gimple_set_no_warning (cond_stmt
, TREE_NO_WARNING (COND_EXPR_COND (expr
)));
4002 gimplify_seq_add_stmt (&seq
, cond_stmt
);
4003 gimple_stmt_iterator gsi
= gsi_last (seq
);
4004 maybe_fold_stmt (&gsi
);
4006 label_cont
= NULL_TREE
;
4007 if (!have_then_clause_p
)
4009 /* For if (...) {} else { code; } put label_true after
4011 if (TREE_OPERAND (expr
, 1) == NULL_TREE
4012 && !have_else_clause_p
4013 && TREE_OPERAND (expr
, 2) != NULL_TREE
)
4014 label_cont
= label_true
;
4017 gimplify_seq_add_stmt (&seq
, gimple_build_label (label_true
));
4018 have_then_clause_p
= gimplify_stmt (&TREE_OPERAND (expr
, 1), &seq
);
4019 /* For if (...) { code; } else {} or
4020 if (...) { code; } else goto label; or
4021 if (...) { code; return; } else { ... }
4022 label_cont isn't needed. */
4023 if (!have_else_clause_p
4024 && TREE_OPERAND (expr
, 2) != NULL_TREE
4025 && gimple_seq_may_fallthru (seq
))
4028 label_cont
= create_artificial_label (UNKNOWN_LOCATION
);
4030 g
= gimple_build_goto (label_cont
);
4032 /* GIMPLE_COND's are very low level; they have embedded
4033 gotos. This particular embedded goto should not be marked
4034 with the location of the original COND_EXPR, as it would
4035 correspond to the COND_EXPR's condition, not the ELSE or the
4036 THEN arms. To avoid marking it with the wrong location, flag
4037 it as "no location". */
4038 gimple_set_do_not_emit_location (g
);
4040 gimplify_seq_add_stmt (&seq
, g
);
4044 if (!have_else_clause_p
)
4046 gimplify_seq_add_stmt (&seq
, gimple_build_label (label_false
));
4047 have_else_clause_p
= gimplify_stmt (&TREE_OPERAND (expr
, 2), &seq
);
4050 gimplify_seq_add_stmt (&seq
, gimple_build_label (label_cont
));
4052 gimple_pop_condition (pre_p
);
4053 gimple_seq_add_seq (pre_p
, seq
);
4055 if (ret
== GS_ERROR
)
4057 else if (have_then_clause_p
|| have_else_clause_p
)
4061 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4062 expr
= TREE_OPERAND (expr
, 0);
4063 gimplify_stmt (&expr
, pre_p
);
4070 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4071 to be marked addressable.
4073 We cannot rely on such an expression being directly markable if a temporary
4074 has been created by the gimplification. In this case, we create another
4075 temporary and initialize it with a copy, which will become a store after we
4076 mark it addressable. This can happen if the front-end passed us something
4077 that it could not mark addressable yet, like a Fortran pass-by-reference
4078 parameter (int) floatvar. */
4081 prepare_gimple_addressable (tree
*expr_p
, gimple_seq
*seq_p
)
4083 while (handled_component_p (*expr_p
))
4084 expr_p
= &TREE_OPERAND (*expr_p
, 0);
4085 if (is_gimple_reg (*expr_p
))
4087 /* Do not allow an SSA name as the temporary. */
4088 tree var
= get_initialized_tmp_var (*expr_p
, seq_p
, NULL
, false);
4089 DECL_GIMPLE_REG_P (var
) = 0;
4094 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4095 a call to __builtin_memcpy. */
4097 static enum gimplify_status
4098 gimplify_modify_expr_to_memcpy (tree
*expr_p
, tree size
, bool want_value
,
4101 tree t
, to
, to_ptr
, from
, from_ptr
;
4103 location_t loc
= EXPR_LOCATION (*expr_p
);
4105 to
= TREE_OPERAND (*expr_p
, 0);
4106 from
= TREE_OPERAND (*expr_p
, 1);
4108 /* Mark the RHS addressable. Beware that it may not be possible to do so
4109 directly if a temporary has been created by the gimplification. */
4110 prepare_gimple_addressable (&from
, seq_p
);
4112 mark_addressable (from
);
4113 from_ptr
= build_fold_addr_expr_loc (loc
, from
);
4114 gimplify_arg (&from_ptr
, seq_p
, loc
);
4116 mark_addressable (to
);
4117 to_ptr
= build_fold_addr_expr_loc (loc
, to
);
4118 gimplify_arg (&to_ptr
, seq_p
, loc
);
4120 t
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
4122 gs
= gimple_build_call (t
, 3, to_ptr
, from_ptr
, size
);
4126 /* tmp = memcpy() */
4127 t
= create_tmp_var (TREE_TYPE (to_ptr
));
4128 gimple_call_set_lhs (gs
, t
);
4129 gimplify_seq_add_stmt (seq_p
, gs
);
4131 *expr_p
= build_simple_mem_ref (t
);
4135 gimplify_seq_add_stmt (seq_p
, gs
);
4140 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4141 a call to __builtin_memset. In this case we know that the RHS is
4142 a CONSTRUCTOR with an empty element list. */
4144 static enum gimplify_status
4145 gimplify_modify_expr_to_memset (tree
*expr_p
, tree size
, bool want_value
,
4148 tree t
, from
, to
, to_ptr
;
4150 location_t loc
= EXPR_LOCATION (*expr_p
);
4152 /* Assert our assumptions, to abort instead of producing wrong code
4153 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4154 not be immediately exposed. */
4155 from
= TREE_OPERAND (*expr_p
, 1);
4156 if (TREE_CODE (from
) == WITH_SIZE_EXPR
)
4157 from
= TREE_OPERAND (from
, 0);
4159 gcc_assert (TREE_CODE (from
) == CONSTRUCTOR
4160 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from
)));
4163 to
= TREE_OPERAND (*expr_p
, 0);
4165 to_ptr
= build_fold_addr_expr_loc (loc
, to
);
4166 gimplify_arg (&to_ptr
, seq_p
, loc
);
4167 t
= builtin_decl_implicit (BUILT_IN_MEMSET
);
4169 gs
= gimple_build_call (t
, 3, to_ptr
, integer_zero_node
, size
);
4173 /* tmp = memset() */
4174 t
= create_tmp_var (TREE_TYPE (to_ptr
));
4175 gimple_call_set_lhs (gs
, t
);
4176 gimplify_seq_add_stmt (seq_p
, gs
);
4178 *expr_p
= build1 (INDIRECT_REF
, TREE_TYPE (to
), t
);
4182 gimplify_seq_add_stmt (seq_p
, gs
);
4187 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4188 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4189 assignment. Return non-null if we detect a potential overlap. */
4191 struct gimplify_init_ctor_preeval_data
4193 /* The base decl of the lhs object. May be NULL, in which case we
4194 have to assume the lhs is indirect. */
4197 /* The alias set of the lhs object. */
4198 alias_set_type lhs_alias_set
;
4202 gimplify_init_ctor_preeval_1 (tree
*tp
, int *walk_subtrees
, void *xdata
)
4204 struct gimplify_init_ctor_preeval_data
*data
4205 = (struct gimplify_init_ctor_preeval_data
*) xdata
;
4208 /* If we find the base object, obviously we have overlap. */
4209 if (data
->lhs_base_decl
== t
)
4212 /* If the constructor component is indirect, determine if we have a
4213 potential overlap with the lhs. The only bits of information we
4214 have to go on at this point are addressability and alias sets. */
4215 if ((INDIRECT_REF_P (t
)
4216 || TREE_CODE (t
) == MEM_REF
)
4217 && (!data
->lhs_base_decl
|| TREE_ADDRESSABLE (data
->lhs_base_decl
))
4218 && alias_sets_conflict_p (data
->lhs_alias_set
, get_alias_set (t
)))
4221 /* If the constructor component is a call, determine if it can hide a
4222 potential overlap with the lhs through an INDIRECT_REF like above.
4223 ??? Ugh - this is completely broken. In fact this whole analysis
4224 doesn't look conservative. */
4225 if (TREE_CODE (t
) == CALL_EXPR
)
4227 tree type
, fntype
= TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t
)));
4229 for (type
= TYPE_ARG_TYPES (fntype
); type
; type
= TREE_CHAIN (type
))
4230 if (POINTER_TYPE_P (TREE_VALUE (type
))
4231 && (!data
->lhs_base_decl
|| TREE_ADDRESSABLE (data
->lhs_base_decl
))
4232 && alias_sets_conflict_p (data
->lhs_alias_set
,
4234 (TREE_TYPE (TREE_VALUE (type
)))))
4238 if (IS_TYPE_OR_DECL_P (t
))
4243 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4244 force values that overlap with the lhs (as described by *DATA)
4245 into temporaries. */
4248 gimplify_init_ctor_preeval (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
4249 struct gimplify_init_ctor_preeval_data
*data
)
4251 enum gimplify_status one
;
4253 /* If the value is constant, then there's nothing to pre-evaluate. */
4254 if (TREE_CONSTANT (*expr_p
))
4256 /* Ensure it does not have side effects, it might contain a reference to
4257 the object we're initializing. */
4258 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p
));
4262 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4263 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p
)))
4266 /* Recurse for nested constructors. */
4267 if (TREE_CODE (*expr_p
) == CONSTRUCTOR
)
4269 unsigned HOST_WIDE_INT ix
;
4270 constructor_elt
*ce
;
4271 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (*expr_p
);
4273 FOR_EACH_VEC_SAFE_ELT (v
, ix
, ce
)
4274 gimplify_init_ctor_preeval (&ce
->value
, pre_p
, post_p
, data
);
4279 /* If this is a variable sized type, we must remember the size. */
4280 maybe_with_size_expr (expr_p
);
4282 /* Gimplify the constructor element to something appropriate for the rhs
4283 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4284 the gimplifier will consider this a store to memory. Doing this
4285 gimplification now means that we won't have to deal with complicated
4286 language-specific trees, nor trees like SAVE_EXPR that can induce
4287 exponential search behavior. */
4288 one
= gimplify_expr (expr_p
, pre_p
, post_p
, is_gimple_mem_rhs
, fb_rvalue
);
4289 if (one
== GS_ERROR
)
4295 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4296 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4297 always be true for all scalars, since is_gimple_mem_rhs insists on a
4298 temporary variable for them. */
4299 if (DECL_P (*expr_p
))
4302 /* If this is of variable size, we have no choice but to assume it doesn't
4303 overlap since we can't make a temporary for it. */
4304 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p
))) != INTEGER_CST
)
4307 /* Otherwise, we must search for overlap ... */
4308 if (!walk_tree (expr_p
, gimplify_init_ctor_preeval_1
, data
, NULL
))
4311 /* ... and if found, force the value into a temporary. */
4312 *expr_p
= get_formal_tmp_var (*expr_p
, pre_p
);
4315 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4316 a RANGE_EXPR in a CONSTRUCTOR for an array.
4320 object[var] = value;
4327 We increment var _after_ the loop exit check because we might otherwise
4328 fail if upper == TYPE_MAX_VALUE (type for upper).
4330 Note that we never have to deal with SAVE_EXPRs here, because this has
4331 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4333 static void gimplify_init_ctor_eval (tree
, vec
<constructor_elt
, va_gc
> *,
4334 gimple_seq
*, bool);
4337 gimplify_init_ctor_eval_range (tree object
, tree lower
, tree upper
,
4338 tree value
, tree array_elt_type
,
4339 gimple_seq
*pre_p
, bool cleared
)
4341 tree loop_entry_label
, loop_exit_label
, fall_thru_label
;
4342 tree var
, var_type
, cref
, tmp
;
4344 loop_entry_label
= create_artificial_label (UNKNOWN_LOCATION
);
4345 loop_exit_label
= create_artificial_label (UNKNOWN_LOCATION
);
4346 fall_thru_label
= create_artificial_label (UNKNOWN_LOCATION
);
4348 /* Create and initialize the index variable. */
4349 var_type
= TREE_TYPE (upper
);
4350 var
= create_tmp_var (var_type
);
4351 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (var
, lower
));
4353 /* Add the loop entry label. */
4354 gimplify_seq_add_stmt (pre_p
, gimple_build_label (loop_entry_label
));
4356 /* Build the reference. */
4357 cref
= build4 (ARRAY_REF
, array_elt_type
, unshare_expr (object
),
4358 var
, NULL_TREE
, NULL_TREE
);
4360 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4361 the store. Otherwise just assign value to the reference. */
4363 if (TREE_CODE (value
) == CONSTRUCTOR
)
4364 /* NB we might have to call ourself recursively through
4365 gimplify_init_ctor_eval if the value is a constructor. */
4366 gimplify_init_ctor_eval (cref
, CONSTRUCTOR_ELTS (value
),
4369 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (cref
, value
));
4371 /* We exit the loop when the index var is equal to the upper bound. */
4372 gimplify_seq_add_stmt (pre_p
,
4373 gimple_build_cond (EQ_EXPR
, var
, upper
,
4374 loop_exit_label
, fall_thru_label
));
4376 gimplify_seq_add_stmt (pre_p
, gimple_build_label (fall_thru_label
));
4378 /* Otherwise, increment the index var... */
4379 tmp
= build2 (PLUS_EXPR
, var_type
, var
,
4380 fold_convert (var_type
, integer_one_node
));
4381 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (var
, tmp
));
4383 /* ...and jump back to the loop entry. */
4384 gimplify_seq_add_stmt (pre_p
, gimple_build_goto (loop_entry_label
));
4386 /* Add the loop exit label. */
4387 gimplify_seq_add_stmt (pre_p
, gimple_build_label (loop_exit_label
));
4390 /* Return true if FDECL is accessing a field that is zero sized. */
4393 zero_sized_field_decl (const_tree fdecl
)
4395 if (TREE_CODE (fdecl
) == FIELD_DECL
&& DECL_SIZE (fdecl
)
4396 && integer_zerop (DECL_SIZE (fdecl
)))
4401 /* Return true if TYPE is zero sized. */
4404 zero_sized_type (const_tree type
)
4406 if (AGGREGATE_TYPE_P (type
) && TYPE_SIZE (type
)
4407 && integer_zerop (TYPE_SIZE (type
)))
4412 /* A subroutine of gimplify_init_constructor. Generate individual
4413 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4414 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4415 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4419 gimplify_init_ctor_eval (tree object
, vec
<constructor_elt
, va_gc
> *elts
,
4420 gimple_seq
*pre_p
, bool cleared
)
4422 tree array_elt_type
= NULL
;
4423 unsigned HOST_WIDE_INT ix
;
4424 tree purpose
, value
;
4426 if (TREE_CODE (TREE_TYPE (object
)) == ARRAY_TYPE
)
4427 array_elt_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object
)));
4429 FOR_EACH_CONSTRUCTOR_ELT (elts
, ix
, purpose
, value
)
4433 /* NULL values are created above for gimplification errors. */
4437 if (cleared
&& initializer_zerop (value
))
4440 /* ??? Here's to hoping the front end fills in all of the indices,
4441 so we don't have to figure out what's missing ourselves. */
4442 gcc_assert (purpose
);
4444 /* Skip zero-sized fields, unless value has side-effects. This can
4445 happen with calls to functions returning a zero-sized type, which
4446 we shouldn't discard. As a number of downstream passes don't
4447 expect sets of zero-sized fields, we rely on the gimplification of
4448 the MODIFY_EXPR we make below to drop the assignment statement. */
4449 if (! TREE_SIDE_EFFECTS (value
) && zero_sized_field_decl (purpose
))
4452 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4454 if (TREE_CODE (purpose
) == RANGE_EXPR
)
4456 tree lower
= TREE_OPERAND (purpose
, 0);
4457 tree upper
= TREE_OPERAND (purpose
, 1);
4459 /* If the lower bound is equal to upper, just treat it as if
4460 upper was the index. */
4461 if (simple_cst_equal (lower
, upper
))
4465 gimplify_init_ctor_eval_range (object
, lower
, upper
, value
,
4466 array_elt_type
, pre_p
, cleared
);
4473 /* Do not use bitsizetype for ARRAY_REF indices. */
4474 if (TYPE_DOMAIN (TREE_TYPE (object
)))
4476 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object
))),
4478 cref
= build4 (ARRAY_REF
, array_elt_type
, unshare_expr (object
),
4479 purpose
, NULL_TREE
, NULL_TREE
);
4483 gcc_assert (TREE_CODE (purpose
) == FIELD_DECL
);
4484 cref
= build3 (COMPONENT_REF
, TREE_TYPE (purpose
),
4485 unshare_expr (object
), purpose
, NULL_TREE
);
4488 if (TREE_CODE (value
) == CONSTRUCTOR
4489 && TREE_CODE (TREE_TYPE (value
)) != VECTOR_TYPE
)
4490 gimplify_init_ctor_eval (cref
, CONSTRUCTOR_ELTS (value
),
4494 tree init
= build2 (INIT_EXPR
, TREE_TYPE (cref
), cref
, value
);
4495 gimplify_and_add (init
, pre_p
);
4501 /* Return the appropriate RHS predicate for this LHS. */
4504 rhs_predicate_for (tree lhs
)
4506 if (is_gimple_reg (lhs
))
4507 return is_gimple_reg_rhs_or_call
;
4509 return is_gimple_mem_rhs_or_call
;
4512 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4513 before the LHS has been gimplified. */
4515 static gimple_predicate
4516 initial_rhs_predicate_for (tree lhs
)
4518 if (is_gimple_reg_type (TREE_TYPE (lhs
)))
4519 return is_gimple_reg_rhs_or_call
;
4521 return is_gimple_mem_rhs_or_call
;
4524 /* Gimplify a C99 compound literal expression. This just means adding
4525 the DECL_EXPR before the current statement and using its anonymous
4528 static enum gimplify_status
4529 gimplify_compound_literal_expr (tree
*expr_p
, gimple_seq
*pre_p
,
4530 bool (*gimple_test_f
) (tree
),
4531 fallback_t fallback
)
4533 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p
);
4534 tree decl
= DECL_EXPR_DECL (decl_s
);
4535 tree init
= DECL_INITIAL (decl
);
4536 /* Mark the decl as addressable if the compound literal
4537 expression is addressable now, otherwise it is marked too late
4538 after we gimplify the initialization expression. */
4539 if (TREE_ADDRESSABLE (*expr_p
))
4540 TREE_ADDRESSABLE (decl
) = 1;
4541 /* Otherwise, if we don't need an lvalue and have a literal directly
4542 substitute it. Check if it matches the gimple predicate, as
4543 otherwise we'd generate a new temporary, and we can as well just
4544 use the decl we already have. */
4545 else if (!TREE_ADDRESSABLE (decl
)
4547 && (fallback
& fb_lvalue
) == 0
4548 && gimple_test_f (init
))
4554 /* Preliminarily mark non-addressed complex variables as eligible
4555 for promotion to gimple registers. We'll transform their uses
4557 if ((TREE_CODE (TREE_TYPE (decl
)) == COMPLEX_TYPE
4558 || TREE_CODE (TREE_TYPE (decl
)) == VECTOR_TYPE
)
4559 && !TREE_THIS_VOLATILE (decl
)
4560 && !needs_to_live_in_memory (decl
))
4561 DECL_GIMPLE_REG_P (decl
) = 1;
4563 /* If the decl is not addressable, then it is being used in some
4564 expression or on the right hand side of a statement, and it can
4565 be put into a readonly data section. */
4566 if (!TREE_ADDRESSABLE (decl
) && (fallback
& fb_lvalue
) == 0)
4567 TREE_READONLY (decl
) = 1;
4569 /* This decl isn't mentioned in the enclosing block, so add it to the
4570 list of temps. FIXME it seems a bit of a kludge to say that
4571 anonymous artificial vars aren't pushed, but everything else is. */
4572 if (DECL_NAME (decl
) == NULL_TREE
&& !DECL_SEEN_IN_BIND_EXPR_P (decl
))
4573 gimple_add_tmp_var (decl
);
4575 gimplify_and_add (decl_s
, pre_p
);
4580 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4581 return a new CONSTRUCTOR if something changed. */
4584 optimize_compound_literals_in_ctor (tree orig_ctor
)
4586 tree ctor
= orig_ctor
;
4587 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
4588 unsigned int idx
, num
= vec_safe_length (elts
);
4590 for (idx
= 0; idx
< num
; idx
++)
4592 tree value
= (*elts
)[idx
].value
;
4593 tree newval
= value
;
4594 if (TREE_CODE (value
) == CONSTRUCTOR
)
4595 newval
= optimize_compound_literals_in_ctor (value
);
4596 else if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
4598 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (value
);
4599 tree decl
= DECL_EXPR_DECL (decl_s
);
4600 tree init
= DECL_INITIAL (decl
);
4602 if (!TREE_ADDRESSABLE (value
)
4603 && !TREE_ADDRESSABLE (decl
)
4605 && TREE_CODE (init
) == CONSTRUCTOR
)
4606 newval
= optimize_compound_literals_in_ctor (init
);
4608 if (newval
== value
)
4611 if (ctor
== orig_ctor
)
4613 ctor
= copy_node (orig_ctor
);
4614 CONSTRUCTOR_ELTS (ctor
) = vec_safe_copy (elts
);
4615 elts
= CONSTRUCTOR_ELTS (ctor
);
4617 (*elts
)[idx
].value
= newval
;
4622 /* A subroutine of gimplify_modify_expr. Break out elements of a
4623 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4625 Note that we still need to clear any elements that don't have explicit
4626 initializers, so if not all elements are initialized we keep the
4627 original MODIFY_EXPR, we just remove all of the constructor elements.
4629 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4630 GS_ERROR if we would have to create a temporary when gimplifying
4631 this constructor. Otherwise, return GS_OK.
4633 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4635 static enum gimplify_status
4636 gimplify_init_constructor (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
4637 bool want_value
, bool notify_temp_creation
)
4639 tree object
, ctor
, type
;
4640 enum gimplify_status ret
;
4641 vec
<constructor_elt
, va_gc
> *elts
;
4643 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p
, 1)) == CONSTRUCTOR
);
4645 if (!notify_temp_creation
)
4647 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
4648 is_gimple_lvalue
, fb_lvalue
);
4649 if (ret
== GS_ERROR
)
4653 object
= TREE_OPERAND (*expr_p
, 0);
4654 ctor
= TREE_OPERAND (*expr_p
, 1)
4655 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p
, 1));
4656 type
= TREE_TYPE (ctor
);
4657 elts
= CONSTRUCTOR_ELTS (ctor
);
4660 switch (TREE_CODE (type
))
4664 case QUAL_UNION_TYPE
:
4667 struct gimplify_init_ctor_preeval_data preeval_data
;
4668 HOST_WIDE_INT num_ctor_elements
, num_nonzero_elements
;
4669 bool cleared
, complete_p
, valid_const_initializer
;
4671 /* Aggregate types must lower constructors to initialization of
4672 individual elements. The exception is that a CONSTRUCTOR node
4673 with no elements indicates zero-initialization of the whole. */
4674 if (vec_safe_is_empty (elts
))
4676 if (notify_temp_creation
)
4681 /* Fetch information about the constructor to direct later processing.
4682 We might want to make static versions of it in various cases, and
4683 can only do so if it known to be a valid constant initializer. */
4684 valid_const_initializer
4685 = categorize_ctor_elements (ctor
, &num_nonzero_elements
,
4686 &num_ctor_elements
, &complete_p
);
4688 /* If a const aggregate variable is being initialized, then it
4689 should never be a lose to promote the variable to be static. */
4690 if (valid_const_initializer
4691 && num_nonzero_elements
> 1
4692 && TREE_READONLY (object
)
4694 && (flag_merge_constants
>= 2 || !TREE_ADDRESSABLE (object
)))
4696 if (notify_temp_creation
)
4698 DECL_INITIAL (object
) = ctor
;
4699 TREE_STATIC (object
) = 1;
4700 if (!DECL_NAME (object
))
4701 DECL_NAME (object
) = create_tmp_var_name ("C");
4702 walk_tree (&DECL_INITIAL (object
), force_labels_r
, NULL
, NULL
);
4704 /* ??? C++ doesn't automatically append a .<number> to the
4705 assembler name, and even when it does, it looks at FE private
4706 data structures to figure out what that number should be,
4707 which are not set for this variable. I suppose this is
4708 important for local statics for inline functions, which aren't
4709 "local" in the object file sense. So in order to get a unique
4710 TU-local symbol, we must invoke the lhd version now. */
4711 lhd_set_decl_assembler_name (object
);
4713 *expr_p
= NULL_TREE
;
4717 /* If there are "lots" of initialized elements, even discounting
4718 those that are not address constants (and thus *must* be
4719 computed at runtime), then partition the constructor into
4720 constant and non-constant parts. Block copy the constant
4721 parts in, then generate code for the non-constant parts. */
4722 /* TODO. There's code in cp/typeck.c to do this. */
4724 if (int_size_in_bytes (TREE_TYPE (ctor
)) < 0)
4725 /* store_constructor will ignore the clearing of variable-sized
4726 objects. Initializers for such objects must explicitly set
4727 every field that needs to be set. */
4729 else if (!complete_p
&& !CONSTRUCTOR_NO_CLEARING (ctor
))
4730 /* If the constructor isn't complete, clear the whole object
4731 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4733 ??? This ought not to be needed. For any element not present
4734 in the initializer, we should simply set them to zero. Except
4735 we'd need to *find* the elements that are not present, and that
4736 requires trickery to avoid quadratic compile-time behavior in
4737 large cases or excessive memory use in small cases. */
4739 else if (num_ctor_elements
- num_nonzero_elements
4740 > CLEAR_RATIO (optimize_function_for_speed_p (cfun
))
4741 && num_nonzero_elements
< num_ctor_elements
/ 4)
4742 /* If there are "lots" of zeros, it's more efficient to clear
4743 the memory and then set the nonzero elements. */
4748 /* If there are "lots" of initialized elements, and all of them
4749 are valid address constants, then the entire initializer can
4750 be dropped to memory, and then memcpy'd out. Don't do this
4751 for sparse arrays, though, as it's more efficient to follow
4752 the standard CONSTRUCTOR behavior of memset followed by
4753 individual element initialization. Also don't do this for small
4754 all-zero initializers (which aren't big enough to merit
4755 clearing), and don't try to make bitwise copies of
4756 TREE_ADDRESSABLE types.
4758 We cannot apply such transformation when compiling chkp static
4759 initializer because creation of initializer image in the memory
4760 will require static initialization of bounds for it. It should
4761 result in another gimplification of similar initializer and we
4762 may fall into infinite loop. */
4763 if (valid_const_initializer
4764 && !(cleared
|| num_nonzero_elements
== 0)
4765 && !TREE_ADDRESSABLE (type
)
4766 && (!current_function_decl
4767 || !lookup_attribute ("chkp ctor",
4768 DECL_ATTRIBUTES (current_function_decl
))))
4770 HOST_WIDE_INT size
= int_size_in_bytes (type
);
4773 /* ??? We can still get unbounded array types, at least
4774 from the C++ front end. This seems wrong, but attempt
4775 to work around it for now. */
4778 size
= int_size_in_bytes (TREE_TYPE (object
));
4780 TREE_TYPE (ctor
) = type
= TREE_TYPE (object
);
4783 /* Find the maximum alignment we can assume for the object. */
4784 /* ??? Make use of DECL_OFFSET_ALIGN. */
4785 if (DECL_P (object
))
4786 align
= DECL_ALIGN (object
);
4788 align
= TYPE_ALIGN (type
);
4790 /* Do a block move either if the size is so small as to make
4791 each individual move a sub-unit move on average, or if it
4792 is so large as to make individual moves inefficient. */
4794 && num_nonzero_elements
> 1
4795 && (size
< num_nonzero_elements
4796 || !can_move_by_pieces (size
, align
)))
4798 if (notify_temp_creation
)
4801 walk_tree (&ctor
, force_labels_r
, NULL
, NULL
);
4802 ctor
= tree_output_constant_def (ctor
);
4803 if (!useless_type_conversion_p (type
, TREE_TYPE (ctor
)))
4804 ctor
= build1 (VIEW_CONVERT_EXPR
, type
, ctor
);
4805 TREE_OPERAND (*expr_p
, 1) = ctor
;
4807 /* This is no longer an assignment of a CONSTRUCTOR, but
4808 we still may have processing to do on the LHS. So
4809 pretend we didn't do anything here to let that happen. */
4810 return GS_UNHANDLED
;
4814 /* If the target is volatile, we have non-zero elements and more than
4815 one field to assign, initialize the target from a temporary. */
4816 if (TREE_THIS_VOLATILE (object
)
4817 && !TREE_ADDRESSABLE (type
)
4818 && num_nonzero_elements
> 0
4819 && vec_safe_length (elts
) > 1)
4821 tree temp
= create_tmp_var (TYPE_MAIN_VARIANT (type
));
4822 TREE_OPERAND (*expr_p
, 0) = temp
;
4823 *expr_p
= build2 (COMPOUND_EXPR
, TREE_TYPE (*expr_p
),
4825 build2 (MODIFY_EXPR
, void_type_node
,
4830 if (notify_temp_creation
)
4833 /* If there are nonzero elements and if needed, pre-evaluate to capture
4834 elements overlapping with the lhs into temporaries. We must do this
4835 before clearing to fetch the values before they are zeroed-out. */
4836 if (num_nonzero_elements
> 0 && TREE_CODE (*expr_p
) != INIT_EXPR
)
4838 preeval_data
.lhs_base_decl
= get_base_address (object
);
4839 if (!DECL_P (preeval_data
.lhs_base_decl
))
4840 preeval_data
.lhs_base_decl
= NULL
;
4841 preeval_data
.lhs_alias_set
= get_alias_set (object
);
4843 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p
, 1),
4844 pre_p
, post_p
, &preeval_data
);
4847 bool ctor_has_side_effects_p
4848 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p
, 1));
4852 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4853 Note that we still have to gimplify, in order to handle the
4854 case of variable sized types. Avoid shared tree structures. */
4855 CONSTRUCTOR_ELTS (ctor
) = NULL
;
4856 TREE_SIDE_EFFECTS (ctor
) = 0;
4857 object
= unshare_expr (object
);
4858 gimplify_stmt (expr_p
, pre_p
);
4861 /* If we have not block cleared the object, or if there are nonzero
4862 elements in the constructor, or if the constructor has side effects,
4863 add assignments to the individual scalar fields of the object. */
4865 || num_nonzero_elements
> 0
4866 || ctor_has_side_effects_p
)
4867 gimplify_init_ctor_eval (object
, elts
, pre_p
, cleared
);
4869 *expr_p
= NULL_TREE
;
4877 if (notify_temp_creation
)
4880 /* Extract the real and imaginary parts out of the ctor. */
4881 gcc_assert (elts
->length () == 2);
4882 r
= (*elts
)[0].value
;
4883 i
= (*elts
)[1].value
;
4884 if (r
== NULL
|| i
== NULL
)
4886 tree zero
= build_zero_cst (TREE_TYPE (type
));
4893 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4894 represent creation of a complex value. */
4895 if (TREE_CONSTANT (r
) && TREE_CONSTANT (i
))
4897 ctor
= build_complex (type
, r
, i
);
4898 TREE_OPERAND (*expr_p
, 1) = ctor
;
4902 ctor
= build2 (COMPLEX_EXPR
, type
, r
, i
);
4903 TREE_OPERAND (*expr_p
, 1) = ctor
;
4904 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1),
4907 rhs_predicate_for (TREE_OPERAND (*expr_p
, 0)),
4915 unsigned HOST_WIDE_INT ix
;
4916 constructor_elt
*ce
;
4918 if (notify_temp_creation
)
4921 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4922 if (TREE_CONSTANT (ctor
))
4924 bool constant_p
= true;
4927 /* Even when ctor is constant, it might contain non-*_CST
4928 elements, such as addresses or trapping values like
4929 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4930 in VECTOR_CST nodes. */
4931 FOR_EACH_CONSTRUCTOR_VALUE (elts
, ix
, value
)
4932 if (!CONSTANT_CLASS_P (value
))
4940 TREE_OPERAND (*expr_p
, 1) = build_vector_from_ctor (type
, elts
);
4944 TREE_CONSTANT (ctor
) = 0;
4947 /* Vector types use CONSTRUCTOR all the way through gimple
4948 compilation as a general initializer. */
4949 FOR_EACH_VEC_SAFE_ELT (elts
, ix
, ce
)
4951 enum gimplify_status tret
;
4952 tret
= gimplify_expr (&ce
->value
, pre_p
, post_p
, is_gimple_val
,
4954 if (tret
== GS_ERROR
)
4956 else if (TREE_STATIC (ctor
)
4957 && !initializer_constant_valid_p (ce
->value
,
4958 TREE_TYPE (ce
->value
)))
4959 TREE_STATIC (ctor
) = 0;
4961 if (!is_gimple_reg (TREE_OPERAND (*expr_p
, 0)))
4962 TREE_OPERAND (*expr_p
, 1) = get_formal_tmp_var (ctor
, pre_p
);
4967 /* So how did we get a CONSTRUCTOR for a scalar type? */
4971 if (ret
== GS_ERROR
)
4973 /* If we have gimplified both sides of the initializer but have
4974 not emitted an assignment, do so now. */
4977 tree lhs
= TREE_OPERAND (*expr_p
, 0);
4978 tree rhs
= TREE_OPERAND (*expr_p
, 1);
4979 if (want_value
&& object
== lhs
)
4980 lhs
= unshare_expr (lhs
);
4981 gassign
*init
= gimple_build_assign (lhs
, rhs
);
4982 gimplify_seq_add_stmt (pre_p
, init
);
4996 /* Given a pointer value OP0, return a simplified version of an
4997 indirection through OP0, or NULL_TREE if no simplification is
4998 possible. This may only be applied to a rhs of an expression.
4999 Note that the resulting type may be different from the type pointed
5000 to in the sense that it is still compatible from the langhooks
5004 gimple_fold_indirect_ref_rhs (tree t
)
5006 return gimple_fold_indirect_ref (t
);
5009 /* Subroutine of gimplify_modify_expr to do simplifications of
5010 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5011 something changes. */
5013 static enum gimplify_status
5014 gimplify_modify_expr_rhs (tree
*expr_p
, tree
*from_p
, tree
*to_p
,
5015 gimple_seq
*pre_p
, gimple_seq
*post_p
,
5018 enum gimplify_status ret
= GS_UNHANDLED
;
5024 switch (TREE_CODE (*from_p
))
5027 /* If we're assigning from a read-only variable initialized with
5028 a constructor, do the direct assignment from the constructor,
5029 but only if neither source nor target are volatile since this
5030 latter assignment might end up being done on a per-field basis. */
5031 if (DECL_INITIAL (*from_p
)
5032 && TREE_READONLY (*from_p
)
5033 && !TREE_THIS_VOLATILE (*from_p
)
5034 && !TREE_THIS_VOLATILE (*to_p
)
5035 && TREE_CODE (DECL_INITIAL (*from_p
)) == CONSTRUCTOR
)
5037 tree old_from
= *from_p
;
5038 enum gimplify_status subret
;
5040 /* Move the constructor into the RHS. */
5041 *from_p
= unshare_expr (DECL_INITIAL (*from_p
));
5043 /* Let's see if gimplify_init_constructor will need to put
5045 subret
= gimplify_init_constructor (expr_p
, NULL
, NULL
,
5047 if (subret
== GS_ERROR
)
5049 /* If so, revert the change. */
5061 /* If we have code like
5065 where the type of "x" is a (possibly cv-qualified variant
5066 of "A"), treat the entire expression as identical to "x".
5067 This kind of code arises in C++ when an object is bound
5068 to a const reference, and if "x" is a TARGET_EXPR we want
5069 to take advantage of the optimization below. */
5070 bool volatile_p
= TREE_THIS_VOLATILE (*from_p
);
5071 tree t
= gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p
, 0));
5074 if (TREE_THIS_VOLATILE (t
) != volatile_p
)
5077 t
= build_simple_mem_ref_loc (EXPR_LOCATION (*from_p
),
5078 build_fold_addr_expr (t
));
5079 if (REFERENCE_CLASS_P (t
))
5080 TREE_THIS_VOLATILE (t
) = volatile_p
;
5091 /* If we are initializing something from a TARGET_EXPR, strip the
5092 TARGET_EXPR and initialize it directly, if possible. This can't
5093 be done if the initializer is void, since that implies that the
5094 temporary is set in some non-trivial way.
5096 ??? What about code that pulls out the temp and uses it
5097 elsewhere? I think that such code never uses the TARGET_EXPR as
5098 an initializer. If I'm wrong, we'll die because the temp won't
5099 have any RTL. In that case, I guess we'll need to replace
5100 references somehow. */
5101 tree init
= TARGET_EXPR_INITIAL (*from_p
);
5104 && !VOID_TYPE_P (TREE_TYPE (init
)))
5114 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5116 gimplify_compound_expr (from_p
, pre_p
, true);
5122 /* If we already made some changes, let the front end have a
5123 crack at this before we break it down. */
5124 if (ret
!= GS_UNHANDLED
)
5126 /* If we're initializing from a CONSTRUCTOR, break this into
5127 individual MODIFY_EXPRs. */
5128 return gimplify_init_constructor (expr_p
, pre_p
, post_p
, want_value
,
5132 /* If we're assigning to a non-register type, push the assignment
5133 down into the branches. This is mandatory for ADDRESSABLE types,
5134 since we cannot generate temporaries for such, but it saves a
5135 copy in other cases as well. */
5136 if (!is_gimple_reg_type (TREE_TYPE (*from_p
)))
5138 /* This code should mirror the code in gimplify_cond_expr. */
5139 enum tree_code code
= TREE_CODE (*expr_p
);
5140 tree cond
= *from_p
;
5141 tree result
= *to_p
;
5143 ret
= gimplify_expr (&result
, pre_p
, post_p
,
5144 is_gimple_lvalue
, fb_lvalue
);
5145 if (ret
!= GS_ERROR
)
5148 /* If we are going to write RESULT more than once, clear
5149 TREE_READONLY flag, otherwise we might incorrectly promote
5150 the variable to static const and initialize it at compile
5151 time in one of the branches. */
5153 && TREE_TYPE (TREE_OPERAND (cond
, 1)) != void_type_node
5154 && TREE_TYPE (TREE_OPERAND (cond
, 2)) != void_type_node
)
5155 TREE_READONLY (result
) = 0;
5156 if (TREE_TYPE (TREE_OPERAND (cond
, 1)) != void_type_node
)
5157 TREE_OPERAND (cond
, 1)
5158 = build2 (code
, void_type_node
, result
,
5159 TREE_OPERAND (cond
, 1));
5160 if (TREE_TYPE (TREE_OPERAND (cond
, 2)) != void_type_node
)
5161 TREE_OPERAND (cond
, 2)
5162 = build2 (code
, void_type_node
, unshare_expr (result
),
5163 TREE_OPERAND (cond
, 2));
5165 TREE_TYPE (cond
) = void_type_node
;
5166 recalculate_side_effects (cond
);
5170 gimplify_and_add (cond
, pre_p
);
5171 *expr_p
= unshare_expr (result
);
5180 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5181 return slot so that we don't generate a temporary. */
5182 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p
)
5183 && aggregate_value_p (*from_p
, *from_p
))
5187 if (!(rhs_predicate_for (*to_p
))(*from_p
))
5188 /* If we need a temporary, *to_p isn't accurate. */
5190 /* It's OK to use the return slot directly unless it's an NRV. */
5191 else if (TREE_CODE (*to_p
) == RESULT_DECL
5192 && DECL_NAME (*to_p
) == NULL_TREE
5193 && needs_to_live_in_memory (*to_p
))
5195 else if (is_gimple_reg_type (TREE_TYPE (*to_p
))
5196 || (DECL_P (*to_p
) && DECL_REGISTER (*to_p
)))
5197 /* Don't force regs into memory. */
5199 else if (TREE_CODE (*expr_p
) == INIT_EXPR
)
5200 /* It's OK to use the target directly if it's being
5203 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p
)))
5205 /* Always use the target and thus RSO for variable-sized types.
5206 GIMPLE cannot deal with a variable-sized assignment
5207 embedded in a call statement. */
5209 else if (TREE_CODE (*to_p
) != SSA_NAME
5210 && (!is_gimple_variable (*to_p
)
5211 || needs_to_live_in_memory (*to_p
)))
5212 /* Don't use the original target if it's already addressable;
5213 if its address escapes, and the called function uses the
5214 NRV optimization, a conforming program could see *to_p
5215 change before the called function returns; see c++/19317.
5216 When optimizing, the return_slot pass marks more functions
5217 as safe after we have escape info. */
5224 CALL_EXPR_RETURN_SLOT_OPT (*from_p
) = 1;
5225 mark_addressable (*to_p
);
5230 case WITH_SIZE_EXPR
:
5231 /* Likewise for calls that return an aggregate of non-constant size,
5232 since we would not be able to generate a temporary at all. */
5233 if (TREE_CODE (TREE_OPERAND (*from_p
, 0)) == CALL_EXPR
)
5235 *from_p
= TREE_OPERAND (*from_p
, 0);
5236 /* We don't change ret in this case because the
5237 WITH_SIZE_EXPR might have been added in
5238 gimplify_modify_expr, so returning GS_OK would lead to an
5244 /* If we're initializing from a container, push the initialization
5246 case CLEANUP_POINT_EXPR
:
5248 case STATEMENT_LIST
:
5250 tree wrap
= *from_p
;
5253 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_min_lval
,
5255 if (ret
!= GS_ERROR
)
5258 t
= voidify_wrapper_expr (wrap
, *expr_p
);
5259 gcc_assert (t
== *expr_p
);
5263 gimplify_and_add (wrap
, pre_p
);
5264 *expr_p
= unshare_expr (*to_p
);
5271 case COMPOUND_LITERAL_EXPR
:
5273 tree complit
= TREE_OPERAND (*expr_p
, 1);
5274 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (complit
);
5275 tree decl
= DECL_EXPR_DECL (decl_s
);
5276 tree init
= DECL_INITIAL (decl
);
5278 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5279 into struct T x = { 0, 1, 2 } if the address of the
5280 compound literal has never been taken. */
5281 if (!TREE_ADDRESSABLE (complit
)
5282 && !TREE_ADDRESSABLE (decl
)
5285 *expr_p
= copy_node (*expr_p
);
5286 TREE_OPERAND (*expr_p
, 1) = init
;
5301 /* Return true if T looks like a valid GIMPLE statement. */
5304 is_gimple_stmt (tree t
)
5306 const enum tree_code code
= TREE_CODE (t
);
5311 /* The only valid NOP_EXPR is the empty statement. */
5312 return IS_EMPTY_STMT (t
);
5316 /* These are only valid if they're void. */
5317 return TREE_TYPE (t
) == NULL
|| VOID_TYPE_P (TREE_TYPE (t
));
5323 case CASE_LABEL_EXPR
:
5324 case TRY_CATCH_EXPR
:
5325 case TRY_FINALLY_EXPR
:
5326 case EH_FILTER_EXPR
:
5329 case STATEMENT_LIST
:
5333 case OACC_HOST_DATA
:
5336 case OACC_ENTER_DATA
:
5337 case OACC_EXIT_DATA
:
5343 case OMP_DISTRIBUTE
:
5354 case OMP_TARGET_DATA
:
5355 case OMP_TARGET_UPDATE
:
5356 case OMP_TARGET_ENTER_DATA
:
5357 case OMP_TARGET_EXIT_DATA
:
5360 /* These are always void. */
5366 /* These are valid regardless of their type. */
5375 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5376 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5377 DECL_GIMPLE_REG_P set.
5379 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5380 other, unmodified part of the complex object just before the total store.
5381 As a consequence, if the object is still uninitialized, an undefined value
5382 will be loaded into a register, which may result in a spurious exception
5383 if the register is floating-point and the value happens to be a signaling
5384 NaN for example. Then the fully-fledged complex operations lowering pass
5385 followed by a DCE pass are necessary in order to fix things up. */
5387 static enum gimplify_status
5388 gimplify_modify_expr_complex_part (tree
*expr_p
, gimple_seq
*pre_p
,
5391 enum tree_code code
, ocode
;
5392 tree lhs
, rhs
, new_rhs
, other
, realpart
, imagpart
;
5394 lhs
= TREE_OPERAND (*expr_p
, 0);
5395 rhs
= TREE_OPERAND (*expr_p
, 1);
5396 code
= TREE_CODE (lhs
);
5397 lhs
= TREE_OPERAND (lhs
, 0);
5399 ocode
= code
== REALPART_EXPR
? IMAGPART_EXPR
: REALPART_EXPR
;
5400 other
= build1 (ocode
, TREE_TYPE (rhs
), lhs
);
5401 TREE_NO_WARNING (other
) = 1;
5402 other
= get_formal_tmp_var (other
, pre_p
);
5404 realpart
= code
== REALPART_EXPR
? rhs
: other
;
5405 imagpart
= code
== REALPART_EXPR
? other
: rhs
;
5407 if (TREE_CONSTANT (realpart
) && TREE_CONSTANT (imagpart
))
5408 new_rhs
= build_complex (TREE_TYPE (lhs
), realpart
, imagpart
);
5410 new_rhs
= build2 (COMPLEX_EXPR
, TREE_TYPE (lhs
), realpart
, imagpart
);
5412 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (lhs
, new_rhs
));
5413 *expr_p
= (want_value
) ? rhs
: NULL_TREE
;
5418 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5424 PRE_P points to the list where side effects that must happen before
5425 *EXPR_P should be stored.
5427 POST_P points to the list where side effects that must happen after
5428 *EXPR_P should be stored.
5430 WANT_VALUE is nonzero iff we want to use the value of this expression
5431 in another expression. */
5433 static enum gimplify_status
5434 gimplify_modify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
5437 tree
*from_p
= &TREE_OPERAND (*expr_p
, 1);
5438 tree
*to_p
= &TREE_OPERAND (*expr_p
, 0);
5439 enum gimplify_status ret
= GS_UNHANDLED
;
5441 location_t loc
= EXPR_LOCATION (*expr_p
);
5442 gimple_stmt_iterator gsi
;
5444 gcc_assert (TREE_CODE (*expr_p
) == MODIFY_EXPR
5445 || TREE_CODE (*expr_p
) == INIT_EXPR
);
5447 /* Trying to simplify a clobber using normal logic doesn't work,
5448 so handle it here. */
5449 if (TREE_CLOBBER_P (*from_p
))
5451 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
5452 if (ret
== GS_ERROR
)
5454 gcc_assert (!want_value
5455 && (VAR_P (*to_p
) || TREE_CODE (*to_p
) == MEM_REF
));
5456 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (*to_p
, *from_p
));
5461 /* Insert pointer conversions required by the middle-end that are not
5462 required by the frontend. This fixes middle-end type checking for
5463 for example gcc.dg/redecl-6.c. */
5464 if (POINTER_TYPE_P (TREE_TYPE (*to_p
)))
5466 STRIP_USELESS_TYPE_CONVERSION (*from_p
);
5467 if (!useless_type_conversion_p (TREE_TYPE (*to_p
), TREE_TYPE (*from_p
)))
5468 *from_p
= fold_convert_loc (loc
, TREE_TYPE (*to_p
), *from_p
);
5471 /* See if any simplifications can be done based on what the RHS is. */
5472 ret
= gimplify_modify_expr_rhs (expr_p
, from_p
, to_p
, pre_p
, post_p
,
5474 if (ret
!= GS_UNHANDLED
)
5477 /* For zero sized types only gimplify the left hand side and right hand
5478 side as statements and throw away the assignment. Do this after
5479 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5481 if (zero_sized_type (TREE_TYPE (*from_p
))
5483 /* Don't do this for calls that return addressable types, expand_call
5484 relies on those having a lhs. */
5485 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p
))
5486 && TREE_CODE (*from_p
) == CALL_EXPR
))
5488 gimplify_stmt (from_p
, pre_p
);
5489 gimplify_stmt (to_p
, pre_p
);
5490 *expr_p
= NULL_TREE
;
5494 /* If the value being copied is of variable width, compute the length
5495 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5496 before gimplifying any of the operands so that we can resolve any
5497 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5498 the size of the expression to be copied, not of the destination, so
5499 that is what we must do here. */
5500 maybe_with_size_expr (from_p
);
5502 /* As a special case, we have to temporarily allow for assignments
5503 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5504 a toplevel statement, when gimplifying the GENERIC expression
5505 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5506 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5508 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5509 prevent gimplify_expr from trying to create a new temporary for
5510 foo's LHS, we tell it that it should only gimplify until it
5511 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5512 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5513 and all we need to do here is set 'a' to be its LHS. */
5515 /* Gimplify the RHS first for C++17 and bug 71104. */
5516 gimple_predicate initial_pred
= initial_rhs_predicate_for (*to_p
);
5517 ret
= gimplify_expr (from_p
, pre_p
, post_p
, initial_pred
, fb_rvalue
);
5518 if (ret
== GS_ERROR
)
5521 /* Then gimplify the LHS. */
5522 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5523 twice we have to make sure to gimplify into non-SSA as otherwise
5524 the abnormal edge added later will make those defs not dominate
5526 ??? Technically this applies only to the registers used in the
5527 resulting non-register *TO_P. */
5528 bool saved_into_ssa
= gimplify_ctxp
->into_ssa
;
5530 && TREE_CODE (*from_p
) == CALL_EXPR
5531 && call_expr_flags (*from_p
) & ECF_RETURNS_TWICE
)
5532 gimplify_ctxp
->into_ssa
= false;
5533 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
5534 gimplify_ctxp
->into_ssa
= saved_into_ssa
;
5535 if (ret
== GS_ERROR
)
5538 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5539 guess for the predicate was wrong. */
5540 gimple_predicate final_pred
= rhs_predicate_for (*to_p
);
5541 if (final_pred
!= initial_pred
)
5543 ret
= gimplify_expr (from_p
, pre_p
, post_p
, final_pred
, fb_rvalue
);
5544 if (ret
== GS_ERROR
)
5548 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5549 size as argument to the call. */
5550 if (TREE_CODE (*from_p
) == WITH_SIZE_EXPR
)
5552 tree call
= TREE_OPERAND (*from_p
, 0);
5553 tree vlasize
= TREE_OPERAND (*from_p
, 1);
5555 if (TREE_CODE (call
) == CALL_EXPR
5556 && CALL_EXPR_IFN (call
) == IFN_VA_ARG
)
5558 int nargs
= call_expr_nargs (call
);
5559 tree type
= TREE_TYPE (call
);
5560 tree ap
= CALL_EXPR_ARG (call
, 0);
5561 tree tag
= CALL_EXPR_ARG (call
, 1);
5562 tree aptag
= CALL_EXPR_ARG (call
, 2);
5563 tree newcall
= build_call_expr_internal_loc (EXPR_LOCATION (call
),
5567 TREE_OPERAND (*from_p
, 0) = newcall
;
5571 /* Now see if the above changed *from_p to something we handle specially. */
5572 ret
= gimplify_modify_expr_rhs (expr_p
, from_p
, to_p
, pre_p
, post_p
,
5574 if (ret
!= GS_UNHANDLED
)
5577 /* If we've got a variable sized assignment between two lvalues (i.e. does
5578 not involve a call), then we can make things a bit more straightforward
5579 by converting the assignment to memcpy or memset. */
5580 if (TREE_CODE (*from_p
) == WITH_SIZE_EXPR
)
5582 tree from
= TREE_OPERAND (*from_p
, 0);
5583 tree size
= TREE_OPERAND (*from_p
, 1);
5585 if (TREE_CODE (from
) == CONSTRUCTOR
)
5586 return gimplify_modify_expr_to_memset (expr_p
, size
, want_value
, pre_p
);
5588 if (is_gimple_addressable (from
))
5591 return gimplify_modify_expr_to_memcpy (expr_p
, size
, want_value
,
5596 /* Transform partial stores to non-addressable complex variables into
5597 total stores. This allows us to use real instead of virtual operands
5598 for these variables, which improves optimization. */
5599 if ((TREE_CODE (*to_p
) == REALPART_EXPR
5600 || TREE_CODE (*to_p
) == IMAGPART_EXPR
)
5601 && is_gimple_reg (TREE_OPERAND (*to_p
, 0)))
5602 return gimplify_modify_expr_complex_part (expr_p
, pre_p
, want_value
);
5604 /* Try to alleviate the effects of the gimplification creating artificial
5605 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5606 make sure not to create DECL_DEBUG_EXPR links across functions. */
5607 if (!gimplify_ctxp
->into_ssa
5609 && DECL_IGNORED_P (*from_p
)
5611 && !DECL_IGNORED_P (*to_p
)
5612 && decl_function_context (*to_p
) == current_function_decl
5613 && decl_function_context (*from_p
) == current_function_decl
)
5615 if (!DECL_NAME (*from_p
) && DECL_NAME (*to_p
))
5617 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p
)));
5618 DECL_HAS_DEBUG_EXPR_P (*from_p
) = 1;
5619 SET_DECL_DEBUG_EXPR (*from_p
, *to_p
);
5622 if (want_value
&& TREE_THIS_VOLATILE (*to_p
))
5623 *from_p
= get_initialized_tmp_var (*from_p
, pre_p
, post_p
);
5625 if (TREE_CODE (*from_p
) == CALL_EXPR
)
5627 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5628 instead of a GIMPLE_ASSIGN. */
5630 if (CALL_EXPR_FN (*from_p
) == NULL_TREE
)
5632 /* Gimplify internal functions created in the FEs. */
5633 int nargs
= call_expr_nargs (*from_p
), i
;
5634 enum internal_fn ifn
= CALL_EXPR_IFN (*from_p
);
5635 auto_vec
<tree
> vargs (nargs
);
5637 for (i
= 0; i
< nargs
; i
++)
5639 gimplify_arg (&CALL_EXPR_ARG (*from_p
, i
), pre_p
,
5640 EXPR_LOCATION (*from_p
));
5641 vargs
.quick_push (CALL_EXPR_ARG (*from_p
, i
));
5643 call_stmt
= gimple_build_call_internal_vec (ifn
, vargs
);
5644 gimple_call_set_nothrow (call_stmt
, TREE_NOTHROW (*from_p
));
5645 gimple_set_location (call_stmt
, EXPR_LOCATION (*expr_p
));
5649 tree fnptrtype
= TREE_TYPE (CALL_EXPR_FN (*from_p
));
5650 CALL_EXPR_FN (*from_p
) = TREE_OPERAND (CALL_EXPR_FN (*from_p
), 0);
5651 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p
));
5652 tree fndecl
= get_callee_fndecl (*from_p
);
5654 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
5655 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
5656 && call_expr_nargs (*from_p
) == 3)
5657 call_stmt
= gimple_build_call_internal (IFN_BUILTIN_EXPECT
, 3,
5658 CALL_EXPR_ARG (*from_p
, 0),
5659 CALL_EXPR_ARG (*from_p
, 1),
5660 CALL_EXPR_ARG (*from_p
, 2));
5663 call_stmt
= gimple_build_call_from_tree (*from_p
, fnptrtype
);
5666 notice_special_calls (call_stmt
);
5667 if (!gimple_call_noreturn_p (call_stmt
) || !should_remove_lhs_p (*to_p
))
5668 gimple_call_set_lhs (call_stmt
, *to_p
);
5669 else if (TREE_CODE (*to_p
) == SSA_NAME
)
5670 /* The above is somewhat premature, avoid ICEing later for a
5671 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5672 ??? This doesn't make it a default-def. */
5673 SSA_NAME_DEF_STMT (*to_p
) = gimple_build_nop ();
5675 if (EXPR_CILK_SPAWN (*from_p
))
5676 gimplify_cilk_detach (pre_p
);
5681 assign
= gimple_build_assign (*to_p
, *from_p
);
5682 gimple_set_location (assign
, EXPR_LOCATION (*expr_p
));
5683 if (COMPARISON_CLASS_P (*from_p
))
5684 gimple_set_no_warning (assign
, TREE_NO_WARNING (*from_p
));
5687 if (gimplify_ctxp
->into_ssa
&& is_gimple_reg (*to_p
))
5689 /* We should have got an SSA name from the start. */
5690 gcc_assert (TREE_CODE (*to_p
) == SSA_NAME
5691 || ! gimple_in_ssa_p (cfun
));
5694 gimplify_seq_add_stmt (pre_p
, assign
);
5695 gsi
= gsi_last (*pre_p
);
5696 maybe_fold_stmt (&gsi
);
5700 *expr_p
= TREE_THIS_VOLATILE (*to_p
) ? *from_p
: unshare_expr (*to_p
);
5709 /* Gimplify a comparison between two variable-sized objects. Do this
5710 with a call to BUILT_IN_MEMCMP. */
5712 static enum gimplify_status
5713 gimplify_variable_sized_compare (tree
*expr_p
)
5715 location_t loc
= EXPR_LOCATION (*expr_p
);
5716 tree op0
= TREE_OPERAND (*expr_p
, 0);
5717 tree op1
= TREE_OPERAND (*expr_p
, 1);
5718 tree t
, arg
, dest
, src
, expr
;
5720 arg
= TYPE_SIZE_UNIT (TREE_TYPE (op0
));
5721 arg
= unshare_expr (arg
);
5722 arg
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg
, op0
);
5723 src
= build_fold_addr_expr_loc (loc
, op1
);
5724 dest
= build_fold_addr_expr_loc (loc
, op0
);
5725 t
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
5726 t
= build_call_expr_loc (loc
, t
, 3, dest
, src
, arg
);
5729 = build2 (TREE_CODE (*expr_p
), TREE_TYPE (*expr_p
), t
, integer_zero_node
);
5730 SET_EXPR_LOCATION (expr
, loc
);
5736 /* Gimplify a comparison between two aggregate objects of integral scalar
5737 mode as a comparison between the bitwise equivalent scalar values. */
5739 static enum gimplify_status
5740 gimplify_scalar_mode_aggregate_compare (tree
*expr_p
)
5742 location_t loc
= EXPR_LOCATION (*expr_p
);
5743 tree op0
= TREE_OPERAND (*expr_p
, 0);
5744 tree op1
= TREE_OPERAND (*expr_p
, 1);
5746 tree type
= TREE_TYPE (op0
);
5747 tree scalar_type
= lang_hooks
.types
.type_for_mode (TYPE_MODE (type
), 1);
5749 op0
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, scalar_type
, op0
);
5750 op1
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, scalar_type
, op1
);
5753 = fold_build2_loc (loc
, TREE_CODE (*expr_p
), TREE_TYPE (*expr_p
), op0
, op1
);
5758 /* Gimplify an expression sequence. This function gimplifies each
5759 expression and rewrites the original expression with the last
5760 expression of the sequence in GIMPLE form.
5762 PRE_P points to the list where the side effects for all the
5763 expressions in the sequence will be emitted.
5765 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5767 static enum gimplify_status
5768 gimplify_compound_expr (tree
*expr_p
, gimple_seq
*pre_p
, bool want_value
)
5774 tree
*sub_p
= &TREE_OPERAND (t
, 0);
5776 if (TREE_CODE (*sub_p
) == COMPOUND_EXPR
)
5777 gimplify_compound_expr (sub_p
, pre_p
, false);
5779 gimplify_stmt (sub_p
, pre_p
);
5781 t
= TREE_OPERAND (t
, 1);
5783 while (TREE_CODE (t
) == COMPOUND_EXPR
);
5790 gimplify_stmt (expr_p
, pre_p
);
5795 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5796 gimplify. After gimplification, EXPR_P will point to a new temporary
5797 that holds the original value of the SAVE_EXPR node.
5799 PRE_P points to the list where side effects that must happen before
5800 *EXPR_P should be stored. */
5802 static enum gimplify_status
5803 gimplify_save_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5805 enum gimplify_status ret
= GS_ALL_DONE
;
5808 gcc_assert (TREE_CODE (*expr_p
) == SAVE_EXPR
);
5809 val
= TREE_OPERAND (*expr_p
, 0);
5811 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5812 if (!SAVE_EXPR_RESOLVED_P (*expr_p
))
5814 /* The operand may be a void-valued expression. It is
5815 being executed only for its side-effects. */
5816 if (TREE_TYPE (val
) == void_type_node
)
5818 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
5819 is_gimple_stmt
, fb_none
);
5823 /* The temporary may not be an SSA name as later abnormal and EH
5824 control flow may invalidate use/def domination. */
5825 val
= get_initialized_tmp_var (val
, pre_p
, post_p
, false);
5827 TREE_OPERAND (*expr_p
, 0) = val
;
5828 SAVE_EXPR_RESOLVED_P (*expr_p
) = 1;
5836 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5843 PRE_P points to the list where side effects that must happen before
5844 *EXPR_P should be stored.
5846 POST_P points to the list where side effects that must happen after
5847 *EXPR_P should be stored. */
5849 static enum gimplify_status
5850 gimplify_addr_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5852 tree expr
= *expr_p
;
5853 tree op0
= TREE_OPERAND (expr
, 0);
5854 enum gimplify_status ret
;
5855 location_t loc
= EXPR_LOCATION (*expr_p
);
5857 switch (TREE_CODE (op0
))
5861 /* Check if we are dealing with an expression of the form '&*ptr'.
5862 While the front end folds away '&*ptr' into 'ptr', these
5863 expressions may be generated internally by the compiler (e.g.,
5864 builtins like __builtin_va_end). */
5865 /* Caution: the silent array decomposition semantics we allow for
5866 ADDR_EXPR means we can't always discard the pair. */
5867 /* Gimplification of the ADDR_EXPR operand may drop
5868 cv-qualification conversions, so make sure we add them if
5871 tree op00
= TREE_OPERAND (op0
, 0);
5872 tree t_expr
= TREE_TYPE (expr
);
5873 tree t_op00
= TREE_TYPE (op00
);
5875 if (!useless_type_conversion_p (t_expr
, t_op00
))
5876 op00
= fold_convert_loc (loc
, TREE_TYPE (expr
), op00
);
5882 case VIEW_CONVERT_EXPR
:
5883 /* Take the address of our operand and then convert it to the type of
5886 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5887 all clear. The impact of this transformation is even less clear. */
5889 /* If the operand is a useless conversion, look through it. Doing so
5890 guarantees that the ADDR_EXPR and its operand will remain of the
5892 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0
, 0)))
5893 op0
= TREE_OPERAND (op0
, 0);
5895 *expr_p
= fold_convert_loc (loc
, TREE_TYPE (expr
),
5896 build_fold_addr_expr_loc (loc
,
5897 TREE_OPERAND (op0
, 0)));
5902 if (integer_zerop (TREE_OPERAND (op0
, 1)))
5903 goto do_indirect_ref
;
5908 /* If we see a call to a declared builtin or see its address
5909 being taken (we can unify those cases here) then we can mark
5910 the builtin for implicit generation by GCC. */
5911 if (TREE_CODE (op0
) == FUNCTION_DECL
5912 && DECL_BUILT_IN_CLASS (op0
) == BUILT_IN_NORMAL
5913 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0
)))
5914 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0
), true);
5916 /* We use fb_either here because the C frontend sometimes takes
5917 the address of a call that returns a struct; see
5918 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5919 the implied temporary explicit. */
5921 /* Make the operand addressable. */
5922 ret
= gimplify_expr (&TREE_OPERAND (expr
, 0), pre_p
, post_p
,
5923 is_gimple_addressable
, fb_either
);
5924 if (ret
== GS_ERROR
)
5927 /* Then mark it. Beware that it may not be possible to do so directly
5928 if a temporary has been created by the gimplification. */
5929 prepare_gimple_addressable (&TREE_OPERAND (expr
, 0), pre_p
);
5931 op0
= TREE_OPERAND (expr
, 0);
5933 /* For various reasons, the gimplification of the expression
5934 may have made a new INDIRECT_REF. */
5935 if (TREE_CODE (op0
) == INDIRECT_REF
)
5936 goto do_indirect_ref
;
5938 mark_addressable (TREE_OPERAND (expr
, 0));
5940 /* The FEs may end up building ADDR_EXPRs early on a decl with
5941 an incomplete type. Re-build ADDR_EXPRs in canonical form
5943 if (!types_compatible_p (TREE_TYPE (op0
), TREE_TYPE (TREE_TYPE (expr
))))
5944 *expr_p
= build_fold_addr_expr (op0
);
5946 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5947 recompute_tree_invariant_for_addr_expr (*expr_p
);
5949 /* If we re-built the ADDR_EXPR add a conversion to the original type
5951 if (!useless_type_conversion_p (TREE_TYPE (expr
), TREE_TYPE (*expr_p
)))
5952 *expr_p
= fold_convert (TREE_TYPE (expr
), *expr_p
);
5960 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5961 value; output operands should be a gimple lvalue. */
5963 static enum gimplify_status
5964 gimplify_asm_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5968 const char **oconstraints
;
5971 const char *constraint
;
5972 bool allows_mem
, allows_reg
, is_inout
;
5973 enum gimplify_status ret
, tret
;
5975 vec
<tree
, va_gc
> *inputs
;
5976 vec
<tree
, va_gc
> *outputs
;
5977 vec
<tree
, va_gc
> *clobbers
;
5978 vec
<tree
, va_gc
> *labels
;
5982 noutputs
= list_length (ASM_OUTPUTS (expr
));
5983 oconstraints
= (const char **) alloca ((noutputs
) * sizeof (const char *));
5991 link_next
= NULL_TREE
;
5992 for (i
= 0, link
= ASM_OUTPUTS (expr
); link
; ++i
, link
= link_next
)
5995 size_t constraint_len
;
5997 link_next
= TREE_CHAIN (link
);
6001 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
6002 constraint_len
= strlen (constraint
);
6003 if (constraint_len
== 0)
6006 ok
= parse_output_constraint (&constraint
, i
, 0, 0,
6007 &allows_mem
, &allows_reg
, &is_inout
);
6014 if (!allows_reg
&& allows_mem
)
6015 mark_addressable (TREE_VALUE (link
));
6017 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
6018 is_inout
? is_gimple_min_lval
: is_gimple_lvalue
,
6019 fb_lvalue
| fb_mayfail
);
6020 if (tret
== GS_ERROR
)
6022 error ("invalid lvalue in asm output %d", i
);
6026 /* If the constraint does not allow memory make sure we gimplify
6027 it to a register if it is not already but its base is. This
6028 happens for complex and vector components. */
6031 tree op
= TREE_VALUE (link
);
6032 if (! is_gimple_val (op
)
6033 && is_gimple_reg_type (TREE_TYPE (op
))
6034 && is_gimple_reg (get_base_address (op
)))
6036 tree tem
= create_tmp_reg (TREE_TYPE (op
));
6040 ass
= build2 (MODIFY_EXPR
, TREE_TYPE (tem
),
6041 tem
, unshare_expr (op
));
6042 gimplify_and_add (ass
, pre_p
);
6044 ass
= build2 (MODIFY_EXPR
, TREE_TYPE (tem
), op
, tem
);
6045 gimplify_and_add (ass
, post_p
);
6047 TREE_VALUE (link
) = tem
;
6052 vec_safe_push (outputs
, link
);
6053 TREE_CHAIN (link
) = NULL_TREE
;
6057 /* An input/output operand. To give the optimizers more
6058 flexibility, split it into separate input and output
6061 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6064 /* Turn the in/out constraint into an output constraint. */
6065 char *p
= xstrdup (constraint
);
6067 TREE_VALUE (TREE_PURPOSE (link
)) = build_string (constraint_len
, p
);
6069 /* And add a matching input constraint. */
6072 sprintf (buf
, "%u", i
);
6074 /* If there are multiple alternatives in the constraint,
6075 handle each of them individually. Those that allow register
6076 will be replaced with operand number, the others will stay
6078 if (strchr (p
, ',') != NULL
)
6080 size_t len
= 0, buflen
= strlen (buf
);
6081 char *beg
, *end
, *str
, *dst
;
6085 end
= strchr (beg
, ',');
6087 end
= strchr (beg
, '\0');
6088 if ((size_t) (end
- beg
) < buflen
)
6091 len
+= end
- beg
+ 1;
6098 str
= (char *) alloca (len
);
6099 for (beg
= p
+ 1, dst
= str
;;)
6102 bool mem_p
, reg_p
, inout_p
;
6104 end
= strchr (beg
, ',');
6109 parse_output_constraint (&tem
, i
, 0, 0,
6110 &mem_p
, ®_p
, &inout_p
);
6115 memcpy (dst
, buf
, buflen
);
6124 memcpy (dst
, beg
, len
);
6133 input
= build_string (dst
- str
, str
);
6136 input
= build_string (strlen (buf
), buf
);
6139 input
= build_string (constraint_len
- 1, constraint
+ 1);
6143 input
= build_tree_list (build_tree_list (NULL_TREE
, input
),
6144 unshare_expr (TREE_VALUE (link
)));
6145 ASM_INPUTS (expr
) = chainon (ASM_INPUTS (expr
), input
);
6149 link_next
= NULL_TREE
;
6150 for (link
= ASM_INPUTS (expr
); link
; ++i
, link
= link_next
)
6152 link_next
= TREE_CHAIN (link
);
6153 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
6154 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
6155 oconstraints
, &allows_mem
, &allows_reg
);
6157 /* If we can't make copies, we can only accept memory. */
6158 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link
))))
6164 error ("impossible constraint in %<asm%>");
6165 error ("non-memory input %d must stay in memory", i
);
6170 /* If the operand is a memory input, it should be an lvalue. */
6171 if (!allows_reg
&& allows_mem
)
6173 tree inputv
= TREE_VALUE (link
);
6174 STRIP_NOPS (inputv
);
6175 if (TREE_CODE (inputv
) == PREDECREMENT_EXPR
6176 || TREE_CODE (inputv
) == PREINCREMENT_EXPR
6177 || TREE_CODE (inputv
) == POSTDECREMENT_EXPR
6178 || TREE_CODE (inputv
) == POSTINCREMENT_EXPR
6179 || TREE_CODE (inputv
) == MODIFY_EXPR
)
6180 TREE_VALUE (link
) = error_mark_node
;
6181 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
6182 is_gimple_lvalue
, fb_lvalue
| fb_mayfail
);
6183 if (tret
!= GS_ERROR
)
6185 /* Unlike output operands, memory inputs are not guaranteed
6186 to be lvalues by the FE, and while the expressions are
6187 marked addressable there, if it is e.g. a statement
6188 expression, temporaries in it might not end up being
6189 addressable. They might be already used in the IL and thus
6190 it is too late to make them addressable now though. */
6191 tree x
= TREE_VALUE (link
);
6192 while (handled_component_p (x
))
6193 x
= TREE_OPERAND (x
, 0);
6194 if (TREE_CODE (x
) == MEM_REF
6195 && TREE_CODE (TREE_OPERAND (x
, 0)) == ADDR_EXPR
)
6196 x
= TREE_OPERAND (TREE_OPERAND (x
, 0), 0);
6198 || TREE_CODE (x
) == PARM_DECL
6199 || TREE_CODE (x
) == RESULT_DECL
)
6200 && !TREE_ADDRESSABLE (x
)
6201 && is_gimple_reg (x
))
6203 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link
),
6205 "memory input %d is not directly addressable",
6207 prepare_gimple_addressable (&TREE_VALUE (link
), pre_p
);
6210 mark_addressable (TREE_VALUE (link
));
6211 if (tret
== GS_ERROR
)
6213 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link
), input_location
),
6214 "memory input %d is not directly addressable", i
);
6220 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
6221 is_gimple_asm_val
, fb_rvalue
);
6222 if (tret
== GS_ERROR
)
6226 TREE_CHAIN (link
) = NULL_TREE
;
6227 vec_safe_push (inputs
, link
);
6230 link_next
= NULL_TREE
;
6231 for (link
= ASM_CLOBBERS (expr
); link
; ++i
, link
= link_next
)
6233 link_next
= TREE_CHAIN (link
);
6234 TREE_CHAIN (link
) = NULL_TREE
;
6235 vec_safe_push (clobbers
, link
);
6238 link_next
= NULL_TREE
;
6239 for (link
= ASM_LABELS (expr
); link
; ++i
, link
= link_next
)
6241 link_next
= TREE_CHAIN (link
);
6242 TREE_CHAIN (link
) = NULL_TREE
;
6243 vec_safe_push (labels
, link
);
6246 /* Do not add ASMs with errors to the gimple IL stream. */
6247 if (ret
!= GS_ERROR
)
6249 stmt
= gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr
)),
6250 inputs
, outputs
, clobbers
, labels
);
6252 gimple_asm_set_volatile (stmt
, ASM_VOLATILE_P (expr
) || noutputs
== 0);
6253 gimple_asm_set_input (stmt
, ASM_INPUT_P (expr
));
6255 gimplify_seq_add_stmt (pre_p
, stmt
);
6261 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6262 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6263 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6264 return to this function.
6266 FIXME should we complexify the prequeue handling instead? Or use flags
6267 for all the cleanups and let the optimizer tighten them up? The current
6268 code seems pretty fragile; it will break on a cleanup within any
6269 non-conditional nesting. But any such nesting would be broken, anyway;
6270 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6271 and continues out of it. We can do that at the RTL level, though, so
6272 having an optimizer to tighten up try/finally regions would be a Good
6275 static enum gimplify_status
6276 gimplify_cleanup_point_expr (tree
*expr_p
, gimple_seq
*pre_p
)
6278 gimple_stmt_iterator iter
;
6279 gimple_seq body_sequence
= NULL
;
6281 tree temp
= voidify_wrapper_expr (*expr_p
, NULL
);
6283 /* We only care about the number of conditions between the innermost
6284 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6285 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6286 int old_conds
= gimplify_ctxp
->conditions
;
6287 gimple_seq old_cleanups
= gimplify_ctxp
->conditional_cleanups
;
6288 bool old_in_cleanup_point_expr
= gimplify_ctxp
->in_cleanup_point_expr
;
6289 gimplify_ctxp
->conditions
= 0;
6290 gimplify_ctxp
->conditional_cleanups
= NULL
;
6291 gimplify_ctxp
->in_cleanup_point_expr
= true;
6293 gimplify_stmt (&TREE_OPERAND (*expr_p
, 0), &body_sequence
);
6295 gimplify_ctxp
->conditions
= old_conds
;
6296 gimplify_ctxp
->conditional_cleanups
= old_cleanups
;
6297 gimplify_ctxp
->in_cleanup_point_expr
= old_in_cleanup_point_expr
;
6299 for (iter
= gsi_start (body_sequence
); !gsi_end_p (iter
); )
6301 gimple
*wce
= gsi_stmt (iter
);
6303 if (gimple_code (wce
) == GIMPLE_WITH_CLEANUP_EXPR
)
6305 if (gsi_one_before_end_p (iter
))
6307 /* Note that gsi_insert_seq_before and gsi_remove do not
6308 scan operands, unlike some other sequence mutators. */
6309 if (!gimple_wce_cleanup_eh_only (wce
))
6310 gsi_insert_seq_before_without_update (&iter
,
6311 gimple_wce_cleanup (wce
),
6313 gsi_remove (&iter
, true);
6320 enum gimple_try_flags kind
;
6322 if (gimple_wce_cleanup_eh_only (wce
))
6323 kind
= GIMPLE_TRY_CATCH
;
6325 kind
= GIMPLE_TRY_FINALLY
;
6326 seq
= gsi_split_seq_after (iter
);
6328 gtry
= gimple_build_try (seq
, gimple_wce_cleanup (wce
), kind
);
6329 /* Do not use gsi_replace here, as it may scan operands.
6330 We want to do a simple structural modification only. */
6331 gsi_set_stmt (&iter
, gtry
);
6332 iter
= gsi_start (gtry
->eval
);
6339 gimplify_seq_add_seq (pre_p
, body_sequence
);
6352 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6353 is the cleanup action required. EH_ONLY is true if the cleanup should
6354 only be executed if an exception is thrown, not on normal exit.
6355 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6356 only valid for clobbers. */
6359 gimple_push_cleanup (tree var
, tree cleanup
, bool eh_only
, gimple_seq
*pre_p
,
6360 bool force_uncond
= false)
6363 gimple_seq cleanup_stmts
= NULL
;
6365 /* Errors can result in improperly nested cleanups. Which results in
6366 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6370 if (gimple_conditional_context ())
6372 /* If we're in a conditional context, this is more complex. We only
6373 want to run the cleanup if we actually ran the initialization that
6374 necessitates it, but we want to run it after the end of the
6375 conditional context. So we wrap the try/finally around the
6376 condition and use a flag to determine whether or not to actually
6377 run the destructor. Thus
6381 becomes (approximately)
6385 if (test) { A::A(temp); flag = 1; val = f(temp); }
6388 if (flag) A::~A(temp);
6394 gimplify_stmt (&cleanup
, &cleanup_stmts
);
6395 wce
= gimple_build_wce (cleanup_stmts
);
6396 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, wce
);
6400 tree flag
= create_tmp_var (boolean_type_node
, "cleanup");
6401 gassign
*ffalse
= gimple_build_assign (flag
, boolean_false_node
);
6402 gassign
*ftrue
= gimple_build_assign (flag
, boolean_true_node
);
6404 cleanup
= build3 (COND_EXPR
, void_type_node
, flag
, cleanup
, NULL
);
6405 gimplify_stmt (&cleanup
, &cleanup_stmts
);
6406 wce
= gimple_build_wce (cleanup_stmts
);
6408 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, ffalse
);
6409 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, wce
);
6410 gimplify_seq_add_stmt (pre_p
, ftrue
);
6412 /* Because of this manipulation, and the EH edges that jump
6413 threading cannot redirect, the temporary (VAR) will appear
6414 to be used uninitialized. Don't warn. */
6415 TREE_NO_WARNING (var
) = 1;
6420 gimplify_stmt (&cleanup
, &cleanup_stmts
);
6421 wce
= gimple_build_wce (cleanup_stmts
);
6422 gimple_wce_set_cleanup_eh_only (wce
, eh_only
);
6423 gimplify_seq_add_stmt (pre_p
, wce
);
6427 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6429 static enum gimplify_status
6430 gimplify_target_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
6432 tree targ
= *expr_p
;
6433 tree temp
= TARGET_EXPR_SLOT (targ
);
6434 tree init
= TARGET_EXPR_INITIAL (targ
);
6435 enum gimplify_status ret
;
6437 bool unpoison_empty_seq
= false;
6438 gimple_stmt_iterator unpoison_it
;
6442 tree cleanup
= NULL_TREE
;
6444 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6445 to the temps list. Handle also variable length TARGET_EXPRs. */
6446 if (TREE_CODE (DECL_SIZE (temp
)) != INTEGER_CST
)
6448 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp
)))
6449 gimplify_type_sizes (TREE_TYPE (temp
), pre_p
);
6450 gimplify_vla_decl (temp
, pre_p
);
6454 /* Save location where we need to place unpoisoning. It's possible
6455 that a variable will be converted to needs_to_live_in_memory. */
6456 unpoison_it
= gsi_last (*pre_p
);
6457 unpoison_empty_seq
= gsi_end_p (unpoison_it
);
6459 gimple_add_tmp_var (temp
);
6462 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6463 expression is supposed to initialize the slot. */
6464 if (VOID_TYPE_P (TREE_TYPE (init
)))
6465 ret
= gimplify_expr (&init
, pre_p
, post_p
, is_gimple_stmt
, fb_none
);
6468 tree init_expr
= build2 (INIT_EXPR
, void_type_node
, temp
, init
);
6470 ret
= gimplify_expr (&init
, pre_p
, post_p
, is_gimple_stmt
, fb_none
);
6472 ggc_free (init_expr
);
6474 if (ret
== GS_ERROR
)
6476 /* PR c++/28266 Make sure this is expanded only once. */
6477 TARGET_EXPR_INITIAL (targ
) = NULL_TREE
;
6481 gimplify_and_add (init
, pre_p
);
6483 /* If needed, push the cleanup for the temp. */
6484 if (TARGET_EXPR_CLEANUP (targ
))
6486 if (CLEANUP_EH_ONLY (targ
))
6487 gimple_push_cleanup (temp
, TARGET_EXPR_CLEANUP (targ
),
6488 CLEANUP_EH_ONLY (targ
), pre_p
);
6490 cleanup
= TARGET_EXPR_CLEANUP (targ
);
6493 /* Add a clobber for the temporary going out of scope, like
6494 gimplify_bind_expr. */
6495 if (gimplify_ctxp
->in_cleanup_point_expr
6496 && needs_to_live_in_memory (temp
))
6498 if (flag_stack_reuse
== SR_ALL
)
6500 tree clobber
= build_constructor (TREE_TYPE (temp
),
6502 TREE_THIS_VOLATILE (clobber
) = true;
6503 clobber
= build2 (MODIFY_EXPR
, TREE_TYPE (temp
), temp
, clobber
);
6504 gimple_push_cleanup (temp
, clobber
, false, pre_p
, true);
6506 if (asan_poisoned_variables
6507 && DECL_ALIGN (temp
) <= MAX_SUPPORTED_STACK_ALIGNMENT
6508 && dbg_cnt (asan_use_after_scope
))
6510 tree asan_cleanup
= build_asan_poison_call_expr (temp
);
6513 if (unpoison_empty_seq
)
6514 unpoison_it
= gsi_start (*pre_p
);
6516 asan_poison_variable (temp
, false, &unpoison_it
,
6517 unpoison_empty_seq
);
6518 gimple_push_cleanup (temp
, asan_cleanup
, false, pre_p
);
6523 gimple_push_cleanup (temp
, cleanup
, false, pre_p
);
6525 /* Only expand this once. */
6526 TREE_OPERAND (targ
, 3) = init
;
6527 TARGET_EXPR_INITIAL (targ
) = NULL_TREE
;
6530 /* We should have expanded this before. */
6531 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp
));
6537 /* Gimplification of expression trees. */
6539 /* Gimplify an expression which appears at statement context. The
6540 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6541 NULL, a new sequence is allocated.
6543 Return true if we actually added a statement to the queue. */
6546 gimplify_stmt (tree
*stmt_p
, gimple_seq
*seq_p
)
6548 gimple_seq_node last
;
6550 last
= gimple_seq_last (*seq_p
);
6551 gimplify_expr (stmt_p
, seq_p
, NULL
, is_gimple_stmt
, fb_none
);
6552 return last
!= gimple_seq_last (*seq_p
);
6555 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6556 to CTX. If entries already exist, force them to be some flavor of private.
6557 If there is no enclosing parallel, do nothing. */
6560 omp_firstprivatize_variable (struct gimplify_omp_ctx
*ctx
, tree decl
)
6564 if (decl
== NULL
|| !DECL_P (decl
) || ctx
->region_type
== ORT_NONE
)
6569 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
6572 if (n
->value
& GOVD_SHARED
)
6573 n
->value
= GOVD_FIRSTPRIVATE
| (n
->value
& GOVD_SEEN
);
6574 else if (n
->value
& GOVD_MAP
)
6575 n
->value
|= GOVD_MAP_TO_ONLY
;
6579 else if ((ctx
->region_type
& ORT_TARGET
) != 0)
6581 if (ctx
->target_map_scalars_firstprivate
)
6582 omp_add_variable (ctx
, decl
, GOVD_FIRSTPRIVATE
);
6584 omp_add_variable (ctx
, decl
, GOVD_MAP
| GOVD_MAP_TO_ONLY
);
6586 else if (ctx
->region_type
!= ORT_WORKSHARE
6587 && ctx
->region_type
!= ORT_SIMD
6588 && ctx
->region_type
!= ORT_ACC
6589 && !(ctx
->region_type
& ORT_TARGET_DATA
))
6590 omp_add_variable (ctx
, decl
, GOVD_FIRSTPRIVATE
);
6592 ctx
= ctx
->outer_context
;
6597 /* Similarly for each of the type sizes of TYPE. */
6600 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx
*ctx
, tree type
)
6602 if (type
== NULL
|| type
== error_mark_node
)
6604 type
= TYPE_MAIN_VARIANT (type
);
6606 if (ctx
->privatized_types
->add (type
))
6609 switch (TREE_CODE (type
))
6615 case FIXED_POINT_TYPE
:
6616 omp_firstprivatize_variable (ctx
, TYPE_MIN_VALUE (type
));
6617 omp_firstprivatize_variable (ctx
, TYPE_MAX_VALUE (type
));
6621 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (type
));
6622 omp_firstprivatize_type_sizes (ctx
, TYPE_DOMAIN (type
));
6627 case QUAL_UNION_TYPE
:
6630 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
6631 if (TREE_CODE (field
) == FIELD_DECL
)
6633 omp_firstprivatize_variable (ctx
, DECL_FIELD_OFFSET (field
));
6634 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (field
));
6640 case REFERENCE_TYPE
:
6641 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (type
));
6648 omp_firstprivatize_variable (ctx
, TYPE_SIZE (type
));
6649 omp_firstprivatize_variable (ctx
, TYPE_SIZE_UNIT (type
));
6650 lang_hooks
.types
.omp_firstprivatize_type_sizes (ctx
, type
);
6653 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6656 omp_add_variable (struct gimplify_omp_ctx
*ctx
, tree decl
, unsigned int flags
)
6659 unsigned int nflags
;
6662 if (error_operand_p (decl
) || ctx
->region_type
== ORT_NONE
)
6665 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6666 there are constructors involved somewhere. Exception is a shared clause,
6667 there is nothing privatized in that case. */
6668 if ((flags
& GOVD_SHARED
) == 0
6669 && (TREE_ADDRESSABLE (TREE_TYPE (decl
))
6670 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
))))
6673 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
6674 if (n
!= NULL
&& (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
6676 /* We shouldn't be re-adding the decl with the same data
6678 gcc_assert ((n
->value
& GOVD_DATA_SHARE_CLASS
& flags
) == 0);
6679 nflags
= n
->value
| flags
;
6680 /* The only combination of data sharing classes we should see is
6681 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6682 reduction variables to be used in data sharing clauses. */
6683 gcc_assert ((ctx
->region_type
& ORT_ACC
) != 0
6684 || ((nflags
& GOVD_DATA_SHARE_CLASS
)
6685 == (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
))
6686 || (flags
& GOVD_DATA_SHARE_CLASS
) == 0);
6691 /* When adding a variable-sized variable, we have to handle all sorts
6692 of additional bits of data: the pointer replacement variable, and
6693 the parameters of the type. */
6694 if (DECL_SIZE (decl
) && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
6696 /* Add the pointer replacement variable as PRIVATE if the variable
6697 replacement is private, else FIRSTPRIVATE since we'll need the
6698 address of the original variable either for SHARED, or for the
6699 copy into or out of the context. */
6700 if (!(flags
& GOVD_LOCAL
))
6702 if (flags
& GOVD_MAP
)
6703 nflags
= GOVD_MAP
| GOVD_MAP_TO_ONLY
| GOVD_EXPLICIT
;
6704 else if (flags
& GOVD_PRIVATE
)
6705 nflags
= GOVD_PRIVATE
;
6706 else if ((ctx
->region_type
& (ORT_TARGET
| ORT_TARGET_DATA
)) != 0
6707 && (flags
& GOVD_FIRSTPRIVATE
))
6708 nflags
= GOVD_PRIVATE
| GOVD_EXPLICIT
;
6710 nflags
= GOVD_FIRSTPRIVATE
;
6711 nflags
|= flags
& GOVD_SEEN
;
6712 t
= DECL_VALUE_EXPR (decl
);
6713 gcc_assert (TREE_CODE (t
) == INDIRECT_REF
);
6714 t
= TREE_OPERAND (t
, 0);
6715 gcc_assert (DECL_P (t
));
6716 omp_add_variable (ctx
, t
, nflags
);
6719 /* Add all of the variable and type parameters (which should have
6720 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6721 omp_firstprivatize_variable (ctx
, DECL_SIZE_UNIT (decl
));
6722 omp_firstprivatize_variable (ctx
, DECL_SIZE (decl
));
6723 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (decl
));
6725 /* The variable-sized variable itself is never SHARED, only some form
6726 of PRIVATE. The sharing would take place via the pointer variable
6727 which we remapped above. */
6728 if (flags
& GOVD_SHARED
)
6729 flags
= GOVD_SHARED
| GOVD_DEBUG_PRIVATE
6730 | (flags
& (GOVD_SEEN
| GOVD_EXPLICIT
));
6732 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6733 alloca statement we generate for the variable, so make sure it
6734 is available. This isn't automatically needed for the SHARED
6735 case, since we won't be allocating local storage then.
6736 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6737 in this case omp_notice_variable will be called later
6738 on when it is gimplified. */
6739 else if (! (flags
& (GOVD_LOCAL
| GOVD_MAP
))
6740 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl
))))
6741 omp_notice_variable (ctx
, TYPE_SIZE_UNIT (TREE_TYPE (decl
)), true);
6743 else if ((flags
& (GOVD_MAP
| GOVD_LOCAL
)) == 0
6744 && lang_hooks
.decls
.omp_privatize_by_reference (decl
))
6746 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (decl
));
6748 /* Similar to the direct variable sized case above, we'll need the
6749 size of references being privatized. */
6750 if ((flags
& GOVD_SHARED
) == 0)
6752 t
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)));
6754 omp_notice_variable (ctx
, t
, true);
6761 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl
, flags
);
6763 /* For reductions clauses in OpenACC loop directives, by default create a
6764 copy clause on the enclosing parallel construct for carrying back the
6766 if (ctx
->region_type
== ORT_ACC
&& (flags
& GOVD_REDUCTION
))
6768 struct gimplify_omp_ctx
*outer_ctx
= ctx
->outer_context
;
6771 n
= splay_tree_lookup (outer_ctx
->variables
, (splay_tree_key
)decl
);
6774 /* Ignore local variables and explicitly declared clauses. */
6775 if (n
->value
& (GOVD_LOCAL
| GOVD_EXPLICIT
))
6777 else if (outer_ctx
->region_type
== ORT_ACC_KERNELS
)
6779 /* According to the OpenACC spec, such a reduction variable
6780 should already have a copy map on a kernels construct,
6781 verify that here. */
6782 gcc_assert (!(n
->value
& GOVD_FIRSTPRIVATE
)
6783 && (n
->value
& GOVD_MAP
));
6785 else if (outer_ctx
->region_type
== ORT_ACC_PARALLEL
)
6787 /* Remove firstprivate and make it a copy map. */
6788 n
->value
&= ~GOVD_FIRSTPRIVATE
;
6789 n
->value
|= GOVD_MAP
;
6792 else if (outer_ctx
->region_type
== ORT_ACC_PARALLEL
)
6794 splay_tree_insert (outer_ctx
->variables
, (splay_tree_key
)decl
,
6795 GOVD_MAP
| GOVD_SEEN
);
6798 outer_ctx
= outer_ctx
->outer_context
;
6803 /* Notice a threadprivate variable DECL used in OMP context CTX.
6804 This just prints out diagnostics about threadprivate variable uses
6805 in untied tasks. If DECL2 is non-NULL, prevent this warning
6806 on that variable. */
6809 omp_notice_threadprivate_variable (struct gimplify_omp_ctx
*ctx
, tree decl
,
6813 struct gimplify_omp_ctx
*octx
;
6815 for (octx
= ctx
; octx
; octx
= octx
->outer_context
)
6816 if ((octx
->region_type
& ORT_TARGET
) != 0)
6818 n
= splay_tree_lookup (octx
->variables
, (splay_tree_key
)decl
);
6821 error ("threadprivate variable %qE used in target region",
6823 error_at (octx
->location
, "enclosing target region");
6824 splay_tree_insert (octx
->variables
, (splay_tree_key
)decl
, 0);
6827 splay_tree_insert (octx
->variables
, (splay_tree_key
)decl2
, 0);
6830 if (ctx
->region_type
!= ORT_UNTIED_TASK
)
6832 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
6835 error ("threadprivate variable %qE used in untied task",
6837 error_at (ctx
->location
, "enclosing task");
6838 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl
, 0);
6841 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl2
, 0);
6845 /* Return true if global var DECL is device resident. */
6848 device_resident_p (tree decl
)
6850 tree attr
= lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl
));
6855 for (tree t
= TREE_VALUE (attr
); t
; t
= TREE_PURPOSE (t
))
6857 tree c
= TREE_VALUE (t
);
6858 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_DEVICE_RESIDENT
)
6865 /* Return true if DECL has an ACC DECLARE attribute. */
6868 is_oacc_declared (tree decl
)
6870 tree t
= TREE_CODE (decl
) == MEM_REF
? TREE_OPERAND (decl
, 0) : decl
;
6871 tree declared
= lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t
));
6872 return declared
!= NULL_TREE
;
6875 /* Determine outer default flags for DECL mentioned in an OMP region
6876 but not declared in an enclosing clause.
6878 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6879 remapped firstprivate instead of shared. To some extent this is
6880 addressed in omp_firstprivatize_type_sizes, but not
6884 omp_default_clause (struct gimplify_omp_ctx
*ctx
, tree decl
,
6885 bool in_code
, unsigned flags
)
6887 enum omp_clause_default_kind default_kind
= ctx
->default_kind
;
6888 enum omp_clause_default_kind kind
;
6890 kind
= lang_hooks
.decls
.omp_predetermined_sharing (decl
);
6891 if (kind
!= OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
6892 default_kind
= kind
;
6894 switch (default_kind
)
6896 case OMP_CLAUSE_DEFAULT_NONE
:
6900 if (ctx
->region_type
& ORT_PARALLEL
)
6902 else if (ctx
->region_type
& ORT_TASK
)
6904 else if (ctx
->region_type
& ORT_TEAMS
)
6909 error ("%qE not specified in enclosing %qs",
6910 DECL_NAME (lang_hooks
.decls
.omp_report_decl (decl
)), rtype
);
6911 error_at (ctx
->location
, "enclosing %qs", rtype
);
6914 case OMP_CLAUSE_DEFAULT_SHARED
:
6915 flags
|= GOVD_SHARED
;
6917 case OMP_CLAUSE_DEFAULT_PRIVATE
:
6918 flags
|= GOVD_PRIVATE
;
6920 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE
:
6921 flags
|= GOVD_FIRSTPRIVATE
;
6923 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
6924 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6925 gcc_assert ((ctx
->region_type
& ORT_TASK
) != 0);
6926 if (struct gimplify_omp_ctx
*octx
= ctx
->outer_context
)
6928 omp_notice_variable (octx
, decl
, in_code
);
6929 for (; octx
; octx
= octx
->outer_context
)
6933 n2
= splay_tree_lookup (octx
->variables
, (splay_tree_key
) decl
);
6934 if ((octx
->region_type
& (ORT_TARGET_DATA
| ORT_TARGET
)) != 0
6935 && (n2
== NULL
|| (n2
->value
& GOVD_DATA_SHARE_CLASS
) == 0))
6937 if (n2
&& (n2
->value
& GOVD_DATA_SHARE_CLASS
) != GOVD_SHARED
)
6939 flags
|= GOVD_FIRSTPRIVATE
;
6942 if ((octx
->region_type
& (ORT_PARALLEL
| ORT_TEAMS
)) != 0)
6944 flags
|= GOVD_SHARED
;
6950 if (TREE_CODE (decl
) == PARM_DECL
6951 || (!is_global_var (decl
)
6952 && DECL_CONTEXT (decl
) == current_function_decl
))
6953 flags
|= GOVD_FIRSTPRIVATE
;
6955 flags
|= GOVD_SHARED
;
6967 /* Determine outer default flags for DECL mentioned in an OACC region
6968 but not declared in an enclosing clause. */
6971 oacc_default_clause (struct gimplify_omp_ctx
*ctx
, tree decl
, unsigned flags
)
6974 bool on_device
= false;
6975 bool declared
= is_oacc_declared (decl
);
6976 tree type
= TREE_TYPE (decl
);
6978 if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
6979 type
= TREE_TYPE (type
);
6981 if ((ctx
->region_type
& (ORT_ACC_PARALLEL
| ORT_ACC_KERNELS
)) != 0
6982 && is_global_var (decl
)
6983 && device_resident_p (decl
))
6986 flags
|= GOVD_MAP_TO_ONLY
;
6989 switch (ctx
->region_type
)
6991 case ORT_ACC_KERNELS
:
6994 if (AGGREGATE_TYPE_P (type
))
6996 /* Aggregates default to 'present_or_copy', or 'present'. */
6997 if (ctx
->default_kind
!= OMP_CLAUSE_DEFAULT_PRESENT
)
7000 flags
|= GOVD_MAP
| GOVD_MAP_FORCE_PRESENT
;
7003 /* Scalars default to 'copy'. */
7004 flags
|= GOVD_MAP
| GOVD_MAP_FORCE
;
7008 case ORT_ACC_PARALLEL
:
7011 if (on_device
|| declared
)
7013 else if (AGGREGATE_TYPE_P (type
))
7015 /* Aggregates default to 'present_or_copy', or 'present'. */
7016 if (ctx
->default_kind
!= OMP_CLAUSE_DEFAULT_PRESENT
)
7019 flags
|= GOVD_MAP
| GOVD_MAP_FORCE_PRESENT
;
7022 /* Scalars default to 'firstprivate'. */
7023 flags
|= GOVD_FIRSTPRIVATE
;
7031 if (DECL_ARTIFICIAL (decl
))
7032 ; /* We can get compiler-generated decls, and should not complain
7034 else if (ctx
->default_kind
== OMP_CLAUSE_DEFAULT_NONE
)
7036 error ("%qE not specified in enclosing OpenACC %qs construct",
7037 DECL_NAME (lang_hooks
.decls
.omp_report_decl (decl
)), rkind
);
7038 inform (ctx
->location
, "enclosing OpenACC %qs construct", rkind
);
7040 else if (ctx
->default_kind
== OMP_CLAUSE_DEFAULT_PRESENT
)
7041 ; /* Handled above. */
7043 gcc_checking_assert (ctx
->default_kind
== OMP_CLAUSE_DEFAULT_SHARED
);
7048 /* Record the fact that DECL was used within the OMP context CTX.
7049 IN_CODE is true when real code uses DECL, and false when we should
7050 merely emit default(none) errors. Return true if DECL is going to
7051 be remapped and thus DECL shouldn't be gimplified into its
7052 DECL_VALUE_EXPR (if any). */
7055 omp_notice_variable (struct gimplify_omp_ctx
*ctx
, tree decl
, bool in_code
)
7058 unsigned flags
= in_code
? GOVD_SEEN
: 0;
7059 bool ret
= false, shared
;
7061 if (error_operand_p (decl
))
7064 if (ctx
->region_type
== ORT_NONE
)
7065 return lang_hooks
.decls
.omp_disregard_value_expr (decl
, false);
7067 if (is_global_var (decl
))
7069 /* Threadprivate variables are predetermined. */
7070 if (DECL_THREAD_LOCAL_P (decl
))
7071 return omp_notice_threadprivate_variable (ctx
, decl
, NULL_TREE
);
7073 if (DECL_HAS_VALUE_EXPR_P (decl
))
7075 tree value
= get_base_address (DECL_VALUE_EXPR (decl
));
7077 if (value
&& DECL_P (value
) && DECL_THREAD_LOCAL_P (value
))
7078 return omp_notice_threadprivate_variable (ctx
, decl
, value
);
7081 if (gimplify_omp_ctxp
->outer_context
== NULL
7083 && oacc_get_fn_attrib (current_function_decl
))
7085 location_t loc
= DECL_SOURCE_LOCATION (decl
);
7087 if (lookup_attribute ("omp declare target link",
7088 DECL_ATTRIBUTES (decl
)))
7091 "%qE with %<link%> clause used in %<routine%> function",
7095 else if (!lookup_attribute ("omp declare target",
7096 DECL_ATTRIBUTES (decl
)))
7099 "%qE requires a %<declare%> directive for use "
7100 "in a %<routine%> function", DECL_NAME (decl
));
7106 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
7107 if ((ctx
->region_type
& ORT_TARGET
) != 0)
7109 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, true);
7112 unsigned nflags
= flags
;
7113 if (ctx
->target_map_pointers_as_0len_arrays
7114 || ctx
->target_map_scalars_firstprivate
)
7116 bool is_declare_target
= false;
7117 bool is_scalar
= false;
7118 if (is_global_var (decl
)
7119 && varpool_node::get_create (decl
)->offloadable
)
7121 struct gimplify_omp_ctx
*octx
;
7122 for (octx
= ctx
->outer_context
;
7123 octx
; octx
= octx
->outer_context
)
7125 n
= splay_tree_lookup (octx
->variables
,
7126 (splay_tree_key
)decl
);
7128 && (n
->value
& GOVD_DATA_SHARE_CLASS
) != GOVD_SHARED
7129 && (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
7132 is_declare_target
= octx
== NULL
;
7134 if (!is_declare_target
&& ctx
->target_map_scalars_firstprivate
)
7135 is_scalar
= lang_hooks
.decls
.omp_scalar_p (decl
);
7136 if (is_declare_target
)
7138 else if (ctx
->target_map_pointers_as_0len_arrays
7139 && (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
7140 || (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
7141 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl
)))
7143 nflags
|= GOVD_MAP
| GOVD_MAP_0LEN_ARRAY
;
7145 nflags
|= GOVD_FIRSTPRIVATE
;
7148 struct gimplify_omp_ctx
*octx
= ctx
->outer_context
;
7149 if ((ctx
->region_type
& ORT_ACC
) && octx
)
7151 /* Look in outer OpenACC contexts, to see if there's a
7152 data attribute for this variable. */
7153 omp_notice_variable (octx
, decl
, in_code
);
7155 for (; octx
; octx
= octx
->outer_context
)
7157 if (!(octx
->region_type
& (ORT_TARGET_DATA
| ORT_TARGET
)))
7160 = splay_tree_lookup (octx
->variables
,
7161 (splay_tree_key
) decl
);
7164 if (octx
->region_type
== ORT_ACC_HOST_DATA
)
7165 error ("variable %qE declared in enclosing "
7166 "%<host_data%> region", DECL_NAME (decl
));
7168 if (octx
->region_type
== ORT_ACC_DATA
7169 && (n2
->value
& GOVD_MAP_0LEN_ARRAY
))
7170 nflags
|= GOVD_MAP_0LEN_ARRAY
;
7177 tree type
= TREE_TYPE (decl
);
7180 && gimplify_omp_ctxp
->target_firstprivatize_array_bases
7181 && lang_hooks
.decls
.omp_privatize_by_reference (decl
))
7182 type
= TREE_TYPE (type
);
7184 && !lang_hooks
.types
.omp_mappable_type (type
))
7186 error ("%qD referenced in target region does not have "
7187 "a mappable type", decl
);
7188 nflags
|= GOVD_MAP
| GOVD_EXPLICIT
;
7190 else if (nflags
== flags
)
7192 if ((ctx
->region_type
& ORT_ACC
) != 0)
7193 nflags
= oacc_default_clause (ctx
, decl
, flags
);
7199 omp_add_variable (ctx
, decl
, nflags
);
7203 /* If nothing changed, there's nothing left to do. */
7204 if ((n
->value
& flags
) == flags
)
7214 if (ctx
->region_type
== ORT_WORKSHARE
7215 || ctx
->region_type
== ORT_SIMD
7216 || ctx
->region_type
== ORT_ACC
7217 || (ctx
->region_type
& ORT_TARGET_DATA
) != 0)
7220 flags
= omp_default_clause (ctx
, decl
, in_code
, flags
);
7222 if ((flags
& GOVD_PRIVATE
)
7223 && lang_hooks
.decls
.omp_private_outer_ref (decl
))
7224 flags
|= GOVD_PRIVATE_OUTER_REF
;
7226 omp_add_variable (ctx
, decl
, flags
);
7228 shared
= (flags
& GOVD_SHARED
) != 0;
7229 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, shared
);
7233 if ((n
->value
& (GOVD_SEEN
| GOVD_LOCAL
)) == 0
7234 && (flags
& (GOVD_SEEN
| GOVD_LOCAL
)) == GOVD_SEEN
7235 && DECL_SIZE (decl
))
7237 if (TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
7240 tree t
= DECL_VALUE_EXPR (decl
);
7241 gcc_assert (TREE_CODE (t
) == INDIRECT_REF
);
7242 t
= TREE_OPERAND (t
, 0);
7243 gcc_assert (DECL_P (t
));
7244 n2
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
7245 n2
->value
|= GOVD_SEEN
;
7247 else if (lang_hooks
.decls
.omp_privatize_by_reference (decl
)
7248 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)))
7249 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
))))
7253 tree t
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)));
7254 gcc_assert (DECL_P (t
));
7255 n2
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
7257 omp_notice_variable (ctx
, t
, true);
7261 shared
= ((flags
| n
->value
) & GOVD_SHARED
) != 0;
7262 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, shared
);
7264 /* If nothing changed, there's nothing left to do. */
7265 if ((n
->value
& flags
) == flags
)
7271 /* If the variable is private in the current context, then we don't
7272 need to propagate anything to an outer context. */
7273 if ((flags
& GOVD_PRIVATE
) && !(flags
& GOVD_PRIVATE_OUTER_REF
))
7275 if ((flags
& (GOVD_LINEAR
| GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
7276 == (GOVD_LINEAR
| GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
7278 if ((flags
& (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
7279 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
7280 == (GOVD_LASTPRIVATE
| GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
7282 if (ctx
->outer_context
7283 && omp_notice_variable (ctx
->outer_context
, decl
, in_code
))
7288 /* Verify that DECL is private within CTX. If there's specific information
7289 to the contrary in the innermost scope, generate an error. */
7292 omp_is_private (struct gimplify_omp_ctx
*ctx
, tree decl
, int simd
)
7296 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
7299 if (n
->value
& GOVD_SHARED
)
7301 if (ctx
== gimplify_omp_ctxp
)
7304 error ("iteration variable %qE is predetermined linear",
7307 error ("iteration variable %qE should be private",
7309 n
->value
= GOVD_PRIVATE
;
7315 else if ((n
->value
& GOVD_EXPLICIT
) != 0
7316 && (ctx
== gimplify_omp_ctxp
7317 || (ctx
->region_type
== ORT_COMBINED_PARALLEL
7318 && gimplify_omp_ctxp
->outer_context
== ctx
)))
7320 if ((n
->value
& GOVD_FIRSTPRIVATE
) != 0)
7321 error ("iteration variable %qE should not be firstprivate",
7323 else if ((n
->value
& GOVD_REDUCTION
) != 0)
7324 error ("iteration variable %qE should not be reduction",
7326 else if (simd
== 0 && (n
->value
& GOVD_LINEAR
) != 0)
7327 error ("iteration variable %qE should not be linear",
7329 else if (simd
== 1 && (n
->value
& GOVD_LASTPRIVATE
) != 0)
7330 error ("iteration variable %qE should not be lastprivate",
7332 else if (simd
&& (n
->value
& GOVD_PRIVATE
) != 0)
7333 error ("iteration variable %qE should not be private",
7335 else if (simd
== 2 && (n
->value
& GOVD_LINEAR
) != 0)
7336 error ("iteration variable %qE is predetermined linear",
7339 return (ctx
== gimplify_omp_ctxp
7340 || (ctx
->region_type
== ORT_COMBINED_PARALLEL
7341 && gimplify_omp_ctxp
->outer_context
== ctx
));
7344 if (ctx
->region_type
!= ORT_WORKSHARE
7345 && ctx
->region_type
!= ORT_SIMD
7346 && ctx
->region_type
!= ORT_ACC
)
7348 else if (ctx
->outer_context
)
7349 return omp_is_private (ctx
->outer_context
, decl
, simd
);
7353 /* Return true if DECL is private within a parallel region
7354 that binds to the current construct's context or in parallel
7355 region's REDUCTION clause. */
7358 omp_check_private (struct gimplify_omp_ctx
*ctx
, tree decl
, bool copyprivate
)
7364 ctx
= ctx
->outer_context
;
7367 if (is_global_var (decl
))
7370 /* References might be private, but might be shared too,
7371 when checking for copyprivate, assume they might be
7372 private, otherwise assume they might be shared. */
7376 if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
7379 /* Treat C++ privatized non-static data members outside
7380 of the privatization the same. */
7381 if (omp_member_access_dummy_var (decl
))
7387 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
7389 if ((ctx
->region_type
& (ORT_TARGET
| ORT_TARGET_DATA
)) != 0
7390 && (n
== NULL
|| (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0))
7395 if ((n
->value
& GOVD_LOCAL
) != 0
7396 && omp_member_access_dummy_var (decl
))
7398 return (n
->value
& GOVD_SHARED
) == 0;
7401 while (ctx
->region_type
== ORT_WORKSHARE
7402 || ctx
->region_type
== ORT_SIMD
7403 || ctx
->region_type
== ORT_ACC
);
7407 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7410 find_decl_expr (tree
*tp
, int *walk_subtrees
, void *data
)
7414 /* If this node has been visited, unmark it and keep looking. */
7415 if (TREE_CODE (t
) == DECL_EXPR
&& DECL_EXPR_DECL (t
) == (tree
) data
)
7418 if (IS_TYPE_OR_DECL_P (t
))
7423 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7424 and previous omp contexts. */
7427 gimplify_scan_omp_clauses (tree
*list_p
, gimple_seq
*pre_p
,
7428 enum omp_region_type region_type
,
7429 enum tree_code code
)
7431 struct gimplify_omp_ctx
*ctx
, *outer_ctx
;
7433 hash_map
<tree
, tree
> *struct_map_to_clause
= NULL
;
7434 tree
*prev_list_p
= NULL
;
7436 ctx
= new_omp_context (region_type
);
7437 outer_ctx
= ctx
->outer_context
;
7438 if (code
== OMP_TARGET
)
7440 if (!lang_GNU_Fortran ())
7441 ctx
->target_map_pointers_as_0len_arrays
= true;
7442 ctx
->target_map_scalars_firstprivate
= true;
7444 if (!lang_GNU_Fortran ())
7448 case OMP_TARGET_DATA
:
7449 case OMP_TARGET_ENTER_DATA
:
7450 case OMP_TARGET_EXIT_DATA
:
7452 case OACC_HOST_DATA
:
7453 ctx
->target_firstprivatize_array_bases
= true;
7458 while ((c
= *list_p
) != NULL
)
7460 bool remove
= false;
7461 bool notice_outer
= true;
7462 const char *check_non_private
= NULL
;
7466 switch (OMP_CLAUSE_CODE (c
))
7468 case OMP_CLAUSE_PRIVATE
:
7469 flags
= GOVD_PRIVATE
| GOVD_EXPLICIT
;
7470 if (lang_hooks
.decls
.omp_private_outer_ref (OMP_CLAUSE_DECL (c
)))
7472 flags
|= GOVD_PRIVATE_OUTER_REF
;
7473 OMP_CLAUSE_PRIVATE_OUTER_REF (c
) = 1;
7476 notice_outer
= false;
7478 case OMP_CLAUSE_SHARED
:
7479 flags
= GOVD_SHARED
| GOVD_EXPLICIT
;
7481 case OMP_CLAUSE_FIRSTPRIVATE
:
7482 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
7483 check_non_private
= "firstprivate";
7485 case OMP_CLAUSE_LASTPRIVATE
:
7486 flags
= GOVD_LASTPRIVATE
| GOVD_SEEN
| GOVD_EXPLICIT
;
7487 check_non_private
= "lastprivate";
7488 decl
= OMP_CLAUSE_DECL (c
);
7489 if (error_operand_p (decl
))
7492 && (outer_ctx
->region_type
== ORT_COMBINED_PARALLEL
7493 || outer_ctx
->region_type
== ORT_COMBINED_TEAMS
)
7494 && splay_tree_lookup (outer_ctx
->variables
,
7495 (splay_tree_key
) decl
) == NULL
)
7497 omp_add_variable (outer_ctx
, decl
, GOVD_SHARED
| GOVD_SEEN
);
7498 if (outer_ctx
->outer_context
)
7499 omp_notice_variable (outer_ctx
->outer_context
, decl
, true);
7502 && (outer_ctx
->region_type
& ORT_TASK
) != 0
7503 && outer_ctx
->combined_loop
7504 && splay_tree_lookup (outer_ctx
->variables
,
7505 (splay_tree_key
) decl
) == NULL
)
7507 omp_add_variable (outer_ctx
, decl
, GOVD_LASTPRIVATE
| GOVD_SEEN
);
7508 if (outer_ctx
->outer_context
)
7509 omp_notice_variable (outer_ctx
->outer_context
, decl
, true);
7512 && (outer_ctx
->region_type
== ORT_WORKSHARE
7513 || outer_ctx
->region_type
== ORT_ACC
)
7514 && outer_ctx
->combined_loop
7515 && splay_tree_lookup (outer_ctx
->variables
,
7516 (splay_tree_key
) decl
) == NULL
7517 && !omp_check_private (outer_ctx
, decl
, false))
7519 omp_add_variable (outer_ctx
, decl
, GOVD_LASTPRIVATE
| GOVD_SEEN
);
7520 if (outer_ctx
->outer_context
7521 && (outer_ctx
->outer_context
->region_type
7522 == ORT_COMBINED_PARALLEL
)
7523 && splay_tree_lookup (outer_ctx
->outer_context
->variables
,
7524 (splay_tree_key
) decl
) == NULL
)
7526 struct gimplify_omp_ctx
*octx
= outer_ctx
->outer_context
;
7527 omp_add_variable (octx
, decl
, GOVD_SHARED
| GOVD_SEEN
);
7528 if (octx
->outer_context
)
7530 octx
= octx
->outer_context
;
7531 if (octx
->region_type
== ORT_WORKSHARE
7532 && octx
->combined_loop
7533 && splay_tree_lookup (octx
->variables
,
7534 (splay_tree_key
) decl
) == NULL
7535 && !omp_check_private (octx
, decl
, false))
7537 omp_add_variable (octx
, decl
,
7538 GOVD_LASTPRIVATE
| GOVD_SEEN
);
7539 octx
= octx
->outer_context
;
7541 && octx
->region_type
== ORT_COMBINED_TEAMS
7542 && (splay_tree_lookup (octx
->variables
,
7543 (splay_tree_key
) decl
)
7546 omp_add_variable (octx
, decl
,
7547 GOVD_SHARED
| GOVD_SEEN
);
7548 octx
= octx
->outer_context
;
7552 omp_notice_variable (octx
, decl
, true);
7555 else if (outer_ctx
->outer_context
)
7556 omp_notice_variable (outer_ctx
->outer_context
, decl
, true);
7559 case OMP_CLAUSE_REDUCTION
:
7560 flags
= GOVD_REDUCTION
| GOVD_SEEN
| GOVD_EXPLICIT
;
7561 /* OpenACC permits reductions on private variables. */
7562 if (!(region_type
& ORT_ACC
))
7563 check_non_private
= "reduction";
7564 decl
= OMP_CLAUSE_DECL (c
);
7565 if (TREE_CODE (decl
) == MEM_REF
)
7567 tree type
= TREE_TYPE (decl
);
7568 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type
)), pre_p
,
7569 NULL
, is_gimple_val
, fb_rvalue
, false)
7575 tree v
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
7578 omp_firstprivatize_variable (ctx
, v
);
7579 omp_notice_variable (ctx
, v
, true);
7581 decl
= TREE_OPERAND (decl
, 0);
7582 if (TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
7584 if (gimplify_expr (&TREE_OPERAND (decl
, 1), pre_p
,
7585 NULL
, is_gimple_val
, fb_rvalue
, false)
7591 v
= TREE_OPERAND (decl
, 1);
7594 omp_firstprivatize_variable (ctx
, v
);
7595 omp_notice_variable (ctx
, v
, true);
7597 decl
= TREE_OPERAND (decl
, 0);
7599 if (TREE_CODE (decl
) == ADDR_EXPR
7600 || TREE_CODE (decl
) == INDIRECT_REF
)
7601 decl
= TREE_OPERAND (decl
, 0);
7604 case OMP_CLAUSE_LINEAR
:
7605 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c
), pre_p
, NULL
,
7606 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7613 if (code
== OMP_SIMD
7614 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c
))
7616 struct gimplify_omp_ctx
*octx
= outer_ctx
;
7618 && octx
->region_type
== ORT_WORKSHARE
7619 && octx
->combined_loop
7620 && !octx
->distribute
)
7622 if (octx
->outer_context
7623 && (octx
->outer_context
->region_type
7624 == ORT_COMBINED_PARALLEL
))
7625 octx
= octx
->outer_context
->outer_context
;
7627 octx
= octx
->outer_context
;
7630 && octx
->region_type
== ORT_WORKSHARE
7631 && octx
->combined_loop
7632 && octx
->distribute
)
7634 error_at (OMP_CLAUSE_LOCATION (c
),
7635 "%<linear%> clause for variable other than "
7636 "loop iterator specified on construct "
7637 "combined with %<distribute%>");
7642 /* For combined #pragma omp parallel for simd, need to put
7643 lastprivate and perhaps firstprivate too on the
7644 parallel. Similarly for #pragma omp for simd. */
7645 struct gimplify_omp_ctx
*octx
= outer_ctx
;
7649 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c
)
7650 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
7652 decl
= OMP_CLAUSE_DECL (c
);
7653 if (error_operand_p (decl
))
7659 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c
))
7660 flags
|= GOVD_FIRSTPRIVATE
;
7661 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
7662 flags
|= GOVD_LASTPRIVATE
;
7664 && octx
->region_type
== ORT_WORKSHARE
7665 && octx
->combined_loop
)
7667 if (octx
->outer_context
7668 && (octx
->outer_context
->region_type
7669 == ORT_COMBINED_PARALLEL
))
7670 octx
= octx
->outer_context
;
7671 else if (omp_check_private (octx
, decl
, false))
7675 && (octx
->region_type
& ORT_TASK
) != 0
7676 && octx
->combined_loop
)
7679 && octx
->region_type
== ORT_COMBINED_PARALLEL
7680 && ctx
->region_type
== ORT_WORKSHARE
7681 && octx
== outer_ctx
)
7682 flags
= GOVD_SEEN
| GOVD_SHARED
;
7684 && octx
->region_type
== ORT_COMBINED_TEAMS
)
7685 flags
= GOVD_SEEN
| GOVD_SHARED
;
7687 && octx
->region_type
== ORT_COMBINED_TARGET
)
7689 flags
&= ~GOVD_LASTPRIVATE
;
7690 if (flags
== GOVD_SEEN
)
7696 = splay_tree_lookup (octx
->variables
,
7697 (splay_tree_key
) decl
);
7698 if (on
&& (on
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
7703 omp_add_variable (octx
, decl
, flags
);
7704 if (octx
->outer_context
== NULL
)
7706 octx
= octx
->outer_context
;
7711 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c
)
7712 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
)))
7713 omp_notice_variable (octx
, decl
, true);
7715 flags
= GOVD_LINEAR
| GOVD_EXPLICIT
;
7716 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c
)
7717 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
7719 notice_outer
= false;
7720 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
7724 case OMP_CLAUSE_MAP
:
7725 decl
= OMP_CLAUSE_DECL (c
);
7726 if (error_operand_p (decl
))
7733 if (TREE_CODE (TREE_TYPE (decl
)) != ARRAY_TYPE
)
7736 case OMP_TARGET_DATA
:
7737 case OMP_TARGET_ENTER_DATA
:
7738 case OMP_TARGET_EXIT_DATA
:
7739 case OACC_ENTER_DATA
:
7740 case OACC_EXIT_DATA
:
7741 case OACC_HOST_DATA
:
7742 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
7743 || (OMP_CLAUSE_MAP_KIND (c
)
7744 == GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
7745 /* For target {,enter ,exit }data only the array slice is
7746 mapped, but not the pointer to it. */
7754 if (DECL_P (decl
) && outer_ctx
&& (region_type
& ORT_ACC
))
7756 struct gimplify_omp_ctx
*octx
;
7757 for (octx
= outer_ctx
; octx
; octx
= octx
->outer_context
)
7759 if (octx
->region_type
!= ORT_ACC_HOST_DATA
)
7762 = splay_tree_lookup (octx
->variables
,
7763 (splay_tree_key
) decl
);
7765 error_at (OMP_CLAUSE_LOCATION (c
), "variable %qE "
7766 "declared in enclosing %<host_data%> region",
7770 if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
7771 OMP_CLAUSE_SIZE (c
) = DECL_P (decl
) ? DECL_SIZE_UNIT (decl
)
7772 : TYPE_SIZE_UNIT (TREE_TYPE (decl
));
7773 if (gimplify_expr (&OMP_CLAUSE_SIZE (c
), pre_p
,
7774 NULL
, is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7779 else if ((OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
7780 || (OMP_CLAUSE_MAP_KIND (c
)
7781 == GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
7782 && TREE_CODE (OMP_CLAUSE_SIZE (c
)) != INTEGER_CST
)
7785 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c
), pre_p
, NULL
,
7787 omp_add_variable (ctx
, OMP_CLAUSE_SIZE (c
),
7788 GOVD_FIRSTPRIVATE
| GOVD_SEEN
);
7793 if (TREE_CODE (d
) == ARRAY_REF
)
7795 while (TREE_CODE (d
) == ARRAY_REF
)
7796 d
= TREE_OPERAND (d
, 0);
7797 if (TREE_CODE (d
) == COMPONENT_REF
7798 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
)
7801 pd
= &OMP_CLAUSE_DECL (c
);
7803 && TREE_CODE (decl
) == INDIRECT_REF
7804 && TREE_CODE (TREE_OPERAND (decl
, 0)) == COMPONENT_REF
7805 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl
, 0)))
7808 pd
= &TREE_OPERAND (decl
, 0);
7809 decl
= TREE_OPERAND (decl
, 0);
7811 if (TREE_CODE (decl
) == COMPONENT_REF
)
7813 while (TREE_CODE (decl
) == COMPONENT_REF
)
7814 decl
= TREE_OPERAND (decl
, 0);
7815 if (TREE_CODE (decl
) == INDIRECT_REF
7816 && DECL_P (TREE_OPERAND (decl
, 0))
7817 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl
, 0)))
7819 decl
= TREE_OPERAND (decl
, 0);
7821 if (gimplify_expr (pd
, pre_p
, NULL
, is_gimple_lvalue
, fb_lvalue
)
7829 if (error_operand_p (decl
))
7835 tree stype
= TREE_TYPE (decl
);
7836 if (TREE_CODE (stype
) == REFERENCE_TYPE
)
7837 stype
= TREE_TYPE (stype
);
7838 if (TYPE_SIZE_UNIT (stype
) == NULL
7839 || TREE_CODE (TYPE_SIZE_UNIT (stype
)) != INTEGER_CST
)
7841 error_at (OMP_CLAUSE_LOCATION (c
),
7842 "mapping field %qE of variable length "
7843 "structure", OMP_CLAUSE_DECL (c
));
7848 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_POINTER
)
7850 /* Error recovery. */
7851 if (prev_list_p
== NULL
)
7856 if (OMP_CLAUSE_CHAIN (*prev_list_p
) != c
)
7858 tree ch
= OMP_CLAUSE_CHAIN (*prev_list_p
);
7859 if (ch
== NULL_TREE
|| OMP_CLAUSE_CHAIN (ch
) != c
)
7868 HOST_WIDE_INT bitsize
, bitpos
;
7870 int unsignedp
, reversep
, volatilep
= 0;
7871 tree base
= OMP_CLAUSE_DECL (c
);
7872 while (TREE_CODE (base
) == ARRAY_REF
)
7873 base
= TREE_OPERAND (base
, 0);
7874 if (TREE_CODE (base
) == INDIRECT_REF
)
7875 base
= TREE_OPERAND (base
, 0);
7876 base
= get_inner_reference (base
, &bitsize
, &bitpos
, &offset
,
7877 &mode
, &unsignedp
, &reversep
,
7879 tree orig_base
= base
;
7880 if ((TREE_CODE (base
) == INDIRECT_REF
7881 || (TREE_CODE (base
) == MEM_REF
7882 && integer_zerop (TREE_OPERAND (base
, 1))))
7883 && DECL_P (TREE_OPERAND (base
, 0))
7884 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base
, 0)))
7886 base
= TREE_OPERAND (base
, 0);
7887 gcc_assert (base
== decl
7888 && (offset
== NULL_TREE
7889 || TREE_CODE (offset
) == INTEGER_CST
));
7892 = splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
7893 bool ptr
= (OMP_CLAUSE_MAP_KIND (c
)
7894 == GOMP_MAP_ALWAYS_POINTER
);
7895 if (n
== NULL
|| (n
->value
& GOVD_MAP
) == 0)
7897 tree l
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7899 OMP_CLAUSE_SET_MAP_KIND (l
, GOMP_MAP_STRUCT
);
7900 if (orig_base
!= base
)
7901 OMP_CLAUSE_DECL (l
) = unshare_expr (orig_base
);
7903 OMP_CLAUSE_DECL (l
) = decl
;
7904 OMP_CLAUSE_SIZE (l
) = size_int (1);
7905 if (struct_map_to_clause
== NULL
)
7906 struct_map_to_clause
= new hash_map
<tree
, tree
>;
7907 struct_map_to_clause
->put (decl
, l
);
7910 enum gomp_map_kind mkind
7911 = code
== OMP_TARGET_EXIT_DATA
7912 ? GOMP_MAP_RELEASE
: GOMP_MAP_ALLOC
;
7913 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7915 OMP_CLAUSE_SET_MAP_KIND (c2
, mkind
);
7916 OMP_CLAUSE_DECL (c2
)
7917 = unshare_expr (OMP_CLAUSE_DECL (c
));
7918 OMP_CLAUSE_CHAIN (c2
) = *prev_list_p
;
7919 OMP_CLAUSE_SIZE (c2
)
7920 = TYPE_SIZE_UNIT (ptr_type_node
);
7921 OMP_CLAUSE_CHAIN (l
) = c2
;
7922 if (OMP_CLAUSE_CHAIN (*prev_list_p
) != c
)
7924 tree c4
= OMP_CLAUSE_CHAIN (*prev_list_p
);
7926 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7928 OMP_CLAUSE_SET_MAP_KIND (c3
, mkind
);
7929 OMP_CLAUSE_DECL (c3
)
7930 = unshare_expr (OMP_CLAUSE_DECL (c4
));
7931 OMP_CLAUSE_SIZE (c3
)
7932 = TYPE_SIZE_UNIT (ptr_type_node
);
7933 OMP_CLAUSE_CHAIN (c3
) = *prev_list_p
;
7934 OMP_CLAUSE_CHAIN (c2
) = c3
;
7941 OMP_CLAUSE_CHAIN (l
) = c
;
7943 list_p
= &OMP_CLAUSE_CHAIN (l
);
7945 if (orig_base
!= base
&& code
== OMP_TARGET
)
7947 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7949 enum gomp_map_kind mkind
7950 = GOMP_MAP_FIRSTPRIVATE_REFERENCE
;
7951 OMP_CLAUSE_SET_MAP_KIND (c2
, mkind
);
7952 OMP_CLAUSE_DECL (c2
) = decl
;
7953 OMP_CLAUSE_SIZE (c2
) = size_zero_node
;
7954 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (l
);
7955 OMP_CLAUSE_CHAIN (l
) = c2
;
7957 flags
= GOVD_MAP
| GOVD_EXPLICIT
;
7958 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c
)) || ptr
)
7964 tree
*osc
= struct_map_to_clause
->get (decl
);
7965 tree
*sc
= NULL
, *scp
= NULL
;
7966 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c
)) || ptr
)
7967 n
->value
|= GOVD_SEEN
;
7970 o1
= wi::to_offset (offset
);
7974 o1
= o1
+ bitpos
/ BITS_PER_UNIT
;
7975 sc
= &OMP_CLAUSE_CHAIN (*osc
);
7977 && (OMP_CLAUSE_MAP_KIND (*sc
)
7978 == GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
7979 sc
= &OMP_CLAUSE_CHAIN (*sc
);
7980 for (; *sc
!= c
; sc
= &OMP_CLAUSE_CHAIN (*sc
))
7981 if (ptr
&& sc
== prev_list_p
)
7983 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc
))
7985 && (TREE_CODE (OMP_CLAUSE_DECL (*sc
))
7987 && (TREE_CODE (OMP_CLAUSE_DECL (*sc
))
7993 HOST_WIDE_INT bitsize2
, bitpos2
;
7994 base
= OMP_CLAUSE_DECL (*sc
);
7995 if (TREE_CODE (base
) == ARRAY_REF
)
7997 while (TREE_CODE (base
) == ARRAY_REF
)
7998 base
= TREE_OPERAND (base
, 0);
7999 if (TREE_CODE (base
) != COMPONENT_REF
8000 || (TREE_CODE (TREE_TYPE (base
))
8004 else if (TREE_CODE (base
) == INDIRECT_REF
8005 && (TREE_CODE (TREE_OPERAND (base
, 0))
8007 && (TREE_CODE (TREE_TYPE
8008 (TREE_OPERAND (base
, 0)))
8010 base
= TREE_OPERAND (base
, 0);
8011 base
= get_inner_reference (base
, &bitsize2
,
8014 &reversep
, &volatilep
);
8015 if ((TREE_CODE (base
) == INDIRECT_REF
8016 || (TREE_CODE (base
) == MEM_REF
8017 && integer_zerop (TREE_OPERAND (base
,
8019 && DECL_P (TREE_OPERAND (base
, 0))
8020 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base
,
8023 base
= TREE_OPERAND (base
, 0);
8028 gcc_assert (offset
== NULL_TREE
8029 || TREE_CODE (offset
) == INTEGER_CST
);
8030 tree d1
= OMP_CLAUSE_DECL (*sc
);
8031 tree d2
= OMP_CLAUSE_DECL (c
);
8032 while (TREE_CODE (d1
) == ARRAY_REF
)
8033 d1
= TREE_OPERAND (d1
, 0);
8034 while (TREE_CODE (d2
) == ARRAY_REF
)
8035 d2
= TREE_OPERAND (d2
, 0);
8036 if (TREE_CODE (d1
) == INDIRECT_REF
)
8037 d1
= TREE_OPERAND (d1
, 0);
8038 if (TREE_CODE (d2
) == INDIRECT_REF
)
8039 d2
= TREE_OPERAND (d2
, 0);
8040 while (TREE_CODE (d1
) == COMPONENT_REF
)
8041 if (TREE_CODE (d2
) == COMPONENT_REF
8042 && TREE_OPERAND (d1
, 1)
8043 == TREE_OPERAND (d2
, 1))
8045 d1
= TREE_OPERAND (d1
, 0);
8046 d2
= TREE_OPERAND (d2
, 0);
8052 error_at (OMP_CLAUSE_LOCATION (c
),
8053 "%qE appears more than once in map "
8054 "clauses", OMP_CLAUSE_DECL (c
));
8059 o2
= wi::to_offset (offset2
);
8063 o2
= o2
+ bitpos2
/ BITS_PER_UNIT
;
8064 if (wi::ltu_p (o1
, o2
)
8065 || (wi::eq_p (o1
, o2
) && bitpos
< bitpos2
))
8075 OMP_CLAUSE_SIZE (*osc
)
8076 = size_binop (PLUS_EXPR
, OMP_CLAUSE_SIZE (*osc
),
8080 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
8082 tree cl
= NULL_TREE
;
8083 enum gomp_map_kind mkind
8084 = code
== OMP_TARGET_EXIT_DATA
8085 ? GOMP_MAP_RELEASE
: GOMP_MAP_ALLOC
;
8086 OMP_CLAUSE_SET_MAP_KIND (c2
, mkind
);
8087 OMP_CLAUSE_DECL (c2
)
8088 = unshare_expr (OMP_CLAUSE_DECL (c
));
8089 OMP_CLAUSE_CHAIN (c2
) = scp
? *scp
: *prev_list_p
;
8090 OMP_CLAUSE_SIZE (c2
)
8091 = TYPE_SIZE_UNIT (ptr_type_node
);
8092 cl
= scp
? *prev_list_p
: c2
;
8093 if (OMP_CLAUSE_CHAIN (*prev_list_p
) != c
)
8095 tree c4
= OMP_CLAUSE_CHAIN (*prev_list_p
);
8097 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
8099 OMP_CLAUSE_SET_MAP_KIND (c3
, mkind
);
8100 OMP_CLAUSE_DECL (c3
)
8101 = unshare_expr (OMP_CLAUSE_DECL (c4
));
8102 OMP_CLAUSE_SIZE (c3
)
8103 = TYPE_SIZE_UNIT (ptr_type_node
);
8104 OMP_CLAUSE_CHAIN (c3
) = *prev_list_p
;
8106 OMP_CLAUSE_CHAIN (c2
) = c3
;
8112 if (sc
== prev_list_p
)
8119 *prev_list_p
= OMP_CLAUSE_CHAIN (c
);
8120 list_p
= prev_list_p
;
8122 OMP_CLAUSE_CHAIN (c
) = *sc
;
8129 *list_p
= OMP_CLAUSE_CHAIN (c
);
8130 OMP_CLAUSE_CHAIN (c
) = *sc
;
8137 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_ALWAYS_POINTER
8138 && OMP_CLAUSE_CHAIN (c
)
8139 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c
)) == OMP_CLAUSE_MAP
8140 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c
))
8141 == GOMP_MAP_ALWAYS_POINTER
))
8142 prev_list_p
= list_p
;
8145 flags
= GOVD_MAP
| GOVD_EXPLICIT
;
8146 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_TO
8147 || OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_TOFROM
)
8148 flags
|= GOVD_MAP_ALWAYS_TO
;
8151 case OMP_CLAUSE_DEPEND
:
8152 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
8154 tree deps
= OMP_CLAUSE_DECL (c
);
8155 while (deps
&& TREE_CODE (deps
) == TREE_LIST
)
8157 if (TREE_CODE (TREE_PURPOSE (deps
)) == TRUNC_DIV_EXPR
8158 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps
), 1)))
8159 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps
), 1),
8160 pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
8161 deps
= TREE_CHAIN (deps
);
8165 else if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SOURCE
)
8167 if (TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPOUND_EXPR
)
8169 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0), pre_p
,
8170 NULL
, is_gimple_val
, fb_rvalue
);
8171 OMP_CLAUSE_DECL (c
) = TREE_OPERAND (OMP_CLAUSE_DECL (c
), 1);
8173 if (error_operand_p (OMP_CLAUSE_DECL (c
)))
8178 OMP_CLAUSE_DECL (c
) = build_fold_addr_expr (OMP_CLAUSE_DECL (c
));
8179 if (gimplify_expr (&OMP_CLAUSE_DECL (c
), pre_p
, NULL
,
8180 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
8188 case OMP_CLAUSE_FROM
:
8189 case OMP_CLAUSE__CACHE_
:
8190 decl
= OMP_CLAUSE_DECL (c
);
8191 if (error_operand_p (decl
))
8196 if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
8197 OMP_CLAUSE_SIZE (c
) = DECL_P (decl
) ? DECL_SIZE_UNIT (decl
)
8198 : TYPE_SIZE_UNIT (TREE_TYPE (decl
));
8199 if (gimplify_expr (&OMP_CLAUSE_SIZE (c
), pre_p
,
8200 NULL
, is_gimple_val
, fb_rvalue
) == GS_ERROR
)
8207 if (gimplify_expr (&OMP_CLAUSE_DECL (c
), pre_p
,
8208 NULL
, is_gimple_lvalue
, fb_lvalue
)
8218 case OMP_CLAUSE_USE_DEVICE_PTR
:
8219 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
8221 case OMP_CLAUSE_IS_DEVICE_PTR
:
8222 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
8226 decl
= OMP_CLAUSE_DECL (c
);
8228 if (error_operand_p (decl
))
8233 if (DECL_NAME (decl
) == NULL_TREE
&& (flags
& GOVD_SHARED
) == 0)
8235 tree t
= omp_member_access_dummy_var (decl
);
8238 tree v
= DECL_VALUE_EXPR (decl
);
8239 DECL_NAME (decl
) = DECL_NAME (TREE_OPERAND (v
, 1));
8241 omp_notice_variable (outer_ctx
, t
, true);
8244 if (code
== OACC_DATA
8245 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
8246 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
8247 flags
|= GOVD_MAP_0LEN_ARRAY
;
8248 omp_add_variable (ctx
, decl
, flags
);
8249 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
8250 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
8252 omp_add_variable (ctx
, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
),
8253 GOVD_LOCAL
| GOVD_SEEN
);
8254 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
)
8255 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
8257 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
),
8259 omp_add_variable (ctx
,
8260 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
),
8261 GOVD_LOCAL
| GOVD_SEEN
);
8262 gimplify_omp_ctxp
= ctx
;
8263 push_gimplify_context ();
8265 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
) = NULL
;
8266 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
) = NULL
;
8268 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c
),
8269 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
));
8270 pop_gimplify_context
8271 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
)));
8272 push_gimplify_context ();
8273 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c
),
8274 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
));
8275 pop_gimplify_context
8276 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
)));
8277 OMP_CLAUSE_REDUCTION_INIT (c
) = NULL_TREE
;
8278 OMP_CLAUSE_REDUCTION_MERGE (c
) = NULL_TREE
;
8280 gimplify_omp_ctxp
= outer_ctx
;
8282 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
8283 && OMP_CLAUSE_LASTPRIVATE_STMT (c
))
8285 gimplify_omp_ctxp
= ctx
;
8286 push_gimplify_context ();
8287 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c
)) != BIND_EXPR
)
8289 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
,
8291 TREE_SIDE_EFFECTS (bind
) = 1;
8292 BIND_EXPR_BODY (bind
) = OMP_CLAUSE_LASTPRIVATE_STMT (c
);
8293 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = bind
;
8295 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c
),
8296 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
));
8297 pop_gimplify_context
8298 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
)));
8299 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = NULL_TREE
;
8301 gimplify_omp_ctxp
= outer_ctx
;
8303 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
8304 && OMP_CLAUSE_LINEAR_STMT (c
))
8306 gimplify_omp_ctxp
= ctx
;
8307 push_gimplify_context ();
8308 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c
)) != BIND_EXPR
)
8310 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
,
8312 TREE_SIDE_EFFECTS (bind
) = 1;
8313 BIND_EXPR_BODY (bind
) = OMP_CLAUSE_LINEAR_STMT (c
);
8314 OMP_CLAUSE_LINEAR_STMT (c
) = bind
;
8316 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c
),
8317 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
));
8318 pop_gimplify_context
8319 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
)));
8320 OMP_CLAUSE_LINEAR_STMT (c
) = NULL_TREE
;
8322 gimplify_omp_ctxp
= outer_ctx
;
8328 case OMP_CLAUSE_COPYIN
:
8329 case OMP_CLAUSE_COPYPRIVATE
:
8330 decl
= OMP_CLAUSE_DECL (c
);
8331 if (error_operand_p (decl
))
8336 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_COPYPRIVATE
8338 && !omp_check_private (ctx
, decl
, true))
8341 if (is_global_var (decl
))
8343 if (DECL_THREAD_LOCAL_P (decl
))
8345 else if (DECL_HAS_VALUE_EXPR_P (decl
))
8347 tree value
= get_base_address (DECL_VALUE_EXPR (decl
));
8351 && DECL_THREAD_LOCAL_P (value
))
8356 error_at (OMP_CLAUSE_LOCATION (c
),
8357 "copyprivate variable %qE is not threadprivate"
8358 " or private in outer context", DECL_NAME (decl
));
8362 omp_notice_variable (outer_ctx
, decl
, true);
8363 if (check_non_private
8364 && region_type
== ORT_WORKSHARE
8365 && (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
8366 || decl
== OMP_CLAUSE_DECL (c
)
8367 || (TREE_CODE (OMP_CLAUSE_DECL (c
)) == MEM_REF
8368 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0))
8370 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0))
8371 == POINTER_PLUS_EXPR
8372 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8373 (OMP_CLAUSE_DECL (c
), 0), 0))
8375 && omp_check_private (ctx
, decl
, false))
8377 error ("%s variable %qE is private in outer context",
8378 check_non_private
, DECL_NAME (decl
));
8384 if (OMP_CLAUSE_IF_MODIFIER (c
) != ERROR_MARK
8385 && OMP_CLAUSE_IF_MODIFIER (c
) != code
)
8388 for (int i
= 0; i
< 2; i
++)
8389 switch (i
? OMP_CLAUSE_IF_MODIFIER (c
) : code
)
8391 case OMP_PARALLEL
: p
[i
] = "parallel"; break;
8392 case OMP_TASK
: p
[i
] = "task"; break;
8393 case OMP_TASKLOOP
: p
[i
] = "taskloop"; break;
8394 case OMP_TARGET_DATA
: p
[i
] = "target data"; break;
8395 case OMP_TARGET
: p
[i
] = "target"; break;
8396 case OMP_TARGET_UPDATE
: p
[i
] = "target update"; break;
8397 case OMP_TARGET_ENTER_DATA
:
8398 p
[i
] = "target enter data"; break;
8399 case OMP_TARGET_EXIT_DATA
: p
[i
] = "target exit data"; break;
8400 default: gcc_unreachable ();
8402 error_at (OMP_CLAUSE_LOCATION (c
),
8403 "expected %qs %<if%> clause modifier rather than %qs",
8409 case OMP_CLAUSE_FINAL
:
8410 OMP_CLAUSE_OPERAND (c
, 0)
8411 = gimple_boolify (OMP_CLAUSE_OPERAND (c
, 0));
8414 case OMP_CLAUSE_SCHEDULE
:
8415 case OMP_CLAUSE_NUM_THREADS
:
8416 case OMP_CLAUSE_NUM_TEAMS
:
8417 case OMP_CLAUSE_THREAD_LIMIT
:
8418 case OMP_CLAUSE_DIST_SCHEDULE
:
8419 case OMP_CLAUSE_DEVICE
:
8420 case OMP_CLAUSE_PRIORITY
:
8421 case OMP_CLAUSE_GRAINSIZE
:
8422 case OMP_CLAUSE_NUM_TASKS
:
8423 case OMP_CLAUSE_HINT
:
8424 case OMP_CLAUSE__CILK_FOR_COUNT_
:
8425 case OMP_CLAUSE_ASYNC
:
8426 case OMP_CLAUSE_WAIT
:
8427 case OMP_CLAUSE_NUM_GANGS
:
8428 case OMP_CLAUSE_NUM_WORKERS
:
8429 case OMP_CLAUSE_VECTOR_LENGTH
:
8430 case OMP_CLAUSE_WORKER
:
8431 case OMP_CLAUSE_VECTOR
:
8432 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 0), pre_p
, NULL
,
8433 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
8437 case OMP_CLAUSE_GANG
:
8438 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 0), pre_p
, NULL
,
8439 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
8441 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 1), pre_p
, NULL
,
8442 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
8446 case OMP_CLAUSE_NOWAIT
:
8447 case OMP_CLAUSE_ORDERED
:
8448 case OMP_CLAUSE_UNTIED
:
8449 case OMP_CLAUSE_COLLAPSE
:
8450 case OMP_CLAUSE_TILE
:
8451 case OMP_CLAUSE_AUTO
:
8452 case OMP_CLAUSE_SEQ
:
8453 case OMP_CLAUSE_INDEPENDENT
:
8454 case OMP_CLAUSE_MERGEABLE
:
8455 case OMP_CLAUSE_PROC_BIND
:
8456 case OMP_CLAUSE_SAFELEN
:
8457 case OMP_CLAUSE_SIMDLEN
:
8458 case OMP_CLAUSE_NOGROUP
:
8459 case OMP_CLAUSE_THREADS
:
8460 case OMP_CLAUSE_SIMD
:
8463 case OMP_CLAUSE_DEFAULTMAP
:
8464 ctx
->target_map_scalars_firstprivate
= false;
8467 case OMP_CLAUSE_ALIGNED
:
8468 decl
= OMP_CLAUSE_DECL (c
);
8469 if (error_operand_p (decl
))
8474 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c
), pre_p
, NULL
,
8475 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
8480 if (!is_global_var (decl
)
8481 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
8482 omp_add_variable (ctx
, decl
, GOVD_ALIGNED
);
8485 case OMP_CLAUSE_DEFAULT
:
8486 ctx
->default_kind
= OMP_CLAUSE_DEFAULT_KIND (c
);
8493 if (code
== OACC_DATA
8494 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
8495 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
8498 *list_p
= OMP_CLAUSE_CHAIN (c
);
8500 list_p
= &OMP_CLAUSE_CHAIN (c
);
8503 gimplify_omp_ctxp
= ctx
;
8504 if (struct_map_to_clause
)
8505 delete struct_map_to_clause
;
8508 /* Return true if DECL is a candidate for shared to firstprivate
8509 optimization. We only consider non-addressable scalars, not
8510 too big, and not references. */
8513 omp_shared_to_firstprivate_optimizable_decl_p (tree decl
)
8515 if (TREE_ADDRESSABLE (decl
))
8517 tree type
= TREE_TYPE (decl
);
8518 if (!is_gimple_reg_type (type
)
8519 || TREE_CODE (type
) == REFERENCE_TYPE
8520 || TREE_ADDRESSABLE (type
))
8522 /* Don't optimize too large decls, as each thread/task will have
8524 HOST_WIDE_INT len
= int_size_in_bytes (type
);
8525 if (len
== -1 || len
> 4 * POINTER_SIZE
/ BITS_PER_UNIT
)
8527 if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
8532 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8533 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8534 GOVD_WRITTEN in outer contexts. */
8537 omp_mark_stores (struct gimplify_omp_ctx
*ctx
, tree decl
)
8539 for (; ctx
; ctx
= ctx
->outer_context
)
8541 splay_tree_node n
= splay_tree_lookup (ctx
->variables
,
8542 (splay_tree_key
) decl
);
8545 else if (n
->value
& GOVD_SHARED
)
8547 n
->value
|= GOVD_WRITTEN
;
8550 else if (n
->value
& GOVD_DATA_SHARE_CLASS
)
8555 /* Helper callback for walk_gimple_seq to discover possible stores
8556 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8557 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8561 omp_find_stores_op (tree
*tp
, int *walk_subtrees
, void *data
)
8563 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
8572 if (handled_component_p (op
))
8573 op
= TREE_OPERAND (op
, 0);
8574 else if ((TREE_CODE (op
) == MEM_REF
|| TREE_CODE (op
) == TARGET_MEM_REF
)
8575 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
)
8576 op
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
8581 if (!DECL_P (op
) || !omp_shared_to_firstprivate_optimizable_decl_p (op
))
8584 omp_mark_stores (gimplify_omp_ctxp
, op
);
8588 /* Helper callback for walk_gimple_seq to discover possible stores
8589 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8590 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8594 omp_find_stores_stmt (gimple_stmt_iterator
*gsi_p
,
8595 bool *handled_ops_p
,
8596 struct walk_stmt_info
*wi
)
8598 gimple
*stmt
= gsi_stmt (*gsi_p
);
8599 switch (gimple_code (stmt
))
8601 /* Don't recurse on OpenMP constructs for which
8602 gimplify_adjust_omp_clauses already handled the bodies,
8603 except handle gimple_omp_for_pre_body. */
8604 case GIMPLE_OMP_FOR
:
8605 *handled_ops_p
= true;
8606 if (gimple_omp_for_pre_body (stmt
))
8607 walk_gimple_seq (gimple_omp_for_pre_body (stmt
),
8608 omp_find_stores_stmt
, omp_find_stores_op
, wi
);
8610 case GIMPLE_OMP_PARALLEL
:
8611 case GIMPLE_OMP_TASK
:
8612 case GIMPLE_OMP_SECTIONS
:
8613 case GIMPLE_OMP_SINGLE
:
8614 case GIMPLE_OMP_TARGET
:
8615 case GIMPLE_OMP_TEAMS
:
8616 case GIMPLE_OMP_CRITICAL
:
8617 *handled_ops_p
= true;
8625 struct gimplify_adjust_omp_clauses_data
8631 /* For all variables that were not actually used within the context,
8632 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8635 gimplify_adjust_omp_clauses_1 (splay_tree_node n
, void *data
)
8637 tree
*list_p
= ((struct gimplify_adjust_omp_clauses_data
*) data
)->list_p
;
8639 = ((struct gimplify_adjust_omp_clauses_data
*) data
)->pre_p
;
8640 tree decl
= (tree
) n
->key
;
8641 unsigned flags
= n
->value
;
8642 enum omp_clause_code code
;
8646 if (flags
& (GOVD_EXPLICIT
| GOVD_LOCAL
))
8648 if ((flags
& GOVD_SEEN
) == 0)
8650 if (flags
& GOVD_DEBUG_PRIVATE
)
8652 gcc_assert ((flags
& GOVD_DATA_SHARE_CLASS
) == GOVD_SHARED
);
8653 private_debug
= true;
8655 else if (flags
& GOVD_MAP
)
8656 private_debug
= false;
8659 = lang_hooks
.decls
.omp_private_debug_clause (decl
,
8660 !!(flags
& GOVD_SHARED
));
8662 code
= OMP_CLAUSE_PRIVATE
;
8663 else if (flags
& GOVD_MAP
)
8665 code
= OMP_CLAUSE_MAP
;
8666 if ((gimplify_omp_ctxp
->region_type
& ORT_ACC
) == 0
8667 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl
))))
8669 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl
);
8673 else if (flags
& GOVD_SHARED
)
8675 if (is_global_var (decl
))
8677 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
->outer_context
;
8681 = splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8682 if (on
&& (on
->value
& (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
8683 | GOVD_PRIVATE
| GOVD_REDUCTION
8684 | GOVD_LINEAR
| GOVD_MAP
)) != 0)
8686 ctx
= ctx
->outer_context
;
8691 code
= OMP_CLAUSE_SHARED
;
8693 else if (flags
& GOVD_PRIVATE
)
8694 code
= OMP_CLAUSE_PRIVATE
;
8695 else if (flags
& GOVD_FIRSTPRIVATE
)
8697 code
= OMP_CLAUSE_FIRSTPRIVATE
;
8698 if ((gimplify_omp_ctxp
->region_type
& ORT_TARGET
)
8699 && (gimplify_omp_ctxp
->region_type
& ORT_ACC
) == 0
8700 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl
))))
8702 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8703 "%<target%> construct", decl
);
8707 else if (flags
& GOVD_LASTPRIVATE
)
8708 code
= OMP_CLAUSE_LASTPRIVATE
;
8709 else if (flags
& GOVD_ALIGNED
)
8714 if (((flags
& GOVD_LASTPRIVATE
)
8715 || (code
== OMP_CLAUSE_SHARED
&& (flags
& GOVD_WRITTEN
)))
8716 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
8717 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
8719 tree chain
= *list_p
;
8720 clause
= build_omp_clause (input_location
, code
);
8721 OMP_CLAUSE_DECL (clause
) = decl
;
8722 OMP_CLAUSE_CHAIN (clause
) = chain
;
8724 OMP_CLAUSE_PRIVATE_DEBUG (clause
) = 1;
8725 else if (code
== OMP_CLAUSE_PRIVATE
&& (flags
& GOVD_PRIVATE_OUTER_REF
))
8726 OMP_CLAUSE_PRIVATE_OUTER_REF (clause
) = 1;
8727 else if (code
== OMP_CLAUSE_SHARED
8728 && (flags
& GOVD_WRITTEN
) == 0
8729 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
8730 OMP_CLAUSE_SHARED_READONLY (clause
) = 1;
8731 else if (code
== OMP_CLAUSE_FIRSTPRIVATE
&& (flags
& GOVD_EXPLICIT
) == 0)
8732 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause
) = 1;
8733 else if (code
== OMP_CLAUSE_MAP
&& (flags
& GOVD_MAP_0LEN_ARRAY
) != 0)
8735 tree nc
= build_omp_clause (input_location
, OMP_CLAUSE_MAP
);
8736 OMP_CLAUSE_DECL (nc
) = decl
;
8737 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
8738 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) == POINTER_TYPE
)
8739 OMP_CLAUSE_DECL (clause
)
8740 = build_simple_mem_ref_loc (input_location
, decl
);
8741 OMP_CLAUSE_DECL (clause
)
8742 = build2 (MEM_REF
, char_type_node
, OMP_CLAUSE_DECL (clause
),
8743 build_int_cst (build_pointer_type (char_type_node
), 0));
8744 OMP_CLAUSE_SIZE (clause
) = size_zero_node
;
8745 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
8746 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_ALLOC
);
8747 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause
) = 1;
8748 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
8749 OMP_CLAUSE_CHAIN (nc
) = chain
;
8750 OMP_CLAUSE_CHAIN (clause
) = nc
;
8751 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
8752 gimplify_omp_ctxp
= ctx
->outer_context
;
8753 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause
), 0),
8754 pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
8755 gimplify_omp_ctxp
= ctx
;
8757 else if (code
== OMP_CLAUSE_MAP
)
8760 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8761 switch (flags
& (GOVD_MAP_TO_ONLY
8763 | GOVD_MAP_FORCE_PRESENT
))
8766 kind
= GOMP_MAP_TOFROM
;
8768 case GOVD_MAP_FORCE
:
8769 kind
= GOMP_MAP_TOFROM
| GOMP_MAP_FLAG_FORCE
;
8771 case GOVD_MAP_TO_ONLY
:
8774 case GOVD_MAP_TO_ONLY
| GOVD_MAP_FORCE
:
8775 kind
= GOMP_MAP_TO
| GOMP_MAP_FLAG_FORCE
;
8777 case GOVD_MAP_FORCE_PRESENT
:
8778 kind
= GOMP_MAP_FORCE_PRESENT
;
8783 OMP_CLAUSE_SET_MAP_KIND (clause
, kind
);
8784 if (DECL_SIZE (decl
)
8785 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
8787 tree decl2
= DECL_VALUE_EXPR (decl
);
8788 gcc_assert (TREE_CODE (decl2
) == INDIRECT_REF
);
8789 decl2
= TREE_OPERAND (decl2
, 0);
8790 gcc_assert (DECL_P (decl2
));
8791 tree mem
= build_simple_mem_ref (decl2
);
8792 OMP_CLAUSE_DECL (clause
) = mem
;
8793 OMP_CLAUSE_SIZE (clause
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
8794 if (gimplify_omp_ctxp
->outer_context
)
8796 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
->outer_context
;
8797 omp_notice_variable (ctx
, decl2
, true);
8798 omp_notice_variable (ctx
, OMP_CLAUSE_SIZE (clause
), true);
8800 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (clause
),
8802 OMP_CLAUSE_DECL (nc
) = decl
;
8803 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
8804 if (gimplify_omp_ctxp
->target_firstprivatize_array_bases
)
8805 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
8807 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_POINTER
);
8808 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (clause
);
8809 OMP_CLAUSE_CHAIN (clause
) = nc
;
8811 else if (gimplify_omp_ctxp
->target_firstprivatize_array_bases
8812 && lang_hooks
.decls
.omp_privatize_by_reference (decl
))
8814 OMP_CLAUSE_DECL (clause
) = build_simple_mem_ref (decl
);
8815 OMP_CLAUSE_SIZE (clause
)
8816 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
))));
8817 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
8818 gimplify_omp_ctxp
= ctx
->outer_context
;
8819 gimplify_expr (&OMP_CLAUSE_SIZE (clause
),
8820 pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
8821 gimplify_omp_ctxp
= ctx
;
8822 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (clause
),
8824 OMP_CLAUSE_DECL (nc
) = decl
;
8825 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
8826 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_FIRSTPRIVATE_REFERENCE
);
8827 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (clause
);
8828 OMP_CLAUSE_CHAIN (clause
) = nc
;
8831 OMP_CLAUSE_SIZE (clause
) = DECL_SIZE_UNIT (decl
);
8833 if (code
== OMP_CLAUSE_FIRSTPRIVATE
&& (flags
& GOVD_LASTPRIVATE
) != 0)
8835 tree nc
= build_omp_clause (input_location
, OMP_CLAUSE_LASTPRIVATE
);
8836 OMP_CLAUSE_DECL (nc
) = decl
;
8837 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc
) = 1;
8838 OMP_CLAUSE_CHAIN (nc
) = chain
;
8839 OMP_CLAUSE_CHAIN (clause
) = nc
;
8840 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
8841 gimplify_omp_ctxp
= ctx
->outer_context
;
8842 lang_hooks
.decls
.omp_finish_clause (nc
, pre_p
);
8843 gimplify_omp_ctxp
= ctx
;
8846 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
8847 gimplify_omp_ctxp
= ctx
->outer_context
;
8848 lang_hooks
.decls
.omp_finish_clause (clause
, pre_p
);
8849 if (gimplify_omp_ctxp
)
8850 for (; clause
!= chain
; clause
= OMP_CLAUSE_CHAIN (clause
))
8851 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_MAP
8852 && DECL_P (OMP_CLAUSE_SIZE (clause
)))
8853 omp_notice_variable (gimplify_omp_ctxp
, OMP_CLAUSE_SIZE (clause
),
8855 gimplify_omp_ctxp
= ctx
;
8860 gimplify_adjust_omp_clauses (gimple_seq
*pre_p
, gimple_seq body
, tree
*list_p
,
8861 enum tree_code code
)
8863 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
8868 struct gimplify_omp_ctx
*octx
;
8869 for (octx
= ctx
; octx
; octx
= octx
->outer_context
)
8870 if ((octx
->region_type
& (ORT_PARALLEL
| ORT_TASK
| ORT_TEAMS
)) != 0)
8874 struct walk_stmt_info wi
;
8875 memset (&wi
, 0, sizeof (wi
));
8876 walk_gimple_seq (body
, omp_find_stores_stmt
,
8877 omp_find_stores_op
, &wi
);
8880 while ((c
= *list_p
) != NULL
)
8883 bool remove
= false;
8885 switch (OMP_CLAUSE_CODE (c
))
8887 case OMP_CLAUSE_FIRSTPRIVATE
:
8888 if ((ctx
->region_type
& ORT_TARGET
)
8889 && (ctx
->region_type
& ORT_ACC
) == 0
8890 && TYPE_ATOMIC (strip_array_types
8891 (TREE_TYPE (OMP_CLAUSE_DECL (c
)))))
8893 error_at (OMP_CLAUSE_LOCATION (c
),
8894 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8895 "%<target%> construct", OMP_CLAUSE_DECL (c
));
8900 case OMP_CLAUSE_PRIVATE
:
8901 case OMP_CLAUSE_SHARED
:
8902 case OMP_CLAUSE_LINEAR
:
8903 decl
= OMP_CLAUSE_DECL (c
);
8904 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8905 remove
= !(n
->value
& GOVD_SEEN
);
8908 bool shared
= OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
;
8909 if ((n
->value
& GOVD_DEBUG_PRIVATE
)
8910 || lang_hooks
.decls
.omp_private_debug_clause (decl
, shared
))
8912 gcc_assert ((n
->value
& GOVD_DEBUG_PRIVATE
) == 0
8913 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
8915 OMP_CLAUSE_SET_CODE (c
, OMP_CLAUSE_PRIVATE
);
8916 OMP_CLAUSE_PRIVATE_DEBUG (c
) = 1;
8918 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
8919 && (n
->value
& GOVD_WRITTEN
) == 0
8921 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
8922 OMP_CLAUSE_SHARED_READONLY (c
) = 1;
8923 else if (DECL_P (decl
)
8924 && ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
8925 && (n
->value
& GOVD_WRITTEN
) != 0)
8926 || (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
8927 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
)))
8928 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
8929 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
8933 case OMP_CLAUSE_LASTPRIVATE
:
8934 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
8935 accurately reflect the presence of a FIRSTPRIVATE clause. */
8936 decl
= OMP_CLAUSE_DECL (c
);
8937 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8938 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
)
8939 = (n
->value
& GOVD_FIRSTPRIVATE
) != 0;
8940 if (code
== OMP_DISTRIBUTE
8941 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
8944 error_at (OMP_CLAUSE_LOCATION (c
),
8945 "same variable used in %<firstprivate%> and "
8946 "%<lastprivate%> clauses on %<distribute%> "
8950 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
8952 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
8953 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
8956 case OMP_CLAUSE_ALIGNED
:
8957 decl
= OMP_CLAUSE_DECL (c
);
8958 if (!is_global_var (decl
))
8960 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8961 remove
= n
== NULL
|| !(n
->value
& GOVD_SEEN
);
8962 if (!remove
&& TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
8964 struct gimplify_omp_ctx
*octx
;
8966 && (n
->value
& (GOVD_DATA_SHARE_CLASS
8967 & ~GOVD_FIRSTPRIVATE
)))
8970 for (octx
= ctx
->outer_context
; octx
;
8971 octx
= octx
->outer_context
)
8973 n
= splay_tree_lookup (octx
->variables
,
8974 (splay_tree_key
) decl
);
8977 if (n
->value
& GOVD_LOCAL
)
8979 /* We have to avoid assigning a shared variable
8980 to itself when trying to add
8981 __builtin_assume_aligned. */
8982 if (n
->value
& GOVD_SHARED
)
8990 else if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
8992 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8993 if (n
!= NULL
&& (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
8998 case OMP_CLAUSE_MAP
:
8999 if (code
== OMP_TARGET_EXIT_DATA
9000 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_POINTER
)
9005 decl
= OMP_CLAUSE_DECL (c
);
9006 /* Data clauses associated with acc parallel reductions must be
9007 compatible with present_or_copy. Warn and adjust the clause
9008 if that is not the case. */
9009 if (ctx
->region_type
== ORT_ACC_PARALLEL
)
9011 tree t
= DECL_P (decl
) ? decl
: TREE_OPERAND (decl
, 0);
9015 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
9017 if (n
&& (n
->value
& GOVD_REDUCTION
))
9019 enum gomp_map_kind kind
= OMP_CLAUSE_MAP_KIND (c
);
9021 OMP_CLAUSE_MAP_IN_REDUCTION (c
) = 1;
9022 if ((kind
& GOMP_MAP_TOFROM
) != GOMP_MAP_TOFROM
9023 && kind
!= GOMP_MAP_FORCE_PRESENT
9024 && kind
!= GOMP_MAP_POINTER
)
9026 warning_at (OMP_CLAUSE_LOCATION (c
), 0,
9027 "incompatible data clause with reduction "
9028 "on %qE; promoting to present_or_copy",
9030 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
9036 if ((ctx
->region_type
& ORT_TARGET
) != 0
9037 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
9039 if (TREE_CODE (decl
) == INDIRECT_REF
9040 && TREE_CODE (TREE_OPERAND (decl
, 0)) == COMPONENT_REF
9041 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl
, 0)))
9043 decl
= TREE_OPERAND (decl
, 0);
9044 if (TREE_CODE (decl
) == COMPONENT_REF
)
9046 while (TREE_CODE (decl
) == COMPONENT_REF
)
9047 decl
= TREE_OPERAND (decl
, 0);
9050 n
= splay_tree_lookup (ctx
->variables
,
9051 (splay_tree_key
) decl
);
9052 if (!(n
->value
& GOVD_SEEN
))
9059 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
9060 if ((ctx
->region_type
& ORT_TARGET
) != 0
9061 && !(n
->value
& GOVD_SEEN
)
9062 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c
)) == 0
9063 && (!is_global_var (decl
)
9064 || !lookup_attribute ("omp declare target link",
9065 DECL_ATTRIBUTES (decl
))))
9068 /* For struct element mapping, if struct is never referenced
9069 in target block and none of the mapping has always modifier,
9070 remove all the struct element mappings, which immediately
9071 follow the GOMP_MAP_STRUCT map clause. */
9072 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_STRUCT
)
9074 HOST_WIDE_INT cnt
= tree_to_shwi (OMP_CLAUSE_SIZE (c
));
9076 OMP_CLAUSE_CHAIN (c
)
9077 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c
));
9080 else if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_STRUCT
9081 && code
== OMP_TARGET_EXIT_DATA
)
9083 else if (DECL_SIZE (decl
)
9084 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
9085 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_POINTER
9086 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FIRSTPRIVATE_POINTER
9087 && (OMP_CLAUSE_MAP_KIND (c
)
9088 != GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
9090 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9091 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9093 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
9095 tree decl2
= DECL_VALUE_EXPR (decl
);
9096 gcc_assert (TREE_CODE (decl2
) == INDIRECT_REF
);
9097 decl2
= TREE_OPERAND (decl2
, 0);
9098 gcc_assert (DECL_P (decl2
));
9099 tree mem
= build_simple_mem_ref (decl2
);
9100 OMP_CLAUSE_DECL (c
) = mem
;
9101 OMP_CLAUSE_SIZE (c
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
9102 if (ctx
->outer_context
)
9104 omp_notice_variable (ctx
->outer_context
, decl2
, true);
9105 omp_notice_variable (ctx
->outer_context
,
9106 OMP_CLAUSE_SIZE (c
), true);
9108 if (((ctx
->region_type
& ORT_TARGET
) != 0
9109 || !ctx
->target_firstprivatize_array_bases
)
9110 && ((n
->value
& GOVD_SEEN
) == 0
9111 || (n
->value
& (GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
)) == 0))
9113 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
9115 OMP_CLAUSE_DECL (nc
) = decl
;
9116 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
9117 if (ctx
->target_firstprivatize_array_bases
)
9118 OMP_CLAUSE_SET_MAP_KIND (nc
,
9119 GOMP_MAP_FIRSTPRIVATE_POINTER
);
9121 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_POINTER
);
9122 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (c
);
9123 OMP_CLAUSE_CHAIN (c
) = nc
;
9129 if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
9130 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
9131 gcc_assert ((n
->value
& GOVD_SEEN
) == 0
9132 || ((n
->value
& (GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
))
9138 case OMP_CLAUSE_FROM
:
9139 case OMP_CLAUSE__CACHE_
:
9140 decl
= OMP_CLAUSE_DECL (c
);
9143 if (DECL_SIZE (decl
)
9144 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
9146 tree decl2
= DECL_VALUE_EXPR (decl
);
9147 gcc_assert (TREE_CODE (decl2
) == INDIRECT_REF
);
9148 decl2
= TREE_OPERAND (decl2
, 0);
9149 gcc_assert (DECL_P (decl2
));
9150 tree mem
= build_simple_mem_ref (decl2
);
9151 OMP_CLAUSE_DECL (c
) = mem
;
9152 OMP_CLAUSE_SIZE (c
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
9153 if (ctx
->outer_context
)
9155 omp_notice_variable (ctx
->outer_context
, decl2
, true);
9156 omp_notice_variable (ctx
->outer_context
,
9157 OMP_CLAUSE_SIZE (c
), true);
9160 else if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
9161 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
9164 case OMP_CLAUSE_REDUCTION
:
9165 decl
= OMP_CLAUSE_DECL (c
);
9166 /* OpenACC reductions need a present_or_copy data clause.
9167 Add one if necessary. Error is the reduction is private. */
9168 if (ctx
->region_type
== ORT_ACC_PARALLEL
)
9170 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
9171 if (n
->value
& (GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
))
9172 error_at (OMP_CLAUSE_LOCATION (c
), "invalid private "
9173 "reduction on %qE", DECL_NAME (decl
));
9174 else if ((n
->value
& GOVD_MAP
) == 0)
9176 tree next
= OMP_CLAUSE_CHAIN (c
);
9177 tree nc
= build_omp_clause (UNKNOWN_LOCATION
, OMP_CLAUSE_MAP
);
9178 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_TOFROM
);
9179 OMP_CLAUSE_DECL (nc
) = decl
;
9180 OMP_CLAUSE_CHAIN (c
) = nc
;
9181 lang_hooks
.decls
.omp_finish_clause (nc
, pre_p
);
9184 OMP_CLAUSE_MAP_IN_REDUCTION (nc
) = 1;
9185 if (OMP_CLAUSE_CHAIN (nc
) == NULL
)
9187 nc
= OMP_CLAUSE_CHAIN (nc
);
9189 OMP_CLAUSE_CHAIN (nc
) = next
;
9190 n
->value
|= GOVD_MAP
;
9194 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
9195 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
9197 case OMP_CLAUSE_COPYIN
:
9198 case OMP_CLAUSE_COPYPRIVATE
:
9200 case OMP_CLAUSE_NUM_THREADS
:
9201 case OMP_CLAUSE_NUM_TEAMS
:
9202 case OMP_CLAUSE_THREAD_LIMIT
:
9203 case OMP_CLAUSE_DIST_SCHEDULE
:
9204 case OMP_CLAUSE_DEVICE
:
9205 case OMP_CLAUSE_SCHEDULE
:
9206 case OMP_CLAUSE_NOWAIT
:
9207 case OMP_CLAUSE_ORDERED
:
9208 case OMP_CLAUSE_DEFAULT
:
9209 case OMP_CLAUSE_UNTIED
:
9210 case OMP_CLAUSE_COLLAPSE
:
9211 case OMP_CLAUSE_FINAL
:
9212 case OMP_CLAUSE_MERGEABLE
:
9213 case OMP_CLAUSE_PROC_BIND
:
9214 case OMP_CLAUSE_SAFELEN
:
9215 case OMP_CLAUSE_SIMDLEN
:
9216 case OMP_CLAUSE_DEPEND
:
9217 case OMP_CLAUSE_PRIORITY
:
9218 case OMP_CLAUSE_GRAINSIZE
:
9219 case OMP_CLAUSE_NUM_TASKS
:
9220 case OMP_CLAUSE_NOGROUP
:
9221 case OMP_CLAUSE_THREADS
:
9222 case OMP_CLAUSE_SIMD
:
9223 case OMP_CLAUSE_HINT
:
9224 case OMP_CLAUSE_DEFAULTMAP
:
9225 case OMP_CLAUSE_USE_DEVICE_PTR
:
9226 case OMP_CLAUSE_IS_DEVICE_PTR
:
9227 case OMP_CLAUSE__CILK_FOR_COUNT_
:
9228 case OMP_CLAUSE_ASYNC
:
9229 case OMP_CLAUSE_WAIT
:
9230 case OMP_CLAUSE_INDEPENDENT
:
9231 case OMP_CLAUSE_NUM_GANGS
:
9232 case OMP_CLAUSE_NUM_WORKERS
:
9233 case OMP_CLAUSE_VECTOR_LENGTH
:
9234 case OMP_CLAUSE_GANG
:
9235 case OMP_CLAUSE_WORKER
:
9236 case OMP_CLAUSE_VECTOR
:
9237 case OMP_CLAUSE_AUTO
:
9238 case OMP_CLAUSE_SEQ
:
9239 case OMP_CLAUSE_TILE
:
9247 *list_p
= OMP_CLAUSE_CHAIN (c
);
9249 list_p
= &OMP_CLAUSE_CHAIN (c
);
9252 /* Add in any implicit data sharing. */
9253 struct gimplify_adjust_omp_clauses_data data
;
9254 data
.list_p
= list_p
;
9256 splay_tree_foreach (ctx
->variables
, gimplify_adjust_omp_clauses_1
, &data
);
9258 gimplify_omp_ctxp
= ctx
->outer_context
;
9259 delete_omp_context (ctx
);
9262 /* Gimplify OACC_CACHE. */
9265 gimplify_oacc_cache (tree
*expr_p
, gimple_seq
*pre_p
)
9267 tree expr
= *expr_p
;
9269 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr
), pre_p
, ORT_ACC
,
9271 gimplify_adjust_omp_clauses (pre_p
, NULL
, &OACC_CACHE_CLAUSES (expr
),
9274 /* TODO: Do something sensible with this information. */
9276 *expr_p
= NULL_TREE
;
9279 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9280 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9281 kind. The entry kind will replace the one in CLAUSE, while the exit
9282 kind will be used in a new omp_clause and returned to the caller. */
9285 gimplify_oacc_declare_1 (tree clause
)
9287 HOST_WIDE_INT kind
, new_op
;
9291 kind
= OMP_CLAUSE_MAP_KIND (clause
);
9295 case GOMP_MAP_ALLOC
:
9296 case GOMP_MAP_FORCE_ALLOC
:
9297 case GOMP_MAP_FORCE_TO
:
9298 new_op
= GOMP_MAP_DELETE
;
9302 case GOMP_MAP_FORCE_FROM
:
9303 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_FORCE_ALLOC
);
9304 new_op
= GOMP_MAP_FORCE_FROM
;
9308 case GOMP_MAP_FORCE_TOFROM
:
9309 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_FORCE_TO
);
9310 new_op
= GOMP_MAP_FORCE_FROM
;
9315 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_FORCE_ALLOC
);
9316 new_op
= GOMP_MAP_FROM
;
9320 case GOMP_MAP_TOFROM
:
9321 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_TO
);
9322 new_op
= GOMP_MAP_FROM
;
9326 case GOMP_MAP_DEVICE_RESIDENT
:
9327 case GOMP_MAP_FORCE_DEVICEPTR
:
9328 case GOMP_MAP_FORCE_PRESENT
:
9330 case GOMP_MAP_POINTER
:
9341 c
= build_omp_clause (OMP_CLAUSE_LOCATION (clause
), OMP_CLAUSE_MAP
);
9342 OMP_CLAUSE_SET_MAP_KIND (c
, new_op
);
9343 OMP_CLAUSE_DECL (c
) = OMP_CLAUSE_DECL (clause
);
9349 /* Gimplify OACC_DECLARE. */
9352 gimplify_oacc_declare (tree
*expr_p
, gimple_seq
*pre_p
)
9354 tree expr
= *expr_p
;
9356 tree clauses
, t
, decl
;
9358 clauses
= OACC_DECLARE_CLAUSES (expr
);
9360 gimplify_scan_omp_clauses (&clauses
, pre_p
, ORT_TARGET_DATA
, OACC_DECLARE
);
9361 gimplify_adjust_omp_clauses (pre_p
, NULL
, &clauses
, OACC_DECLARE
);
9363 for (t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
9365 decl
= OMP_CLAUSE_DECL (t
);
9367 if (TREE_CODE (decl
) == MEM_REF
)
9368 decl
= TREE_OPERAND (decl
, 0);
9370 if (VAR_P (decl
) && !is_oacc_declared (decl
))
9372 tree attr
= get_identifier ("oacc declare target");
9373 DECL_ATTRIBUTES (decl
) = tree_cons (attr
, NULL_TREE
,
9374 DECL_ATTRIBUTES (decl
));
9378 && !is_global_var (decl
)
9379 && DECL_CONTEXT (decl
) == current_function_decl
)
9381 tree c
= gimplify_oacc_declare_1 (t
);
9384 if (oacc_declare_returns
== NULL
)
9385 oacc_declare_returns
= new hash_map
<tree
, tree
>;
9387 oacc_declare_returns
->put (decl
, c
);
9391 if (gimplify_omp_ctxp
)
9392 omp_add_variable (gimplify_omp_ctxp
, decl
, GOVD_SEEN
);
9395 stmt
= gimple_build_omp_target (NULL
, GF_OMP_TARGET_KIND_OACC_DECLARE
,
9398 gimplify_seq_add_stmt (pre_p
, stmt
);
9400 *expr_p
= NULL_TREE
;
9403 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9404 gimplification of the body, as well as scanning the body for used
9405 variables. We need to do this scan now, because variable-sized
9406 decls will be decomposed during gimplification. */
9409 gimplify_omp_parallel (tree
*expr_p
, gimple_seq
*pre_p
)
9411 tree expr
= *expr_p
;
9413 gimple_seq body
= NULL
;
9415 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr
), pre_p
,
9416 OMP_PARALLEL_COMBINED (expr
)
9417 ? ORT_COMBINED_PARALLEL
9418 : ORT_PARALLEL
, OMP_PARALLEL
);
9420 push_gimplify_context ();
9422 g
= gimplify_and_return_first (OMP_PARALLEL_BODY (expr
), &body
);
9423 if (gimple_code (g
) == GIMPLE_BIND
)
9424 pop_gimplify_context (g
);
9426 pop_gimplify_context (NULL
);
9428 gimplify_adjust_omp_clauses (pre_p
, body
, &OMP_PARALLEL_CLAUSES (expr
),
9431 g
= gimple_build_omp_parallel (body
,
9432 OMP_PARALLEL_CLAUSES (expr
),
9433 NULL_TREE
, NULL_TREE
);
9434 if (OMP_PARALLEL_COMBINED (expr
))
9435 gimple_omp_set_subcode (g
, GF_OMP_PARALLEL_COMBINED
);
9436 gimplify_seq_add_stmt (pre_p
, g
);
9437 *expr_p
= NULL_TREE
;
9440 /* Gimplify the contents of an OMP_TASK statement. This involves
9441 gimplification of the body, as well as scanning the body for used
9442 variables. We need to do this scan now, because variable-sized
9443 decls will be decomposed during gimplification. */
9446 gimplify_omp_task (tree
*expr_p
, gimple_seq
*pre_p
)
9448 tree expr
= *expr_p
;
9450 gimple_seq body
= NULL
;
9452 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr
), pre_p
,
9453 omp_find_clause (OMP_TASK_CLAUSES (expr
),
9455 ? ORT_UNTIED_TASK
: ORT_TASK
, OMP_TASK
);
9457 push_gimplify_context ();
9459 g
= gimplify_and_return_first (OMP_TASK_BODY (expr
), &body
);
9460 if (gimple_code (g
) == GIMPLE_BIND
)
9461 pop_gimplify_context (g
);
9463 pop_gimplify_context (NULL
);
9465 gimplify_adjust_omp_clauses (pre_p
, body
, &OMP_TASK_CLAUSES (expr
),
9468 g
= gimple_build_omp_task (body
,
9469 OMP_TASK_CLAUSES (expr
),
9470 NULL_TREE
, NULL_TREE
,
9471 NULL_TREE
, NULL_TREE
, NULL_TREE
);
9472 gimplify_seq_add_stmt (pre_p
, g
);
9473 *expr_p
= NULL_TREE
;
9476 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9477 with non-NULL OMP_FOR_INIT. */
9480 find_combined_omp_for (tree
*tp
, int *walk_subtrees
, void *)
9483 switch (TREE_CODE (*tp
))
9489 if (OMP_FOR_INIT (*tp
) != NULL_TREE
)
9493 case STATEMENT_LIST
:
9503 /* Gimplify the gross structure of an OMP_FOR statement. */
9505 static enum gimplify_status
9506 gimplify_omp_for (tree
*expr_p
, gimple_seq
*pre_p
)
9508 tree for_stmt
, orig_for_stmt
, inner_for_stmt
= NULL_TREE
, decl
, var
, t
;
9509 enum gimplify_status ret
= GS_ALL_DONE
;
9510 enum gimplify_status tret
;
9512 gimple_seq for_body
, for_pre_body
;
9514 bitmap has_decl_expr
= NULL
;
9515 enum omp_region_type ort
= ORT_WORKSHARE
;
9517 orig_for_stmt
= for_stmt
= *expr_p
;
9519 switch (TREE_CODE (for_stmt
))
9523 case OMP_DISTRIBUTE
:
9529 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_UNTIED
))
9530 ort
= ORT_UNTIED_TASK
;
9542 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9543 clause for the IV. */
9544 if (ort
== ORT_SIMD
&& TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) == 1)
9546 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), 0);
9547 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
9548 decl
= TREE_OPERAND (t
, 0);
9549 for (tree c
= OMP_FOR_CLAUSES (for_stmt
); c
; c
= OMP_CLAUSE_CHAIN (c
))
9550 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
9551 && OMP_CLAUSE_DECL (c
) == decl
)
9553 OMP_CLAUSE_LINEAR_NO_COPYIN (c
) = 1;
9558 if (OMP_FOR_INIT (for_stmt
) == NULL_TREE
)
9560 gcc_assert (TREE_CODE (for_stmt
) != OACC_LOOP
);
9561 inner_for_stmt
= walk_tree (&OMP_FOR_BODY (for_stmt
),
9562 find_combined_omp_for
, NULL
, NULL
);
9563 if (inner_for_stmt
== NULL_TREE
)
9565 gcc_assert (seen_error ());
9566 *expr_p
= NULL_TREE
;
9571 if (TREE_CODE (for_stmt
) != OMP_TASKLOOP
)
9572 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt
), pre_p
, ort
,
9573 TREE_CODE (for_stmt
));
9575 if (TREE_CODE (for_stmt
) == OMP_DISTRIBUTE
)
9576 gimplify_omp_ctxp
->distribute
= true;
9578 /* Handle OMP_FOR_INIT. */
9579 for_pre_body
= NULL
;
9580 if (ort
== ORT_SIMD
&& OMP_FOR_PRE_BODY (for_stmt
))
9582 has_decl_expr
= BITMAP_ALLOC (NULL
);
9583 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt
)) == DECL_EXPR
9584 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt
)))
9587 t
= OMP_FOR_PRE_BODY (for_stmt
);
9588 bitmap_set_bit (has_decl_expr
, DECL_UID (DECL_EXPR_DECL (t
)));
9590 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt
)) == STATEMENT_LIST
)
9592 tree_stmt_iterator si
;
9593 for (si
= tsi_start (OMP_FOR_PRE_BODY (for_stmt
)); !tsi_end_p (si
);
9597 if (TREE_CODE (t
) == DECL_EXPR
9598 && TREE_CODE (DECL_EXPR_DECL (t
)) == VAR_DECL
)
9599 bitmap_set_bit (has_decl_expr
, DECL_UID (DECL_EXPR_DECL (t
)));
9603 if (OMP_FOR_PRE_BODY (for_stmt
))
9605 if (TREE_CODE (for_stmt
) != OMP_TASKLOOP
|| gimplify_omp_ctxp
)
9606 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt
), &for_pre_body
);
9609 struct gimplify_omp_ctx ctx
;
9610 memset (&ctx
, 0, sizeof (ctx
));
9611 ctx
.region_type
= ORT_NONE
;
9612 gimplify_omp_ctxp
= &ctx
;
9613 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt
), &for_pre_body
);
9614 gimplify_omp_ctxp
= NULL
;
9617 OMP_FOR_PRE_BODY (for_stmt
) = NULL_TREE
;
9619 if (OMP_FOR_INIT (for_stmt
) == NULL_TREE
)
9620 for_stmt
= inner_for_stmt
;
9622 /* For taskloop, need to gimplify the start, end and step before the
9623 taskloop, outside of the taskloop omp context. */
9624 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
9626 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
9628 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
9629 if (!is_gimple_constant (TREE_OPERAND (t
, 1)))
9632 = get_initialized_tmp_var (TREE_OPERAND (t
, 1),
9633 pre_p
, NULL
, false);
9634 tree c
= build_omp_clause (input_location
,
9635 OMP_CLAUSE_FIRSTPRIVATE
);
9636 OMP_CLAUSE_DECL (c
) = TREE_OPERAND (t
, 1);
9637 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (orig_for_stmt
);
9638 OMP_FOR_CLAUSES (orig_for_stmt
) = c
;
9641 /* Handle OMP_FOR_COND. */
9642 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
9643 if (!is_gimple_constant (TREE_OPERAND (t
, 1)))
9646 = get_initialized_tmp_var (TREE_OPERAND (t
, 1),
9647 gimple_seq_empty_p (for_pre_body
)
9648 ? pre_p
: &for_pre_body
, NULL
,
9650 tree c
= build_omp_clause (input_location
,
9651 OMP_CLAUSE_FIRSTPRIVATE
);
9652 OMP_CLAUSE_DECL (c
) = TREE_OPERAND (t
, 1);
9653 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (orig_for_stmt
);
9654 OMP_FOR_CLAUSES (orig_for_stmt
) = c
;
9657 /* Handle OMP_FOR_INCR. */
9658 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
9659 if (TREE_CODE (t
) == MODIFY_EXPR
)
9661 decl
= TREE_OPERAND (t
, 0);
9662 t
= TREE_OPERAND (t
, 1);
9663 tree
*tp
= &TREE_OPERAND (t
, 1);
9664 if (TREE_CODE (t
) == PLUS_EXPR
&& *tp
== decl
)
9665 tp
= &TREE_OPERAND (t
, 0);
9667 if (!is_gimple_constant (*tp
))
9669 gimple_seq
*seq
= gimple_seq_empty_p (for_pre_body
)
9670 ? pre_p
: &for_pre_body
;
9671 *tp
= get_initialized_tmp_var (*tp
, seq
, NULL
, false);
9672 tree c
= build_omp_clause (input_location
,
9673 OMP_CLAUSE_FIRSTPRIVATE
);
9674 OMP_CLAUSE_DECL (c
) = *tp
;
9675 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (orig_for_stmt
);
9676 OMP_FOR_CLAUSES (orig_for_stmt
) = c
;
9681 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt
), pre_p
, ort
,
9685 if (orig_for_stmt
!= for_stmt
)
9686 gimplify_omp_ctxp
->combined_loop
= true;
9689 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
9690 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt
)));
9691 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
9692 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt
)));
9694 tree c
= omp_find_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_ORDERED
);
9695 bool is_doacross
= false;
9696 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
9699 gimplify_omp_ctxp
->loop_iter_var
.create (TREE_VEC_LENGTH
9700 (OMP_FOR_INIT (for_stmt
))
9703 int collapse
= 1, tile
= 0;
9704 c
= omp_find_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_COLLAPSE
);
9706 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c
));
9707 c
= omp_find_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_TILE
);
9709 tile
= list_length (OMP_CLAUSE_TILE_LIST (c
));
9710 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
9712 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
9713 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
9714 decl
= TREE_OPERAND (t
, 0);
9715 gcc_assert (DECL_P (decl
));
9716 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl
))
9717 || POINTER_TYPE_P (TREE_TYPE (decl
)));
9720 if (TREE_CODE (for_stmt
) == OMP_FOR
&& OMP_FOR_ORIG_DECLS (for_stmt
))
9721 gimplify_omp_ctxp
->loop_iter_var
.quick_push
9722 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt
), i
));
9724 gimplify_omp_ctxp
->loop_iter_var
.quick_push (decl
);
9725 gimplify_omp_ctxp
->loop_iter_var
.quick_push (decl
);
9728 /* Make sure the iteration variable is private. */
9730 tree c2
= NULL_TREE
;
9731 if (orig_for_stmt
!= for_stmt
)
9732 /* Do this only on innermost construct for combined ones. */;
9733 else if (ort
== ORT_SIMD
)
9735 splay_tree_node n
= splay_tree_lookup (gimplify_omp_ctxp
->variables
,
9736 (splay_tree_key
) decl
);
9737 omp_is_private (gimplify_omp_ctxp
, decl
,
9738 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
9740 if (n
!= NULL
&& (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
9741 omp_notice_variable (gimplify_omp_ctxp
, decl
, true);
9742 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) == 1)
9744 c
= build_omp_clause (input_location
, OMP_CLAUSE_LINEAR
);
9745 OMP_CLAUSE_LINEAR_NO_COPYIN (c
) = 1;
9746 unsigned int flags
= GOVD_LINEAR
| GOVD_EXPLICIT
| GOVD_SEEN
;
9748 && bitmap_bit_p (has_decl_expr
, DECL_UID (decl
)))
9750 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
9751 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
9753 struct gimplify_omp_ctx
*outer
9754 = gimplify_omp_ctxp
->outer_context
;
9755 if (outer
&& !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
9757 if (outer
->region_type
== ORT_WORKSHARE
9758 && outer
->combined_loop
)
9760 n
= splay_tree_lookup (outer
->variables
,
9761 (splay_tree_key
)decl
);
9762 if (n
!= NULL
&& (n
->value
& GOVD_LOCAL
) != 0)
9764 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
9765 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
9769 struct gimplify_omp_ctx
*octx
= outer
->outer_context
;
9771 && octx
->region_type
== ORT_COMBINED_PARALLEL
9772 && octx
->outer_context
9773 && (octx
->outer_context
->region_type
9775 && octx
->outer_context
->combined_loop
)
9777 octx
= octx
->outer_context
;
9778 n
= splay_tree_lookup (octx
->variables
,
9779 (splay_tree_key
)decl
);
9780 if (n
!= NULL
&& (n
->value
& GOVD_LOCAL
) != 0)
9782 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
9783 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
9790 OMP_CLAUSE_DECL (c
) = decl
;
9791 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (for_stmt
);
9792 OMP_FOR_CLAUSES (for_stmt
) = c
;
9793 omp_add_variable (gimplify_omp_ctxp
, decl
, flags
);
9794 if (outer
&& !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
9796 if (outer
->region_type
== ORT_WORKSHARE
9797 && outer
->combined_loop
)
9799 if (outer
->outer_context
9800 && (outer
->outer_context
->region_type
9801 == ORT_COMBINED_PARALLEL
))
9802 outer
= outer
->outer_context
;
9803 else if (omp_check_private (outer
, decl
, false))
9806 else if (((outer
->region_type
& ORT_TASK
) != 0)
9807 && outer
->combined_loop
9808 && !omp_check_private (gimplify_omp_ctxp
,
9811 else if (outer
->region_type
!= ORT_COMBINED_PARALLEL
)
9813 omp_notice_variable (outer
, decl
, true);
9818 n
= splay_tree_lookup (outer
->variables
,
9819 (splay_tree_key
)decl
);
9820 if (n
== NULL
|| (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
9822 omp_add_variable (outer
, decl
,
9823 GOVD_LASTPRIVATE
| GOVD_SEEN
);
9824 if (outer
->region_type
== ORT_COMBINED_PARALLEL
9825 && outer
->outer_context
9826 && (outer
->outer_context
->region_type
9828 && outer
->outer_context
->combined_loop
)
9830 outer
= outer
->outer_context
;
9831 n
= splay_tree_lookup (outer
->variables
,
9832 (splay_tree_key
)decl
);
9833 if (omp_check_private (outer
, decl
, false))
9836 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
9838 omp_add_variable (outer
, decl
,
9844 if (outer
&& outer
->outer_context
9845 && (outer
->outer_context
->region_type
9846 == ORT_COMBINED_TEAMS
))
9848 outer
= outer
->outer_context
;
9849 n
= splay_tree_lookup (outer
->variables
,
9850 (splay_tree_key
)decl
);
9852 || (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
9853 omp_add_variable (outer
, decl
,
9854 GOVD_SHARED
| GOVD_SEEN
);
9858 if (outer
&& outer
->outer_context
)
9859 omp_notice_variable (outer
->outer_context
, decl
,
9869 || !bitmap_bit_p (has_decl_expr
, DECL_UID (decl
)));
9870 struct gimplify_omp_ctx
*outer
9871 = gimplify_omp_ctxp
->outer_context
;
9872 if (outer
&& lastprivate
)
9874 if (outer
->region_type
== ORT_WORKSHARE
9875 && outer
->combined_loop
)
9877 n
= splay_tree_lookup (outer
->variables
,
9878 (splay_tree_key
)decl
);
9879 if (n
!= NULL
&& (n
->value
& GOVD_LOCAL
) != 0)
9881 lastprivate
= false;
9884 else if (outer
->outer_context
9885 && (outer
->outer_context
->region_type
9886 == ORT_COMBINED_PARALLEL
))
9887 outer
= outer
->outer_context
;
9888 else if (omp_check_private (outer
, decl
, false))
9891 else if (((outer
->region_type
& ORT_TASK
) != 0)
9892 && outer
->combined_loop
9893 && !omp_check_private (gimplify_omp_ctxp
,
9896 else if (outer
->region_type
!= ORT_COMBINED_PARALLEL
)
9898 omp_notice_variable (outer
, decl
, true);
9903 n
= splay_tree_lookup (outer
->variables
,
9904 (splay_tree_key
)decl
);
9905 if (n
== NULL
|| (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
9907 omp_add_variable (outer
, decl
,
9908 GOVD_LASTPRIVATE
| GOVD_SEEN
);
9909 if (outer
->region_type
== ORT_COMBINED_PARALLEL
9910 && outer
->outer_context
9911 && (outer
->outer_context
->region_type
9913 && outer
->outer_context
->combined_loop
)
9915 outer
= outer
->outer_context
;
9916 n
= splay_tree_lookup (outer
->variables
,
9917 (splay_tree_key
)decl
);
9918 if (omp_check_private (outer
, decl
, false))
9921 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
9923 omp_add_variable (outer
, decl
,
9929 if (outer
&& outer
->outer_context
9930 && (outer
->outer_context
->region_type
9931 == ORT_COMBINED_TEAMS
))
9933 outer
= outer
->outer_context
;
9934 n
= splay_tree_lookup (outer
->variables
,
9935 (splay_tree_key
)decl
);
9937 || (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
9938 omp_add_variable (outer
, decl
,
9939 GOVD_SHARED
| GOVD_SEEN
);
9943 if (outer
&& outer
->outer_context
)
9944 omp_notice_variable (outer
->outer_context
, decl
,
9950 c
= build_omp_clause (input_location
,
9951 lastprivate
? OMP_CLAUSE_LASTPRIVATE
9952 : OMP_CLAUSE_PRIVATE
);
9953 OMP_CLAUSE_DECL (c
) = decl
;
9954 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (for_stmt
);
9955 OMP_FOR_CLAUSES (for_stmt
) = c
;
9956 omp_add_variable (gimplify_omp_ctxp
, decl
,
9957 (lastprivate
? GOVD_LASTPRIVATE
: GOVD_PRIVATE
)
9958 | GOVD_EXPLICIT
| GOVD_SEEN
);
9962 else if (omp_is_private (gimplify_omp_ctxp
, decl
, 0))
9963 omp_notice_variable (gimplify_omp_ctxp
, decl
, true);
9965 omp_add_variable (gimplify_omp_ctxp
, decl
, GOVD_PRIVATE
| GOVD_SEEN
);
9967 /* If DECL is not a gimple register, create a temporary variable to act
9968 as an iteration counter. This is valid, since DECL cannot be
9969 modified in the body of the loop. Similarly for any iteration vars
9970 in simd with collapse > 1 where the iterator vars must be
9972 if (orig_for_stmt
!= for_stmt
)
9974 else if (!is_gimple_reg (decl
)
9976 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) > 1))
9978 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
9979 /* Make sure omp_add_variable is not called on it prematurely.
9980 We call it ourselves a few lines later. */
9981 gimplify_omp_ctxp
= NULL
;
9982 var
= create_tmp_var (TREE_TYPE (decl
), get_name (decl
));
9983 gimplify_omp_ctxp
= ctx
;
9984 TREE_OPERAND (t
, 0) = var
;
9986 gimplify_seq_add_stmt (&for_body
, gimple_build_assign (decl
, var
));
9989 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) == 1)
9991 c2
= build_omp_clause (input_location
, OMP_CLAUSE_LINEAR
);
9992 OMP_CLAUSE_LINEAR_NO_COPYIN (c2
) = 1;
9993 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2
) = 1;
9994 OMP_CLAUSE_DECL (c2
) = var
;
9995 OMP_CLAUSE_CHAIN (c2
) = OMP_FOR_CLAUSES (for_stmt
);
9996 OMP_FOR_CLAUSES (for_stmt
) = c2
;
9997 omp_add_variable (gimplify_omp_ctxp
, var
,
9998 GOVD_LINEAR
| GOVD_EXPLICIT
| GOVD_SEEN
);
10006 omp_add_variable (gimplify_omp_ctxp
, var
,
10007 GOVD_PRIVATE
| GOVD_SEEN
);
10012 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
10013 is_gimple_val
, fb_rvalue
, false);
10014 ret
= MIN (ret
, tret
);
10015 if (ret
== GS_ERROR
)
10018 /* Handle OMP_FOR_COND. */
10019 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
10020 gcc_assert (COMPARISON_CLASS_P (t
));
10021 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
10023 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
10024 is_gimple_val
, fb_rvalue
, false);
10025 ret
= MIN (ret
, tret
);
10027 /* Handle OMP_FOR_INCR. */
10028 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
10029 switch (TREE_CODE (t
))
10031 case PREINCREMENT_EXPR
:
10032 case POSTINCREMENT_EXPR
:
10034 tree decl
= TREE_OPERAND (t
, 0);
10035 /* c_omp_for_incr_canonicalize_ptr() should have been
10036 called to massage things appropriately. */
10037 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl
)));
10039 if (orig_for_stmt
!= for_stmt
)
10041 t
= build_int_cst (TREE_TYPE (decl
), 1);
10043 OMP_CLAUSE_LINEAR_STEP (c
) = t
;
10044 t
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), var
, t
);
10045 t
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, t
);
10046 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
) = t
;
10050 case PREDECREMENT_EXPR
:
10051 case POSTDECREMENT_EXPR
:
10052 /* c_omp_for_incr_canonicalize_ptr() should have been
10053 called to massage things appropriately. */
10054 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl
)));
10055 if (orig_for_stmt
!= for_stmt
)
10057 t
= build_int_cst (TREE_TYPE (decl
), -1);
10059 OMP_CLAUSE_LINEAR_STEP (c
) = t
;
10060 t
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), var
, t
);
10061 t
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, t
);
10062 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
) = t
;
10066 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
10067 TREE_OPERAND (t
, 0) = var
;
10069 t
= TREE_OPERAND (t
, 1);
10070 switch (TREE_CODE (t
))
10073 if (TREE_OPERAND (t
, 1) == decl
)
10075 TREE_OPERAND (t
, 1) = TREE_OPERAND (t
, 0);
10076 TREE_OPERAND (t
, 0) = var
;
10082 case POINTER_PLUS_EXPR
:
10083 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
10084 TREE_OPERAND (t
, 0) = var
;
10087 gcc_unreachable ();
10090 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
10091 is_gimple_val
, fb_rvalue
, false);
10092 ret
= MIN (ret
, tret
);
10095 tree step
= TREE_OPERAND (t
, 1);
10096 tree stept
= TREE_TYPE (decl
);
10097 if (POINTER_TYPE_P (stept
))
10099 step
= fold_convert (stept
, step
);
10100 if (TREE_CODE (t
) == MINUS_EXPR
)
10101 step
= fold_build1 (NEGATE_EXPR
, stept
, step
);
10102 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
10103 if (step
!= TREE_OPERAND (t
, 1))
10105 tret
= gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c
),
10106 &for_pre_body
, NULL
,
10107 is_gimple_val
, fb_rvalue
, false);
10108 ret
= MIN (ret
, tret
);
10114 gcc_unreachable ();
10120 OMP_CLAUSE_LINEAR_STEP (c2
) = OMP_CLAUSE_LINEAR_STEP (c
);
10123 if ((var
!= decl
|| collapse
> 1 || tile
) && orig_for_stmt
== for_stmt
)
10125 for (c
= OMP_FOR_CLAUSES (for_stmt
); c
; c
= OMP_CLAUSE_CHAIN (c
))
10126 if (((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
10127 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
) == NULL
)
10128 || (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
10129 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
)
10130 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
) == NULL
))
10131 && OMP_CLAUSE_DECL (c
) == decl
)
10133 if (is_doacross
&& (collapse
== 1 || i
>= collapse
))
10137 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
10138 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
10139 gcc_assert (TREE_OPERAND (t
, 0) == var
);
10140 t
= TREE_OPERAND (t
, 1);
10141 gcc_assert (TREE_CODE (t
) == PLUS_EXPR
10142 || TREE_CODE (t
) == MINUS_EXPR
10143 || TREE_CODE (t
) == POINTER_PLUS_EXPR
);
10144 gcc_assert (TREE_OPERAND (t
, 0) == var
);
10145 t
= build2 (TREE_CODE (t
), TREE_TYPE (decl
),
10146 is_doacross
? var
: decl
,
10147 TREE_OPERAND (t
, 1));
10150 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
10151 seq
= &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
);
10153 seq
= &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
);
10154 gimplify_assign (decl
, t
, seq
);
10159 BITMAP_FREE (has_decl_expr
);
10161 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
10163 push_gimplify_context ();
10164 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt
)) != BIND_EXPR
)
10166 OMP_FOR_BODY (orig_for_stmt
)
10167 = build3 (BIND_EXPR
, void_type_node
, NULL
,
10168 OMP_FOR_BODY (orig_for_stmt
), NULL
);
10169 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt
)) = 1;
10173 gimple
*g
= gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt
),
10176 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
10178 if (gimple_code (g
) == GIMPLE_BIND
)
10179 pop_gimplify_context (g
);
10181 pop_gimplify_context (NULL
);
10184 if (orig_for_stmt
!= for_stmt
)
10185 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
10187 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
10188 decl
= TREE_OPERAND (t
, 0);
10189 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
10190 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
10191 gimplify_omp_ctxp
= ctx
->outer_context
;
10192 var
= create_tmp_var (TREE_TYPE (decl
), get_name (decl
));
10193 gimplify_omp_ctxp
= ctx
;
10194 omp_add_variable (gimplify_omp_ctxp
, var
, GOVD_PRIVATE
| GOVD_SEEN
);
10195 TREE_OPERAND (t
, 0) = var
;
10196 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
10197 TREE_OPERAND (t
, 1) = copy_node (TREE_OPERAND (t
, 1));
10198 TREE_OPERAND (TREE_OPERAND (t
, 1), 0) = var
;
10201 gimplify_adjust_omp_clauses (pre_p
, for_body
,
10202 &OMP_FOR_CLAUSES (orig_for_stmt
),
10203 TREE_CODE (orig_for_stmt
));
10206 switch (TREE_CODE (orig_for_stmt
))
10208 case OMP_FOR
: kind
= GF_OMP_FOR_KIND_FOR
; break;
10209 case OMP_SIMD
: kind
= GF_OMP_FOR_KIND_SIMD
; break;
10210 case CILK_SIMD
: kind
= GF_OMP_FOR_KIND_CILKSIMD
; break;
10211 case CILK_FOR
: kind
= GF_OMP_FOR_KIND_CILKFOR
; break;
10212 case OMP_DISTRIBUTE
: kind
= GF_OMP_FOR_KIND_DISTRIBUTE
; break;
10213 case OMP_TASKLOOP
: kind
= GF_OMP_FOR_KIND_TASKLOOP
; break;
10214 case OACC_LOOP
: kind
= GF_OMP_FOR_KIND_OACC_LOOP
; break;
10216 gcc_unreachable ();
10218 gfor
= gimple_build_omp_for (for_body
, kind
, OMP_FOR_CLAUSES (orig_for_stmt
),
10219 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)),
10221 if (orig_for_stmt
!= for_stmt
)
10222 gimple_omp_for_set_combined_p (gfor
, true);
10223 if (gimplify_omp_ctxp
10224 && (gimplify_omp_ctxp
->combined_loop
10225 || (gimplify_omp_ctxp
->region_type
== ORT_COMBINED_PARALLEL
10226 && gimplify_omp_ctxp
->outer_context
10227 && gimplify_omp_ctxp
->outer_context
->combined_loop
)))
10229 gimple_omp_for_set_combined_into_p (gfor
, true);
10230 if (gimplify_omp_ctxp
->combined_loop
)
10231 gcc_assert (TREE_CODE (orig_for_stmt
) == OMP_SIMD
);
10233 gcc_assert (TREE_CODE (orig_for_stmt
) == OMP_FOR
);
10236 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
10238 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
10239 gimple_omp_for_set_index (gfor
, i
, TREE_OPERAND (t
, 0));
10240 gimple_omp_for_set_initial (gfor
, i
, TREE_OPERAND (t
, 1));
10241 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
10242 gimple_omp_for_set_cond (gfor
, i
, TREE_CODE (t
));
10243 gimple_omp_for_set_final (gfor
, i
, TREE_OPERAND (t
, 1));
10244 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
10245 gimple_omp_for_set_incr (gfor
, i
, TREE_OPERAND (t
, 1));
10248 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10249 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10250 The outer taskloop stands for computing the number of iterations,
10251 counts for collapsed loops and holding taskloop specific clauses.
10252 The task construct stands for the effect of data sharing on the
10253 explicit task it creates and the inner taskloop stands for expansion
10254 of the static loop inside of the explicit task construct. */
10255 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
10257 tree
*gfor_clauses_ptr
= gimple_omp_for_clauses_ptr (gfor
);
10258 tree task_clauses
= NULL_TREE
;
10259 tree c
= *gfor_clauses_ptr
;
10260 tree
*gtask_clauses_ptr
= &task_clauses
;
10261 tree outer_for_clauses
= NULL_TREE
;
10262 tree
*gforo_clauses_ptr
= &outer_for_clauses
;
10263 for (; c
; c
= OMP_CLAUSE_CHAIN (c
))
10264 switch (OMP_CLAUSE_CODE (c
))
10266 /* These clauses are allowed on task, move them there. */
10267 case OMP_CLAUSE_SHARED
:
10268 case OMP_CLAUSE_FIRSTPRIVATE
:
10269 case OMP_CLAUSE_DEFAULT
:
10270 case OMP_CLAUSE_IF
:
10271 case OMP_CLAUSE_UNTIED
:
10272 case OMP_CLAUSE_FINAL
:
10273 case OMP_CLAUSE_MERGEABLE
:
10274 case OMP_CLAUSE_PRIORITY
:
10275 *gtask_clauses_ptr
= c
;
10276 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
10278 case OMP_CLAUSE_PRIVATE
:
10279 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c
))
10281 /* We want private on outer for and firstprivate
10284 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
10285 OMP_CLAUSE_FIRSTPRIVATE
);
10286 OMP_CLAUSE_DECL (*gtask_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
10287 lang_hooks
.decls
.omp_finish_clause (*gtask_clauses_ptr
, NULL
);
10288 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr
);
10289 *gforo_clauses_ptr
= c
;
10290 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
10294 *gtask_clauses_ptr
= c
;
10295 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
10298 /* These clauses go into outer taskloop clauses. */
10299 case OMP_CLAUSE_GRAINSIZE
:
10300 case OMP_CLAUSE_NUM_TASKS
:
10301 case OMP_CLAUSE_NOGROUP
:
10302 *gforo_clauses_ptr
= c
;
10303 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
10305 /* Taskloop clause we duplicate on both taskloops. */
10306 case OMP_CLAUSE_COLLAPSE
:
10307 *gfor_clauses_ptr
= c
;
10308 gfor_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
10309 *gforo_clauses_ptr
= copy_node (c
);
10310 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr
);
10312 /* For lastprivate, keep the clause on inner taskloop, and add
10313 a shared clause on task. If the same decl is also firstprivate,
10314 add also firstprivate clause on the inner taskloop. */
10315 case OMP_CLAUSE_LASTPRIVATE
:
10316 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c
))
10318 /* For taskloop C++ lastprivate IVs, we want:
10319 1) private on outer taskloop
10320 2) firstprivate and shared on task
10321 3) lastprivate on inner taskloop */
10323 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
10324 OMP_CLAUSE_FIRSTPRIVATE
);
10325 OMP_CLAUSE_DECL (*gtask_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
10326 lang_hooks
.decls
.omp_finish_clause (*gtask_clauses_ptr
, NULL
);
10327 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr
);
10328 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
) = 1;
10329 *gforo_clauses_ptr
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
10330 OMP_CLAUSE_PRIVATE
);
10331 OMP_CLAUSE_DECL (*gforo_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
10332 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr
) = 1;
10333 TREE_TYPE (*gforo_clauses_ptr
) = TREE_TYPE (c
);
10334 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr
);
10336 *gfor_clauses_ptr
= c
;
10337 gfor_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
10339 = build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_SHARED
);
10340 OMP_CLAUSE_DECL (*gtask_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
10341 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
10342 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr
) = 1;
10344 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr
);
10347 gcc_unreachable ();
10349 *gfor_clauses_ptr
= NULL_TREE
;
10350 *gtask_clauses_ptr
= NULL_TREE
;
10351 *gforo_clauses_ptr
= NULL_TREE
;
10352 g
= gimple_build_bind (NULL_TREE
, gfor
, NULL_TREE
);
10353 g
= gimple_build_omp_task (g
, task_clauses
, NULL_TREE
, NULL_TREE
,
10354 NULL_TREE
, NULL_TREE
, NULL_TREE
);
10355 gimple_omp_task_set_taskloop_p (g
, true);
10356 g
= gimple_build_bind (NULL_TREE
, g
, NULL_TREE
);
10358 = gimple_build_omp_for (g
, GF_OMP_FOR_KIND_TASKLOOP
, outer_for_clauses
,
10359 gimple_omp_for_collapse (gfor
),
10360 gimple_omp_for_pre_body (gfor
));
10361 gimple_omp_for_set_pre_body (gfor
, NULL
);
10362 gimple_omp_for_set_combined_p (gforo
, true);
10363 gimple_omp_for_set_combined_into_p (gfor
, true);
10364 for (i
= 0; i
< (int) gimple_omp_for_collapse (gfor
); i
++)
10366 tree type
= TREE_TYPE (gimple_omp_for_index (gfor
, i
));
10367 tree v
= create_tmp_var (type
);
10368 gimple_omp_for_set_index (gforo
, i
, v
);
10369 t
= unshare_expr (gimple_omp_for_initial (gfor
, i
));
10370 gimple_omp_for_set_initial (gforo
, i
, t
);
10371 gimple_omp_for_set_cond (gforo
, i
,
10372 gimple_omp_for_cond (gfor
, i
));
10373 t
= unshare_expr (gimple_omp_for_final (gfor
, i
));
10374 gimple_omp_for_set_final (gforo
, i
, t
);
10375 t
= unshare_expr (gimple_omp_for_incr (gfor
, i
));
10376 gcc_assert (TREE_OPERAND (t
, 0) == gimple_omp_for_index (gfor
, i
));
10377 TREE_OPERAND (t
, 0) = v
;
10378 gimple_omp_for_set_incr (gforo
, i
, t
);
10379 t
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
10380 OMP_CLAUSE_DECL (t
) = v
;
10381 OMP_CLAUSE_CHAIN (t
) = gimple_omp_for_clauses (gforo
);
10382 gimple_omp_for_set_clauses (gforo
, t
);
10384 gimplify_seq_add_stmt (pre_p
, gforo
);
10387 gimplify_seq_add_stmt (pre_p
, gfor
);
10388 if (ret
!= GS_ALL_DONE
)
10390 *expr_p
= NULL_TREE
;
10391 return GS_ALL_DONE
;
10394 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10395 of OMP_TARGET's body. */
10398 find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
10400 *walk_subtrees
= 0;
10401 switch (TREE_CODE (*tp
))
10406 case STATEMENT_LIST
:
10407 *walk_subtrees
= 1;
10415 /* Helper function of optimize_target_teams, determine if the expression
10416 can be computed safely before the target construct on the host. */
10419 computable_teams_clause (tree
*tp
, int *walk_subtrees
, void *)
10425 *walk_subtrees
= 0;
10428 switch (TREE_CODE (*tp
))
10433 *walk_subtrees
= 0;
10434 if (error_operand_p (*tp
)
10435 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp
))
10436 || DECL_HAS_VALUE_EXPR_P (*tp
)
10437 || DECL_THREAD_LOCAL_P (*tp
)
10438 || TREE_SIDE_EFFECTS (*tp
)
10439 || TREE_THIS_VOLATILE (*tp
))
10441 if (is_global_var (*tp
)
10442 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp
))
10443 || lookup_attribute ("omp declare target link",
10444 DECL_ATTRIBUTES (*tp
))))
10447 && !DECL_SEEN_IN_BIND_EXPR_P (*tp
)
10448 && !is_global_var (*tp
)
10449 && decl_function_context (*tp
) == current_function_decl
)
10451 n
= splay_tree_lookup (gimplify_omp_ctxp
->variables
,
10452 (splay_tree_key
) *tp
);
10455 if (gimplify_omp_ctxp
->target_map_scalars_firstprivate
)
10459 else if (n
->value
& GOVD_LOCAL
)
10461 else if (n
->value
& GOVD_FIRSTPRIVATE
)
10463 else if ((n
->value
& (GOVD_MAP
| GOVD_MAP_ALWAYS_TO
))
10464 == (GOVD_MAP
| GOVD_MAP_ALWAYS_TO
))
10468 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp
)))
10472 if (TARGET_EXPR_INITIAL (*tp
)
10473 || TREE_CODE (TARGET_EXPR_SLOT (*tp
)) != VAR_DECL
)
10475 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp
),
10476 walk_subtrees
, NULL
);
10477 /* Allow some reasonable subset of integral arithmetics. */
10481 case TRUNC_DIV_EXPR
:
10482 case CEIL_DIV_EXPR
:
10483 case FLOOR_DIV_EXPR
:
10484 case ROUND_DIV_EXPR
:
10485 case TRUNC_MOD_EXPR
:
10486 case CEIL_MOD_EXPR
:
10487 case FLOOR_MOD_EXPR
:
10488 case ROUND_MOD_EXPR
:
10490 case EXACT_DIV_EXPR
:
10501 case NON_LVALUE_EXPR
:
10503 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp
)))
10506 /* And disallow anything else, except for comparisons. */
10508 if (COMPARISON_CLASS_P (*tp
))
10514 /* Try to determine if the num_teams and/or thread_limit expressions
10515 can have their values determined already before entering the
10517 INTEGER_CSTs trivially are,
10518 integral decls that are firstprivate (explicitly or implicitly)
10519 or explicitly map(always, to:) or map(always, tofrom:) on the target
10520 region too, and expressions involving simple arithmetics on those
10521 too, function calls are not ok, dereferencing something neither etc.
10522 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10523 EXPR based on what we find:
10524 0 stands for clause not specified at all, use implementation default
10525 -1 stands for value that can't be determined easily before entering
10526 the target construct.
10527 If teams construct is not present at all, use 1 for num_teams
10528 and 0 for thread_limit (only one team is involved, and the thread
10529 limit is implementation defined. */
10532 optimize_target_teams (tree target
, gimple_seq
*pre_p
)
10534 tree body
= OMP_BODY (target
);
10535 tree teams
= walk_tree (&body
, find_omp_teams
, NULL
, NULL
);
10536 tree num_teams
= integer_zero_node
;
10537 tree thread_limit
= integer_zero_node
;
10538 location_t num_teams_loc
= EXPR_LOCATION (target
);
10539 location_t thread_limit_loc
= EXPR_LOCATION (target
);
10541 struct gimplify_omp_ctx
*target_ctx
= gimplify_omp_ctxp
;
10543 if (teams
== NULL_TREE
)
10544 num_teams
= integer_one_node
;
10546 for (c
= OMP_TEAMS_CLAUSES (teams
); c
; c
= OMP_CLAUSE_CHAIN (c
))
10548 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
)
10551 num_teams_loc
= OMP_CLAUSE_LOCATION (c
);
10553 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
10556 thread_limit_loc
= OMP_CLAUSE_LOCATION (c
);
10560 expr
= OMP_CLAUSE_OPERAND (c
, 0);
10561 if (TREE_CODE (expr
) == INTEGER_CST
)
10566 if (walk_tree (&expr
, computable_teams_clause
, NULL
, NULL
))
10568 *p
= integer_minus_one_node
;
10572 gimplify_omp_ctxp
= gimplify_omp_ctxp
->outer_context
;
10573 if (gimplify_expr (p
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
, false)
10576 gimplify_omp_ctxp
= target_ctx
;
10577 *p
= integer_minus_one_node
;
10580 gimplify_omp_ctxp
= target_ctx
;
10581 if (!DECL_P (expr
) && TREE_CODE (expr
) != TARGET_EXPR
)
10582 OMP_CLAUSE_OPERAND (c
, 0) = *p
;
10584 c
= build_omp_clause (thread_limit_loc
, OMP_CLAUSE_THREAD_LIMIT
);
10585 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = thread_limit
;
10586 OMP_CLAUSE_CHAIN (c
) = OMP_TARGET_CLAUSES (target
);
10587 OMP_TARGET_CLAUSES (target
) = c
;
10588 c
= build_omp_clause (num_teams_loc
, OMP_CLAUSE_NUM_TEAMS
);
10589 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = num_teams
;
10590 OMP_CLAUSE_CHAIN (c
) = OMP_TARGET_CLAUSES (target
);
10591 OMP_TARGET_CLAUSES (target
) = c
;
10594 /* Gimplify the gross structure of several OMP constructs. */
10597 gimplify_omp_workshare (tree
*expr_p
, gimple_seq
*pre_p
)
10599 tree expr
= *expr_p
;
10601 gimple_seq body
= NULL
;
10602 enum omp_region_type ort
;
10604 switch (TREE_CODE (expr
))
10608 ort
= ORT_WORKSHARE
;
10611 ort
= OMP_TARGET_COMBINED (expr
) ? ORT_COMBINED_TARGET
: ORT_TARGET
;
10614 ort
= ORT_ACC_KERNELS
;
10616 case OACC_PARALLEL
:
10617 ort
= ORT_ACC_PARALLEL
;
10620 ort
= ORT_ACC_DATA
;
10622 case OMP_TARGET_DATA
:
10623 ort
= ORT_TARGET_DATA
;
10626 ort
= OMP_TEAMS_COMBINED (expr
) ? ORT_COMBINED_TEAMS
: ORT_TEAMS
;
10628 case OACC_HOST_DATA
:
10629 ort
= ORT_ACC_HOST_DATA
;
10632 gcc_unreachable ();
10634 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr
), pre_p
, ort
,
10636 if (TREE_CODE (expr
) == OMP_TARGET
)
10637 optimize_target_teams (expr
, pre_p
);
10638 if ((ort
& (ORT_TARGET
| ORT_TARGET_DATA
)) != 0)
10640 push_gimplify_context ();
10641 gimple
*g
= gimplify_and_return_first (OMP_BODY (expr
), &body
);
10642 if (gimple_code (g
) == GIMPLE_BIND
)
10643 pop_gimplify_context (g
);
10645 pop_gimplify_context (NULL
);
10646 if ((ort
& ORT_TARGET_DATA
) != 0)
10648 enum built_in_function end_ix
;
10649 switch (TREE_CODE (expr
))
10652 case OACC_HOST_DATA
:
10653 end_ix
= BUILT_IN_GOACC_DATA_END
;
10655 case OMP_TARGET_DATA
:
10656 end_ix
= BUILT_IN_GOMP_TARGET_END_DATA
;
10659 gcc_unreachable ();
10661 tree fn
= builtin_decl_explicit (end_ix
);
10662 g
= gimple_build_call (fn
, 0);
10663 gimple_seq cleanup
= NULL
;
10664 gimple_seq_add_stmt (&cleanup
, g
);
10665 g
= gimple_build_try (body
, cleanup
, GIMPLE_TRY_FINALLY
);
10667 gimple_seq_add_stmt (&body
, g
);
10671 gimplify_and_add (OMP_BODY (expr
), &body
);
10672 gimplify_adjust_omp_clauses (pre_p
, body
, &OMP_CLAUSES (expr
),
10675 switch (TREE_CODE (expr
))
10678 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_DATA
,
10679 OMP_CLAUSES (expr
));
10682 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_KERNELS
,
10683 OMP_CLAUSES (expr
));
10685 case OACC_HOST_DATA
:
10686 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_HOST_DATA
,
10687 OMP_CLAUSES (expr
));
10689 case OACC_PARALLEL
:
10690 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_PARALLEL
,
10691 OMP_CLAUSES (expr
));
10694 stmt
= gimple_build_omp_sections (body
, OMP_CLAUSES (expr
));
10697 stmt
= gimple_build_omp_single (body
, OMP_CLAUSES (expr
));
10700 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_REGION
,
10701 OMP_CLAUSES (expr
));
10703 case OMP_TARGET_DATA
:
10704 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_DATA
,
10705 OMP_CLAUSES (expr
));
10708 stmt
= gimple_build_omp_teams (body
, OMP_CLAUSES (expr
));
10711 gcc_unreachable ();
10714 gimplify_seq_add_stmt (pre_p
, stmt
);
10715 *expr_p
= NULL_TREE
;
10718 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10719 target update constructs. */
10722 gimplify_omp_target_update (tree
*expr_p
, gimple_seq
*pre_p
)
10724 tree expr
= *expr_p
;
10727 enum omp_region_type ort
= ORT_WORKSHARE
;
10729 switch (TREE_CODE (expr
))
10731 case OACC_ENTER_DATA
:
10732 case OACC_EXIT_DATA
:
10733 kind
= GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA
;
10737 kind
= GF_OMP_TARGET_KIND_OACC_UPDATE
;
10740 case OMP_TARGET_UPDATE
:
10741 kind
= GF_OMP_TARGET_KIND_UPDATE
;
10743 case OMP_TARGET_ENTER_DATA
:
10744 kind
= GF_OMP_TARGET_KIND_ENTER_DATA
;
10746 case OMP_TARGET_EXIT_DATA
:
10747 kind
= GF_OMP_TARGET_KIND_EXIT_DATA
;
10750 gcc_unreachable ();
10752 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr
), pre_p
,
10753 ort
, TREE_CODE (expr
));
10754 gimplify_adjust_omp_clauses (pre_p
, NULL
, &OMP_STANDALONE_CLAUSES (expr
),
10756 stmt
= gimple_build_omp_target (NULL
, kind
, OMP_STANDALONE_CLAUSES (expr
));
10758 gimplify_seq_add_stmt (pre_p
, stmt
);
10759 *expr_p
= NULL_TREE
;
10762 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10763 stabilized the lhs of the atomic operation as *ADDR. Return true if
10764 EXPR is this stabilized form. */
10767 goa_lhs_expr_p (tree expr
, tree addr
)
10769 /* Also include casts to other type variants. The C front end is fond
10770 of adding these for e.g. volatile variables. This is like
10771 STRIP_TYPE_NOPS but includes the main variant lookup. */
10772 STRIP_USELESS_TYPE_CONVERSION (expr
);
10774 if (TREE_CODE (expr
) == INDIRECT_REF
)
10776 expr
= TREE_OPERAND (expr
, 0);
10777 while (expr
!= addr
10778 && (CONVERT_EXPR_P (expr
)
10779 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
10780 && TREE_CODE (expr
) == TREE_CODE (addr
)
10781 && types_compatible_p (TREE_TYPE (expr
), TREE_TYPE (addr
)))
10783 expr
= TREE_OPERAND (expr
, 0);
10784 addr
= TREE_OPERAND (addr
, 0);
10788 return (TREE_CODE (addr
) == ADDR_EXPR
10789 && TREE_CODE (expr
) == ADDR_EXPR
10790 && TREE_OPERAND (addr
, 0) == TREE_OPERAND (expr
, 0));
10792 if (TREE_CODE (addr
) == ADDR_EXPR
&& expr
== TREE_OPERAND (addr
, 0))
10797 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10798 expression does not involve the lhs, evaluate it into a temporary.
10799 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10800 or -1 if an error was encountered. */
10803 goa_stabilize_expr (tree
*expr_p
, gimple_seq
*pre_p
, tree lhs_addr
,
10806 tree expr
= *expr_p
;
10809 if (goa_lhs_expr_p (expr
, lhs_addr
))
10814 if (is_gimple_val (expr
))
10818 switch (TREE_CODE_CLASS (TREE_CODE (expr
)))
10821 case tcc_comparison
:
10822 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 1), pre_p
, lhs_addr
,
10826 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
, lhs_addr
,
10829 case tcc_expression
:
10830 switch (TREE_CODE (expr
))
10832 case TRUTH_ANDIF_EXPR
:
10833 case TRUTH_ORIF_EXPR
:
10834 case TRUTH_AND_EXPR
:
10835 case TRUTH_OR_EXPR
:
10836 case TRUTH_XOR_EXPR
:
10837 case BIT_INSERT_EXPR
:
10838 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 1), pre_p
,
10839 lhs_addr
, lhs_var
);
10841 case TRUTH_NOT_EXPR
:
10842 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
,
10843 lhs_addr
, lhs_var
);
10845 case COMPOUND_EXPR
:
10846 /* Break out any preevaluations from cp_build_modify_expr. */
10847 for (; TREE_CODE (expr
) == COMPOUND_EXPR
;
10848 expr
= TREE_OPERAND (expr
, 1))
10849 gimplify_stmt (&TREE_OPERAND (expr
, 0), pre_p
);
10851 return goa_stabilize_expr (expr_p
, pre_p
, lhs_addr
, lhs_var
);
10856 case tcc_reference
:
10857 if (TREE_CODE (expr
) == BIT_FIELD_REF
)
10858 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
,
10859 lhs_addr
, lhs_var
);
10867 enum gimplify_status gs
;
10868 gs
= gimplify_expr (expr_p
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
10869 if (gs
!= GS_ALL_DONE
)
10876 /* Gimplify an OMP_ATOMIC statement. */
10878 static enum gimplify_status
10879 gimplify_omp_atomic (tree
*expr_p
, gimple_seq
*pre_p
)
10881 tree addr
= TREE_OPERAND (*expr_p
, 0);
10882 tree rhs
= TREE_CODE (*expr_p
) == OMP_ATOMIC_READ
10883 ? NULL
: TREE_OPERAND (*expr_p
, 1);
10884 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr
)));
10886 gomp_atomic_load
*loadstmt
;
10887 gomp_atomic_store
*storestmt
;
10889 tmp_load
= create_tmp_reg (type
);
10890 if (rhs
&& goa_stabilize_expr (&rhs
, pre_p
, addr
, tmp_load
) < 0)
10893 if (gimplify_expr (&addr
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
)
10897 loadstmt
= gimple_build_omp_atomic_load (tmp_load
, addr
);
10898 gimplify_seq_add_stmt (pre_p
, loadstmt
);
10899 if (rhs
&& gimplify_expr (&rhs
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
)
10903 if (TREE_CODE (*expr_p
) == OMP_ATOMIC_READ
)
10905 storestmt
= gimple_build_omp_atomic_store (rhs
);
10906 gimplify_seq_add_stmt (pre_p
, storestmt
);
10907 if (OMP_ATOMIC_SEQ_CST (*expr_p
))
10909 gimple_omp_atomic_set_seq_cst (loadstmt
);
10910 gimple_omp_atomic_set_seq_cst (storestmt
);
10912 switch (TREE_CODE (*expr_p
))
10914 case OMP_ATOMIC_READ
:
10915 case OMP_ATOMIC_CAPTURE_OLD
:
10916 *expr_p
= tmp_load
;
10917 gimple_omp_atomic_set_need_value (loadstmt
);
10919 case OMP_ATOMIC_CAPTURE_NEW
:
10921 gimple_omp_atomic_set_need_value (storestmt
);
10928 return GS_ALL_DONE
;
10931 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
10932 body, and adding some EH bits. */
10934 static enum gimplify_status
10935 gimplify_transaction (tree
*expr_p
, gimple_seq
*pre_p
)
10937 tree expr
= *expr_p
, temp
, tbody
= TRANSACTION_EXPR_BODY (expr
);
10939 gtransaction
*trans_stmt
;
10940 gimple_seq body
= NULL
;
10943 /* Wrap the transaction body in a BIND_EXPR so we have a context
10944 where to put decls for OMP. */
10945 if (TREE_CODE (tbody
) != BIND_EXPR
)
10947 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, tbody
, NULL
);
10948 TREE_SIDE_EFFECTS (bind
) = 1;
10949 SET_EXPR_LOCATION (bind
, EXPR_LOCATION (tbody
));
10950 TRANSACTION_EXPR_BODY (expr
) = bind
;
10953 push_gimplify_context ();
10954 temp
= voidify_wrapper_expr (*expr_p
, NULL
);
10956 body_stmt
= gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr
), &body
);
10957 pop_gimplify_context (body_stmt
);
10959 trans_stmt
= gimple_build_transaction (body
);
10960 if (TRANSACTION_EXPR_OUTER (expr
))
10961 subcode
= GTMA_IS_OUTER
;
10962 else if (TRANSACTION_EXPR_RELAXED (expr
))
10963 subcode
= GTMA_IS_RELAXED
;
10964 gimple_transaction_set_subcode (trans_stmt
, subcode
);
10966 gimplify_seq_add_stmt (pre_p
, trans_stmt
);
10974 *expr_p
= NULL_TREE
;
10975 return GS_ALL_DONE
;
10978 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
10979 is the OMP_BODY of the original EXPR (which has already been
10980 gimplified so it's not present in the EXPR).
10982 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
10985 gimplify_omp_ordered (tree expr
, gimple_seq body
)
10990 tree source_c
= NULL_TREE
;
10991 tree sink_c
= NULL_TREE
;
10993 if (gimplify_omp_ctxp
)
10995 for (c
= OMP_ORDERED_CLAUSES (expr
); c
; c
= OMP_CLAUSE_CHAIN (c
))
10996 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
10997 && gimplify_omp_ctxp
->loop_iter_var
.is_empty ()
10998 && (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
10999 || OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SOURCE
))
11001 error_at (OMP_CLAUSE_LOCATION (c
),
11002 "%<ordered%> construct with %<depend%> clause must be "
11003 "closely nested inside a loop with %<ordered%> clause "
11004 "with a parameter");
11007 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
11008 && OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
11011 for (decls
= OMP_CLAUSE_DECL (c
), i
= 0;
11012 decls
&& TREE_CODE (decls
) == TREE_LIST
;
11013 decls
= TREE_CHAIN (decls
), ++i
)
11014 if (i
>= gimplify_omp_ctxp
->loop_iter_var
.length () / 2)
11016 else if (TREE_VALUE (decls
)
11017 != gimplify_omp_ctxp
->loop_iter_var
[2 * i
])
11019 error_at (OMP_CLAUSE_LOCATION (c
),
11020 "variable %qE is not an iteration "
11021 "of outermost loop %d, expected %qE",
11022 TREE_VALUE (decls
), i
+ 1,
11023 gimplify_omp_ctxp
->loop_iter_var
[2 * i
]);
11029 = gimplify_omp_ctxp
->loop_iter_var
[2 * i
+ 1];
11030 if (!fail
&& i
!= gimplify_omp_ctxp
->loop_iter_var
.length () / 2)
11032 error_at (OMP_CLAUSE_LOCATION (c
),
11033 "number of variables in %<depend(sink)%> "
11034 "clause does not match number of "
11035 "iteration variables");
11040 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
11041 && OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SOURCE
)
11045 error_at (OMP_CLAUSE_LOCATION (c
),
11046 "more than one %<depend(source)%> clause on an "
11047 "%<ordered%> construct");
11054 if (source_c
&& sink_c
)
11056 error_at (OMP_CLAUSE_LOCATION (source_c
),
11057 "%<depend(source)%> clause specified together with "
11058 "%<depend(sink:)%> clauses on the same construct");
11063 return gimple_build_nop ();
11064 return gimple_build_omp_ordered (body
, OMP_ORDERED_CLAUSES (expr
));
11067 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11068 expression produces a value to be used as an operand inside a GIMPLE
11069 statement, the value will be stored back in *EXPR_P. This value will
11070 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11071 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11072 emitted in PRE_P and POST_P.
11074 Additionally, this process may overwrite parts of the input
11075 expression during gimplification. Ideally, it should be
11076 possible to do non-destructive gimplification.
11078 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11079 the expression needs to evaluate to a value to be used as
11080 an operand in a GIMPLE statement, this value will be stored in
11081 *EXPR_P on exit. This happens when the caller specifies one
11082 of fb_lvalue or fb_rvalue fallback flags.
11084 PRE_P will contain the sequence of GIMPLE statements corresponding
11085 to the evaluation of EXPR and all the side-effects that must
11086 be executed before the main expression. On exit, the last
11087 statement of PRE_P is the core statement being gimplified. For
11088 instance, when gimplifying 'if (++a)' the last statement in
11089 PRE_P will be 'if (t.1)' where t.1 is the result of
11090 pre-incrementing 'a'.
11092 POST_P will contain the sequence of GIMPLE statements corresponding
11093 to the evaluation of all the side-effects that must be executed
11094 after the main expression. If this is NULL, the post
11095 side-effects are stored at the end of PRE_P.
11097 The reason why the output is split in two is to handle post
11098 side-effects explicitly. In some cases, an expression may have
11099 inner and outer post side-effects which need to be emitted in
11100 an order different from the one given by the recursive
11101 traversal. For instance, for the expression (*p--)++ the post
11102 side-effects of '--' must actually occur *after* the post
11103 side-effects of '++'. However, gimplification will first visit
11104 the inner expression, so if a separate POST sequence was not
11105 used, the resulting sequence would be:
11112 However, the post-decrement operation in line #2 must not be
11113 evaluated until after the store to *p at line #4, so the
11114 correct sequence should be:
11121 So, by specifying a separate post queue, it is possible
11122 to emit the post side-effects in the correct order.
11123 If POST_P is NULL, an internal queue will be used. Before
11124 returning to the caller, the sequence POST_P is appended to
11125 the main output sequence PRE_P.
11127 GIMPLE_TEST_F points to a function that takes a tree T and
11128 returns nonzero if T is in the GIMPLE form requested by the
11129 caller. The GIMPLE predicates are in gimple.c.
11131 FALLBACK tells the function what sort of a temporary we want if
11132 gimplification cannot produce an expression that complies with
11135 fb_none means that no temporary should be generated
11136 fb_rvalue means that an rvalue is OK to generate
11137 fb_lvalue means that an lvalue is OK to generate
11138 fb_either means that either is OK, but an lvalue is preferable.
11139 fb_mayfail means that gimplification may fail (in which case
11140 GS_ERROR will be returned)
11142 The return value is either GS_ERROR or GS_ALL_DONE, since this
11143 function iterates until EXPR is completely gimplified or an error
11146 enum gimplify_status
11147 gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
11148 bool (*gimple_test_f
) (tree
), fallback_t fallback
)
11151 gimple_seq internal_pre
= NULL
;
11152 gimple_seq internal_post
= NULL
;
11155 location_t saved_location
;
11156 enum gimplify_status ret
;
11157 gimple_stmt_iterator pre_last_gsi
, post_last_gsi
;
11160 save_expr
= *expr_p
;
11161 if (save_expr
== NULL_TREE
)
11162 return GS_ALL_DONE
;
11164 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11165 is_statement
= gimple_test_f
== is_gimple_stmt
;
11167 gcc_assert (pre_p
);
11169 /* Consistency checks. */
11170 if (gimple_test_f
== is_gimple_reg
)
11171 gcc_assert (fallback
& (fb_rvalue
| fb_lvalue
));
11172 else if (gimple_test_f
== is_gimple_val
11173 || gimple_test_f
== is_gimple_call_addr
11174 || gimple_test_f
== is_gimple_condexpr
11175 || gimple_test_f
== is_gimple_mem_rhs
11176 || gimple_test_f
== is_gimple_mem_rhs_or_call
11177 || gimple_test_f
== is_gimple_reg_rhs
11178 || gimple_test_f
== is_gimple_reg_rhs_or_call
11179 || gimple_test_f
== is_gimple_asm_val
11180 || gimple_test_f
== is_gimple_mem_ref_addr
)
11181 gcc_assert (fallback
& fb_rvalue
);
11182 else if (gimple_test_f
== is_gimple_min_lval
11183 || gimple_test_f
== is_gimple_lvalue
)
11184 gcc_assert (fallback
& fb_lvalue
);
11185 else if (gimple_test_f
== is_gimple_addressable
)
11186 gcc_assert (fallback
& fb_either
);
11187 else if (gimple_test_f
== is_gimple_stmt
)
11188 gcc_assert (fallback
== fb_none
);
11191 /* We should have recognized the GIMPLE_TEST_F predicate to
11192 know what kind of fallback to use in case a temporary is
11193 needed to hold the value or address of *EXPR_P. */
11194 gcc_unreachable ();
11197 /* We used to check the predicate here and return immediately if it
11198 succeeds. This is wrong; the design is for gimplification to be
11199 idempotent, and for the predicates to only test for valid forms, not
11200 whether they are fully simplified. */
11202 pre_p
= &internal_pre
;
11204 if (post_p
== NULL
)
11205 post_p
= &internal_post
;
11207 /* Remember the last statements added to PRE_P and POST_P. Every
11208 new statement added by the gimplification helpers needs to be
11209 annotated with location information. To centralize the
11210 responsibility, we remember the last statement that had been
11211 added to both queues before gimplifying *EXPR_P. If
11212 gimplification produces new statements in PRE_P and POST_P, those
11213 statements will be annotated with the same location information
11215 pre_last_gsi
= gsi_last (*pre_p
);
11216 post_last_gsi
= gsi_last (*post_p
);
11218 saved_location
= input_location
;
11219 if (save_expr
!= error_mark_node
11220 && EXPR_HAS_LOCATION (*expr_p
))
11221 input_location
= EXPR_LOCATION (*expr_p
);
11223 /* Loop over the specific gimplifiers until the toplevel node
11224 remains the same. */
11227 /* Strip away as many useless type conversions as possible
11228 at the toplevel. */
11229 STRIP_USELESS_TYPE_CONVERSION (*expr_p
);
11231 /* Remember the expr. */
11232 save_expr
= *expr_p
;
11234 /* Die, die, die, my darling. */
11235 if (save_expr
== error_mark_node
11236 || (TREE_TYPE (save_expr
)
11237 && TREE_TYPE (save_expr
) == error_mark_node
))
11243 /* Do any language-specific gimplification. */
11244 ret
= ((enum gimplify_status
)
11245 lang_hooks
.gimplify_expr (expr_p
, pre_p
, post_p
));
11248 if (*expr_p
== NULL_TREE
)
11250 if (*expr_p
!= save_expr
)
11253 else if (ret
!= GS_UNHANDLED
)
11256 /* Make sure that all the cases set 'ret' appropriately. */
11257 ret
= GS_UNHANDLED
;
11258 switch (TREE_CODE (*expr_p
))
11260 /* First deal with the special cases. */
11262 case POSTINCREMENT_EXPR
:
11263 case POSTDECREMENT_EXPR
:
11264 case PREINCREMENT_EXPR
:
11265 case PREDECREMENT_EXPR
:
11266 ret
= gimplify_self_mod_expr (expr_p
, pre_p
, post_p
,
11267 fallback
!= fb_none
,
11268 TREE_TYPE (*expr_p
));
11271 case VIEW_CONVERT_EXPR
:
11272 if (is_gimple_reg_type (TREE_TYPE (*expr_p
))
11273 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p
, 0))))
11275 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11276 post_p
, is_gimple_val
, fb_rvalue
);
11277 recalculate_side_effects (*expr_p
);
11283 case ARRAY_RANGE_REF
:
11284 case REALPART_EXPR
:
11285 case IMAGPART_EXPR
:
11286 case COMPONENT_REF
:
11287 ret
= gimplify_compound_lval (expr_p
, pre_p
, post_p
,
11288 fallback
? fallback
: fb_rvalue
);
11292 ret
= gimplify_cond_expr (expr_p
, pre_p
, fallback
);
11294 /* C99 code may assign to an array in a structure value of a
11295 conditional expression, and this has undefined behavior
11296 only on execution, so create a temporary if an lvalue is
11298 if (fallback
== fb_lvalue
)
11300 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
, false);
11301 mark_addressable (*expr_p
);
11307 ret
= gimplify_call_expr (expr_p
, pre_p
, fallback
!= fb_none
);
11309 /* C99 code may assign to an array in a structure returned
11310 from a function, and this has undefined behavior only on
11311 execution, so create a temporary if an lvalue is
11313 if (fallback
== fb_lvalue
)
11315 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
, false);
11316 mark_addressable (*expr_p
);
11322 gcc_unreachable ();
11324 case COMPOUND_EXPR
:
11325 ret
= gimplify_compound_expr (expr_p
, pre_p
, fallback
!= fb_none
);
11328 case COMPOUND_LITERAL_EXPR
:
11329 ret
= gimplify_compound_literal_expr (expr_p
, pre_p
,
11330 gimple_test_f
, fallback
);
11335 ret
= gimplify_modify_expr (expr_p
, pre_p
, post_p
,
11336 fallback
!= fb_none
);
11339 case TRUTH_ANDIF_EXPR
:
11340 case TRUTH_ORIF_EXPR
:
11342 /* Preserve the original type of the expression and the
11343 source location of the outer expression. */
11344 tree org_type
= TREE_TYPE (*expr_p
);
11345 *expr_p
= gimple_boolify (*expr_p
);
11346 *expr_p
= build3_loc (input_location
, COND_EXPR
,
11350 org_type
, boolean_true_node
),
11353 org_type
, boolean_false_node
));
11358 case TRUTH_NOT_EXPR
:
11360 tree type
= TREE_TYPE (*expr_p
);
11361 /* The parsers are careful to generate TRUTH_NOT_EXPR
11362 only with operands that are always zero or one.
11363 We do not fold here but handle the only interesting case
11364 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11365 *expr_p
= gimple_boolify (*expr_p
);
11366 if (TYPE_PRECISION (TREE_TYPE (*expr_p
)) == 1)
11367 *expr_p
= build1_loc (input_location
, BIT_NOT_EXPR
,
11368 TREE_TYPE (*expr_p
),
11369 TREE_OPERAND (*expr_p
, 0));
11371 *expr_p
= build2_loc (input_location
, BIT_XOR_EXPR
,
11372 TREE_TYPE (*expr_p
),
11373 TREE_OPERAND (*expr_p
, 0),
11374 build_int_cst (TREE_TYPE (*expr_p
), 1));
11375 if (!useless_type_conversion_p (type
, TREE_TYPE (*expr_p
)))
11376 *expr_p
= fold_convert_loc (input_location
, type
, *expr_p
);
11382 ret
= gimplify_addr_expr (expr_p
, pre_p
, post_p
);
11385 case ANNOTATE_EXPR
:
11387 tree cond
= TREE_OPERAND (*expr_p
, 0);
11388 tree kind
= TREE_OPERAND (*expr_p
, 1);
11389 tree type
= TREE_TYPE (cond
);
11390 if (!INTEGRAL_TYPE_P (type
))
11396 tree tmp
= create_tmp_var (type
);
11397 gimplify_arg (&cond
, pre_p
, EXPR_LOCATION (*expr_p
));
11399 = gimple_build_call_internal (IFN_ANNOTATE
, 2, cond
, kind
);
11400 gimple_call_set_lhs (call
, tmp
);
11401 gimplify_seq_add_stmt (pre_p
, call
);
11408 ret
= gimplify_va_arg_expr (expr_p
, pre_p
, post_p
);
11412 if (IS_EMPTY_STMT (*expr_p
))
11418 if (VOID_TYPE_P (TREE_TYPE (*expr_p
))
11419 || fallback
== fb_none
)
11421 /* Just strip a conversion to void (or in void context) and
11423 *expr_p
= TREE_OPERAND (*expr_p
, 0);
11428 ret
= gimplify_conversion (expr_p
);
11429 if (ret
== GS_ERROR
)
11431 if (*expr_p
!= save_expr
)
11435 case FIX_TRUNC_EXPR
:
11436 /* unary_expr: ... | '(' cast ')' val | ... */
11437 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
11438 is_gimple_val
, fb_rvalue
);
11439 recalculate_side_effects (*expr_p
);
11444 bool volatilep
= TREE_THIS_VOLATILE (*expr_p
);
11445 bool notrap
= TREE_THIS_NOTRAP (*expr_p
);
11446 tree saved_ptr_type
= TREE_TYPE (TREE_OPERAND (*expr_p
, 0));
11448 *expr_p
= fold_indirect_ref_loc (input_location
, *expr_p
);
11449 if (*expr_p
!= save_expr
)
11455 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
11456 is_gimple_reg
, fb_rvalue
);
11457 if (ret
== GS_ERROR
)
11460 recalculate_side_effects (*expr_p
);
11461 *expr_p
= fold_build2_loc (input_location
, MEM_REF
,
11462 TREE_TYPE (*expr_p
),
11463 TREE_OPERAND (*expr_p
, 0),
11464 build_int_cst (saved_ptr_type
, 0));
11465 TREE_THIS_VOLATILE (*expr_p
) = volatilep
;
11466 TREE_THIS_NOTRAP (*expr_p
) = notrap
;
11471 /* We arrive here through the various re-gimplifcation paths. */
11473 /* First try re-folding the whole thing. */
11474 tmp
= fold_binary (MEM_REF
, TREE_TYPE (*expr_p
),
11475 TREE_OPERAND (*expr_p
, 0),
11476 TREE_OPERAND (*expr_p
, 1));
11479 REF_REVERSE_STORAGE_ORDER (tmp
)
11480 = REF_REVERSE_STORAGE_ORDER (*expr_p
);
11482 recalculate_side_effects (*expr_p
);
11486 /* Avoid re-gimplifying the address operand if it is already
11487 in suitable form. Re-gimplifying would mark the address
11488 operand addressable. Always gimplify when not in SSA form
11489 as we still may have to gimplify decls with value-exprs. */
11490 if (!gimplify_ctxp
|| !gimple_in_ssa_p (cfun
)
11491 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p
, 0)))
11493 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
11494 is_gimple_mem_ref_addr
, fb_rvalue
);
11495 if (ret
== GS_ERROR
)
11498 recalculate_side_effects (*expr_p
);
11502 /* Constants need not be gimplified. */
11509 /* Drop the overflow flag on constants, we do not want
11510 that in the GIMPLE IL. */
11511 if (TREE_OVERFLOW_P (*expr_p
))
11512 *expr_p
= drop_tree_overflow (*expr_p
);
11517 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11518 CONST_DECL node. Otherwise the decl is replaceable by its
11520 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11521 if (fallback
& fb_lvalue
)
11525 *expr_p
= DECL_INITIAL (*expr_p
);
11531 ret
= gimplify_decl_expr (expr_p
, pre_p
);
11535 ret
= gimplify_bind_expr (expr_p
, pre_p
);
11539 ret
= gimplify_loop_expr (expr_p
, pre_p
);
11543 ret
= gimplify_switch_expr (expr_p
, pre_p
);
11547 ret
= gimplify_exit_expr (expr_p
);
11551 /* If the target is not LABEL, then it is a computed jump
11552 and the target needs to be gimplified. */
11553 if (TREE_CODE (GOTO_DESTINATION (*expr_p
)) != LABEL_DECL
)
11555 ret
= gimplify_expr (&GOTO_DESTINATION (*expr_p
), pre_p
,
11556 NULL
, is_gimple_val
, fb_rvalue
);
11557 if (ret
== GS_ERROR
)
11560 gimplify_seq_add_stmt (pre_p
,
11561 gimple_build_goto (GOTO_DESTINATION (*expr_p
)));
11566 gimplify_seq_add_stmt (pre_p
,
11567 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p
),
11568 PREDICT_EXPR_OUTCOME (*expr_p
)));
11573 ret
= gimplify_label_expr (expr_p
, pre_p
);
11574 label
= LABEL_EXPR_LABEL (*expr_p
);
11575 gcc_assert (decl_function_context (label
) == current_function_decl
);
11577 /* If the label is used in a goto statement, or address of the label
11578 is taken, we need to unpoison all variables that were seen so far.
11579 Doing so would prevent us from reporting a false positives. */
11580 if (asan_poisoned_variables
11581 && asan_used_labels
!= NULL
11582 && asan_used_labels
->contains (label
))
11583 asan_poison_variables (asan_poisoned_variables
, false, pre_p
);
11586 case CASE_LABEL_EXPR
:
11587 ret
= gimplify_case_label_expr (expr_p
, pre_p
);
11589 if (gimplify_ctxp
->live_switch_vars
)
11590 asan_poison_variables (gimplify_ctxp
->live_switch_vars
, false,
11595 ret
= gimplify_return_expr (*expr_p
, pre_p
);
11599 /* Don't reduce this in place; let gimplify_init_constructor work its
11600 magic. Buf if we're just elaborating this for side effects, just
11601 gimplify any element that has side-effects. */
11602 if (fallback
== fb_none
)
11604 unsigned HOST_WIDE_INT ix
;
11606 tree temp
= NULL_TREE
;
11607 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p
), ix
, val
)
11608 if (TREE_SIDE_EFFECTS (val
))
11609 append_to_statement_list (val
, &temp
);
11612 ret
= temp
? GS_OK
: GS_ALL_DONE
;
11614 /* C99 code may assign to an array in a constructed
11615 structure or union, and this has undefined behavior only
11616 on execution, so create a temporary if an lvalue is
11618 else if (fallback
== fb_lvalue
)
11620 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
, false);
11621 mark_addressable (*expr_p
);
11628 /* The following are special cases that are not handled by the
11629 original GIMPLE grammar. */
11631 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11634 ret
= gimplify_save_expr (expr_p
, pre_p
, post_p
);
11637 case BIT_FIELD_REF
:
11638 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11639 post_p
, is_gimple_lvalue
, fb_either
);
11640 recalculate_side_effects (*expr_p
);
11643 case TARGET_MEM_REF
:
11645 enum gimplify_status r0
= GS_ALL_DONE
, r1
= GS_ALL_DONE
;
11647 if (TMR_BASE (*expr_p
))
11648 r0
= gimplify_expr (&TMR_BASE (*expr_p
), pre_p
,
11649 post_p
, is_gimple_mem_ref_addr
, fb_either
);
11650 if (TMR_INDEX (*expr_p
))
11651 r1
= gimplify_expr (&TMR_INDEX (*expr_p
), pre_p
,
11652 post_p
, is_gimple_val
, fb_rvalue
);
11653 if (TMR_INDEX2 (*expr_p
))
11654 r1
= gimplify_expr (&TMR_INDEX2 (*expr_p
), pre_p
,
11655 post_p
, is_gimple_val
, fb_rvalue
);
11656 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11657 ret
= MIN (r0
, r1
);
11661 case NON_LVALUE_EXPR
:
11662 /* This should have been stripped above. */
11663 gcc_unreachable ();
11666 ret
= gimplify_asm_expr (expr_p
, pre_p
, post_p
);
11669 case TRY_FINALLY_EXPR
:
11670 case TRY_CATCH_EXPR
:
11672 gimple_seq eval
, cleanup
;
11675 /* Calls to destructors are generated automatically in FINALLY/CATCH
11676 block. They should have location as UNKNOWN_LOCATION. However,
11677 gimplify_call_expr will reset these call stmts to input_location
11678 if it finds stmt's location is unknown. To prevent resetting for
11679 destructors, we set the input_location to unknown.
11680 Note that this only affects the destructor calls in FINALLY/CATCH
11681 block, and will automatically reset to its original value by the
11682 end of gimplify_expr. */
11683 input_location
= UNKNOWN_LOCATION
;
11684 eval
= cleanup
= NULL
;
11685 gimplify_and_add (TREE_OPERAND (*expr_p
, 0), &eval
);
11686 gimplify_and_add (TREE_OPERAND (*expr_p
, 1), &cleanup
);
11687 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11688 if (gimple_seq_empty_p (cleanup
))
11690 gimple_seq_add_seq (pre_p
, eval
);
11694 try_
= gimple_build_try (eval
, cleanup
,
11695 TREE_CODE (*expr_p
) == TRY_FINALLY_EXPR
11696 ? GIMPLE_TRY_FINALLY
11697 : GIMPLE_TRY_CATCH
);
11698 if (EXPR_HAS_LOCATION (save_expr
))
11699 gimple_set_location (try_
, EXPR_LOCATION (save_expr
));
11700 else if (LOCATION_LOCUS (saved_location
) != UNKNOWN_LOCATION
)
11701 gimple_set_location (try_
, saved_location
);
11702 if (TREE_CODE (*expr_p
) == TRY_CATCH_EXPR
)
11703 gimple_try_set_catch_is_cleanup (try_
,
11704 TRY_CATCH_IS_CLEANUP (*expr_p
));
11705 gimplify_seq_add_stmt (pre_p
, try_
);
11710 case CLEANUP_POINT_EXPR
:
11711 ret
= gimplify_cleanup_point_expr (expr_p
, pre_p
);
11715 ret
= gimplify_target_expr (expr_p
, pre_p
, post_p
);
11721 gimple_seq handler
= NULL
;
11722 gimplify_and_add (CATCH_BODY (*expr_p
), &handler
);
11723 c
= gimple_build_catch (CATCH_TYPES (*expr_p
), handler
);
11724 gimplify_seq_add_stmt (pre_p
, c
);
11729 case EH_FILTER_EXPR
:
11732 gimple_seq failure
= NULL
;
11734 gimplify_and_add (EH_FILTER_FAILURE (*expr_p
), &failure
);
11735 ehf
= gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p
), failure
);
11736 gimple_set_no_warning (ehf
, TREE_NO_WARNING (*expr_p
));
11737 gimplify_seq_add_stmt (pre_p
, ehf
);
11744 enum gimplify_status r0
, r1
;
11745 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
,
11746 post_p
, is_gimple_val
, fb_rvalue
);
11747 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
,
11748 post_p
, is_gimple_val
, fb_rvalue
);
11749 TREE_SIDE_EFFECTS (*expr_p
) = 0;
11750 ret
= MIN (r0
, r1
);
11755 /* We get here when taking the address of a label. We mark
11756 the label as "forced"; meaning it can never be removed and
11757 it is a potential target for any computed goto. */
11758 FORCED_LABEL (*expr_p
) = 1;
11762 case STATEMENT_LIST
:
11763 ret
= gimplify_statement_list (expr_p
, pre_p
);
11766 case WITH_SIZE_EXPR
:
11768 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11769 post_p
== &internal_post
? NULL
: post_p
,
11770 gimple_test_f
, fallback
);
11771 gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
, post_p
,
11772 is_gimple_val
, fb_rvalue
);
11779 ret
= gimplify_var_or_parm_decl (expr_p
);
11783 /* When within an OMP context, notice uses of variables. */
11784 if (gimplify_omp_ctxp
)
11785 omp_notice_variable (gimplify_omp_ctxp
, *expr_p
, true);
11790 /* Allow callbacks into the gimplifier during optimization. */
11795 gimplify_omp_parallel (expr_p
, pre_p
);
11800 gimplify_omp_task (expr_p
, pre_p
);
11808 case OMP_DISTRIBUTE
:
11811 ret
= gimplify_omp_for (expr_p
, pre_p
);
11815 gimplify_oacc_cache (expr_p
, pre_p
);
11820 gimplify_oacc_declare (expr_p
, pre_p
);
11824 case OACC_HOST_DATA
:
11827 case OACC_PARALLEL
:
11831 case OMP_TARGET_DATA
:
11833 gimplify_omp_workshare (expr_p
, pre_p
);
11837 case OACC_ENTER_DATA
:
11838 case OACC_EXIT_DATA
:
11840 case OMP_TARGET_UPDATE
:
11841 case OMP_TARGET_ENTER_DATA
:
11842 case OMP_TARGET_EXIT_DATA
:
11843 gimplify_omp_target_update (expr_p
, pre_p
);
11849 case OMP_TASKGROUP
:
11853 gimple_seq body
= NULL
;
11856 gimplify_and_add (OMP_BODY (*expr_p
), &body
);
11857 switch (TREE_CODE (*expr_p
))
11860 g
= gimple_build_omp_section (body
);
11863 g
= gimple_build_omp_master (body
);
11865 case OMP_TASKGROUP
:
11867 gimple_seq cleanup
= NULL
;
11869 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END
);
11870 g
= gimple_build_call (fn
, 0);
11871 gimple_seq_add_stmt (&cleanup
, g
);
11872 g
= gimple_build_try (body
, cleanup
, GIMPLE_TRY_FINALLY
);
11874 gimple_seq_add_stmt (&body
, g
);
11875 g
= gimple_build_omp_taskgroup (body
);
11879 g
= gimplify_omp_ordered (*expr_p
, body
);
11882 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p
),
11883 pre_p
, ORT_WORKSHARE
, OMP_CRITICAL
);
11884 gimplify_adjust_omp_clauses (pre_p
, body
,
11885 &OMP_CRITICAL_CLAUSES (*expr_p
),
11887 g
= gimple_build_omp_critical (body
,
11888 OMP_CRITICAL_NAME (*expr_p
),
11889 OMP_CRITICAL_CLAUSES (*expr_p
));
11892 gcc_unreachable ();
11894 gimplify_seq_add_stmt (pre_p
, g
);
11900 case OMP_ATOMIC_READ
:
11901 case OMP_ATOMIC_CAPTURE_OLD
:
11902 case OMP_ATOMIC_CAPTURE_NEW
:
11903 ret
= gimplify_omp_atomic (expr_p
, pre_p
);
11906 case TRANSACTION_EXPR
:
11907 ret
= gimplify_transaction (expr_p
, pre_p
);
11910 case TRUTH_AND_EXPR
:
11911 case TRUTH_OR_EXPR
:
11912 case TRUTH_XOR_EXPR
:
11914 tree orig_type
= TREE_TYPE (*expr_p
);
11915 tree new_type
, xop0
, xop1
;
11916 *expr_p
= gimple_boolify (*expr_p
);
11917 new_type
= TREE_TYPE (*expr_p
);
11918 if (!useless_type_conversion_p (orig_type
, new_type
))
11920 *expr_p
= fold_convert_loc (input_location
, orig_type
, *expr_p
);
11925 /* Boolified binary truth expressions are semantically equivalent
11926 to bitwise binary expressions. Canonicalize them to the
11927 bitwise variant. */
11928 switch (TREE_CODE (*expr_p
))
11930 case TRUTH_AND_EXPR
:
11931 TREE_SET_CODE (*expr_p
, BIT_AND_EXPR
);
11933 case TRUTH_OR_EXPR
:
11934 TREE_SET_CODE (*expr_p
, BIT_IOR_EXPR
);
11936 case TRUTH_XOR_EXPR
:
11937 TREE_SET_CODE (*expr_p
, BIT_XOR_EXPR
);
11942 /* Now make sure that operands have compatible type to
11943 expression's new_type. */
11944 xop0
= TREE_OPERAND (*expr_p
, 0);
11945 xop1
= TREE_OPERAND (*expr_p
, 1);
11946 if (!useless_type_conversion_p (new_type
, TREE_TYPE (xop0
)))
11947 TREE_OPERAND (*expr_p
, 0) = fold_convert_loc (input_location
,
11950 if (!useless_type_conversion_p (new_type
, TREE_TYPE (xop1
)))
11951 TREE_OPERAND (*expr_p
, 1) = fold_convert_loc (input_location
,
11954 /* Continue classified as tcc_binary. */
11958 case VEC_COND_EXPR
:
11960 enum gimplify_status r0
, r1
, r2
;
11962 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11963 post_p
, is_gimple_condexpr
, fb_rvalue
);
11964 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
11965 post_p
, is_gimple_val
, fb_rvalue
);
11966 r2
= gimplify_expr (&TREE_OPERAND (*expr_p
, 2), pre_p
,
11967 post_p
, is_gimple_val
, fb_rvalue
);
11969 ret
= MIN (MIN (r0
, r1
), r2
);
11970 recalculate_side_effects (*expr_p
);
11975 case VEC_PERM_EXPR
:
11976 /* Classified as tcc_expression. */
11979 case BIT_INSERT_EXPR
:
11980 /* Argument 3 is a constant. */
11983 case POINTER_PLUS_EXPR
:
11985 enum gimplify_status r0
, r1
;
11986 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11987 post_p
, is_gimple_val
, fb_rvalue
);
11988 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
11989 post_p
, is_gimple_val
, fb_rvalue
);
11990 recalculate_side_effects (*expr_p
);
11991 ret
= MIN (r0
, r1
);
11995 case CILK_SYNC_STMT
:
11997 if (!fn_contains_cilk_spawn_p (cfun
))
11999 error_at (EXPR_LOCATION (*expr_p
),
12000 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
12005 gimplify_cilk_sync (expr_p
, pre_p
);
12012 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p
)))
12014 case tcc_comparison
:
12015 /* Handle comparison of objects of non scalar mode aggregates
12016 with a call to memcmp. It would be nice to only have to do
12017 this for variable-sized objects, but then we'd have to allow
12018 the same nest of reference nodes we allow for MODIFY_EXPR and
12019 that's too complex.
12021 Compare scalar mode aggregates as scalar mode values. Using
12022 memcmp for them would be very inefficient at best, and is
12023 plain wrong if bitfields are involved. */
12025 tree type
= TREE_TYPE (TREE_OPERAND (*expr_p
, 1));
12027 /* Vector comparisons need no boolification. */
12028 if (TREE_CODE (type
) == VECTOR_TYPE
)
12030 else if (!AGGREGATE_TYPE_P (type
))
12032 tree org_type
= TREE_TYPE (*expr_p
);
12033 *expr_p
= gimple_boolify (*expr_p
);
12034 if (!useless_type_conversion_p (org_type
,
12035 TREE_TYPE (*expr_p
)))
12037 *expr_p
= fold_convert_loc (input_location
,
12038 org_type
, *expr_p
);
12044 else if (TYPE_MODE (type
) != BLKmode
)
12045 ret
= gimplify_scalar_mode_aggregate_compare (expr_p
);
12047 ret
= gimplify_variable_sized_compare (expr_p
);
12052 /* If *EXPR_P does not need to be special-cased, handle it
12053 according to its class. */
12055 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
12056 post_p
, is_gimple_val
, fb_rvalue
);
12062 enum gimplify_status r0
, r1
;
12064 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
12065 post_p
, is_gimple_val
, fb_rvalue
);
12066 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
12067 post_p
, is_gimple_val
, fb_rvalue
);
12069 ret
= MIN (r0
, r1
);
12075 enum gimplify_status r0
, r1
, r2
;
12077 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
12078 post_p
, is_gimple_val
, fb_rvalue
);
12079 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
12080 post_p
, is_gimple_val
, fb_rvalue
);
12081 r2
= gimplify_expr (&TREE_OPERAND (*expr_p
, 2), pre_p
,
12082 post_p
, is_gimple_val
, fb_rvalue
);
12084 ret
= MIN (MIN (r0
, r1
), r2
);
12088 case tcc_declaration
:
12091 goto dont_recalculate
;
12094 gcc_unreachable ();
12097 recalculate_side_effects (*expr_p
);
12103 gcc_assert (*expr_p
|| ret
!= GS_OK
);
12105 while (ret
== GS_OK
);
12107 /* If we encountered an error_mark somewhere nested inside, either
12108 stub out the statement or propagate the error back out. */
12109 if (ret
== GS_ERROR
)
12116 /* This was only valid as a return value from the langhook, which
12117 we handled. Make sure it doesn't escape from any other context. */
12118 gcc_assert (ret
!= GS_UNHANDLED
);
12120 if (fallback
== fb_none
&& *expr_p
&& !is_gimple_stmt (*expr_p
))
12122 /* We aren't looking for a value, and we don't have a valid
12123 statement. If it doesn't have side-effects, throw it away.
12124 We can also get here with code such as "*&&L;", where L is
12125 a LABEL_DECL that is marked as FORCED_LABEL. */
12126 if (TREE_CODE (*expr_p
) == LABEL_DECL
12127 || !TREE_SIDE_EFFECTS (*expr_p
))
12129 else if (!TREE_THIS_VOLATILE (*expr_p
))
12131 /* This is probably a _REF that contains something nested that
12132 has side effects. Recurse through the operands to find it. */
12133 enum tree_code code
= TREE_CODE (*expr_p
);
12137 case COMPONENT_REF
:
12138 case REALPART_EXPR
:
12139 case IMAGPART_EXPR
:
12140 case VIEW_CONVERT_EXPR
:
12141 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
12142 gimple_test_f
, fallback
);
12146 case ARRAY_RANGE_REF
:
12147 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
12148 gimple_test_f
, fallback
);
12149 gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
, post_p
,
12150 gimple_test_f
, fallback
);
12154 /* Anything else with side-effects must be converted to
12155 a valid statement before we get here. */
12156 gcc_unreachable ();
12161 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p
))
12162 && TYPE_MODE (TREE_TYPE (*expr_p
)) != BLKmode
)
12164 /* Historically, the compiler has treated a bare reference
12165 to a non-BLKmode volatile lvalue as forcing a load. */
12166 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p
));
12168 /* Normally, we do not want to create a temporary for a
12169 TREE_ADDRESSABLE type because such a type should not be
12170 copied by bitwise-assignment. However, we make an
12171 exception here, as all we are doing here is ensuring that
12172 we read the bytes that make up the type. We use
12173 create_tmp_var_raw because create_tmp_var will abort when
12174 given a TREE_ADDRESSABLE type. */
12175 tree tmp
= create_tmp_var_raw (type
, "vol");
12176 gimple_add_tmp_var (tmp
);
12177 gimplify_assign (tmp
, *expr_p
, pre_p
);
12181 /* We can't do anything useful with a volatile reference to
12182 an incomplete type, so just throw it away. Likewise for
12183 a BLKmode type, since any implicit inner load should
12184 already have been turned into an explicit one by the
12185 gimplification process. */
12189 /* If we are gimplifying at the statement level, we're done. Tack
12190 everything together and return. */
12191 if (fallback
== fb_none
|| is_statement
)
12193 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12194 it out for GC to reclaim it. */
12195 *expr_p
= NULL_TREE
;
12197 if (!gimple_seq_empty_p (internal_pre
)
12198 || !gimple_seq_empty_p (internal_post
))
12200 gimplify_seq_add_seq (&internal_pre
, internal_post
);
12201 gimplify_seq_add_seq (pre_p
, internal_pre
);
12204 /* The result of gimplifying *EXPR_P is going to be the last few
12205 statements in *PRE_P and *POST_P. Add location information
12206 to all the statements that were added by the gimplification
12208 if (!gimple_seq_empty_p (*pre_p
))
12209 annotate_all_with_location_after (*pre_p
, pre_last_gsi
, input_location
);
12211 if (!gimple_seq_empty_p (*post_p
))
12212 annotate_all_with_location_after (*post_p
, post_last_gsi
,
12218 #ifdef ENABLE_GIMPLE_CHECKING
12221 enum tree_code code
= TREE_CODE (*expr_p
);
12222 /* These expressions should already be in gimple IR form. */
12223 gcc_assert (code
!= MODIFY_EXPR
12224 && code
!= ASM_EXPR
12225 && code
!= BIND_EXPR
12226 && code
!= CATCH_EXPR
12227 && (code
!= COND_EXPR
|| gimplify_ctxp
->allow_rhs_cond_expr
)
12228 && code
!= EH_FILTER_EXPR
12229 && code
!= GOTO_EXPR
12230 && code
!= LABEL_EXPR
12231 && code
!= LOOP_EXPR
12232 && code
!= SWITCH_EXPR
12233 && code
!= TRY_FINALLY_EXPR
12234 && code
!= OACC_PARALLEL
12235 && code
!= OACC_KERNELS
12236 && code
!= OACC_DATA
12237 && code
!= OACC_HOST_DATA
12238 && code
!= OACC_DECLARE
12239 && code
!= OACC_UPDATE
12240 && code
!= OACC_ENTER_DATA
12241 && code
!= OACC_EXIT_DATA
12242 && code
!= OACC_CACHE
12243 && code
!= OMP_CRITICAL
12245 && code
!= OACC_LOOP
12246 && code
!= OMP_MASTER
12247 && code
!= OMP_TASKGROUP
12248 && code
!= OMP_ORDERED
12249 && code
!= OMP_PARALLEL
12250 && code
!= OMP_SECTIONS
12251 && code
!= OMP_SECTION
12252 && code
!= OMP_SINGLE
);
12256 /* Otherwise we're gimplifying a subexpression, so the resulting
12257 value is interesting. If it's a valid operand that matches
12258 GIMPLE_TEST_F, we're done. Unless we are handling some
12259 post-effects internally; if that's the case, we need to copy into
12260 a temporary before adding the post-effects to POST_P. */
12261 if (gimple_seq_empty_p (internal_post
) && (*gimple_test_f
) (*expr_p
))
12264 /* Otherwise, we need to create a new temporary for the gimplified
12267 /* We can't return an lvalue if we have an internal postqueue. The
12268 object the lvalue refers to would (probably) be modified by the
12269 postqueue; we need to copy the value out first, which means an
12271 if ((fallback
& fb_lvalue
)
12272 && gimple_seq_empty_p (internal_post
)
12273 && is_gimple_addressable (*expr_p
))
12275 /* An lvalue will do. Take the address of the expression, store it
12276 in a temporary, and replace the expression with an INDIRECT_REF of
12278 tmp
= build_fold_addr_expr_loc (input_location
, *expr_p
);
12279 gimplify_expr (&tmp
, pre_p
, post_p
, is_gimple_reg
, fb_rvalue
);
12280 *expr_p
= build_simple_mem_ref (tmp
);
12282 else if ((fallback
& fb_rvalue
) && is_gimple_reg_rhs_or_call (*expr_p
))
12284 /* An rvalue will do. Assign the gimplified expression into a
12285 new temporary TMP and replace the original expression with
12286 TMP. First, make sure that the expression has a type so that
12287 it can be assigned into a temporary. */
12288 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p
)));
12289 *expr_p
= get_formal_tmp_var (*expr_p
, pre_p
);
12293 #ifdef ENABLE_GIMPLE_CHECKING
12294 if (!(fallback
& fb_mayfail
))
12296 fprintf (stderr
, "gimplification failed:\n");
12297 print_generic_expr (stderr
, *expr_p
);
12298 debug_tree (*expr_p
);
12299 internal_error ("gimplification failed");
12302 gcc_assert (fallback
& fb_mayfail
);
12304 /* If this is an asm statement, and the user asked for the
12305 impossible, don't die. Fail and let gimplify_asm_expr
12311 /* Make sure the temporary matches our predicate. */
12312 gcc_assert ((*gimple_test_f
) (*expr_p
));
12314 if (!gimple_seq_empty_p (internal_post
))
12316 annotate_all_with_location (internal_post
, input_location
);
12317 gimplify_seq_add_seq (pre_p
, internal_post
);
12321 input_location
= saved_location
;
12325 /* Like gimplify_expr but make sure the gimplified result is not itself
12326 a SSA name (but a decl if it were). Temporaries required by
12327 evaluating *EXPR_P may be still SSA names. */
12329 static enum gimplify_status
12330 gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
12331 bool (*gimple_test_f
) (tree
), fallback_t fallback
,
12334 bool was_ssa_name_p
= TREE_CODE (*expr_p
) == SSA_NAME
;
12335 enum gimplify_status ret
= gimplify_expr (expr_p
, pre_p
, post_p
,
12336 gimple_test_f
, fallback
);
12338 && TREE_CODE (*expr_p
) == SSA_NAME
)
12340 tree name
= *expr_p
;
12341 if (was_ssa_name_p
)
12342 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, NULL
, false);
12345 /* Avoid the extra copy if possible. */
12346 *expr_p
= create_tmp_reg (TREE_TYPE (name
));
12347 gimple_set_lhs (SSA_NAME_DEF_STMT (name
), *expr_p
);
12348 release_ssa_name (name
);
12354 /* Look through TYPE for variable-sized objects and gimplify each such
12355 size that we find. Add to LIST_P any statements generated. */
12358 gimplify_type_sizes (tree type
, gimple_seq
*list_p
)
12362 if (type
== NULL
|| type
== error_mark_node
)
12365 /* We first do the main variant, then copy into any other variants. */
12366 type
= TYPE_MAIN_VARIANT (type
);
12368 /* Avoid infinite recursion. */
12369 if (TYPE_SIZES_GIMPLIFIED (type
))
12372 TYPE_SIZES_GIMPLIFIED (type
) = 1;
12374 switch (TREE_CODE (type
))
12377 case ENUMERAL_TYPE
:
12380 case FIXED_POINT_TYPE
:
12381 gimplify_one_sizepos (&TYPE_MIN_VALUE (type
), list_p
);
12382 gimplify_one_sizepos (&TYPE_MAX_VALUE (type
), list_p
);
12384 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12386 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
12387 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
12392 /* These types may not have declarations, so handle them here. */
12393 gimplify_type_sizes (TREE_TYPE (type
), list_p
);
12394 gimplify_type_sizes (TYPE_DOMAIN (type
), list_p
);
12395 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12396 with assigned stack slots, for -O1+ -g they should be tracked
12398 if (!(TYPE_NAME (type
)
12399 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
12400 && DECL_IGNORED_P (TYPE_NAME (type
)))
12401 && TYPE_DOMAIN (type
)
12402 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type
)))
12404 t
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
12405 if (t
&& VAR_P (t
) && DECL_ARTIFICIAL (t
))
12406 DECL_IGNORED_P (t
) = 0;
12407 t
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
12408 if (t
&& VAR_P (t
) && DECL_ARTIFICIAL (t
))
12409 DECL_IGNORED_P (t
) = 0;
12415 case QUAL_UNION_TYPE
:
12416 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
12417 if (TREE_CODE (field
) == FIELD_DECL
)
12419 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field
), list_p
);
12420 gimplify_one_sizepos (&DECL_SIZE (field
), list_p
);
12421 gimplify_one_sizepos (&DECL_SIZE_UNIT (field
), list_p
);
12422 gimplify_type_sizes (TREE_TYPE (field
), list_p
);
12427 case REFERENCE_TYPE
:
12428 /* We used to recurse on the pointed-to type here, which turned out to
12429 be incorrect because its definition might refer to variables not
12430 yet initialized at this point if a forward declaration is involved.
12432 It was actually useful for anonymous pointed-to types to ensure
12433 that the sizes evaluation dominates every possible later use of the
12434 values. Restricting to such types here would be safe since there
12435 is no possible forward declaration around, but would introduce an
12436 undesirable middle-end semantic to anonymity. We then defer to
12437 front-ends the responsibility of ensuring that the sizes are
12438 evaluated both early and late enough, e.g. by attaching artificial
12439 type declarations to the tree. */
12446 gimplify_one_sizepos (&TYPE_SIZE (type
), list_p
);
12447 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type
), list_p
);
12449 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12451 TYPE_SIZE (t
) = TYPE_SIZE (type
);
12452 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
12453 TYPE_SIZES_GIMPLIFIED (t
) = 1;
12457 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12458 a size or position, has had all of its SAVE_EXPRs evaluated.
12459 We add any required statements to *STMT_P. */
12462 gimplify_one_sizepos (tree
*expr_p
, gimple_seq
*stmt_p
)
12464 tree expr
= *expr_p
;
12466 /* We don't do anything if the value isn't there, is constant, or contains
12467 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12468 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12469 will want to replace it with a new variable, but that will cause problems
12470 if this type is from outside the function. It's OK to have that here. */
12471 if (is_gimple_sizepos (expr
))
12474 *expr_p
= unshare_expr (expr
);
12476 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12477 if the def vanishes. */
12478 gimplify_expr (expr_p
, stmt_p
, NULL
, is_gimple_val
, fb_rvalue
, false);
12481 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12482 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12483 is true, also gimplify the parameters. */
12486 gimplify_body (tree fndecl
, bool do_parms
)
12488 location_t saved_location
= input_location
;
12489 gimple_seq parm_stmts
, seq
;
12490 gimple
*outer_stmt
;
12492 struct cgraph_node
*cgn
;
12494 timevar_push (TV_TREE_GIMPLIFY
);
12496 init_tree_ssa (cfun
);
12498 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12500 default_rtl_profile ();
12502 gcc_assert (gimplify_ctxp
== NULL
);
12503 push_gimplify_context (true);
12505 if (flag_openacc
|| flag_openmp
)
12507 gcc_assert (gimplify_omp_ctxp
== NULL
);
12508 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl
)))
12509 gimplify_omp_ctxp
= new_omp_context (ORT_TARGET
);
12512 /* Unshare most shared trees in the body and in that of any nested functions.
12513 It would seem we don't have to do this for nested functions because
12514 they are supposed to be output and then the outer function gimplified
12515 first, but the g++ front end doesn't always do it that way. */
12516 unshare_body (fndecl
);
12517 unvisit_body (fndecl
);
12519 cgn
= cgraph_node::get (fndecl
);
12520 if (cgn
&& cgn
->origin
)
12521 nonlocal_vlas
= new hash_set
<tree
>;
12523 /* Make sure input_location isn't set to something weird. */
12524 input_location
= DECL_SOURCE_LOCATION (fndecl
);
12526 /* Resolve callee-copies. This has to be done before processing
12527 the body so that DECL_VALUE_EXPR gets processed correctly. */
12528 parm_stmts
= do_parms
? gimplify_parameters () : NULL
;
12530 /* Gimplify the function's body. */
12532 gimplify_stmt (&DECL_SAVED_TREE (fndecl
), &seq
);
12533 outer_stmt
= gimple_seq_first_stmt (seq
);
12536 outer_stmt
= gimple_build_nop ();
12537 gimplify_seq_add_stmt (&seq
, outer_stmt
);
12540 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12541 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12542 if (gimple_code (outer_stmt
) == GIMPLE_BIND
12543 && gimple_seq_first (seq
) == gimple_seq_last (seq
))
12544 outer_bind
= as_a
<gbind
*> (outer_stmt
);
12546 outer_bind
= gimple_build_bind (NULL_TREE
, seq
, NULL
);
12548 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
12550 /* If we had callee-copies statements, insert them at the beginning
12551 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12552 if (!gimple_seq_empty_p (parm_stmts
))
12556 gimplify_seq_add_seq (&parm_stmts
, gimple_bind_body (outer_bind
));
12557 gimple_bind_set_body (outer_bind
, parm_stmts
);
12559 for (parm
= DECL_ARGUMENTS (current_function_decl
);
12560 parm
; parm
= DECL_CHAIN (parm
))
12561 if (DECL_HAS_VALUE_EXPR_P (parm
))
12563 DECL_HAS_VALUE_EXPR_P (parm
) = 0;
12564 DECL_IGNORED_P (parm
) = 0;
12570 if (nonlocal_vla_vars
)
12572 /* tree-nested.c may later on call declare_vars (..., true);
12573 which relies on BLOCK_VARS chain to be the tail of the
12574 gimple_bind_vars chain. Ensure we don't violate that
12576 if (gimple_bind_block (outer_bind
)
12577 == DECL_INITIAL (current_function_decl
))
12578 declare_vars (nonlocal_vla_vars
, outer_bind
, true);
12580 BLOCK_VARS (DECL_INITIAL (current_function_decl
))
12581 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl
)),
12582 nonlocal_vla_vars
);
12583 nonlocal_vla_vars
= NULL_TREE
;
12585 delete nonlocal_vlas
;
12586 nonlocal_vlas
= NULL
;
12589 if ((flag_openacc
|| flag_openmp
|| flag_openmp_simd
)
12590 && gimplify_omp_ctxp
)
12592 delete_omp_context (gimplify_omp_ctxp
);
12593 gimplify_omp_ctxp
= NULL
;
12596 pop_gimplify_context (outer_bind
);
12597 gcc_assert (gimplify_ctxp
== NULL
);
12599 if (flag_checking
&& !seen_error ())
12600 verify_gimple_in_seq (gimple_bind_body (outer_bind
));
12602 timevar_pop (TV_TREE_GIMPLIFY
);
12603 input_location
= saved_location
;
12608 typedef char *char_p
; /* For DEF_VEC_P. */
12610 /* Return whether we should exclude FNDECL from instrumentation. */
12613 flag_instrument_functions_exclude_p (tree fndecl
)
12617 v
= (vec
<char_p
> *) flag_instrument_functions_exclude_functions
;
12618 if (v
&& v
->length () > 0)
12624 name
= lang_hooks
.decl_printable_name (fndecl
, 0);
12625 FOR_EACH_VEC_ELT (*v
, i
, s
)
12626 if (strstr (name
, s
) != NULL
)
12630 v
= (vec
<char_p
> *) flag_instrument_functions_exclude_files
;
12631 if (v
&& v
->length () > 0)
12637 name
= DECL_SOURCE_FILE (fndecl
);
12638 FOR_EACH_VEC_ELT (*v
, i
, s
)
12639 if (strstr (name
, s
) != NULL
)
12646 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12647 node for the function we want to gimplify.
12649 Return the sequence of GIMPLE statements corresponding to the body
12653 gimplify_function_tree (tree fndecl
)
12659 gcc_assert (!gimple_body (fndecl
));
12661 if (DECL_STRUCT_FUNCTION (fndecl
))
12662 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
12664 push_struct_function (fndecl
);
12666 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12668 cfun
->curr_properties
|= PROP_gimple_lva
;
12670 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
12672 /* Preliminarily mark non-addressed complex variables as eligible
12673 for promotion to gimple registers. We'll transform their uses
12674 as we find them. */
12675 if ((TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
12676 || TREE_CODE (TREE_TYPE (parm
)) == VECTOR_TYPE
)
12677 && !TREE_THIS_VOLATILE (parm
)
12678 && !needs_to_live_in_memory (parm
))
12679 DECL_GIMPLE_REG_P (parm
) = 1;
12682 ret
= DECL_RESULT (fndecl
);
12683 if ((TREE_CODE (TREE_TYPE (ret
)) == COMPLEX_TYPE
12684 || TREE_CODE (TREE_TYPE (ret
)) == VECTOR_TYPE
)
12685 && !needs_to_live_in_memory (ret
))
12686 DECL_GIMPLE_REG_P (ret
) = 1;
12688 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS
))
12689 asan_poisoned_variables
= new hash_set
<tree
> ();
12690 bind
= gimplify_body (fndecl
, true);
12691 if (asan_poisoned_variables
)
12693 delete asan_poisoned_variables
;
12694 asan_poisoned_variables
= NULL
;
12697 /* The tree body of the function is no longer needed, replace it
12698 with the new GIMPLE body. */
12700 gimple_seq_add_stmt (&seq
, bind
);
12701 gimple_set_body (fndecl
, seq
);
12703 /* If we're instrumenting function entry/exit, then prepend the call to
12704 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12705 catch the exit hook. */
12706 /* ??? Add some way to ignore exceptions for this TFE. */
12707 if (flag_instrument_function_entry_exit
12708 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl
)
12709 /* Do not instrument extern inline functions. */
12710 && !(DECL_DECLARED_INLINE_P (fndecl
)
12711 && DECL_EXTERNAL (fndecl
)
12712 && DECL_DISREGARD_INLINE_LIMITS (fndecl
))
12713 && !flag_instrument_functions_exclude_p (fndecl
))
12718 gimple_seq cleanup
= NULL
, body
= NULL
;
12722 x
= builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS
);
12723 call
= gimple_build_call (x
, 1, integer_zero_node
);
12724 tmp_var
= create_tmp_var (ptr_type_node
, "return_addr");
12725 gimple_call_set_lhs (call
, tmp_var
);
12726 gimplify_seq_add_stmt (&cleanup
, call
);
12727 x
= builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT
);
12728 call
= gimple_build_call (x
, 2,
12729 build_fold_addr_expr (current_function_decl
),
12731 gimplify_seq_add_stmt (&cleanup
, call
);
12732 tf
= gimple_build_try (seq
, cleanup
, GIMPLE_TRY_FINALLY
);
12734 x
= builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS
);
12735 call
= gimple_build_call (x
, 1, integer_zero_node
);
12736 tmp_var
= create_tmp_var (ptr_type_node
, "return_addr");
12737 gimple_call_set_lhs (call
, tmp_var
);
12738 gimplify_seq_add_stmt (&body
, call
);
12739 x
= builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER
);
12740 call
= gimple_build_call (x
, 2,
12741 build_fold_addr_expr (current_function_decl
),
12743 gimplify_seq_add_stmt (&body
, call
);
12744 gimplify_seq_add_stmt (&body
, tf
);
12745 new_bind
= gimple_build_bind (NULL
, body
, NULL
);
12747 /* Replace the current function body with the body
12748 wrapped in the try/finally TF. */
12750 gimple_seq_add_stmt (&seq
, new_bind
);
12751 gimple_set_body (fndecl
, seq
);
12755 if (sanitize_flags_p (SANITIZE_THREAD
))
12757 gcall
*call
= gimple_build_call_internal (IFN_TSAN_FUNC_EXIT
, 0);
12758 gimple
*tf
= gimple_build_try (seq
, call
, GIMPLE_TRY_FINALLY
);
12759 gbind
*new_bind
= gimple_build_bind (NULL
, tf
, NULL
);
12760 /* Replace the current function body with the body
12761 wrapped in the try/finally TF. */
12763 gimple_seq_add_stmt (&seq
, new_bind
);
12764 gimple_set_body (fndecl
, seq
);
12767 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
12768 cfun
->curr_properties
|= PROP_gimple_any
;
12772 dump_function (TDI_gimple
, fndecl
);
12775 /* Return a dummy expression of type TYPE in order to keep going after an
12779 dummy_object (tree type
)
12781 tree t
= build_int_cst (build_pointer_type (type
), 0);
12782 return build2 (MEM_REF
, type
, t
, t
);
12785 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12786 builtin function, but a very special sort of operator. */
12788 enum gimplify_status
12789 gimplify_va_arg_expr (tree
*expr_p
, gimple_seq
*pre_p
,
12790 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
12792 tree promoted_type
, have_va_type
;
12793 tree valist
= TREE_OPERAND (*expr_p
, 0);
12794 tree type
= TREE_TYPE (*expr_p
);
12795 tree t
, tag
, aptag
;
12796 location_t loc
= EXPR_LOCATION (*expr_p
);
12798 /* Verify that valist is of the proper type. */
12799 have_va_type
= TREE_TYPE (valist
);
12800 if (have_va_type
== error_mark_node
)
12802 have_va_type
= targetm
.canonical_va_list_type (have_va_type
);
12803 if (have_va_type
== NULL_TREE
12804 && POINTER_TYPE_P (TREE_TYPE (valist
)))
12805 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
12807 = targetm
.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist
)));
12808 gcc_assert (have_va_type
!= NULL_TREE
);
12810 /* Generate a diagnostic for requesting data of a type that cannot
12811 be passed through `...' due to type promotion at the call site. */
12812 if ((promoted_type
= lang_hooks
.types
.type_promotes_to (type
))
12815 static bool gave_help
;
12817 /* Use the expansion point to handle cases such as passing bool (defined
12818 in a system header) through `...'. */
12819 source_location xloc
12820 = expansion_point_location_if_in_system_header (loc
);
12822 /* Unfortunately, this is merely undefined, rather than a constraint
12823 violation, so we cannot make this an error. If this call is never
12824 executed, the program is still strictly conforming. */
12825 warned
= warning_at (xloc
, 0,
12826 "%qT is promoted to %qT when passed through %<...%>",
12827 type
, promoted_type
);
12828 if (!gave_help
&& warned
)
12831 inform (xloc
, "(so you should pass %qT not %qT to %<va_arg%>)",
12832 promoted_type
, type
);
12835 /* We can, however, treat "undefined" any way we please.
12836 Call abort to encourage the user to fix the program. */
12838 inform (xloc
, "if this code is reached, the program will abort");
12839 /* Before the abort, allow the evaluation of the va_list
12840 expression to exit or longjmp. */
12841 gimplify_and_add (valist
, pre_p
);
12842 t
= build_call_expr_loc (loc
,
12843 builtin_decl_implicit (BUILT_IN_TRAP
), 0);
12844 gimplify_and_add (t
, pre_p
);
12846 /* This is dead code, but go ahead and finish so that the
12847 mode of the result comes out right. */
12848 *expr_p
= dummy_object (type
);
12849 return GS_ALL_DONE
;
12852 tag
= build_int_cst (build_pointer_type (type
), 0);
12853 aptag
= build_int_cst (TREE_TYPE (valist
), 0);
12855 *expr_p
= build_call_expr_internal_loc (loc
, IFN_VA_ARG
, type
, 3,
12856 valist
, tag
, aptag
);
12858 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12859 needs to be expanded. */
12860 cfun
->curr_properties
&= ~PROP_gimple_lva
;
12865 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12867 DST/SRC are the destination and source respectively. You can pass
12868 ungimplified trees in DST or SRC, in which case they will be
12869 converted to a gimple operand if necessary.
12871 This function returns the newly created GIMPLE_ASSIGN tuple. */
12874 gimplify_assign (tree dst
, tree src
, gimple_seq
*seq_p
)
12876 tree t
= build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
12877 gimplify_and_add (t
, seq_p
);
12879 return gimple_seq_last_stmt (*seq_p
);
12883 gimplify_hasher::hash (const elt_t
*p
)
12886 return iterative_hash_expr (t
, 0);
12890 gimplify_hasher::equal (const elt_t
*p1
, const elt_t
*p2
)
12894 enum tree_code code
= TREE_CODE (t1
);
12896 if (TREE_CODE (t2
) != code
12897 || TREE_TYPE (t1
) != TREE_TYPE (t2
))
12900 if (!operand_equal_p (t1
, t2
, 0))
12903 /* Only allow them to compare equal if they also hash equal; otherwise
12904 results are nondeterminate, and we fail bootstrap comparison. */
12905 gcc_checking_assert (hash (p1
) == hash (p2
));