1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
30 #include "stringpool.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
35 #include "tree-inline.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
41 #include "langhooks.h"
42 #include "gimple-low.h"
43 #include "gomp-constants.h"
46 /* The object of this pass is to lower the representation of a set of nested
47 functions in order to expose all of the gory details of the various
48 nonlocal references. We want to do this sooner rather than later, in
49 order to give us more freedom in emitting all of the functions in question.
51 Back in olden times, when gcc was young, we developed an insanely
52 complicated scheme whereby variables which were referenced nonlocally
53 were forced to live in the stack of the declaring function, and then
54 the nested functions magically discovered where these variables were
55 placed. In order for this scheme to function properly, it required
56 that the outer function be partially expanded, then we switch to
57 compiling the inner function, and once done with those we switch back
58 to compiling the outer function. Such delicate ordering requirements
59 makes it difficult to do whole translation unit optimizations
60 involving such functions.
62 The implementation here is much more direct. Everything that can be
63 referenced by an inner function is a member of an explicitly created
64 structure herein called the "nonlocal frame struct". The incoming
65 static chain for a nested function is a pointer to this struct in
66 the parent. In this way, we settle on known offsets from a known
67 base, and so are decoupled from the logic that places objects in the
68 function's stack frame. More importantly, we don't have to wait for
69 that to happen -- since the compilation of the inner function is no
70 longer tied to a real stack frame, the nonlocal frame struct can be
71 allocated anywhere. Which means that the outer function is now
74 Theory of operation here is very simple. Iterate over all the
75 statements in all the functions (depth first) several times,
76 allocating structures and fields on demand. In general we want to
77 examine inner functions first, so that we can avoid making changes
78 to outer functions which are unnecessary.
80 The order of the passes matters a bit, in that later passes will be
81 skipped if it is discovered that the functions don't actually interact
82 at all. That is, they're nested in the lexical sense but could have
83 been written as independent functions without change. */
88 struct nesting_info
*outer
;
89 struct nesting_info
*inner
;
90 struct nesting_info
*next
;
92 hash_map
<tree
, tree
> *field_map
;
93 hash_map
<tree
, tree
> *var_map
;
94 hash_set
<tree
*> *mem_refs
;
95 bitmap suppress_expansion
;
98 tree new_local_var_chain
;
106 bool any_parm_remapped
;
107 bool any_tramp_created
;
108 bool any_descr_created
;
109 char static_chain_added
;
113 /* Iterate over the nesting tree, starting with ROOT, depth first. */
115 static inline struct nesting_info
*
116 iter_nestinfo_start (struct nesting_info
*root
)
123 static inline struct nesting_info
*
124 iter_nestinfo_next (struct nesting_info
*node
)
127 return iter_nestinfo_start (node
->next
);
131 #define FOR_EACH_NEST_INFO(I, ROOT) \
132 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
134 /* Obstack used for the bitmaps in the struct above. */
135 static struct bitmap_obstack nesting_info_bitmap_obstack
;
138 /* We're working in so many different function contexts simultaneously,
139 that create_tmp_var is dangerous. Prevent mishap. */
140 #define create_tmp_var cant_use_create_tmp_var_here_dummy
142 /* Like create_tmp_var, except record the variable for registration at
143 the given nesting level. */
146 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
150 /* If the type is of variable size or a type which must be created by the
151 frontend, something is wrong. Note that we explicitly allow
152 incomplete types here, since we create them ourselves here. */
153 gcc_assert (!TREE_ADDRESSABLE (type
));
154 gcc_assert (!TYPE_SIZE_UNIT (type
)
155 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
157 tmp_var
= create_tmp_var_raw (type
, prefix
);
158 DECL_CONTEXT (tmp_var
) = info
->context
;
159 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
160 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
161 if (TREE_CODE (type
) == COMPLEX_TYPE
162 || TREE_CODE (type
) == VECTOR_TYPE
)
163 DECL_GIMPLE_REG_P (tmp_var
) = 1;
165 info
->new_local_var_chain
= tmp_var
;
170 /* Take the address of EXP to be used within function CONTEXT.
171 Mark it for addressability as necessary. */
174 build_addr (tree exp
)
176 mark_addressable (exp
);
177 return build_fold_addr_expr (exp
);
180 /* Insert FIELD into TYPE, sorted by alignment requirements. */
183 insert_field_into_struct (tree type
, tree field
)
187 DECL_CONTEXT (field
) = type
;
189 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
190 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
193 DECL_CHAIN (field
) = *p
;
196 /* Set correct alignment for frame struct type. */
197 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
198 SET_TYPE_ALIGN (type
, DECL_ALIGN (field
));
201 /* Build or return the RECORD_TYPE that describes the frame state that is
202 shared between INFO->CONTEXT and its nested functions. This record will
203 not be complete until finalize_nesting_tree; up until that point we'll
204 be adding fields as necessary.
206 We also build the DECL that represents this frame in the function. */
209 get_frame_type (struct nesting_info
*info
)
211 tree type
= info
->frame_type
;
216 type
= make_node (RECORD_TYPE
);
218 name
= concat ("FRAME.",
219 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
221 TYPE_NAME (type
) = get_identifier (name
);
224 info
->frame_type
= type
;
225 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
226 DECL_NONLOCAL_FRAME (info
->frame_decl
) = 1;
228 /* ??? Always make it addressable for now, since it is meant to
229 be pointed to by the static chain pointer. This pessimizes
230 when it turns out that no static chains are needed because
231 the nested functions referencing non-local variables are not
232 reachable, but the true pessimization is to create the non-
233 local frame structure in the first place. */
234 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
239 /* Return true if DECL should be referenced by pointer in the non-local
243 use_pointer_in_frame (tree decl
)
245 if (TREE_CODE (decl
) == PARM_DECL
)
247 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
248 sized decls, and inefficient to copy large aggregates. Don't bother
249 moving anything but scalar variables. */
250 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
254 /* Variable sized types make things "interesting" in the frame. */
255 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
259 /* Given DECL, a non-locally accessed variable, find or create a field
260 in the non-local frame structure for the given nesting context. */
263 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
264 enum insert_option insert
)
266 if (insert
== NO_INSERT
)
268 tree
*slot
= info
->field_map
->get (decl
);
269 return slot
? *slot
: NULL_TREE
;
272 tree
*slot
= &info
->field_map
->get_or_insert (decl
);
275 tree field
= make_node (FIELD_DECL
);
276 DECL_NAME (field
) = DECL_NAME (decl
);
278 if (use_pointer_in_frame (decl
))
280 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
281 SET_DECL_ALIGN (field
, TYPE_ALIGN (TREE_TYPE (field
)));
282 DECL_NONADDRESSABLE_P (field
) = 1;
286 TREE_TYPE (field
) = TREE_TYPE (decl
);
287 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
288 SET_DECL_ALIGN (field
, DECL_ALIGN (decl
));
289 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
290 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
291 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
292 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
295 insert_field_into_struct (get_frame_type (info
), field
);
298 if (TREE_CODE (decl
) == PARM_DECL
)
299 info
->any_parm_remapped
= true;
305 /* Build or return the variable that holds the static chain within
306 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
309 get_chain_decl (struct nesting_info
*info
)
311 tree decl
= info
->chain_decl
;
317 type
= get_frame_type (info
->outer
);
318 type
= build_pointer_type (type
);
320 /* Note that this variable is *not* entered into any BIND_EXPR;
321 the construction of this variable is handled specially in
322 expand_function_start and initialize_inlined_parameters.
323 Note also that it's represented as a parameter. This is more
324 close to the truth, since the initial value does come from
326 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
327 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
328 DECL_ARTIFICIAL (decl
) = 1;
329 DECL_IGNORED_P (decl
) = 1;
330 TREE_USED (decl
) = 1;
331 DECL_CONTEXT (decl
) = info
->context
;
332 DECL_ARG_TYPE (decl
) = type
;
334 /* Tell tree-inline.c that we never write to this variable, so
335 it can copy-prop the replacement value immediately. */
336 TREE_READONLY (decl
) = 1;
338 info
->chain_decl
= decl
;
341 && (dump_flags
& TDF_DETAILS
)
342 && !DECL_STATIC_CHAIN (info
->context
))
343 fprintf (dump_file
, "Setting static-chain for %s\n",
344 lang_hooks
.decl_printable_name (info
->context
, 2));
346 DECL_STATIC_CHAIN (info
->context
) = 1;
351 /* Build or return the field within the non-local frame state that holds
352 the static chain for INFO->CONTEXT. This is the way to walk back up
353 multiple nesting levels. */
356 get_chain_field (struct nesting_info
*info
)
358 tree field
= info
->chain_field
;
362 tree type
= build_pointer_type (get_frame_type (info
->outer
));
364 field
= make_node (FIELD_DECL
);
365 DECL_NAME (field
) = get_identifier ("__chain");
366 TREE_TYPE (field
) = type
;
367 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
368 DECL_NONADDRESSABLE_P (field
) = 1;
370 insert_field_into_struct (get_frame_type (info
), field
);
372 info
->chain_field
= field
;
375 && (dump_flags
& TDF_DETAILS
)
376 && !DECL_STATIC_CHAIN (info
->context
))
377 fprintf (dump_file
, "Setting static-chain for %s\n",
378 lang_hooks
.decl_printable_name (info
->context
, 2));
380 DECL_STATIC_CHAIN (info
->context
) = 1;
385 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
388 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
393 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
394 gimple_call_set_lhs (call
, t
);
395 if (! gsi_end_p (*gsi
))
396 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
397 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
403 /* Copy EXP into a temporary. Allocate the temporary in the context of
404 INFO and insert the initialization statement before GSI. */
407 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
412 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
413 stmt
= gimple_build_assign (t
, exp
);
414 if (! gsi_end_p (*gsi
))
415 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
416 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
422 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
425 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
426 gimple_stmt_iterator
*gsi
)
428 if (is_gimple_val (exp
))
431 return init_tmp_var (info
, exp
, gsi
);
434 /* Similarly, but copy from the temporary and insert the statement
435 after the iterator. */
438 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
443 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
444 stmt
= gimple_build_assign (exp
, t
);
445 if (! gsi_end_p (*gsi
))
446 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
447 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
452 /* Build or return the type used to represent a nested function trampoline. */
454 static GTY(()) tree trampoline_type
;
457 get_trampoline_type (struct nesting_info
*info
)
459 unsigned align
, size
;
463 return trampoline_type
;
465 align
= TRAMPOLINE_ALIGNMENT
;
466 size
= TRAMPOLINE_SIZE
;
468 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
469 then allocate extra space so that we can do dynamic alignment. */
470 if (align
> STACK_BOUNDARY
)
472 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
473 align
= STACK_BOUNDARY
;
476 t
= build_index_type (size_int (size
- 1));
477 t
= build_array_type (char_type_node
, t
);
478 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
479 FIELD_DECL
, get_identifier ("__data"), t
);
480 SET_DECL_ALIGN (t
, align
);
481 DECL_USER_ALIGN (t
) = 1;
483 trampoline_type
= make_node (RECORD_TYPE
);
484 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
485 TYPE_FIELDS (trampoline_type
) = t
;
486 layout_type (trampoline_type
);
487 DECL_CONTEXT (t
) = trampoline_type
;
489 return trampoline_type
;
492 /* Build or return the type used to represent a nested function descriptor. */
494 static GTY(()) tree descriptor_type
;
497 get_descriptor_type (struct nesting_info
*info
)
499 /* The base alignment is that of a function. */
500 const unsigned align
= FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY
);
504 return descriptor_type
;
506 t
= build_index_type (integer_one_node
);
507 t
= build_array_type (ptr_type_node
, t
);
508 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
509 FIELD_DECL
, get_identifier ("__data"), t
);
510 SET_DECL_ALIGN (t
, MAX (TYPE_ALIGN (ptr_type_node
), align
));
511 DECL_USER_ALIGN (t
) = 1;
513 descriptor_type
= make_node (RECORD_TYPE
);
514 TYPE_NAME (descriptor_type
) = get_identifier ("__builtin_descriptor");
515 TYPE_FIELDS (descriptor_type
) = t
;
516 layout_type (descriptor_type
);
517 DECL_CONTEXT (t
) = descriptor_type
;
519 return descriptor_type
;
522 /* Given DECL, a nested function, find or create an element in the
523 var map for this function. */
526 lookup_element_for_decl (struct nesting_info
*info
, tree decl
,
527 enum insert_option insert
)
529 if (insert
== NO_INSERT
)
531 tree
*slot
= info
->var_map
->get (decl
);
532 return slot
? *slot
: NULL_TREE
;
535 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
537 *slot
= build_tree_list (NULL_TREE
, NULL_TREE
);
542 /* Given DECL, a nested function, create a field in the non-local
543 frame structure for this function. */
546 create_field_for_decl (struct nesting_info
*info
, tree decl
, tree type
)
548 tree field
= make_node (FIELD_DECL
);
549 DECL_NAME (field
) = DECL_NAME (decl
);
550 TREE_TYPE (field
) = type
;
551 TREE_ADDRESSABLE (field
) = 1;
552 insert_field_into_struct (get_frame_type (info
), field
);
556 /* Given DECL, a nested function, find or create a field in the non-local
557 frame structure for a trampoline for this function. */
560 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
561 enum insert_option insert
)
565 elt
= lookup_element_for_decl (info
, decl
, insert
);
569 field
= TREE_PURPOSE (elt
);
571 if (!field
&& insert
== INSERT
)
573 field
= create_field_for_decl (info
, decl
, get_trampoline_type (info
));
574 TREE_PURPOSE (elt
) = field
;
575 info
->any_tramp_created
= true;
581 /* Given DECL, a nested function, find or create a field in the non-local
582 frame structure for a descriptor for this function. */
585 lookup_descr_for_decl (struct nesting_info
*info
, tree decl
,
586 enum insert_option insert
)
590 elt
= lookup_element_for_decl (info
, decl
, insert
);
594 field
= TREE_VALUE (elt
);
596 if (!field
&& insert
== INSERT
)
598 field
= create_field_for_decl (info
, decl
, get_descriptor_type (info
));
599 TREE_VALUE (elt
) = field
;
600 info
->any_descr_created
= true;
606 /* Build or return the field within the non-local frame state that holds
607 the non-local goto "jmp_buf". The buffer itself is maintained by the
608 rtl middle-end as dynamic stack space is allocated. */
611 get_nl_goto_field (struct nesting_info
*info
)
613 tree field
= info
->nl_goto_field
;
619 /* For __builtin_nonlocal_goto, we need N words. The first is the
620 frame pointer, the rest is for the target's stack pointer save
621 area. The number of words is controlled by STACK_SAVEAREA_MODE;
622 not the best interface, but it'll do for now. */
623 if (Pmode
== ptr_mode
)
624 type
= ptr_type_node
;
626 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
629 = as_a
<scalar_int_mode
> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
630 size
= GET_MODE_SIZE (mode
);
631 size
= size
/ GET_MODE_SIZE (Pmode
);
634 type
= build_array_type
635 (type
, build_index_type (size_int (size
)));
637 field
= make_node (FIELD_DECL
);
638 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
639 TREE_TYPE (field
) = type
;
640 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
641 TREE_ADDRESSABLE (field
) = 1;
643 insert_field_into_struct (get_frame_type (info
), field
);
645 info
->nl_goto_field
= field
;
651 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
654 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
655 struct nesting_info
*info
, gimple_seq
*pseq
)
657 struct walk_stmt_info wi
;
659 memset (&wi
, 0, sizeof (wi
));
662 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
666 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
669 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
670 struct nesting_info
*info
)
672 gimple_seq body
= gimple_body (info
->context
);
673 walk_body (callback_stmt
, callback_op
, info
, &body
);
674 gimple_set_body (info
->context
, body
);
677 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
680 walk_gimple_omp_for (gomp_for
*for_stmt
,
681 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
682 struct nesting_info
*info
)
684 struct walk_stmt_info wi
;
689 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
692 memset (&wi
, 0, sizeof (wi
));
694 wi
.gsi
= gsi_last (seq
);
696 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
699 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
703 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
708 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
711 t
= gimple_omp_for_incr (for_stmt
, i
);
712 gcc_assert (BINARY_CLASS_P (t
));
714 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
717 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
720 seq
= gsi_seq (wi
.gsi
);
721 if (!gimple_seq_empty_p (seq
))
723 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
724 annotate_all_with_location (seq
, gimple_location (for_stmt
));
725 gimple_seq_add_seq (&pre_body
, seq
);
726 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
730 /* Similarly for ROOT and all functions nested underneath, depth first. */
733 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
734 struct nesting_info
*root
)
736 struct nesting_info
*n
;
737 FOR_EACH_NEST_INFO (n
, root
)
738 walk_function (callback_stmt
, callback_op
, n
);
742 /* We have to check for a fairly pathological case. The operands of function
743 nested function are to be interpreted in the context of the enclosing
744 function. So if any are variably-sized, they will get remapped when the
745 enclosing function is inlined. But that remapping would also have to be
746 done in the types of the PARM_DECLs of the nested function, meaning the
747 argument types of that function will disagree with the arguments in the
748 calls to that function. So we'd either have to make a copy of the nested
749 function corresponding to each time the enclosing function was inlined or
750 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
751 function. The former is not practical. The latter would still require
752 detecting this case to know when to add the conversions. So, for now at
753 least, we don't inline such an enclosing function.
755 We have to do that check recursively, so here return indicating whether
756 FNDECL has such a nested function. ORIG_FN is the function we were
757 trying to inline to use for checking whether any argument is variably
758 modified by anything in it.
760 It would be better to do this in tree-inline.c so that we could give
761 the appropriate warning for why a function can't be inlined, but that's
762 too late since the nesting structure has already been flattened and
763 adding a flag just to record this fact seems a waste of a flag. */
766 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
768 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
771 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
773 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
774 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
777 if (check_for_nested_with_variably_modified (cgn
->decl
,
785 /* Construct our local datastructure describing the function nesting
786 tree rooted by CGN. */
788 static struct nesting_info
*
789 create_nesting_tree (struct cgraph_node
*cgn
)
791 struct nesting_info
*info
= XCNEW (struct nesting_info
);
792 info
->field_map
= new hash_map
<tree
, tree
>;
793 info
->var_map
= new hash_map
<tree
, tree
>;
794 info
->mem_refs
= new hash_set
<tree
*>;
795 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
796 info
->context
= cgn
->decl
;
798 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
800 struct nesting_info
*sub
= create_nesting_tree (cgn
);
802 sub
->next
= info
->inner
;
806 /* See discussion at check_for_nested_with_variably_modified for a
807 discussion of why this has to be here. */
808 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
809 DECL_UNINLINABLE (info
->context
) = true;
814 /* Return an expression computing the static chain for TARGET_CONTEXT
815 from INFO->CONTEXT. Insert any necessary computations before TSI. */
818 get_static_chain (struct nesting_info
*info
, tree target_context
,
819 gimple_stmt_iterator
*gsi
)
821 struct nesting_info
*i
;
824 if (info
->context
== target_context
)
826 x
= build_addr (info
->frame_decl
);
827 info
->static_chain_added
|= 1;
831 x
= get_chain_decl (info
);
832 info
->static_chain_added
|= 2;
834 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
836 tree field
= get_chain_field (i
);
838 x
= build_simple_mem_ref (x
);
839 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
840 x
= init_tmp_var (info
, x
, gsi
);
848 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
849 frame as seen from INFO->CONTEXT. Insert any necessary computations
853 get_frame_field (struct nesting_info
*info
, tree target_context
,
854 tree field
, gimple_stmt_iterator
*gsi
)
856 struct nesting_info
*i
;
859 if (info
->context
== target_context
)
861 /* Make sure frame_decl gets created. */
862 (void) get_frame_type (info
);
863 x
= info
->frame_decl
;
864 info
->static_chain_added
|= 1;
868 x
= get_chain_decl (info
);
869 info
->static_chain_added
|= 2;
871 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
873 tree field
= get_chain_field (i
);
875 x
= build_simple_mem_ref (x
);
876 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
877 x
= init_tmp_var (info
, x
, gsi
);
880 x
= build_simple_mem_ref (x
);
883 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
887 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
889 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
890 in the nested function with DECL_VALUE_EXPR set to reference the true
891 variable in the parent function. This is used both for debug info
892 and in OMP lowering. */
895 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
898 struct nesting_info
*i
;
899 tree x
, field
, new_decl
;
901 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
906 target_context
= decl_function_context (decl
);
908 /* A copy of the code in get_frame_field, but without the temporaries. */
909 if (info
->context
== target_context
)
911 /* Make sure frame_decl gets created. */
912 (void) get_frame_type (info
);
913 x
= info
->frame_decl
;
915 info
->static_chain_added
|= 1;
919 x
= get_chain_decl (info
);
920 info
->static_chain_added
|= 2;
921 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
923 field
= get_chain_field (i
);
924 x
= build_simple_mem_ref (x
);
925 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
927 x
= build_simple_mem_ref (x
);
930 field
= lookup_field_for_decl (i
, decl
, INSERT
);
931 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
932 if (use_pointer_in_frame (decl
))
933 x
= build_simple_mem_ref (x
);
935 /* ??? We should be remapping types as well, surely. */
936 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
937 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
938 DECL_CONTEXT (new_decl
) = info
->context
;
939 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
940 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
941 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
942 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
943 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
944 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
945 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
946 if ((TREE_CODE (decl
) == PARM_DECL
947 || TREE_CODE (decl
) == RESULT_DECL
949 && DECL_BY_REFERENCE (decl
))
950 DECL_BY_REFERENCE (new_decl
) = 1;
952 SET_DECL_VALUE_EXPR (new_decl
, x
);
953 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
956 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
957 info
->debug_var_chain
= new_decl
;
960 && info
->context
!= target_context
961 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
962 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
968 /* Callback for walk_gimple_stmt, rewrite all references to VAR
969 and PARM_DECLs that belong to outer functions.
971 The rewrite will involve some number of structure accesses back up
972 the static chain. E.g. for a variable FOO up one nesting level it'll
973 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
974 indirections apply to decls for which use_pointer_in_frame is true. */
977 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
979 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
980 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
984 switch (TREE_CODE (t
))
987 /* Non-automatic variables are never processed. */
988 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
993 if (decl_function_context (t
) != info
->context
)
998 x
= get_nonlocal_debug_decl (info
, t
);
999 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1001 tree target_context
= decl_function_context (t
);
1002 struct nesting_info
*i
;
1003 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
1005 x
= lookup_field_for_decl (i
, t
, INSERT
);
1006 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1007 if (use_pointer_in_frame (t
))
1009 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1010 x
= build_simple_mem_ref (x
);
1017 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1019 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1027 /* We're taking the address of a label from a parent function, but
1028 this is not itself a non-local goto. Mark the label such that it
1029 will not be deleted, much as we would with a label address in
1031 if (decl_function_context (t
) != info
->context
)
1032 FORCED_LABEL (t
) = 1;
1037 bool save_val_only
= wi
->val_only
;
1039 wi
->val_only
= false;
1041 wi
->changed
= false;
1042 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
1043 wi
->val_only
= true;
1049 /* If we changed anything, we might no longer be directly
1050 referencing a decl. */
1051 save_context
= current_function_decl
;
1052 current_function_decl
= info
->context
;
1053 recompute_tree_invariant_for_addr_expr (t
);
1054 current_function_decl
= save_context
;
1056 /* If the callback converted the address argument in a context
1057 where we only accept variables (and min_invariant, presumably),
1058 then compute the address into a temporary. */
1060 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1070 case ARRAY_RANGE_REF
:
1072 /* Go down this entire nest and just look at the final prefix and
1073 anything that describes the references. Otherwise, we lose track
1074 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1075 wi
->val_only
= true;
1077 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1079 if (TREE_CODE (t
) == COMPONENT_REF
)
1080 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1082 else if (TREE_CODE (t
) == ARRAY_REF
1083 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1085 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1087 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1089 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1093 wi
->val_only
= false;
1094 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1097 case VIEW_CONVERT_EXPR
:
1098 /* Just request to look at the subtrees, leaving val_only and lhs
1099 untouched. This might actually be for !val_only + lhs, in which
1100 case we don't want to force a replacement by a temporary. */
1105 if (!IS_TYPE_OR_DECL_P (t
))
1108 wi
->val_only
= true;
1117 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1118 struct walk_stmt_info
*);
1120 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1121 and PARM_DECLs that belong to outer functions. */
1124 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1126 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1127 bool need_chain
= false, need_stmts
= false;
1130 bitmap new_suppress
;
1132 new_suppress
= BITMAP_GGC_ALLOC ();
1133 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1135 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1137 switch (OMP_CLAUSE_CODE (clause
))
1139 case OMP_CLAUSE_REDUCTION
:
1140 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1142 goto do_decl_clause
;
1144 case OMP_CLAUSE_LASTPRIVATE
:
1145 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1147 goto do_decl_clause
;
1149 case OMP_CLAUSE_LINEAR
:
1150 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1152 wi
->val_only
= true;
1154 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
),
1156 goto do_decl_clause
;
1158 case OMP_CLAUSE_PRIVATE
:
1159 case OMP_CLAUSE_FIRSTPRIVATE
:
1160 case OMP_CLAUSE_COPYPRIVATE
:
1161 case OMP_CLAUSE_SHARED
:
1162 case OMP_CLAUSE_TO_DECLARE
:
1163 case OMP_CLAUSE_LINK
:
1164 case OMP_CLAUSE_USE_DEVICE_PTR
:
1165 case OMP_CLAUSE_IS_DEVICE_PTR
:
1167 decl
= OMP_CLAUSE_DECL (clause
);
1169 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1171 if (decl_function_context (decl
) != info
->context
)
1173 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1174 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1175 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1176 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1177 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1182 case OMP_CLAUSE_SCHEDULE
:
1183 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1186 case OMP_CLAUSE_FINAL
:
1188 case OMP_CLAUSE_NUM_THREADS
:
1189 case OMP_CLAUSE_DEPEND
:
1190 case OMP_CLAUSE_DEVICE
:
1191 case OMP_CLAUSE_NUM_TEAMS
:
1192 case OMP_CLAUSE_THREAD_LIMIT
:
1193 case OMP_CLAUSE_SAFELEN
:
1194 case OMP_CLAUSE_SIMDLEN
:
1195 case OMP_CLAUSE_PRIORITY
:
1196 case OMP_CLAUSE_GRAINSIZE
:
1197 case OMP_CLAUSE_NUM_TASKS
:
1198 case OMP_CLAUSE_HINT
:
1199 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1200 case OMP_CLAUSE_NUM_GANGS
:
1201 case OMP_CLAUSE_NUM_WORKERS
:
1202 case OMP_CLAUSE_VECTOR_LENGTH
:
1203 case OMP_CLAUSE_GANG
:
1204 case OMP_CLAUSE_WORKER
:
1205 case OMP_CLAUSE_VECTOR
:
1206 case OMP_CLAUSE_ASYNC
:
1207 case OMP_CLAUSE_WAIT
:
1208 /* Several OpenACC clauses have optional arguments. Check if they
1210 if (OMP_CLAUSE_OPERAND (clause
, 0))
1212 wi
->val_only
= true;
1214 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1218 /* The gang clause accepts two arguments. */
1219 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
1220 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
1222 wi
->val_only
= true;
1224 convert_nonlocal_reference_op
1225 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
1229 case OMP_CLAUSE_DIST_SCHEDULE
:
1230 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1232 wi
->val_only
= true;
1234 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1239 case OMP_CLAUSE_MAP
:
1241 case OMP_CLAUSE_FROM
:
1242 if (OMP_CLAUSE_SIZE (clause
))
1244 wi
->val_only
= true;
1246 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause
),
1249 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1250 goto do_decl_clause
;
1251 wi
->val_only
= true;
1253 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_nonlocal_reference_op
,
1257 case OMP_CLAUSE_ALIGNED
:
1258 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1260 wi
->val_only
= true;
1262 convert_nonlocal_reference_op
1263 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1265 /* Like do_decl_clause, but don't add any suppression. */
1266 decl
= OMP_CLAUSE_DECL (clause
);
1268 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1270 if (decl_function_context (decl
) != info
->context
)
1272 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1273 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1278 case OMP_CLAUSE_NOWAIT
:
1279 case OMP_CLAUSE_ORDERED
:
1280 case OMP_CLAUSE_DEFAULT
:
1281 case OMP_CLAUSE_COPYIN
:
1282 case OMP_CLAUSE_COLLAPSE
:
1283 case OMP_CLAUSE_TILE
:
1284 case OMP_CLAUSE_UNTIED
:
1285 case OMP_CLAUSE_MERGEABLE
:
1286 case OMP_CLAUSE_PROC_BIND
:
1287 case OMP_CLAUSE_NOGROUP
:
1288 case OMP_CLAUSE_THREADS
:
1289 case OMP_CLAUSE_SIMD
:
1290 case OMP_CLAUSE_DEFAULTMAP
:
1291 case OMP_CLAUSE_SEQ
:
1292 case OMP_CLAUSE_INDEPENDENT
:
1293 case OMP_CLAUSE_AUTO
:
1296 /* The following clause belongs to the OpenACC cache directive, which
1297 is discarded during gimplification. */
1298 case OMP_CLAUSE__CACHE_
:
1299 /* The following clauses are only allowed in the OpenMP declare simd
1300 directive, so not seen here. */
1301 case OMP_CLAUSE_UNIFORM
:
1302 case OMP_CLAUSE_INBRANCH
:
1303 case OMP_CLAUSE_NOTINBRANCH
:
1304 /* The following clauses are only allowed on OpenMP cancel and
1305 cancellation point directives, which at this point have already
1306 been lowered into a function call. */
1307 case OMP_CLAUSE_FOR
:
1308 case OMP_CLAUSE_PARALLEL
:
1309 case OMP_CLAUSE_SECTIONS
:
1310 case OMP_CLAUSE_TASKGROUP
:
1311 /* The following clauses are only added during OMP lowering; nested
1312 function decomposition happens before that. */
1313 case OMP_CLAUSE__LOOPTEMP_
:
1314 case OMP_CLAUSE__SIMDUID_
:
1315 case OMP_CLAUSE__GRIDDIM_
:
1316 /* Anything else. */
1322 info
->suppress_expansion
= new_suppress
;
1325 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1326 switch (OMP_CLAUSE_CODE (clause
))
1328 case OMP_CLAUSE_REDUCTION
:
1329 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1332 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1333 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1335 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1336 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1338 walk_body (convert_nonlocal_reference_stmt
,
1339 convert_nonlocal_reference_op
, info
,
1340 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1341 walk_body (convert_nonlocal_reference_stmt
,
1342 convert_nonlocal_reference_op
, info
,
1343 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1344 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1346 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1347 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1352 case OMP_CLAUSE_LASTPRIVATE
:
1353 walk_body (convert_nonlocal_reference_stmt
,
1354 convert_nonlocal_reference_op
, info
,
1355 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1358 case OMP_CLAUSE_LINEAR
:
1359 walk_body (convert_nonlocal_reference_stmt
,
1360 convert_nonlocal_reference_op
, info
,
1361 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1371 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1374 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1376 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1377 type
= TREE_TYPE (type
);
1379 if (TYPE_NAME (type
)
1380 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1381 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1382 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1384 while (POINTER_TYPE_P (type
)
1385 || TREE_CODE (type
) == VECTOR_TYPE
1386 || TREE_CODE (type
) == FUNCTION_TYPE
1387 || TREE_CODE (type
) == METHOD_TYPE
)
1388 type
= TREE_TYPE (type
);
1390 if (TREE_CODE (type
) == ARRAY_TYPE
)
1394 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1395 domain
= TYPE_DOMAIN (type
);
1398 t
= TYPE_MIN_VALUE (domain
);
1399 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1400 && decl_function_context (t
) != info
->context
)
1401 get_nonlocal_debug_decl (info
, t
);
1402 t
= TYPE_MAX_VALUE (domain
);
1403 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1404 && decl_function_context (t
) != info
->context
)
1405 get_nonlocal_debug_decl (info
, t
);
1410 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1414 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1418 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
1420 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1421 && DECL_HAS_VALUE_EXPR_P (var
)
1422 && decl_function_context (var
) != info
->context
)
1423 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1426 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1427 PARM_DECLs that belong to outer functions. This handles statements
1428 that are not handled via the standard recursion done in
1429 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1430 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1431 operands of STMT have been handled by this function. */
1434 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1435 struct walk_stmt_info
*wi
)
1437 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1438 tree save_local_var_chain
;
1439 bitmap save_suppress
;
1440 gimple
*stmt
= gsi_stmt (*gsi
);
1442 switch (gimple_code (stmt
))
1445 /* Don't walk non-local gotos for now. */
1446 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1448 wi
->val_only
= true;
1450 *handled_ops_p
= false;
1455 case GIMPLE_OMP_PARALLEL
:
1456 case GIMPLE_OMP_TASK
:
1457 save_suppress
= info
->suppress_expansion
;
1458 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1462 decl
= get_chain_decl (info
);
1463 c
= build_omp_clause (gimple_location (stmt
),
1464 OMP_CLAUSE_FIRSTPRIVATE
);
1465 OMP_CLAUSE_DECL (c
) = decl
;
1466 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1467 gimple_omp_taskreg_set_clauses (stmt
, c
);
1470 save_local_var_chain
= info
->new_local_var_chain
;
1471 info
->new_local_var_chain
= NULL
;
1473 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1474 info
, gimple_omp_body_ptr (stmt
));
1476 if (info
->new_local_var_chain
)
1477 declare_vars (info
->new_local_var_chain
,
1478 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1480 info
->new_local_var_chain
= save_local_var_chain
;
1481 info
->suppress_expansion
= save_suppress
;
1484 case GIMPLE_OMP_FOR
:
1485 save_suppress
= info
->suppress_expansion
;
1486 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1487 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
1488 convert_nonlocal_reference_stmt
,
1489 convert_nonlocal_reference_op
, info
);
1490 walk_body (convert_nonlocal_reference_stmt
,
1491 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1492 info
->suppress_expansion
= save_suppress
;
1495 case GIMPLE_OMP_SECTIONS
:
1496 save_suppress
= info
->suppress_expansion
;
1497 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1498 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1499 info
, gimple_omp_body_ptr (stmt
));
1500 info
->suppress_expansion
= save_suppress
;
1503 case GIMPLE_OMP_SINGLE
:
1504 save_suppress
= info
->suppress_expansion
;
1505 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1506 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1507 info
, gimple_omp_body_ptr (stmt
));
1508 info
->suppress_expansion
= save_suppress
;
1511 case GIMPLE_OMP_TARGET
:
1512 if (!is_gimple_omp_offloaded (stmt
))
1514 save_suppress
= info
->suppress_expansion
;
1515 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1517 info
->suppress_expansion
= save_suppress
;
1518 walk_body (convert_nonlocal_reference_stmt
,
1519 convert_nonlocal_reference_op
, info
,
1520 gimple_omp_body_ptr (stmt
));
1523 save_suppress
= info
->suppress_expansion
;
1524 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1528 decl
= get_chain_decl (info
);
1529 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1530 OMP_CLAUSE_DECL (c
) = decl
;
1531 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TO
);
1532 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
1533 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1534 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
1537 save_local_var_chain
= info
->new_local_var_chain
;
1538 info
->new_local_var_chain
= NULL
;
1540 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1541 info
, gimple_omp_body_ptr (stmt
));
1543 if (info
->new_local_var_chain
)
1544 declare_vars (info
->new_local_var_chain
,
1545 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1547 info
->new_local_var_chain
= save_local_var_chain
;
1548 info
->suppress_expansion
= save_suppress
;
1551 case GIMPLE_OMP_TEAMS
:
1552 save_suppress
= info
->suppress_expansion
;
1553 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1554 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1555 info
, gimple_omp_body_ptr (stmt
));
1556 info
->suppress_expansion
= save_suppress
;
1559 case GIMPLE_OMP_SECTION
:
1560 case GIMPLE_OMP_MASTER
:
1561 case GIMPLE_OMP_TASKGROUP
:
1562 case GIMPLE_OMP_ORDERED
:
1563 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1564 info
, gimple_omp_body_ptr (stmt
));
1569 gbind
*bind_stmt
= as_a
<gbind
*> (stmt
);
1570 if (!optimize
&& gimple_bind_block (bind_stmt
))
1571 note_nonlocal_block_vlas (info
, gimple_bind_block (bind_stmt
));
1573 for (tree var
= gimple_bind_vars (bind_stmt
); var
; var
= DECL_CHAIN (var
))
1574 if (TREE_CODE (var
) == NAMELIST_DECL
)
1576 /* Adjust decls mentioned in NAMELIST_DECL. */
1577 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
1581 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
1584 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1586 if (decl_function_context (decl
) != info
->context
)
1587 CONSTRUCTOR_ELT (decls
, i
)->value
1588 = get_nonlocal_debug_decl (info
, decl
);
1592 *handled_ops_p
= false;
1596 wi
->val_only
= true;
1598 *handled_ops_p
= false;
1602 /* For every other statement that we are not interested in
1603 handling here, let the walker traverse the operands. */
1604 *handled_ops_p
= false;
1608 /* We have handled all of STMT operands, no need to traverse the operands. */
1609 *handled_ops_p
= true;
1614 /* A subroutine of convert_local_reference. Create a local variable
1615 in the parent function with DECL_VALUE_EXPR set to reference the
1616 field in FRAME. This is used both for debug info and in OMP
1620 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1624 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1628 /* Make sure frame_decl gets created. */
1629 (void) get_frame_type (info
);
1630 x
= info
->frame_decl
;
1631 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1633 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1634 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1635 DECL_CONTEXT (new_decl
) = info
->context
;
1636 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1637 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1638 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1639 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1640 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1641 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1642 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1643 if ((TREE_CODE (decl
) == PARM_DECL
1644 || TREE_CODE (decl
) == RESULT_DECL
1646 && DECL_BY_REFERENCE (decl
))
1647 DECL_BY_REFERENCE (new_decl
) = 1;
1649 SET_DECL_VALUE_EXPR (new_decl
, x
);
1650 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1653 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1654 info
->debug_var_chain
= new_decl
;
1656 /* Do not emit debug info twice. */
1657 DECL_IGNORED_P (decl
) = 1;
1663 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1664 and PARM_DECLs that were referenced by inner nested functions.
1665 The rewrite will be a structure reference to the local frame variable. */
1667 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1670 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1672 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1673 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1674 tree t
= *tp
, field
, x
;
1678 switch (TREE_CODE (t
))
1681 /* Non-automatic variables are never processed. */
1682 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1687 if (decl_function_context (t
) == info
->context
)
1689 /* If we copied a pointer to the frame, then the original decl
1690 is used unchanged in the parent function. */
1691 if (use_pointer_in_frame (t
))
1694 /* No need to transform anything if no child references the
1696 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1701 x
= get_local_debug_decl (info
, t
, field
);
1702 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1703 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1708 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1710 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1718 save_val_only
= wi
->val_only
;
1719 wi
->val_only
= false;
1721 wi
->changed
= false;
1722 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1723 wi
->val_only
= save_val_only
;
1725 /* If we converted anything ... */
1730 /* Then the frame decl is now addressable. */
1731 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1733 save_context
= current_function_decl
;
1734 current_function_decl
= info
->context
;
1735 recompute_tree_invariant_for_addr_expr (t
);
1736 current_function_decl
= save_context
;
1738 /* If we are in a context where we only accept values, then
1739 compute the address into a temporary. */
1741 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1750 case ARRAY_RANGE_REF
:
1752 /* Go down this entire nest and just look at the final prefix and
1753 anything that describes the references. Otherwise, we lose track
1754 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1755 save_val_only
= wi
->val_only
;
1756 wi
->val_only
= true;
1758 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1760 if (TREE_CODE (t
) == COMPONENT_REF
)
1761 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1763 else if (TREE_CODE (t
) == ARRAY_REF
1764 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1766 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1768 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1770 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1774 wi
->val_only
= false;
1775 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1776 wi
->val_only
= save_val_only
;
1780 save_val_only
= wi
->val_only
;
1781 wi
->val_only
= true;
1783 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1785 /* We need to re-fold the MEM_REF as component references as
1786 part of a ADDR_EXPR address are not allowed. But we cannot
1787 fold here, as the chain record type is not yet finalized. */
1788 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1789 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1790 info
->mem_refs
->add (tp
);
1791 wi
->val_only
= save_val_only
;
1794 case VIEW_CONVERT_EXPR
:
1795 /* Just request to look at the subtrees, leaving val_only and lhs
1796 untouched. This might actually be for !val_only + lhs, in which
1797 case we don't want to force a replacement by a temporary. */
1802 if (!IS_TYPE_OR_DECL_P (t
))
1805 wi
->val_only
= true;
1814 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1815 struct walk_stmt_info
*);
1817 /* Helper for convert_local_reference. Convert all the references in
1818 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1821 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1823 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1824 bool need_frame
= false, need_stmts
= false;
1827 bitmap new_suppress
;
1829 new_suppress
= BITMAP_GGC_ALLOC ();
1830 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1832 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1834 switch (OMP_CLAUSE_CODE (clause
))
1836 case OMP_CLAUSE_REDUCTION
:
1837 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1839 goto do_decl_clause
;
1841 case OMP_CLAUSE_LASTPRIVATE
:
1842 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1844 goto do_decl_clause
;
1846 case OMP_CLAUSE_LINEAR
:
1847 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1849 wi
->val_only
= true;
1851 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
), &dummy
,
1853 goto do_decl_clause
;
1855 case OMP_CLAUSE_PRIVATE
:
1856 case OMP_CLAUSE_FIRSTPRIVATE
:
1857 case OMP_CLAUSE_COPYPRIVATE
:
1858 case OMP_CLAUSE_SHARED
:
1859 case OMP_CLAUSE_TO_DECLARE
:
1860 case OMP_CLAUSE_LINK
:
1861 case OMP_CLAUSE_USE_DEVICE_PTR
:
1862 case OMP_CLAUSE_IS_DEVICE_PTR
:
1864 decl
= OMP_CLAUSE_DECL (clause
);
1866 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1868 if (decl_function_context (decl
) == info
->context
1869 && !use_pointer_in_frame (decl
))
1871 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1874 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1875 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1876 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1877 OMP_CLAUSE_DECL (clause
)
1878 = get_local_debug_decl (info
, decl
, field
);
1884 case OMP_CLAUSE_SCHEDULE
:
1885 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1888 case OMP_CLAUSE_FINAL
:
1890 case OMP_CLAUSE_NUM_THREADS
:
1891 case OMP_CLAUSE_DEPEND
:
1892 case OMP_CLAUSE_DEVICE
:
1893 case OMP_CLAUSE_NUM_TEAMS
:
1894 case OMP_CLAUSE_THREAD_LIMIT
:
1895 case OMP_CLAUSE_SAFELEN
:
1896 case OMP_CLAUSE_SIMDLEN
:
1897 case OMP_CLAUSE_PRIORITY
:
1898 case OMP_CLAUSE_GRAINSIZE
:
1899 case OMP_CLAUSE_NUM_TASKS
:
1900 case OMP_CLAUSE_HINT
:
1901 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1902 case OMP_CLAUSE_NUM_GANGS
:
1903 case OMP_CLAUSE_NUM_WORKERS
:
1904 case OMP_CLAUSE_VECTOR_LENGTH
:
1905 case OMP_CLAUSE_GANG
:
1906 case OMP_CLAUSE_WORKER
:
1907 case OMP_CLAUSE_VECTOR
:
1908 case OMP_CLAUSE_ASYNC
:
1909 case OMP_CLAUSE_WAIT
:
1910 /* Several OpenACC clauses have optional arguments. Check if they
1912 if (OMP_CLAUSE_OPERAND (clause
, 0))
1914 wi
->val_only
= true;
1916 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1920 /* The gang clause accepts two arguments. */
1921 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
1922 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
1924 wi
->val_only
= true;
1926 convert_nonlocal_reference_op
1927 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
1931 case OMP_CLAUSE_DIST_SCHEDULE
:
1932 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1934 wi
->val_only
= true;
1936 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1941 case OMP_CLAUSE_MAP
:
1943 case OMP_CLAUSE_FROM
:
1944 if (OMP_CLAUSE_SIZE (clause
))
1946 wi
->val_only
= true;
1948 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause
),
1951 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1952 goto do_decl_clause
;
1953 wi
->val_only
= true;
1955 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_local_reference_op
,
1959 case OMP_CLAUSE_ALIGNED
:
1960 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1962 wi
->val_only
= true;
1964 convert_local_reference_op
1965 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1967 /* Like do_decl_clause, but don't add any suppression. */
1968 decl
= OMP_CLAUSE_DECL (clause
);
1970 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1972 if (decl_function_context (decl
) == info
->context
1973 && !use_pointer_in_frame (decl
))
1975 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1978 OMP_CLAUSE_DECL (clause
)
1979 = get_local_debug_decl (info
, decl
, field
);
1985 case OMP_CLAUSE_NOWAIT
:
1986 case OMP_CLAUSE_ORDERED
:
1987 case OMP_CLAUSE_DEFAULT
:
1988 case OMP_CLAUSE_COPYIN
:
1989 case OMP_CLAUSE_COLLAPSE
:
1990 case OMP_CLAUSE_TILE
:
1991 case OMP_CLAUSE_UNTIED
:
1992 case OMP_CLAUSE_MERGEABLE
:
1993 case OMP_CLAUSE_PROC_BIND
:
1994 case OMP_CLAUSE_NOGROUP
:
1995 case OMP_CLAUSE_THREADS
:
1996 case OMP_CLAUSE_SIMD
:
1997 case OMP_CLAUSE_DEFAULTMAP
:
1998 case OMP_CLAUSE_SEQ
:
1999 case OMP_CLAUSE_INDEPENDENT
:
2000 case OMP_CLAUSE_AUTO
:
2003 /* The following clause belongs to the OpenACC cache directive, which
2004 is discarded during gimplification. */
2005 case OMP_CLAUSE__CACHE_
:
2006 /* The following clauses are only allowed in the OpenMP declare simd
2007 directive, so not seen here. */
2008 case OMP_CLAUSE_UNIFORM
:
2009 case OMP_CLAUSE_INBRANCH
:
2010 case OMP_CLAUSE_NOTINBRANCH
:
2011 /* The following clauses are only allowed on OpenMP cancel and
2012 cancellation point directives, which at this point have already
2013 been lowered into a function call. */
2014 case OMP_CLAUSE_FOR
:
2015 case OMP_CLAUSE_PARALLEL
:
2016 case OMP_CLAUSE_SECTIONS
:
2017 case OMP_CLAUSE_TASKGROUP
:
2018 /* The following clauses are only added during OMP lowering; nested
2019 function decomposition happens before that. */
2020 case OMP_CLAUSE__LOOPTEMP_
:
2021 case OMP_CLAUSE__SIMDUID_
:
2022 case OMP_CLAUSE__GRIDDIM_
:
2023 /* Anything else. */
2029 info
->suppress_expansion
= new_suppress
;
2032 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
2033 switch (OMP_CLAUSE_CODE (clause
))
2035 case OMP_CLAUSE_REDUCTION
:
2036 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2039 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
2040 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2042 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2043 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2045 walk_body (convert_local_reference_stmt
,
2046 convert_local_reference_op
, info
,
2047 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
2048 walk_body (convert_local_reference_stmt
,
2049 convert_local_reference_op
, info
,
2050 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
2051 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2053 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2054 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2059 case OMP_CLAUSE_LASTPRIVATE
:
2060 walk_body (convert_local_reference_stmt
,
2061 convert_local_reference_op
, info
,
2062 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
2065 case OMP_CLAUSE_LINEAR
:
2066 walk_body (convert_local_reference_stmt
,
2067 convert_local_reference_op
, info
,
2068 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
2079 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2080 and PARM_DECLs that were referenced by inner nested functions.
2081 The rewrite will be a structure reference to the local frame variable. */
2084 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2085 struct walk_stmt_info
*wi
)
2087 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2088 tree save_local_var_chain
;
2089 bitmap save_suppress
;
2090 char save_static_chain_added
;
2091 bool frame_decl_added
;
2092 gimple
*stmt
= gsi_stmt (*gsi
);
2094 switch (gimple_code (stmt
))
2096 case GIMPLE_OMP_PARALLEL
:
2097 case GIMPLE_OMP_TASK
:
2098 save_suppress
= info
->suppress_expansion
;
2099 frame_decl_added
= false;
2100 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
2103 tree c
= build_omp_clause (gimple_location (stmt
),
2105 (void) get_frame_type (info
);
2106 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2107 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2108 gimple_omp_taskreg_set_clauses (stmt
, c
);
2109 info
->static_chain_added
|= 4;
2110 frame_decl_added
= true;
2113 save_local_var_chain
= info
->new_local_var_chain
;
2114 save_static_chain_added
= info
->static_chain_added
;
2115 info
->new_local_var_chain
= NULL
;
2116 info
->static_chain_added
= 0;
2118 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2119 gimple_omp_body_ptr (stmt
));
2121 if ((info
->static_chain_added
& 4) != 0 && !frame_decl_added
)
2123 tree c
= build_omp_clause (gimple_location (stmt
),
2125 (void) get_frame_type (info
);
2126 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2127 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2128 info
->static_chain_added
|= 4;
2129 gimple_omp_taskreg_set_clauses (stmt
, c
);
2131 if (info
->new_local_var_chain
)
2132 declare_vars (info
->new_local_var_chain
,
2133 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2134 info
->new_local_var_chain
= save_local_var_chain
;
2135 info
->suppress_expansion
= save_suppress
;
2136 info
->static_chain_added
|= save_static_chain_added
;
2139 case GIMPLE_OMP_FOR
:
2140 save_suppress
= info
->suppress_expansion
;
2141 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
2142 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
2143 convert_local_reference_stmt
,
2144 convert_local_reference_op
, info
);
2145 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2146 info
, gimple_omp_body_ptr (stmt
));
2147 info
->suppress_expansion
= save_suppress
;
2150 case GIMPLE_OMP_SECTIONS
:
2151 save_suppress
= info
->suppress_expansion
;
2152 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
2153 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2154 info
, gimple_omp_body_ptr (stmt
));
2155 info
->suppress_expansion
= save_suppress
;
2158 case GIMPLE_OMP_SINGLE
:
2159 save_suppress
= info
->suppress_expansion
;
2160 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
2161 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2162 info
, gimple_omp_body_ptr (stmt
));
2163 info
->suppress_expansion
= save_suppress
;
2166 case GIMPLE_OMP_TARGET
:
2167 if (!is_gimple_omp_offloaded (stmt
))
2169 save_suppress
= info
->suppress_expansion
;
2170 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
2171 info
->suppress_expansion
= save_suppress
;
2172 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2173 info
, gimple_omp_body_ptr (stmt
));
2176 save_suppress
= info
->suppress_expansion
;
2177 frame_decl_added
= false;
2178 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
))
2180 tree c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2181 (void) get_frame_type (info
);
2182 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2183 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2184 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2185 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2186 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2187 info
->static_chain_added
|= 4;
2188 frame_decl_added
= true;
2191 save_local_var_chain
= info
->new_local_var_chain
;
2192 save_static_chain_added
= info
->static_chain_added
;
2193 info
->new_local_var_chain
= NULL
;
2194 info
->static_chain_added
= 0;
2196 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2197 gimple_omp_body_ptr (stmt
));
2199 if ((info
->static_chain_added
& 4) != 0 && !frame_decl_added
)
2201 tree c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2202 (void) get_frame_type (info
);
2203 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2204 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2205 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2206 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2207 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2208 info
->static_chain_added
|= 4;
2211 if (info
->new_local_var_chain
)
2212 declare_vars (info
->new_local_var_chain
,
2213 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2214 info
->new_local_var_chain
= save_local_var_chain
;
2215 info
->suppress_expansion
= save_suppress
;
2216 info
->static_chain_added
|= save_static_chain_added
;
2219 case GIMPLE_OMP_TEAMS
:
2220 save_suppress
= info
->suppress_expansion
;
2221 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
2222 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2223 info
, gimple_omp_body_ptr (stmt
));
2224 info
->suppress_expansion
= save_suppress
;
2227 case GIMPLE_OMP_SECTION
:
2228 case GIMPLE_OMP_MASTER
:
2229 case GIMPLE_OMP_TASKGROUP
:
2230 case GIMPLE_OMP_ORDERED
:
2231 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2232 info
, gimple_omp_body_ptr (stmt
));
2236 wi
->val_only
= true;
2238 *handled_ops_p
= false;
2242 if (gimple_clobber_p (stmt
))
2244 tree lhs
= gimple_assign_lhs (stmt
);
2245 if (!use_pointer_in_frame (lhs
)
2246 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
2248 gsi_replace (gsi
, gimple_build_nop (), true);
2252 *handled_ops_p
= false;
2256 for (tree var
= gimple_bind_vars (as_a
<gbind
*> (stmt
));
2258 var
= DECL_CHAIN (var
))
2259 if (TREE_CODE (var
) == NAMELIST_DECL
)
2261 /* Adjust decls mentioned in NAMELIST_DECL. */
2262 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
2266 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
2269 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2271 if (decl_function_context (decl
) == info
->context
2272 && !use_pointer_in_frame (decl
))
2274 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2277 CONSTRUCTOR_ELT (decls
, i
)->value
2278 = get_local_debug_decl (info
, decl
, field
);
2284 *handled_ops_p
= false;
2288 /* For every other statement that we are not interested in
2289 handling here, let the walker traverse the operands. */
2290 *handled_ops_p
= false;
2294 /* Indicate that we have handled all the operands ourselves. */
2295 *handled_ops_p
= true;
2300 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2301 that reference labels from outer functions. The rewrite will be a
2302 call to __builtin_nonlocal_goto. */
2305 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2306 struct walk_stmt_info
*wi
)
2308 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2309 tree label
, new_label
, target_context
, x
, field
;
2311 gimple
*stmt
= gsi_stmt (*gsi
);
2313 if (gimple_code (stmt
) != GIMPLE_GOTO
)
2315 *handled_ops_p
= false;
2319 label
= gimple_goto_dest (stmt
);
2320 if (TREE_CODE (label
) != LABEL_DECL
)
2322 *handled_ops_p
= false;
2326 target_context
= decl_function_context (label
);
2327 if (target_context
== info
->context
)
2329 *handled_ops_p
= false;
2333 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
2336 /* The original user label may also be use for a normal goto, therefore
2337 we must create a new label that will actually receive the abnormal
2338 control transfer. This new label will be marked LABEL_NONLOCAL; this
2339 mark will trigger proper behavior in the cfg, as well as cause the
2340 (hairy target-specific) non-local goto receiver code to be generated
2341 when we expand rtl. Enter this association into var_map so that we
2342 can insert the new label into the IL during a second pass. */
2343 tree
*slot
= &i
->var_map
->get_or_insert (label
);
2346 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
2347 DECL_NONLOCAL (new_label
) = 1;
2353 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2354 field
= get_nl_goto_field (i
);
2355 x
= get_frame_field (info
, target_context
, field
, gsi
);
2357 x
= gsi_gimplify_val (info
, x
, gsi
);
2358 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
2359 2, build_addr (new_label
), x
);
2360 gsi_replace (gsi
, call
, false);
2362 /* We have handled all of STMT's operands, no need to keep going. */
2363 *handled_ops_p
= true;
2368 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2369 are referenced via nonlocal goto from a nested function. The rewrite
2370 will involve installing a newly generated DECL_NONLOCAL label, and
2371 (potentially) a branch around the rtl gunk that is assumed to be
2372 attached to such a label. */
2375 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2376 struct walk_stmt_info
*wi
)
2378 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2379 tree label
, new_label
;
2380 gimple_stmt_iterator tmp_gsi
;
2381 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (*gsi
));
2385 *handled_ops_p
= false;
2389 label
= gimple_label_label (stmt
);
2391 tree
*slot
= info
->var_map
->get (label
);
2394 *handled_ops_p
= false;
2398 /* If there's any possibility that the previous statement falls through,
2399 then we must branch around the new non-local label. */
2401 gsi_prev (&tmp_gsi
);
2402 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
2404 gimple
*stmt
= gimple_build_goto (label
);
2405 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2408 new_label
= (tree
) *slot
;
2409 stmt
= gimple_build_label (new_label
);
2410 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2412 *handled_ops_p
= true;
2417 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2418 of nested functions that require the use of trampolines. The rewrite
2419 will involve a reference a trampoline generated for the occasion. */
2422 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
2424 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2425 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2426 tree t
= *tp
, decl
, target_context
, x
, builtin
;
2431 switch (TREE_CODE (t
))
2435 T.1 = &CHAIN->tramp;
2436 T.2 = __builtin_adjust_trampoline (T.1);
2437 T.3 = (func_type)T.2;
2440 decl
= TREE_OPERAND (t
, 0);
2441 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2444 /* Only need to process nested functions. */
2445 target_context
= decl_function_context (decl
);
2446 if (!target_context
)
2449 /* If the nested function doesn't use a static chain, then
2450 it doesn't need a trampoline. */
2451 if (!DECL_STATIC_CHAIN (decl
))
2454 /* If we don't want a trampoline, then don't build one. */
2455 if (TREE_NO_TRAMPOLINE (t
))
2458 /* Lookup the immediate parent of the callee, as that's where
2459 we need to insert the trampoline. */
2460 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
2463 /* Decide whether to generate a descriptor or a trampoline. */
2464 descr
= FUNC_ADDR_BY_DESCRIPTOR (t
) && !flag_trampolines
;
2467 x
= lookup_descr_for_decl (i
, decl
, INSERT
);
2469 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
2471 /* Compute the address of the field holding the trampoline. */
2472 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
2474 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
2476 /* Do machine-specific ugliness. Normally this will involve
2477 computing extra alignment, but it can really be anything. */
2479 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR
);
2481 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
2482 call
= gimple_build_call (builtin
, 1, x
);
2483 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
2485 /* Cast back to the proper function type. */
2486 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
2487 x
= init_tmp_var (info
, x
, &wi
->gsi
);
2493 if (!IS_TYPE_OR_DECL_P (t
))
2502 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2503 to addresses of nested functions that require the use of
2504 trampolines. The rewrite will involve a reference a trampoline
2505 generated for the occasion. */
2508 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2509 struct walk_stmt_info
*wi
)
2511 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2512 gimple
*stmt
= gsi_stmt (*gsi
);
2514 switch (gimple_code (stmt
))
2518 /* Only walk call arguments, lest we generate trampolines for
2520 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2521 for (i
= 0; i
< nargs
; i
++)
2522 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2527 case GIMPLE_OMP_TARGET
:
2528 if (!is_gimple_omp_offloaded (stmt
))
2530 *handled_ops_p
= false;
2534 case GIMPLE_OMP_PARALLEL
:
2535 case GIMPLE_OMP_TASK
:
2537 tree save_local_var_chain
= info
->new_local_var_chain
;
2538 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2539 info
->new_local_var_chain
= NULL
;
2540 char save_static_chain_added
= info
->static_chain_added
;
2541 info
->static_chain_added
= 0;
2542 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2543 info
, gimple_omp_body_ptr (stmt
));
2544 if (info
->new_local_var_chain
)
2545 declare_vars (info
->new_local_var_chain
,
2546 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2548 for (int i
= 0; i
< 2; i
++)
2551 if ((info
->static_chain_added
& (1 << i
)) == 0)
2553 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2554 /* Don't add CHAIN.* or FRAME.* twice. */
2555 for (c
= gimple_omp_taskreg_clauses (stmt
);
2557 c
= OMP_CLAUSE_CHAIN (c
))
2558 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2559 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2560 && OMP_CLAUSE_DECL (c
) == decl
)
2562 if (c
== NULL
&& gimple_code (stmt
) != GIMPLE_OMP_TARGET
)
2564 c
= build_omp_clause (gimple_location (stmt
),
2565 i
? OMP_CLAUSE_FIRSTPRIVATE
2566 : OMP_CLAUSE_SHARED
);
2567 OMP_CLAUSE_DECL (c
) = decl
;
2568 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2569 gimple_omp_taskreg_set_clauses (stmt
, c
);
2573 c
= build_omp_clause (gimple_location (stmt
),
2575 OMP_CLAUSE_DECL (c
) = decl
;
2576 OMP_CLAUSE_SET_MAP_KIND (c
,
2577 i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2578 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2579 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2580 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2584 info
->new_local_var_chain
= save_local_var_chain
;
2585 info
->static_chain_added
|= save_static_chain_added
;
2590 *handled_ops_p
= false;
2594 *handled_ops_p
= true;
2600 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2601 that reference nested functions to make sure that the static chain
2602 is set up properly for the call. */
2605 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2606 struct walk_stmt_info
*wi
)
2608 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2609 tree decl
, target_context
;
2610 char save_static_chain_added
;
2612 gimple
*stmt
= gsi_stmt (*gsi
);
2614 switch (gimple_code (stmt
))
2617 if (gimple_call_chain (stmt
))
2619 decl
= gimple_call_fndecl (stmt
);
2622 target_context
= decl_function_context (decl
);
2623 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2625 gimple_call_set_chain (as_a
<gcall
*> (stmt
),
2626 get_static_chain (info
, target_context
,
2628 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2632 case GIMPLE_OMP_PARALLEL
:
2633 case GIMPLE_OMP_TASK
:
2634 save_static_chain_added
= info
->static_chain_added
;
2635 info
->static_chain_added
= 0;
2636 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2637 for (i
= 0; i
< 2; i
++)
2640 if ((info
->static_chain_added
& (1 << i
)) == 0)
2642 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2643 /* Don't add CHAIN.* or FRAME.* twice. */
2644 for (c
= gimple_omp_taskreg_clauses (stmt
);
2646 c
= OMP_CLAUSE_CHAIN (c
))
2647 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2648 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2649 && OMP_CLAUSE_DECL (c
) == decl
)
2653 c
= build_omp_clause (gimple_location (stmt
),
2654 i
? OMP_CLAUSE_FIRSTPRIVATE
2655 : OMP_CLAUSE_SHARED
);
2656 OMP_CLAUSE_DECL (c
) = decl
;
2657 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2658 gimple_omp_taskreg_set_clauses (stmt
, c
);
2661 info
->static_chain_added
|= save_static_chain_added
;
2664 case GIMPLE_OMP_TARGET
:
2665 if (!is_gimple_omp_offloaded (stmt
))
2667 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2670 save_static_chain_added
= info
->static_chain_added
;
2671 info
->static_chain_added
= 0;
2672 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2673 for (i
= 0; i
< 2; i
++)
2676 if ((info
->static_chain_added
& (1 << i
)) == 0)
2678 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2679 /* Don't add CHAIN.* or FRAME.* twice. */
2680 for (c
= gimple_omp_target_clauses (stmt
);
2682 c
= OMP_CLAUSE_CHAIN (c
))
2683 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
2684 && OMP_CLAUSE_DECL (c
) == decl
)
2688 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2689 OMP_CLAUSE_DECL (c
) = decl
;
2690 OMP_CLAUSE_SET_MAP_KIND (c
, i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2691 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2692 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2693 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2697 info
->static_chain_added
|= save_static_chain_added
;
2700 case GIMPLE_OMP_FOR
:
2701 walk_body (convert_gimple_call
, NULL
, info
,
2702 gimple_omp_for_pre_body_ptr (stmt
));
2704 case GIMPLE_OMP_SECTIONS
:
2705 case GIMPLE_OMP_SECTION
:
2706 case GIMPLE_OMP_SINGLE
:
2707 case GIMPLE_OMP_TEAMS
:
2708 case GIMPLE_OMP_MASTER
:
2709 case GIMPLE_OMP_TASKGROUP
:
2710 case GIMPLE_OMP_ORDERED
:
2711 case GIMPLE_OMP_CRITICAL
:
2712 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2716 /* Keep looking for other operands. */
2717 *handled_ops_p
= false;
2721 *handled_ops_p
= true;
2725 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2726 call expressions. At the same time, determine if a nested function
2727 actually uses its static chain; if not, remember that. */
2730 convert_all_function_calls (struct nesting_info
*root
)
2732 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2733 struct nesting_info
*n
;
2735 /* First, optimistically clear static_chain for all decls that haven't
2736 used the static chain already for variable access. But always create
2737 it if not optimizing. This makes it possible to reconstruct the static
2738 nesting tree at run time and thus to resolve up-level references from
2739 within the debugger. */
2740 FOR_EACH_NEST_INFO (n
, root
)
2742 tree decl
= n
->context
;
2746 (void) get_frame_type (n
);
2748 (void) get_chain_decl (n
);
2750 else if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2752 DECL_STATIC_CHAIN (decl
) = 0;
2753 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2754 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2755 lang_hooks
.decl_printable_name (decl
, 2));
2758 DECL_STATIC_CHAIN (decl
) = 1;
2759 chain_count
+= DECL_STATIC_CHAIN (decl
);
2762 /* Walk the functions and perform transformations. Note that these
2763 transformations can induce new uses of the static chain, which in turn
2764 require re-examining all users of the decl. */
2765 /* ??? It would make sense to try to use the call graph to speed this up,
2766 but the call graph hasn't really been built yet. Even if it did, we
2767 would still need to iterate in this loop since address-of references
2768 wouldn't show up in the callgraph anyway. */
2772 old_chain_count
= chain_count
;
2776 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2777 fputc ('\n', dump_file
);
2779 FOR_EACH_NEST_INFO (n
, root
)
2781 tree decl
= n
->context
;
2782 walk_function (convert_tramp_reference_stmt
,
2783 convert_tramp_reference_op
, n
);
2784 walk_function (convert_gimple_call
, NULL
, n
);
2785 chain_count
+= DECL_STATIC_CHAIN (decl
);
2788 while (chain_count
!= old_chain_count
);
2790 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2791 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2795 struct nesting_copy_body_data
2798 struct nesting_info
*root
;
2801 /* A helper subroutine for debug_var_chain type remapping. */
2804 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2806 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2807 tree
*slot
= nid
->root
->var_map
->get (decl
);
2810 return (tree
) *slot
;
2812 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2814 tree new_decl
= copy_decl_no_change (decl
, id
);
2815 DECL_ORIGINAL_TYPE (new_decl
)
2816 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2821 || TREE_CODE (decl
) == PARM_DECL
2822 || TREE_CODE (decl
) == RESULT_DECL
)
2825 return copy_decl_no_change (decl
, id
);
2828 /* A helper function for remap_vla_decls. See if *TP contains
2829 some remapped variables. */
2832 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2834 struct nesting_info
*root
= (struct nesting_info
*) data
;
2840 tree
*slot
= root
->var_map
->get (t
);
2848 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2852 remap_vla_decls (tree block
, struct nesting_info
*root
)
2854 tree var
, subblock
, val
, type
;
2855 struct nesting_copy_body_data id
;
2857 for (subblock
= BLOCK_SUBBLOCKS (block
);
2859 subblock
= BLOCK_CHAIN (subblock
))
2860 remap_vla_decls (subblock
, root
);
2862 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
2863 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
2865 val
= DECL_VALUE_EXPR (var
);
2866 type
= TREE_TYPE (var
);
2868 if (!(TREE_CODE (val
) == INDIRECT_REF
2869 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2870 && variably_modified_type_p (type
, NULL
)))
2873 if (root
->var_map
->get (TREE_OPERAND (val
, 0))
2874 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2878 if (var
== NULL_TREE
)
2881 memset (&id
, 0, sizeof (id
));
2882 id
.cb
.copy_decl
= nesting_copy_decl
;
2883 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
2886 for (; var
; var
= DECL_CHAIN (var
))
2887 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
2889 struct nesting_info
*i
;
2892 val
= DECL_VALUE_EXPR (var
);
2893 type
= TREE_TYPE (var
);
2895 if (!(TREE_CODE (val
) == INDIRECT_REF
2896 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2897 && variably_modified_type_p (type
, NULL
)))
2900 tree
*slot
= root
->var_map
->get (TREE_OPERAND (val
, 0));
2901 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2904 context
= decl_function_context (var
);
2905 for (i
= root
; i
; i
= i
->outer
)
2906 if (i
->context
== context
)
2912 /* Fully expand value expressions. This avoids having debug variables
2913 only referenced from them and that can be swept during GC. */
2916 tree t
= (tree
) *slot
;
2917 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
2918 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
2921 id
.cb
.src_fn
= i
->context
;
2922 id
.cb
.dst_fn
= i
->context
;
2923 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2925 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2926 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2928 newt
= TREE_TYPE (newt
);
2929 type
= TREE_TYPE (type
);
2931 if (TYPE_NAME (newt
)
2932 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2933 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2935 && TYPE_NAME (newt
) == TYPE_NAME (type
))
2936 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2938 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2939 if (val
!= DECL_VALUE_EXPR (var
))
2940 SET_DECL_VALUE_EXPR (var
, val
);
2943 delete id
.cb
.decl_map
;
2946 /* Fold the MEM_REF *E. */
2948 fold_mem_refs (tree
*const &e
, void *data ATTRIBUTE_UNUSED
)
2950 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
2951 *ref_p
= fold (*ref_p
);
2955 /* Given DECL, a nested function, build an initialization call for FIELD,
2956 the trampoline or descriptor for DECL, using FUNC as the function. */
2959 build_init_call_stmt (struct nesting_info
*info
, tree decl
, tree field
,
2962 tree arg1
, arg2
, arg3
, x
;
2964 gcc_assert (DECL_STATIC_CHAIN (decl
));
2965 arg3
= build_addr (info
->frame_decl
);
2967 arg2
= build_addr (decl
);
2969 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2970 info
->frame_decl
, field
, NULL_TREE
);
2971 arg1
= build_addr (x
);
2973 return gimple_build_call (func
, 3, arg1
, arg2
, arg3
);
2976 /* Do "everything else" to clean up or complete state collected by the various
2977 walking passes -- create a field to hold the frame base address, lay out the
2978 types and decls, generate code to initialize the frame decl, store critical
2979 expressions in the struct function for rtl to find. */
2982 finalize_nesting_tree_1 (struct nesting_info
*root
)
2984 gimple_seq stmt_list
;
2986 tree context
= root
->context
;
2987 struct function
*sf
;
2991 /* If we created a non-local frame type or decl, we need to lay them
2992 out at this time. */
2993 if (root
->frame_type
)
2995 /* Debugging information needs to compute the frame base address of the
2996 parent frame out of the static chain from the nested frame.
2998 The static chain is the address of the FRAME record, so one could
2999 imagine it would be possible to compute the frame base address just
3000 adding a constant offset to this address. Unfortunately, this is not
3001 possible: if the FRAME object has alignment constraints that are
3002 stronger than the stack, then the offset between the frame base and
3003 the FRAME object will be dynamic.
3005 What we do instead is to append a field to the FRAME object that holds
3006 the frame base address: then debug info just has to fetch this
3009 /* Debugging information will refer to the CFA as the frame base
3010 address: we will do the same here. */
3011 const tree frame_addr_fndecl
3012 = builtin_decl_explicit (BUILT_IN_DWARF_CFA
);
3014 /* Create a field in the FRAME record to hold the frame base address for
3015 this stack frame. Since it will be used only by the debugger, put it
3016 at the end of the record in order not to shift all other offsets. */
3017 tree fb_decl
= make_node (FIELD_DECL
);
3019 DECL_NAME (fb_decl
) = get_identifier ("FRAME_BASE.PARENT");
3020 TREE_TYPE (fb_decl
) = ptr_type_node
;
3021 TREE_ADDRESSABLE (fb_decl
) = 1;
3022 DECL_CONTEXT (fb_decl
) = root
->frame_type
;
3023 TYPE_FIELDS (root
->frame_type
) = chainon (TYPE_FIELDS (root
->frame_type
),
3026 /* In some cases the frame type will trigger the -Wpadded warning.
3027 This is not helpful; suppress it. */
3028 int save_warn_padded
= warn_padded
;
3030 layout_type (root
->frame_type
);
3031 warn_padded
= save_warn_padded
;
3032 layout_decl (root
->frame_decl
, 0);
3034 /* Initialize the frame base address field. If the builtin we need is
3035 not available, set it to NULL so that debugging information does not
3037 tree fb_ref
= build3 (COMPONENT_REF
, TREE_TYPE (fb_decl
),
3038 root
->frame_decl
, fb_decl
, NULL_TREE
);
3041 if (frame_addr_fndecl
!= NULL_TREE
)
3043 gcall
*fb_gimple
= gimple_build_call (frame_addr_fndecl
, 1,
3045 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3047 fb_tmp
= init_tmp_var_with_call (root
, &gsi
, fb_gimple
);
3050 fb_tmp
= build_int_cst (TREE_TYPE (fb_ref
), 0);
3051 gimple_seq_add_stmt (&stmt_list
,
3052 gimple_build_assign (fb_ref
, fb_tmp
));
3054 /* Remove root->frame_decl from root->new_local_var_chain, so
3055 that we can declare it also in the lexical blocks, which
3056 helps ensure virtual regs that end up appearing in its RTL
3057 expression get substituted in instantiate_virtual_regs(). */
3059 for (adjust
= &root
->new_local_var_chain
;
3060 *adjust
!= root
->frame_decl
;
3061 adjust
= &DECL_CHAIN (*adjust
))
3062 gcc_assert (DECL_CHAIN (*adjust
));
3063 *adjust
= DECL_CHAIN (*adjust
);
3065 DECL_CHAIN (root
->frame_decl
) = NULL_TREE
;
3066 declare_vars (root
->frame_decl
,
3067 gimple_seq_first_stmt (gimple_body (context
)), true);
3070 /* If any parameters were referenced non-locally, then we need to
3071 insert a copy. Likewise, if any variables were referenced by
3072 pointer, we need to initialize the address. */
3073 if (root
->any_parm_remapped
)
3076 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
3080 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
3084 if (use_pointer_in_frame (p
))
3089 /* If the assignment is from a non-register the stmt is
3090 not valid gimple. Make it so by using a temporary instead. */
3091 if (!is_gimple_reg (x
)
3092 && is_gimple_reg_type (TREE_TYPE (x
)))
3094 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3095 x
= init_tmp_var (root
, x
, &gsi
);
3098 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3099 root
->frame_decl
, field
, NULL_TREE
);
3100 stmt
= gimple_build_assign (y
, x
);
3101 gimple_seq_add_stmt (&stmt_list
, stmt
);
3105 /* If a chain_field was created, then it needs to be initialized
3107 if (root
->chain_field
)
3109 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
3110 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
3111 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
3112 gimple_seq_add_stmt (&stmt_list
, stmt
);
3115 /* If trampolines were created, then we need to initialize them. */
3116 if (root
->any_tramp_created
)
3118 struct nesting_info
*i
;
3119 for (i
= root
->inner
; i
; i
= i
->next
)
3123 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
3127 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
3128 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3129 gimple_seq_add_stmt (&stmt_list
, stmt
);
3133 /* If descriptors were created, then we need to initialize them. */
3134 if (root
->any_descr_created
)
3136 struct nesting_info
*i
;
3137 for (i
= root
->inner
; i
; i
= i
->next
)
3141 field
= lookup_descr_for_decl (root
, i
->context
, NO_INSERT
);
3145 x
= builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR
);
3146 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3147 gimple_seq_add_stmt (&stmt_list
, stmt
);
3151 /* If we created initialization statements, insert them. */
3155 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
3156 bind
= gimple_seq_first_stmt_as_a_bind (gimple_body (context
));
3157 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
3158 gimple_bind_set_body (bind
, stmt_list
);
3161 /* If a chain_decl was created, then it needs to be registered with
3162 struct function so that it gets initialized from the static chain
3163 register at the beginning of the function. */
3164 sf
= DECL_STRUCT_FUNCTION (root
->context
);
3165 sf
->static_chain_decl
= root
->chain_decl
;
3167 /* Similarly for the non-local goto save area. */
3168 if (root
->nl_goto_field
)
3170 sf
->nonlocal_goto_save_area
3171 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
3172 sf
->has_nonlocal_label
= 1;
3175 /* Make sure all new local variables get inserted into the
3176 proper BIND_EXPR. */
3177 if (root
->new_local_var_chain
)
3178 declare_vars (root
->new_local_var_chain
,
3179 gimple_seq_first_stmt (gimple_body (root
->context
)),
3182 if (root
->debug_var_chain
)
3187 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
3189 for (debug_var
= root
->debug_var_chain
; debug_var
;
3190 debug_var
= DECL_CHAIN (debug_var
))
3191 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3194 /* If there are any debug decls with variable length types,
3195 remap those types using other debug_var_chain variables. */
3198 struct nesting_copy_body_data id
;
3200 memset (&id
, 0, sizeof (id
));
3201 id
.cb
.copy_decl
= nesting_copy_decl
;
3202 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
3205 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
3206 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3208 tree type
= TREE_TYPE (debug_var
);
3209 tree newt
, t
= type
;
3210 struct nesting_info
*i
;
3212 for (i
= root
; i
; i
= i
->outer
)
3213 if (variably_modified_type_p (type
, i
->context
))
3219 id
.cb
.src_fn
= i
->context
;
3220 id
.cb
.dst_fn
= i
->context
;
3221 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
3223 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
3224 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
3226 newt
= TREE_TYPE (newt
);
3229 if (TYPE_NAME (newt
)
3230 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
3231 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
3233 && TYPE_NAME (newt
) == TYPE_NAME (t
))
3234 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
3237 delete id
.cb
.decl_map
;
3240 scope
= gimple_seq_first_stmt_as_a_bind (gimple_body (root
->context
));
3241 if (gimple_bind_block (scope
))
3242 declare_vars (root
->debug_var_chain
, scope
, true);
3244 BLOCK_VARS (DECL_INITIAL (root
->context
))
3245 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
3246 root
->debug_var_chain
);
3249 /* Fold the rewritten MEM_REF trees. */
3250 root
->mem_refs
->traverse
<void *, fold_mem_refs
> (NULL
);
3252 /* Dump the translated tree function. */
3255 fputs ("\n\n", dump_file
);
3256 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
3261 finalize_nesting_tree (struct nesting_info
*root
)
3263 struct nesting_info
*n
;
3264 FOR_EACH_NEST_INFO (n
, root
)
3265 finalize_nesting_tree_1 (n
);
3268 /* Unnest the nodes and pass them to cgraph. */
3271 unnest_nesting_tree_1 (struct nesting_info
*root
)
3273 struct cgraph_node
*node
= cgraph_node::get (root
->context
);
3275 /* For nested functions update the cgraph to reflect unnesting.
3276 We also delay finalizing of these functions up to this point. */
3280 cgraph_node::finalize_function (root
->context
, true);
3285 unnest_nesting_tree (struct nesting_info
*root
)
3287 struct nesting_info
*n
;
3288 FOR_EACH_NEST_INFO (n
, root
)
3289 unnest_nesting_tree_1 (n
);
3292 /* Free the data structures allocated during this pass. */
3295 free_nesting_tree (struct nesting_info
*root
)
3297 struct nesting_info
*node
, *next
;
3299 node
= iter_nestinfo_start (root
);
3302 next
= iter_nestinfo_next (node
);
3303 delete node
->var_map
;
3304 delete node
->field_map
;
3305 delete node
->mem_refs
;
3312 /* Gimplify a function and all its nested functions. */
3314 gimplify_all_functions (struct cgraph_node
*root
)
3316 struct cgraph_node
*iter
;
3317 if (!gimple_body (root
->decl
))
3318 gimplify_function_tree (root
->decl
);
3319 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
3320 gimplify_all_functions (iter
);
3323 /* Main entry point for this pass. Process FNDECL and all of its nested
3324 subroutines and turn them into something less tightly bound. */
3327 lower_nested_functions (tree fndecl
)
3329 struct cgraph_node
*cgn
;
3330 struct nesting_info
*root
;
3332 /* If there are no nested functions, there's nothing to do. */
3333 cgn
= cgraph_node::get (fndecl
);
3337 gimplify_all_functions (cgn
);
3339 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
3341 fprintf (dump_file
, "\n;; Function %s\n\n",
3342 lang_hooks
.decl_printable_name (fndecl
, 2));
3344 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
3345 root
= create_nesting_tree (cgn
);
3347 walk_all_functions (convert_nonlocal_reference_stmt
,
3348 convert_nonlocal_reference_op
,
3350 walk_all_functions (convert_local_reference_stmt
,
3351 convert_local_reference_op
,
3353 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
3354 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
3356 convert_all_function_calls (root
);
3357 finalize_nesting_tree (root
);
3358 unnest_nesting_tree (root
);
3360 free_nesting_tree (root
);
3361 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
3365 dump_end (TDI_nested
, dump_file
);
3370 #include "gt-tree-nested.h"