1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2016 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"
34 #include "tree-dump.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
)
502 return descriptor_type
;
504 t
= build_index_type (integer_one_node
);
505 t
= build_array_type (ptr_type_node
, t
);
506 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
507 FIELD_DECL
, get_identifier ("__data"), t
);
509 descriptor_type
= make_node (RECORD_TYPE
);
510 TYPE_NAME (descriptor_type
) = get_identifier ("__builtin_descriptor");
511 TYPE_FIELDS (descriptor_type
) = t
;
512 layout_type (descriptor_type
);
513 DECL_CONTEXT (t
) = descriptor_type
;
515 return descriptor_type
;
518 /* Given DECL, a nested function, find or create an element in the
519 var map for this function. */
522 lookup_element_for_decl (struct nesting_info
*info
, tree decl
,
523 enum insert_option insert
)
525 if (insert
== NO_INSERT
)
527 tree
*slot
= info
->var_map
->get (decl
);
528 return slot
? *slot
: NULL_TREE
;
531 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
533 *slot
= build_tree_list (NULL_TREE
, NULL_TREE
);
538 /* Given DECL, a nested function, create a field in the non-local
539 frame structure for this function. */
542 create_field_for_decl (struct nesting_info
*info
, tree decl
, tree type
)
544 tree field
= make_node (FIELD_DECL
);
545 DECL_NAME (field
) = DECL_NAME (decl
);
546 TREE_TYPE (field
) = type
;
547 TREE_ADDRESSABLE (field
) = 1;
548 insert_field_into_struct (get_frame_type (info
), field
);
552 /* Given DECL, a nested function, find or create a field in the non-local
553 frame structure for a trampoline for this function. */
556 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
557 enum insert_option insert
)
561 elt
= lookup_element_for_decl (info
, decl
, insert
);
565 field
= TREE_PURPOSE (elt
);
567 if (!field
&& insert
== INSERT
)
569 field
= create_field_for_decl (info
, decl
, get_trampoline_type (info
));
570 TREE_PURPOSE (elt
) = field
;
571 info
->any_tramp_created
= true;
577 /* Given DECL, a nested function, find or create a field in the non-local
578 frame structure for a descriptor for this function. */
581 lookup_descr_for_decl (struct nesting_info
*info
, tree decl
,
582 enum insert_option insert
)
586 elt
= lookup_element_for_decl (info
, decl
, insert
);
590 field
= TREE_VALUE (elt
);
592 if (!field
&& insert
== INSERT
)
594 field
= create_field_for_decl (info
, decl
, get_descriptor_type (info
));
595 TREE_VALUE (elt
) = field
;
596 info
->any_descr_created
= true;
602 /* Build or return the field within the non-local frame state that holds
603 the non-local goto "jmp_buf". The buffer itself is maintained by the
604 rtl middle-end as dynamic stack space is allocated. */
607 get_nl_goto_field (struct nesting_info
*info
)
609 tree field
= info
->nl_goto_field
;
615 /* For __builtin_nonlocal_goto, we need N words. The first is the
616 frame pointer, the rest is for the target's stack pointer save
617 area. The number of words is controlled by STACK_SAVEAREA_MODE;
618 not the best interface, but it'll do for now. */
619 if (Pmode
== ptr_mode
)
620 type
= ptr_type_node
;
622 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
624 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
625 size
= size
/ GET_MODE_SIZE (Pmode
);
628 type
= build_array_type
629 (type
, build_index_type (size_int (size
)));
631 field
= make_node (FIELD_DECL
);
632 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
633 TREE_TYPE (field
) = type
;
634 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
635 TREE_ADDRESSABLE (field
) = 1;
637 insert_field_into_struct (get_frame_type (info
), field
);
639 info
->nl_goto_field
= field
;
645 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
648 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
649 struct nesting_info
*info
, gimple_seq
*pseq
)
651 struct walk_stmt_info wi
;
653 memset (&wi
, 0, sizeof (wi
));
656 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
660 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
663 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
664 struct nesting_info
*info
)
666 gimple_seq body
= gimple_body (info
->context
);
667 walk_body (callback_stmt
, callback_op
, info
, &body
);
668 gimple_set_body (info
->context
, body
);
671 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
674 walk_gimple_omp_for (gomp_for
*for_stmt
,
675 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
676 struct nesting_info
*info
)
678 struct walk_stmt_info wi
;
683 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
686 memset (&wi
, 0, sizeof (wi
));
688 wi
.gsi
= gsi_last (seq
);
690 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
693 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
697 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
702 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
705 t
= gimple_omp_for_incr (for_stmt
, i
);
706 gcc_assert (BINARY_CLASS_P (t
));
708 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
711 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
714 seq
= gsi_seq (wi
.gsi
);
715 if (!gimple_seq_empty_p (seq
))
717 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
718 annotate_all_with_location (seq
, gimple_location (for_stmt
));
719 gimple_seq_add_seq (&pre_body
, seq
);
720 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
724 /* Similarly for ROOT and all functions nested underneath, depth first. */
727 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
728 struct nesting_info
*root
)
730 struct nesting_info
*n
;
731 FOR_EACH_NEST_INFO (n
, root
)
732 walk_function (callback_stmt
, callback_op
, n
);
736 /* We have to check for a fairly pathological case. The operands of function
737 nested function are to be interpreted in the context of the enclosing
738 function. So if any are variably-sized, they will get remapped when the
739 enclosing function is inlined. But that remapping would also have to be
740 done in the types of the PARM_DECLs of the nested function, meaning the
741 argument types of that function will disagree with the arguments in the
742 calls to that function. So we'd either have to make a copy of the nested
743 function corresponding to each time the enclosing function was inlined or
744 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
745 function. The former is not practical. The latter would still require
746 detecting this case to know when to add the conversions. So, for now at
747 least, we don't inline such an enclosing function.
749 We have to do that check recursively, so here return indicating whether
750 FNDECL has such a nested function. ORIG_FN is the function we were
751 trying to inline to use for checking whether any argument is variably
752 modified by anything in it.
754 It would be better to do this in tree-inline.c so that we could give
755 the appropriate warning for why a function can't be inlined, but that's
756 too late since the nesting structure has already been flattened and
757 adding a flag just to record this fact seems a waste of a flag. */
760 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
762 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
765 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
767 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
768 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
771 if (check_for_nested_with_variably_modified (cgn
->decl
,
779 /* Construct our local datastructure describing the function nesting
780 tree rooted by CGN. */
782 static struct nesting_info
*
783 create_nesting_tree (struct cgraph_node
*cgn
)
785 struct nesting_info
*info
= XCNEW (struct nesting_info
);
786 info
->field_map
= new hash_map
<tree
, tree
>;
787 info
->var_map
= new hash_map
<tree
, tree
>;
788 info
->mem_refs
= new hash_set
<tree
*>;
789 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
790 info
->context
= cgn
->decl
;
792 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
794 struct nesting_info
*sub
= create_nesting_tree (cgn
);
796 sub
->next
= info
->inner
;
800 /* See discussion at check_for_nested_with_variably_modified for a
801 discussion of why this has to be here. */
802 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
803 DECL_UNINLINABLE (info
->context
) = true;
808 /* Return an expression computing the static chain for TARGET_CONTEXT
809 from INFO->CONTEXT. Insert any necessary computations before TSI. */
812 get_static_chain (struct nesting_info
*info
, tree target_context
,
813 gimple_stmt_iterator
*gsi
)
815 struct nesting_info
*i
;
818 if (info
->context
== target_context
)
820 x
= build_addr (info
->frame_decl
);
821 info
->static_chain_added
|= 1;
825 x
= get_chain_decl (info
);
826 info
->static_chain_added
|= 2;
828 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
830 tree field
= get_chain_field (i
);
832 x
= build_simple_mem_ref (x
);
833 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
834 x
= init_tmp_var (info
, x
, gsi
);
842 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
843 frame as seen from INFO->CONTEXT. Insert any necessary computations
847 get_frame_field (struct nesting_info
*info
, tree target_context
,
848 tree field
, gimple_stmt_iterator
*gsi
)
850 struct nesting_info
*i
;
853 if (info
->context
== target_context
)
855 /* Make sure frame_decl gets created. */
856 (void) get_frame_type (info
);
857 x
= info
->frame_decl
;
858 info
->static_chain_added
|= 1;
862 x
= get_chain_decl (info
);
863 info
->static_chain_added
|= 2;
865 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
867 tree field
= get_chain_field (i
);
869 x
= build_simple_mem_ref (x
);
870 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
871 x
= init_tmp_var (info
, x
, gsi
);
874 x
= build_simple_mem_ref (x
);
877 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
881 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
883 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
884 in the nested function with DECL_VALUE_EXPR set to reference the true
885 variable in the parent function. This is used both for debug info
886 and in OMP lowering. */
889 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
892 struct nesting_info
*i
;
893 tree x
, field
, new_decl
;
895 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
900 target_context
= decl_function_context (decl
);
902 /* A copy of the code in get_frame_field, but without the temporaries. */
903 if (info
->context
== target_context
)
905 /* Make sure frame_decl gets created. */
906 (void) get_frame_type (info
);
907 x
= info
->frame_decl
;
909 info
->static_chain_added
|= 1;
913 x
= get_chain_decl (info
);
914 info
->static_chain_added
|= 2;
915 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
917 field
= get_chain_field (i
);
918 x
= build_simple_mem_ref (x
);
919 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
921 x
= build_simple_mem_ref (x
);
924 field
= lookup_field_for_decl (i
, decl
, INSERT
);
925 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
926 if (use_pointer_in_frame (decl
))
927 x
= build_simple_mem_ref (x
);
929 /* ??? We should be remapping types as well, surely. */
930 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
931 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
932 DECL_CONTEXT (new_decl
) = info
->context
;
933 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
934 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
935 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
936 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
937 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
938 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
939 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
940 if ((TREE_CODE (decl
) == PARM_DECL
941 || TREE_CODE (decl
) == RESULT_DECL
943 && DECL_BY_REFERENCE (decl
))
944 DECL_BY_REFERENCE (new_decl
) = 1;
946 SET_DECL_VALUE_EXPR (new_decl
, x
);
947 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
950 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
951 info
->debug_var_chain
= new_decl
;
954 && info
->context
!= target_context
955 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
956 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
962 /* Callback for walk_gimple_stmt, rewrite all references to VAR
963 and PARM_DECLs that belong to outer functions.
965 The rewrite will involve some number of structure accesses back up
966 the static chain. E.g. for a variable FOO up one nesting level it'll
967 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
968 indirections apply to decls for which use_pointer_in_frame is true. */
971 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
973 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
974 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
978 switch (TREE_CODE (t
))
981 /* Non-automatic variables are never processed. */
982 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
987 if (decl_function_context (t
) != info
->context
)
992 x
= get_nonlocal_debug_decl (info
, t
);
993 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
995 tree target_context
= decl_function_context (t
);
996 struct nesting_info
*i
;
997 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
999 x
= lookup_field_for_decl (i
, t
, INSERT
);
1000 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1001 if (use_pointer_in_frame (t
))
1003 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1004 x
= build_simple_mem_ref (x
);
1011 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1013 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1021 /* We're taking the address of a label from a parent function, but
1022 this is not itself a non-local goto. Mark the label such that it
1023 will not be deleted, much as we would with a label address in
1025 if (decl_function_context (t
) != info
->context
)
1026 FORCED_LABEL (t
) = 1;
1031 bool save_val_only
= wi
->val_only
;
1033 wi
->val_only
= false;
1035 wi
->changed
= false;
1036 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
1037 wi
->val_only
= true;
1043 /* If we changed anything, we might no longer be directly
1044 referencing a decl. */
1045 save_context
= current_function_decl
;
1046 current_function_decl
= info
->context
;
1047 recompute_tree_invariant_for_addr_expr (t
);
1048 current_function_decl
= save_context
;
1050 /* If the callback converted the address argument in a context
1051 where we only accept variables (and min_invariant, presumably),
1052 then compute the address into a temporary. */
1054 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1064 case ARRAY_RANGE_REF
:
1066 /* Go down this entire nest and just look at the final prefix and
1067 anything that describes the references. Otherwise, we lose track
1068 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1069 wi
->val_only
= true;
1071 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1073 if (TREE_CODE (t
) == COMPONENT_REF
)
1074 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1076 else if (TREE_CODE (t
) == ARRAY_REF
1077 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1079 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1081 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1083 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1087 wi
->val_only
= false;
1088 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1091 case VIEW_CONVERT_EXPR
:
1092 /* Just request to look at the subtrees, leaving val_only and lhs
1093 untouched. This might actually be for !val_only + lhs, in which
1094 case we don't want to force a replacement by a temporary. */
1099 if (!IS_TYPE_OR_DECL_P (t
))
1102 wi
->val_only
= true;
1111 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1112 struct walk_stmt_info
*);
1114 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1115 and PARM_DECLs that belong to outer functions. */
1118 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1120 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1121 bool need_chain
= false, need_stmts
= false;
1124 bitmap new_suppress
;
1126 new_suppress
= BITMAP_GGC_ALLOC ();
1127 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1129 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1131 switch (OMP_CLAUSE_CODE (clause
))
1133 case OMP_CLAUSE_REDUCTION
:
1134 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1136 goto do_decl_clause
;
1138 case OMP_CLAUSE_LASTPRIVATE
:
1139 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1141 goto do_decl_clause
;
1143 case OMP_CLAUSE_LINEAR
:
1144 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1146 wi
->val_only
= true;
1148 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
),
1150 goto do_decl_clause
;
1152 case OMP_CLAUSE_PRIVATE
:
1153 case OMP_CLAUSE_FIRSTPRIVATE
:
1154 case OMP_CLAUSE_COPYPRIVATE
:
1155 case OMP_CLAUSE_SHARED
:
1156 case OMP_CLAUSE_TO_DECLARE
:
1157 case OMP_CLAUSE_LINK
:
1158 case OMP_CLAUSE_USE_DEVICE_PTR
:
1159 case OMP_CLAUSE_IS_DEVICE_PTR
:
1161 decl
= OMP_CLAUSE_DECL (clause
);
1163 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1165 if (decl_function_context (decl
) != info
->context
)
1167 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1168 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1169 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1170 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1171 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1176 case OMP_CLAUSE_SCHEDULE
:
1177 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1180 case OMP_CLAUSE_FINAL
:
1182 case OMP_CLAUSE_NUM_THREADS
:
1183 case OMP_CLAUSE_DEPEND
:
1184 case OMP_CLAUSE_DEVICE
:
1185 case OMP_CLAUSE_NUM_TEAMS
:
1186 case OMP_CLAUSE_THREAD_LIMIT
:
1187 case OMP_CLAUSE_SAFELEN
:
1188 case OMP_CLAUSE_SIMDLEN
:
1189 case OMP_CLAUSE_PRIORITY
:
1190 case OMP_CLAUSE_GRAINSIZE
:
1191 case OMP_CLAUSE_NUM_TASKS
:
1192 case OMP_CLAUSE_HINT
:
1193 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1194 case OMP_CLAUSE_NUM_GANGS
:
1195 case OMP_CLAUSE_NUM_WORKERS
:
1196 case OMP_CLAUSE_VECTOR_LENGTH
:
1197 case OMP_CLAUSE_GANG
:
1198 case OMP_CLAUSE_WORKER
:
1199 case OMP_CLAUSE_VECTOR
:
1200 case OMP_CLAUSE_ASYNC
:
1201 case OMP_CLAUSE_WAIT
:
1202 /* Several OpenACC clauses have optional arguments. Check if they
1204 if (OMP_CLAUSE_OPERAND (clause
, 0))
1206 wi
->val_only
= true;
1208 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1212 /* The gang clause accepts two arguments. */
1213 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
1214 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
1216 wi
->val_only
= true;
1218 convert_nonlocal_reference_op
1219 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
1223 case OMP_CLAUSE_DIST_SCHEDULE
:
1224 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1226 wi
->val_only
= true;
1228 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1233 case OMP_CLAUSE_MAP
:
1235 case OMP_CLAUSE_FROM
:
1236 if (OMP_CLAUSE_SIZE (clause
))
1238 wi
->val_only
= true;
1240 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause
),
1243 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1244 goto do_decl_clause
;
1245 wi
->val_only
= true;
1247 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_nonlocal_reference_op
,
1251 case OMP_CLAUSE_ALIGNED
:
1252 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1254 wi
->val_only
= true;
1256 convert_nonlocal_reference_op
1257 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1259 /* Like do_decl_clause, but don't add any suppression. */
1260 decl
= OMP_CLAUSE_DECL (clause
);
1262 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1264 if (decl_function_context (decl
) != info
->context
)
1266 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1267 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1272 case OMP_CLAUSE_NOWAIT
:
1273 case OMP_CLAUSE_ORDERED
:
1274 case OMP_CLAUSE_DEFAULT
:
1275 case OMP_CLAUSE_COPYIN
:
1276 case OMP_CLAUSE_COLLAPSE
:
1277 case OMP_CLAUSE_UNTIED
:
1278 case OMP_CLAUSE_MERGEABLE
:
1279 case OMP_CLAUSE_PROC_BIND
:
1280 case OMP_CLAUSE_NOGROUP
:
1281 case OMP_CLAUSE_THREADS
:
1282 case OMP_CLAUSE_SIMD
:
1283 case OMP_CLAUSE_DEFAULTMAP
:
1284 case OMP_CLAUSE_SEQ
:
1285 case OMP_CLAUSE_INDEPENDENT
:
1286 case OMP_CLAUSE_AUTO
:
1289 /* OpenACC tile clauses are discarded during gimplification. */
1290 case OMP_CLAUSE_TILE
:
1291 /* The following clause belongs to the OpenACC cache directive, which
1292 is discarded during gimplification. */
1293 case OMP_CLAUSE__CACHE_
:
1294 /* The following clauses are only allowed in the OpenMP declare simd
1295 directive, so not seen here. */
1296 case OMP_CLAUSE_UNIFORM
:
1297 case OMP_CLAUSE_INBRANCH
:
1298 case OMP_CLAUSE_NOTINBRANCH
:
1299 /* The following clauses are only allowed on OpenMP cancel and
1300 cancellation point directives, which at this point have already
1301 been lowered into a function call. */
1302 case OMP_CLAUSE_FOR
:
1303 case OMP_CLAUSE_PARALLEL
:
1304 case OMP_CLAUSE_SECTIONS
:
1305 case OMP_CLAUSE_TASKGROUP
:
1306 /* The following clauses are only added during OMP lowering; nested
1307 function decomposition happens before that. */
1308 case OMP_CLAUSE__LOOPTEMP_
:
1309 case OMP_CLAUSE__SIMDUID_
:
1310 case OMP_CLAUSE__GRIDDIM_
:
1311 /* Anything else. */
1317 info
->suppress_expansion
= new_suppress
;
1320 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1321 switch (OMP_CLAUSE_CODE (clause
))
1323 case OMP_CLAUSE_REDUCTION
:
1324 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1327 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1328 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1330 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1331 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1333 walk_body (convert_nonlocal_reference_stmt
,
1334 convert_nonlocal_reference_op
, info
,
1335 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1336 walk_body (convert_nonlocal_reference_stmt
,
1337 convert_nonlocal_reference_op
, info
,
1338 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1339 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1341 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1342 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1347 case OMP_CLAUSE_LASTPRIVATE
:
1348 walk_body (convert_nonlocal_reference_stmt
,
1349 convert_nonlocal_reference_op
, info
,
1350 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1353 case OMP_CLAUSE_LINEAR
:
1354 walk_body (convert_nonlocal_reference_stmt
,
1355 convert_nonlocal_reference_op
, info
,
1356 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1366 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1369 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1371 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1372 type
= TREE_TYPE (type
);
1374 if (TYPE_NAME (type
)
1375 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1376 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1377 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1379 while (POINTER_TYPE_P (type
)
1380 || TREE_CODE (type
) == VECTOR_TYPE
1381 || TREE_CODE (type
) == FUNCTION_TYPE
1382 || TREE_CODE (type
) == METHOD_TYPE
)
1383 type
= TREE_TYPE (type
);
1385 if (TREE_CODE (type
) == ARRAY_TYPE
)
1389 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1390 domain
= TYPE_DOMAIN (type
);
1393 t
= TYPE_MIN_VALUE (domain
);
1394 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1395 && decl_function_context (t
) != info
->context
)
1396 get_nonlocal_debug_decl (info
, t
);
1397 t
= TYPE_MAX_VALUE (domain
);
1398 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1399 && decl_function_context (t
) != info
->context
)
1400 get_nonlocal_debug_decl (info
, t
);
1405 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1409 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1413 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
1415 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1416 && DECL_HAS_VALUE_EXPR_P (var
)
1417 && decl_function_context (var
) != info
->context
)
1418 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1421 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1422 PARM_DECLs that belong to outer functions. This handles statements
1423 that are not handled via the standard recursion done in
1424 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1425 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1426 operands of STMT have been handled by this function. */
1429 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1430 struct walk_stmt_info
*wi
)
1432 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1433 tree save_local_var_chain
;
1434 bitmap save_suppress
;
1435 gimple
*stmt
= gsi_stmt (*gsi
);
1437 switch (gimple_code (stmt
))
1440 /* Don't walk non-local gotos for now. */
1441 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1443 wi
->val_only
= true;
1445 *handled_ops_p
= false;
1450 case GIMPLE_OMP_PARALLEL
:
1451 case GIMPLE_OMP_TASK
:
1452 save_suppress
= info
->suppress_expansion
;
1453 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1457 decl
= get_chain_decl (info
);
1458 c
= build_omp_clause (gimple_location (stmt
),
1459 OMP_CLAUSE_FIRSTPRIVATE
);
1460 OMP_CLAUSE_DECL (c
) = decl
;
1461 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1462 gimple_omp_taskreg_set_clauses (stmt
, c
);
1465 save_local_var_chain
= info
->new_local_var_chain
;
1466 info
->new_local_var_chain
= NULL
;
1468 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1469 info
, gimple_omp_body_ptr (stmt
));
1471 if (info
->new_local_var_chain
)
1472 declare_vars (info
->new_local_var_chain
,
1473 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1475 info
->new_local_var_chain
= save_local_var_chain
;
1476 info
->suppress_expansion
= save_suppress
;
1479 case GIMPLE_OMP_FOR
:
1480 save_suppress
= info
->suppress_expansion
;
1481 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1482 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
1483 convert_nonlocal_reference_stmt
,
1484 convert_nonlocal_reference_op
, info
);
1485 walk_body (convert_nonlocal_reference_stmt
,
1486 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1487 info
->suppress_expansion
= save_suppress
;
1490 case GIMPLE_OMP_SECTIONS
:
1491 save_suppress
= info
->suppress_expansion
;
1492 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1493 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1494 info
, gimple_omp_body_ptr (stmt
));
1495 info
->suppress_expansion
= save_suppress
;
1498 case GIMPLE_OMP_SINGLE
:
1499 save_suppress
= info
->suppress_expansion
;
1500 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1501 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1502 info
, gimple_omp_body_ptr (stmt
));
1503 info
->suppress_expansion
= save_suppress
;
1506 case GIMPLE_OMP_TARGET
:
1507 if (!is_gimple_omp_offloaded (stmt
))
1509 save_suppress
= info
->suppress_expansion
;
1510 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1512 info
->suppress_expansion
= save_suppress
;
1513 walk_body (convert_nonlocal_reference_stmt
,
1514 convert_nonlocal_reference_op
, info
,
1515 gimple_omp_body_ptr (stmt
));
1518 save_suppress
= info
->suppress_expansion
;
1519 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1523 decl
= get_chain_decl (info
);
1524 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1525 OMP_CLAUSE_DECL (c
) = decl
;
1526 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TO
);
1527 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
1528 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1529 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
1532 save_local_var_chain
= info
->new_local_var_chain
;
1533 info
->new_local_var_chain
= NULL
;
1535 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1536 info
, gimple_omp_body_ptr (stmt
));
1538 if (info
->new_local_var_chain
)
1539 declare_vars (info
->new_local_var_chain
,
1540 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1542 info
->new_local_var_chain
= save_local_var_chain
;
1543 info
->suppress_expansion
= save_suppress
;
1546 case GIMPLE_OMP_TEAMS
:
1547 save_suppress
= info
->suppress_expansion
;
1548 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1549 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1550 info
, gimple_omp_body_ptr (stmt
));
1551 info
->suppress_expansion
= save_suppress
;
1554 case GIMPLE_OMP_SECTION
:
1555 case GIMPLE_OMP_MASTER
:
1556 case GIMPLE_OMP_TASKGROUP
:
1557 case GIMPLE_OMP_ORDERED
:
1558 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1559 info
, gimple_omp_body_ptr (stmt
));
1564 gbind
*bind_stmt
= as_a
<gbind
*> (stmt
);
1565 if (!optimize
&& gimple_bind_block (bind_stmt
))
1566 note_nonlocal_block_vlas (info
, gimple_bind_block (bind_stmt
));
1568 for (tree var
= gimple_bind_vars (bind_stmt
); var
; var
= DECL_CHAIN (var
))
1569 if (TREE_CODE (var
) == NAMELIST_DECL
)
1571 /* Adjust decls mentioned in NAMELIST_DECL. */
1572 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
1576 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
1579 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1581 if (decl_function_context (decl
) != info
->context
)
1582 CONSTRUCTOR_ELT (decls
, i
)->value
1583 = get_nonlocal_debug_decl (info
, decl
);
1587 *handled_ops_p
= false;
1591 wi
->val_only
= true;
1593 *handled_ops_p
= false;
1597 /* For every other statement that we are not interested in
1598 handling here, let the walker traverse the operands. */
1599 *handled_ops_p
= false;
1603 /* We have handled all of STMT operands, no need to traverse the operands. */
1604 *handled_ops_p
= true;
1609 /* A subroutine of convert_local_reference. Create a local variable
1610 in the parent function with DECL_VALUE_EXPR set to reference the
1611 field in FRAME. This is used both for debug info and in OMP
1615 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1619 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1623 /* Make sure frame_decl gets created. */
1624 (void) get_frame_type (info
);
1625 x
= info
->frame_decl
;
1626 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1628 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1629 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1630 DECL_CONTEXT (new_decl
) = info
->context
;
1631 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1632 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1633 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1634 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1635 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1636 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1637 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1638 if ((TREE_CODE (decl
) == PARM_DECL
1639 || TREE_CODE (decl
) == RESULT_DECL
1641 && DECL_BY_REFERENCE (decl
))
1642 DECL_BY_REFERENCE (new_decl
) = 1;
1644 SET_DECL_VALUE_EXPR (new_decl
, x
);
1645 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1648 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1649 info
->debug_var_chain
= new_decl
;
1651 /* Do not emit debug info twice. */
1652 DECL_IGNORED_P (decl
) = 1;
1658 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1659 and PARM_DECLs that were referenced by inner nested functions.
1660 The rewrite will be a structure reference to the local frame variable. */
1662 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1665 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1667 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1668 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1669 tree t
= *tp
, field
, x
;
1673 switch (TREE_CODE (t
))
1676 /* Non-automatic variables are never processed. */
1677 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1682 if (decl_function_context (t
) == info
->context
)
1684 /* If we copied a pointer to the frame, then the original decl
1685 is used unchanged in the parent function. */
1686 if (use_pointer_in_frame (t
))
1689 /* No need to transform anything if no child references the
1691 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1696 x
= get_local_debug_decl (info
, t
, field
);
1697 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1698 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1703 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1705 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1713 save_val_only
= wi
->val_only
;
1714 wi
->val_only
= false;
1716 wi
->changed
= false;
1717 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1718 wi
->val_only
= save_val_only
;
1720 /* If we converted anything ... */
1725 /* Then the frame decl is now addressable. */
1726 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1728 save_context
= current_function_decl
;
1729 current_function_decl
= info
->context
;
1730 recompute_tree_invariant_for_addr_expr (t
);
1731 current_function_decl
= save_context
;
1733 /* If we are in a context where we only accept values, then
1734 compute the address into a temporary. */
1736 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1745 case ARRAY_RANGE_REF
:
1747 /* Go down this entire nest and just look at the final prefix and
1748 anything that describes the references. Otherwise, we lose track
1749 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1750 save_val_only
= wi
->val_only
;
1751 wi
->val_only
= true;
1753 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1755 if (TREE_CODE (t
) == COMPONENT_REF
)
1756 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1758 else if (TREE_CODE (t
) == ARRAY_REF
1759 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1761 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1763 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1765 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1769 wi
->val_only
= false;
1770 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1771 wi
->val_only
= save_val_only
;
1775 save_val_only
= wi
->val_only
;
1776 wi
->val_only
= true;
1778 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1780 /* We need to re-fold the MEM_REF as component references as
1781 part of a ADDR_EXPR address are not allowed. But we cannot
1782 fold here, as the chain record type is not yet finalized. */
1783 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1784 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1785 info
->mem_refs
->add (tp
);
1786 wi
->val_only
= save_val_only
;
1789 case VIEW_CONVERT_EXPR
:
1790 /* Just request to look at the subtrees, leaving val_only and lhs
1791 untouched. This might actually be for !val_only + lhs, in which
1792 case we don't want to force a replacement by a temporary. */
1797 if (!IS_TYPE_OR_DECL_P (t
))
1800 wi
->val_only
= true;
1809 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1810 struct walk_stmt_info
*);
1812 /* Helper for convert_local_reference. Convert all the references in
1813 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1816 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1818 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1819 bool need_frame
= false, need_stmts
= false;
1822 bitmap new_suppress
;
1824 new_suppress
= BITMAP_GGC_ALLOC ();
1825 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1827 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1829 switch (OMP_CLAUSE_CODE (clause
))
1831 case OMP_CLAUSE_REDUCTION
:
1832 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1834 goto do_decl_clause
;
1836 case OMP_CLAUSE_LASTPRIVATE
:
1837 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1839 goto do_decl_clause
;
1841 case OMP_CLAUSE_LINEAR
:
1842 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1844 wi
->val_only
= true;
1846 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
), &dummy
,
1848 goto do_decl_clause
;
1850 case OMP_CLAUSE_PRIVATE
:
1851 case OMP_CLAUSE_FIRSTPRIVATE
:
1852 case OMP_CLAUSE_COPYPRIVATE
:
1853 case OMP_CLAUSE_SHARED
:
1854 case OMP_CLAUSE_TO_DECLARE
:
1855 case OMP_CLAUSE_LINK
:
1856 case OMP_CLAUSE_USE_DEVICE_PTR
:
1857 case OMP_CLAUSE_IS_DEVICE_PTR
:
1859 decl
= OMP_CLAUSE_DECL (clause
);
1861 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1863 if (decl_function_context (decl
) == info
->context
1864 && !use_pointer_in_frame (decl
))
1866 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1869 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1870 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1871 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1872 OMP_CLAUSE_DECL (clause
)
1873 = get_local_debug_decl (info
, decl
, field
);
1879 case OMP_CLAUSE_SCHEDULE
:
1880 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1883 case OMP_CLAUSE_FINAL
:
1885 case OMP_CLAUSE_NUM_THREADS
:
1886 case OMP_CLAUSE_DEPEND
:
1887 case OMP_CLAUSE_DEVICE
:
1888 case OMP_CLAUSE_NUM_TEAMS
:
1889 case OMP_CLAUSE_THREAD_LIMIT
:
1890 case OMP_CLAUSE_SAFELEN
:
1891 case OMP_CLAUSE_SIMDLEN
:
1892 case OMP_CLAUSE_PRIORITY
:
1893 case OMP_CLAUSE_GRAINSIZE
:
1894 case OMP_CLAUSE_NUM_TASKS
:
1895 case OMP_CLAUSE_HINT
:
1896 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1897 case OMP_CLAUSE_NUM_GANGS
:
1898 case OMP_CLAUSE_NUM_WORKERS
:
1899 case OMP_CLAUSE_VECTOR_LENGTH
:
1900 case OMP_CLAUSE_GANG
:
1901 case OMP_CLAUSE_WORKER
:
1902 case OMP_CLAUSE_VECTOR
:
1903 case OMP_CLAUSE_ASYNC
:
1904 case OMP_CLAUSE_WAIT
:
1905 /* Several OpenACC clauses have optional arguments. Check if they
1907 if (OMP_CLAUSE_OPERAND (clause
, 0))
1909 wi
->val_only
= true;
1911 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1915 /* The gang clause accepts two arguments. */
1916 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
1917 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
1919 wi
->val_only
= true;
1921 convert_nonlocal_reference_op
1922 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
1926 case OMP_CLAUSE_DIST_SCHEDULE
:
1927 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1929 wi
->val_only
= true;
1931 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1936 case OMP_CLAUSE_MAP
:
1938 case OMP_CLAUSE_FROM
:
1939 if (OMP_CLAUSE_SIZE (clause
))
1941 wi
->val_only
= true;
1943 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause
),
1946 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1947 goto do_decl_clause
;
1948 wi
->val_only
= true;
1950 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_local_reference_op
,
1954 case OMP_CLAUSE_ALIGNED
:
1955 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1957 wi
->val_only
= true;
1959 convert_local_reference_op
1960 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1962 /* Like do_decl_clause, but don't add any suppression. */
1963 decl
= OMP_CLAUSE_DECL (clause
);
1965 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1967 if (decl_function_context (decl
) == info
->context
1968 && !use_pointer_in_frame (decl
))
1970 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1973 OMP_CLAUSE_DECL (clause
)
1974 = get_local_debug_decl (info
, decl
, field
);
1980 case OMP_CLAUSE_NOWAIT
:
1981 case OMP_CLAUSE_ORDERED
:
1982 case OMP_CLAUSE_DEFAULT
:
1983 case OMP_CLAUSE_COPYIN
:
1984 case OMP_CLAUSE_COLLAPSE
:
1985 case OMP_CLAUSE_UNTIED
:
1986 case OMP_CLAUSE_MERGEABLE
:
1987 case OMP_CLAUSE_PROC_BIND
:
1988 case OMP_CLAUSE_NOGROUP
:
1989 case OMP_CLAUSE_THREADS
:
1990 case OMP_CLAUSE_SIMD
:
1991 case OMP_CLAUSE_DEFAULTMAP
:
1992 case OMP_CLAUSE_SEQ
:
1993 case OMP_CLAUSE_INDEPENDENT
:
1994 case OMP_CLAUSE_AUTO
:
1997 /* OpenACC tile clauses are discarded during gimplification. */
1998 case OMP_CLAUSE_TILE
:
1999 /* The following clause belongs to the OpenACC cache directive, which
2000 is discarded during gimplification. */
2001 case OMP_CLAUSE__CACHE_
:
2002 /* The following clauses are only allowed in the OpenMP declare simd
2003 directive, so not seen here. */
2004 case OMP_CLAUSE_UNIFORM
:
2005 case OMP_CLAUSE_INBRANCH
:
2006 case OMP_CLAUSE_NOTINBRANCH
:
2007 /* The following clauses are only allowed on OpenMP cancel and
2008 cancellation point directives, which at this point have already
2009 been lowered into a function call. */
2010 case OMP_CLAUSE_FOR
:
2011 case OMP_CLAUSE_PARALLEL
:
2012 case OMP_CLAUSE_SECTIONS
:
2013 case OMP_CLAUSE_TASKGROUP
:
2014 /* The following clauses are only added during OMP lowering; nested
2015 function decomposition happens before that. */
2016 case OMP_CLAUSE__LOOPTEMP_
:
2017 case OMP_CLAUSE__SIMDUID_
:
2018 case OMP_CLAUSE__GRIDDIM_
:
2019 /* Anything else. */
2025 info
->suppress_expansion
= new_suppress
;
2028 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
2029 switch (OMP_CLAUSE_CODE (clause
))
2031 case OMP_CLAUSE_REDUCTION
:
2032 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2035 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
2036 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2038 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2039 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2041 walk_body (convert_local_reference_stmt
,
2042 convert_local_reference_op
, info
,
2043 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
2044 walk_body (convert_local_reference_stmt
,
2045 convert_local_reference_op
, info
,
2046 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
2047 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2049 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2050 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2055 case OMP_CLAUSE_LASTPRIVATE
:
2056 walk_body (convert_local_reference_stmt
,
2057 convert_local_reference_op
, info
,
2058 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
2061 case OMP_CLAUSE_LINEAR
:
2062 walk_body (convert_local_reference_stmt
,
2063 convert_local_reference_op
, info
,
2064 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
2075 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2076 and PARM_DECLs that were referenced by inner nested functions.
2077 The rewrite will be a structure reference to the local frame variable. */
2080 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2081 struct walk_stmt_info
*wi
)
2083 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2084 tree save_local_var_chain
;
2085 bitmap save_suppress
;
2086 gimple
*stmt
= gsi_stmt (*gsi
);
2088 switch (gimple_code (stmt
))
2090 case GIMPLE_OMP_PARALLEL
:
2091 case GIMPLE_OMP_TASK
:
2092 save_suppress
= info
->suppress_expansion
;
2093 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
2097 (void) get_frame_type (info
);
2098 c
= build_omp_clause (gimple_location (stmt
),
2100 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2101 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2102 gimple_omp_taskreg_set_clauses (stmt
, c
);
2105 save_local_var_chain
= info
->new_local_var_chain
;
2106 info
->new_local_var_chain
= NULL
;
2108 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2109 gimple_omp_body_ptr (stmt
));
2111 if (info
->new_local_var_chain
)
2112 declare_vars (info
->new_local_var_chain
,
2113 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2114 info
->new_local_var_chain
= save_local_var_chain
;
2115 info
->suppress_expansion
= save_suppress
;
2118 case GIMPLE_OMP_FOR
:
2119 save_suppress
= info
->suppress_expansion
;
2120 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
2121 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
2122 convert_local_reference_stmt
,
2123 convert_local_reference_op
, info
);
2124 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2125 info
, gimple_omp_body_ptr (stmt
));
2126 info
->suppress_expansion
= save_suppress
;
2129 case GIMPLE_OMP_SECTIONS
:
2130 save_suppress
= info
->suppress_expansion
;
2131 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
2132 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2133 info
, gimple_omp_body_ptr (stmt
));
2134 info
->suppress_expansion
= save_suppress
;
2137 case GIMPLE_OMP_SINGLE
:
2138 save_suppress
= info
->suppress_expansion
;
2139 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
2140 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2141 info
, gimple_omp_body_ptr (stmt
));
2142 info
->suppress_expansion
= save_suppress
;
2145 case GIMPLE_OMP_TARGET
:
2146 if (!is_gimple_omp_offloaded (stmt
))
2148 save_suppress
= info
->suppress_expansion
;
2149 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
2150 info
->suppress_expansion
= save_suppress
;
2151 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2152 info
, gimple_omp_body_ptr (stmt
));
2155 save_suppress
= info
->suppress_expansion
;
2156 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
))
2159 (void) get_frame_type (info
);
2160 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2161 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2162 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2163 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2164 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2165 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2168 save_local_var_chain
= info
->new_local_var_chain
;
2169 info
->new_local_var_chain
= NULL
;
2171 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2172 gimple_omp_body_ptr (stmt
));
2174 if (info
->new_local_var_chain
)
2175 declare_vars (info
->new_local_var_chain
,
2176 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2177 info
->new_local_var_chain
= save_local_var_chain
;
2178 info
->suppress_expansion
= save_suppress
;
2181 case GIMPLE_OMP_TEAMS
:
2182 save_suppress
= info
->suppress_expansion
;
2183 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
2184 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2185 info
, gimple_omp_body_ptr (stmt
));
2186 info
->suppress_expansion
= save_suppress
;
2189 case GIMPLE_OMP_SECTION
:
2190 case GIMPLE_OMP_MASTER
:
2191 case GIMPLE_OMP_TASKGROUP
:
2192 case GIMPLE_OMP_ORDERED
:
2193 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2194 info
, gimple_omp_body_ptr (stmt
));
2198 wi
->val_only
= true;
2200 *handled_ops_p
= false;
2204 if (gimple_clobber_p (stmt
))
2206 tree lhs
= gimple_assign_lhs (stmt
);
2207 if (!use_pointer_in_frame (lhs
)
2208 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
2210 gsi_replace (gsi
, gimple_build_nop (), true);
2214 *handled_ops_p
= false;
2218 for (tree var
= gimple_bind_vars (as_a
<gbind
*> (stmt
));
2220 var
= DECL_CHAIN (var
))
2221 if (TREE_CODE (var
) == NAMELIST_DECL
)
2223 /* Adjust decls mentioned in NAMELIST_DECL. */
2224 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
2228 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
2231 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2233 if (decl_function_context (decl
) == info
->context
2234 && !use_pointer_in_frame (decl
))
2236 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2239 CONSTRUCTOR_ELT (decls
, i
)->value
2240 = get_local_debug_decl (info
, decl
, field
);
2246 *handled_ops_p
= false;
2250 /* For every other statement that we are not interested in
2251 handling here, let the walker traverse the operands. */
2252 *handled_ops_p
= false;
2256 /* Indicate that we have handled all the operands ourselves. */
2257 *handled_ops_p
= true;
2262 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2263 that reference labels from outer functions. The rewrite will be a
2264 call to __builtin_nonlocal_goto. */
2267 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2268 struct walk_stmt_info
*wi
)
2270 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2271 tree label
, new_label
, target_context
, x
, field
;
2273 gimple
*stmt
= gsi_stmt (*gsi
);
2275 if (gimple_code (stmt
) != GIMPLE_GOTO
)
2277 *handled_ops_p
= false;
2281 label
= gimple_goto_dest (stmt
);
2282 if (TREE_CODE (label
) != LABEL_DECL
)
2284 *handled_ops_p
= false;
2288 target_context
= decl_function_context (label
);
2289 if (target_context
== info
->context
)
2291 *handled_ops_p
= false;
2295 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
2298 /* The original user label may also be use for a normal goto, therefore
2299 we must create a new label that will actually receive the abnormal
2300 control transfer. This new label will be marked LABEL_NONLOCAL; this
2301 mark will trigger proper behavior in the cfg, as well as cause the
2302 (hairy target-specific) non-local goto receiver code to be generated
2303 when we expand rtl. Enter this association into var_map so that we
2304 can insert the new label into the IL during a second pass. */
2305 tree
*slot
= &i
->var_map
->get_or_insert (label
);
2308 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
2309 DECL_NONLOCAL (new_label
) = 1;
2315 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2316 field
= get_nl_goto_field (i
);
2317 x
= get_frame_field (info
, target_context
, field
, gsi
);
2319 x
= gsi_gimplify_val (info
, x
, gsi
);
2320 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
2321 2, build_addr (new_label
), x
);
2322 gsi_replace (gsi
, call
, false);
2324 /* We have handled all of STMT's operands, no need to keep going. */
2325 *handled_ops_p
= true;
2330 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2331 are referenced via nonlocal goto from a nested function. The rewrite
2332 will involve installing a newly generated DECL_NONLOCAL label, and
2333 (potentially) a branch around the rtl gunk that is assumed to be
2334 attached to such a label. */
2337 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2338 struct walk_stmt_info
*wi
)
2340 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2341 tree label
, new_label
;
2342 gimple_stmt_iterator tmp_gsi
;
2343 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (*gsi
));
2347 *handled_ops_p
= false;
2351 label
= gimple_label_label (stmt
);
2353 tree
*slot
= info
->var_map
->get (label
);
2356 *handled_ops_p
= false;
2360 /* If there's any possibility that the previous statement falls through,
2361 then we must branch around the new non-local label. */
2363 gsi_prev (&tmp_gsi
);
2364 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
2366 gimple
*stmt
= gimple_build_goto (label
);
2367 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2370 new_label
= (tree
) *slot
;
2371 stmt
= gimple_build_label (new_label
);
2372 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2374 *handled_ops_p
= true;
2379 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2380 of nested functions that require the use of trampolines. The rewrite
2381 will involve a reference a trampoline generated for the occasion. */
2384 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
2386 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2387 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2388 tree t
= *tp
, decl
, target_context
, x
, builtin
;
2393 switch (TREE_CODE (t
))
2397 T.1 = &CHAIN->tramp;
2398 T.2 = __builtin_adjust_trampoline (T.1);
2399 T.3 = (func_type)T.2;
2402 decl
= TREE_OPERAND (t
, 0);
2403 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2406 /* Only need to process nested functions. */
2407 target_context
= decl_function_context (decl
);
2408 if (!target_context
)
2411 /* If the nested function doesn't use a static chain, then
2412 it doesn't need a trampoline. */
2413 if (!DECL_STATIC_CHAIN (decl
))
2416 /* If we don't want a trampoline, then don't build one. */
2417 if (TREE_NO_TRAMPOLINE (t
))
2420 /* Lookup the immediate parent of the callee, as that's where
2421 we need to insert the trampoline. */
2422 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
2425 /* Decide whether to generate a descriptor or a trampoline. */
2426 descr
= FUNC_ADDR_BY_DESCRIPTOR (t
) && !flag_trampolines
;
2429 x
= lookup_descr_for_decl (i
, decl
, INSERT
);
2431 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
2433 /* Compute the address of the field holding the trampoline. */
2434 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
2436 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
2438 /* Do machine-specific ugliness. Normally this will involve
2439 computing extra alignment, but it can really be anything. */
2441 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR
);
2443 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
2444 call
= gimple_build_call (builtin
, 1, x
);
2445 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
2447 /* Cast back to the proper function type. */
2448 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
2449 x
= init_tmp_var (info
, x
, &wi
->gsi
);
2455 if (!IS_TYPE_OR_DECL_P (t
))
2464 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2465 to addresses of nested functions that require the use of
2466 trampolines. The rewrite will involve a reference a trampoline
2467 generated for the occasion. */
2470 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2471 struct walk_stmt_info
*wi
)
2473 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2474 gimple
*stmt
= gsi_stmt (*gsi
);
2476 switch (gimple_code (stmt
))
2480 /* Only walk call arguments, lest we generate trampolines for
2482 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2483 for (i
= 0; i
< nargs
; i
++)
2484 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2489 case GIMPLE_OMP_TARGET
:
2490 if (!is_gimple_omp_offloaded (stmt
))
2492 *handled_ops_p
= false;
2496 case GIMPLE_OMP_PARALLEL
:
2497 case GIMPLE_OMP_TASK
:
2499 tree save_local_var_chain
= info
->new_local_var_chain
;
2500 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2501 info
->new_local_var_chain
= NULL
;
2502 char save_static_chain_added
= info
->static_chain_added
;
2503 info
->static_chain_added
= 0;
2504 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2505 info
, gimple_omp_body_ptr (stmt
));
2506 if (info
->new_local_var_chain
)
2507 declare_vars (info
->new_local_var_chain
,
2508 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2510 for (int i
= 0; i
< 2; i
++)
2513 if ((info
->static_chain_added
& (1 << i
)) == 0)
2515 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2516 /* Don't add CHAIN.* or FRAME.* twice. */
2517 for (c
= gimple_omp_taskreg_clauses (stmt
);
2519 c
= OMP_CLAUSE_CHAIN (c
))
2520 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2521 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2522 && OMP_CLAUSE_DECL (c
) == decl
)
2524 if (c
== NULL
&& gimple_code (stmt
) != GIMPLE_OMP_TARGET
)
2526 c
= build_omp_clause (gimple_location (stmt
),
2527 i
? OMP_CLAUSE_FIRSTPRIVATE
2528 : OMP_CLAUSE_SHARED
);
2529 OMP_CLAUSE_DECL (c
) = decl
;
2530 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2531 gimple_omp_taskreg_set_clauses (stmt
, c
);
2535 c
= build_omp_clause (gimple_location (stmt
),
2537 OMP_CLAUSE_DECL (c
) = decl
;
2538 OMP_CLAUSE_SET_MAP_KIND (c
,
2539 i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2540 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2541 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2542 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2546 info
->new_local_var_chain
= save_local_var_chain
;
2547 info
->static_chain_added
|= save_static_chain_added
;
2552 *handled_ops_p
= false;
2556 *handled_ops_p
= true;
2562 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2563 that reference nested functions to make sure that the static chain
2564 is set up properly for the call. */
2567 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2568 struct walk_stmt_info
*wi
)
2570 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2571 tree decl
, target_context
;
2572 char save_static_chain_added
;
2574 gimple
*stmt
= gsi_stmt (*gsi
);
2576 switch (gimple_code (stmt
))
2579 if (gimple_call_chain (stmt
))
2581 decl
= gimple_call_fndecl (stmt
);
2584 target_context
= decl_function_context (decl
);
2585 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2587 gimple_call_set_chain (as_a
<gcall
*> (stmt
),
2588 get_static_chain (info
, target_context
,
2590 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2594 case GIMPLE_OMP_PARALLEL
:
2595 case GIMPLE_OMP_TASK
:
2596 save_static_chain_added
= info
->static_chain_added
;
2597 info
->static_chain_added
= 0;
2598 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2599 for (i
= 0; i
< 2; i
++)
2602 if ((info
->static_chain_added
& (1 << i
)) == 0)
2604 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2605 /* Don't add CHAIN.* or FRAME.* twice. */
2606 for (c
= gimple_omp_taskreg_clauses (stmt
);
2608 c
= OMP_CLAUSE_CHAIN (c
))
2609 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2610 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2611 && OMP_CLAUSE_DECL (c
) == decl
)
2615 c
= build_omp_clause (gimple_location (stmt
),
2616 i
? OMP_CLAUSE_FIRSTPRIVATE
2617 : OMP_CLAUSE_SHARED
);
2618 OMP_CLAUSE_DECL (c
) = decl
;
2619 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2620 gimple_omp_taskreg_set_clauses (stmt
, c
);
2623 info
->static_chain_added
|= save_static_chain_added
;
2626 case GIMPLE_OMP_TARGET
:
2627 if (!is_gimple_omp_offloaded (stmt
))
2629 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2632 save_static_chain_added
= info
->static_chain_added
;
2633 info
->static_chain_added
= 0;
2634 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2635 for (i
= 0; i
< 2; i
++)
2638 if ((info
->static_chain_added
& (1 << i
)) == 0)
2640 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2641 /* Don't add CHAIN.* or FRAME.* twice. */
2642 for (c
= gimple_omp_target_clauses (stmt
);
2644 c
= OMP_CLAUSE_CHAIN (c
))
2645 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
2646 && OMP_CLAUSE_DECL (c
) == decl
)
2650 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2651 OMP_CLAUSE_DECL (c
) = decl
;
2652 OMP_CLAUSE_SET_MAP_KIND (c
, i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2653 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2654 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2655 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2659 info
->static_chain_added
|= save_static_chain_added
;
2662 case GIMPLE_OMP_FOR
:
2663 walk_body (convert_gimple_call
, NULL
, info
,
2664 gimple_omp_for_pre_body_ptr (stmt
));
2666 case GIMPLE_OMP_SECTIONS
:
2667 case GIMPLE_OMP_SECTION
:
2668 case GIMPLE_OMP_SINGLE
:
2669 case GIMPLE_OMP_TEAMS
:
2670 case GIMPLE_OMP_MASTER
:
2671 case GIMPLE_OMP_TASKGROUP
:
2672 case GIMPLE_OMP_ORDERED
:
2673 case GIMPLE_OMP_CRITICAL
:
2674 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2678 /* Keep looking for other operands. */
2679 *handled_ops_p
= false;
2683 *handled_ops_p
= true;
2687 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2688 call expressions. At the same time, determine if a nested function
2689 actually uses its static chain; if not, remember that. */
2692 convert_all_function_calls (struct nesting_info
*root
)
2694 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2695 struct nesting_info
*n
;
2697 /* First, optimistically clear static_chain for all decls that haven't
2698 used the static chain already for variable access. But always create
2699 it if not optimizing. This makes it possible to reconstruct the static
2700 nesting tree at run time and thus to resolve up-level references from
2701 within the debugger. */
2702 FOR_EACH_NEST_INFO (n
, root
)
2704 tree decl
= n
->context
;
2708 (void) get_frame_type (n
);
2710 (void) get_chain_decl (n
);
2712 else if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2714 DECL_STATIC_CHAIN (decl
) = 0;
2715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2716 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2717 lang_hooks
.decl_printable_name (decl
, 2));
2720 DECL_STATIC_CHAIN (decl
) = 1;
2721 chain_count
+= DECL_STATIC_CHAIN (decl
);
2724 /* Walk the functions and perform transformations. Note that these
2725 transformations can induce new uses of the static chain, which in turn
2726 require re-examining all users of the decl. */
2727 /* ??? It would make sense to try to use the call graph to speed this up,
2728 but the call graph hasn't really been built yet. Even if it did, we
2729 would still need to iterate in this loop since address-of references
2730 wouldn't show up in the callgraph anyway. */
2734 old_chain_count
= chain_count
;
2738 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2739 fputc ('\n', dump_file
);
2741 FOR_EACH_NEST_INFO (n
, root
)
2743 tree decl
= n
->context
;
2744 walk_function (convert_tramp_reference_stmt
,
2745 convert_tramp_reference_op
, n
);
2746 walk_function (convert_gimple_call
, NULL
, n
);
2747 chain_count
+= DECL_STATIC_CHAIN (decl
);
2750 while (chain_count
!= old_chain_count
);
2752 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2753 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2757 struct nesting_copy_body_data
2760 struct nesting_info
*root
;
2763 /* A helper subroutine for debug_var_chain type remapping. */
2766 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2768 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2769 tree
*slot
= nid
->root
->var_map
->get (decl
);
2772 return (tree
) *slot
;
2774 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2776 tree new_decl
= copy_decl_no_change (decl
, id
);
2777 DECL_ORIGINAL_TYPE (new_decl
)
2778 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2783 || TREE_CODE (decl
) == PARM_DECL
2784 || TREE_CODE (decl
) == RESULT_DECL
)
2787 return copy_decl_no_change (decl
, id
);
2790 /* A helper function for remap_vla_decls. See if *TP contains
2791 some remapped variables. */
2794 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2796 struct nesting_info
*root
= (struct nesting_info
*) data
;
2802 tree
*slot
= root
->var_map
->get (t
);
2810 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2814 remap_vla_decls (tree block
, struct nesting_info
*root
)
2816 tree var
, subblock
, val
, type
;
2817 struct nesting_copy_body_data id
;
2819 for (subblock
= BLOCK_SUBBLOCKS (block
);
2821 subblock
= BLOCK_CHAIN (subblock
))
2822 remap_vla_decls (subblock
, root
);
2824 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
2825 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
2827 val
= DECL_VALUE_EXPR (var
);
2828 type
= TREE_TYPE (var
);
2830 if (!(TREE_CODE (val
) == INDIRECT_REF
2831 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2832 && variably_modified_type_p (type
, NULL
)))
2835 if (root
->var_map
->get (TREE_OPERAND (val
, 0))
2836 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2840 if (var
== NULL_TREE
)
2843 memset (&id
, 0, sizeof (id
));
2844 id
.cb
.copy_decl
= nesting_copy_decl
;
2845 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
2848 for (; var
; var
= DECL_CHAIN (var
))
2849 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
2851 struct nesting_info
*i
;
2854 val
= DECL_VALUE_EXPR (var
);
2855 type
= TREE_TYPE (var
);
2857 if (!(TREE_CODE (val
) == INDIRECT_REF
2858 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2859 && variably_modified_type_p (type
, NULL
)))
2862 tree
*slot
= root
->var_map
->get (TREE_OPERAND (val
, 0));
2863 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2866 context
= decl_function_context (var
);
2867 for (i
= root
; i
; i
= i
->outer
)
2868 if (i
->context
== context
)
2874 /* Fully expand value expressions. This avoids having debug variables
2875 only referenced from them and that can be swept during GC. */
2878 tree t
= (tree
) *slot
;
2879 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
2880 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
2883 id
.cb
.src_fn
= i
->context
;
2884 id
.cb
.dst_fn
= i
->context
;
2885 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2887 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2888 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2890 newt
= TREE_TYPE (newt
);
2891 type
= TREE_TYPE (type
);
2893 if (TYPE_NAME (newt
)
2894 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2895 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2897 && TYPE_NAME (newt
) == TYPE_NAME (type
))
2898 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2900 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2901 if (val
!= DECL_VALUE_EXPR (var
))
2902 SET_DECL_VALUE_EXPR (var
, val
);
2905 delete id
.cb
.decl_map
;
2908 /* Fold the MEM_REF *E. */
2910 fold_mem_refs (tree
*const &e
, void *data ATTRIBUTE_UNUSED
)
2912 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
2913 *ref_p
= fold (*ref_p
);
2917 /* Given DECL, a nested function, build an initialization call for FIELD,
2918 the trampoline or descriptor for DECL, using FUNC as the function. */
2921 build_init_call_stmt (struct nesting_info
*info
, tree decl
, tree field
,
2924 tree arg1
, arg2
, arg3
, x
;
2926 gcc_assert (DECL_STATIC_CHAIN (decl
));
2927 arg3
= build_addr (info
->frame_decl
);
2929 arg2
= build_addr (decl
);
2931 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2932 info
->frame_decl
, field
, NULL_TREE
);
2933 arg1
= build_addr (x
);
2935 return gimple_build_call (func
, 3, arg1
, arg2
, arg3
);
2938 /* Do "everything else" to clean up or complete state collected by the various
2939 walking passes -- create a field to hold the frame base address, lay out the
2940 types and decls, generate code to initialize the frame decl, store critical
2941 expressions in the struct function for rtl to find. */
2944 finalize_nesting_tree_1 (struct nesting_info
*root
)
2946 gimple_seq stmt_list
;
2948 tree context
= root
->context
;
2949 struct function
*sf
;
2953 /* If we created a non-local frame type or decl, we need to lay them
2954 out at this time. */
2955 if (root
->frame_type
)
2957 /* Debugging information needs to compute the frame base address of the
2958 parent frame out of the static chain from the nested frame.
2960 The static chain is the address of the FRAME record, so one could
2961 imagine it would be possible to compute the frame base address just
2962 adding a constant offset to this address. Unfortunately, this is not
2963 possible: if the FRAME object has alignment constraints that are
2964 stronger than the stack, then the offset between the frame base and
2965 the FRAME object will be dynamic.
2967 What we do instead is to append a field to the FRAME object that holds
2968 the frame base address: then debug info just has to fetch this
2971 /* Debugging information will refer to the CFA as the frame base
2972 address: we will do the same here. */
2973 const tree frame_addr_fndecl
2974 = builtin_decl_explicit (BUILT_IN_DWARF_CFA
);
2976 /* Create a field in the FRAME record to hold the frame base address for
2977 this stack frame. Since it will be used only by the debugger, put it
2978 at the end of the record in order not to shift all other offsets. */
2979 tree fb_decl
= make_node (FIELD_DECL
);
2981 DECL_NAME (fb_decl
) = get_identifier ("FRAME_BASE.PARENT");
2982 TREE_TYPE (fb_decl
) = ptr_type_node
;
2983 TREE_ADDRESSABLE (fb_decl
) = 1;
2984 DECL_CONTEXT (fb_decl
) = root
->frame_type
;
2985 TYPE_FIELDS (root
->frame_type
) = chainon (TYPE_FIELDS (root
->frame_type
),
2988 /* In some cases the frame type will trigger the -Wpadded warning.
2989 This is not helpful; suppress it. */
2990 int save_warn_padded
= warn_padded
;
2992 layout_type (root
->frame_type
);
2993 warn_padded
= save_warn_padded
;
2994 layout_decl (root
->frame_decl
, 0);
2996 /* Initialize the frame base address field. If the builtin we need is
2997 not available, set it to NULL so that debugging information does not
2999 tree fb_ref
= build3 (COMPONENT_REF
, TREE_TYPE (fb_decl
),
3000 root
->frame_decl
, fb_decl
, NULL_TREE
);
3003 if (frame_addr_fndecl
!= NULL_TREE
)
3005 gcall
*fb_gimple
= gimple_build_call (frame_addr_fndecl
, 1,
3007 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3009 fb_tmp
= init_tmp_var_with_call (root
, &gsi
, fb_gimple
);
3012 fb_tmp
= build_int_cst (TREE_TYPE (fb_ref
), 0);
3013 gimple_seq_add_stmt (&stmt_list
,
3014 gimple_build_assign (fb_ref
, fb_tmp
));
3016 /* Remove root->frame_decl from root->new_local_var_chain, so
3017 that we can declare it also in the lexical blocks, which
3018 helps ensure virtual regs that end up appearing in its RTL
3019 expression get substituted in instantiate_virtual_regs(). */
3021 for (adjust
= &root
->new_local_var_chain
;
3022 *adjust
!= root
->frame_decl
;
3023 adjust
= &DECL_CHAIN (*adjust
))
3024 gcc_assert (DECL_CHAIN (*adjust
));
3025 *adjust
= DECL_CHAIN (*adjust
);
3027 DECL_CHAIN (root
->frame_decl
) = NULL_TREE
;
3028 declare_vars (root
->frame_decl
,
3029 gimple_seq_first_stmt (gimple_body (context
)), true);
3032 /* If any parameters were referenced non-locally, then we need to
3033 insert a copy. Likewise, if any variables were referenced by
3034 pointer, we need to initialize the address. */
3035 if (root
->any_parm_remapped
)
3038 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
3042 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
3046 if (use_pointer_in_frame (p
))
3051 /* If the assignment is from a non-register the stmt is
3052 not valid gimple. Make it so by using a temporary instead. */
3053 if (!is_gimple_reg (x
)
3054 && is_gimple_reg_type (TREE_TYPE (x
)))
3056 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3057 x
= init_tmp_var (root
, x
, &gsi
);
3060 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3061 root
->frame_decl
, field
, NULL_TREE
);
3062 stmt
= gimple_build_assign (y
, x
);
3063 gimple_seq_add_stmt (&stmt_list
, stmt
);
3067 /* If a chain_field was created, then it needs to be initialized
3069 if (root
->chain_field
)
3071 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
3072 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
3073 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
3074 gimple_seq_add_stmt (&stmt_list
, stmt
);
3077 /* If trampolines were created, then we need to initialize them. */
3078 if (root
->any_tramp_created
)
3080 struct nesting_info
*i
;
3081 for (i
= root
->inner
; i
; i
= i
->next
)
3085 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
3089 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
3090 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3091 gimple_seq_add_stmt (&stmt_list
, stmt
);
3095 /* If descriptors were created, then we need to initialize them. */
3096 if (root
->any_descr_created
)
3098 struct nesting_info
*i
;
3099 for (i
= root
->inner
; i
; i
= i
->next
)
3103 field
= lookup_descr_for_decl (root
, i
->context
, NO_INSERT
);
3107 x
= builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR
);
3108 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3109 gimple_seq_add_stmt (&stmt_list
, stmt
);
3113 /* If we created initialization statements, insert them. */
3117 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
3118 bind
= gimple_seq_first_stmt_as_a_bind (gimple_body (context
));
3119 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
3120 gimple_bind_set_body (bind
, stmt_list
);
3123 /* If a chain_decl was created, then it needs to be registered with
3124 struct function so that it gets initialized from the static chain
3125 register at the beginning of the function. */
3126 sf
= DECL_STRUCT_FUNCTION (root
->context
);
3127 sf
->static_chain_decl
= root
->chain_decl
;
3129 /* Similarly for the non-local goto save area. */
3130 if (root
->nl_goto_field
)
3132 sf
->nonlocal_goto_save_area
3133 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
3134 sf
->has_nonlocal_label
= 1;
3137 /* Make sure all new local variables get inserted into the
3138 proper BIND_EXPR. */
3139 if (root
->new_local_var_chain
)
3140 declare_vars (root
->new_local_var_chain
,
3141 gimple_seq_first_stmt (gimple_body (root
->context
)),
3144 if (root
->debug_var_chain
)
3149 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
3151 for (debug_var
= root
->debug_var_chain
; debug_var
;
3152 debug_var
= DECL_CHAIN (debug_var
))
3153 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3156 /* If there are any debug decls with variable length types,
3157 remap those types using other debug_var_chain variables. */
3160 struct nesting_copy_body_data id
;
3162 memset (&id
, 0, sizeof (id
));
3163 id
.cb
.copy_decl
= nesting_copy_decl
;
3164 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
3167 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
3168 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3170 tree type
= TREE_TYPE (debug_var
);
3171 tree newt
, t
= type
;
3172 struct nesting_info
*i
;
3174 for (i
= root
; i
; i
= i
->outer
)
3175 if (variably_modified_type_p (type
, i
->context
))
3181 id
.cb
.src_fn
= i
->context
;
3182 id
.cb
.dst_fn
= i
->context
;
3183 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
3185 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
3186 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
3188 newt
= TREE_TYPE (newt
);
3191 if (TYPE_NAME (newt
)
3192 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
3193 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
3195 && TYPE_NAME (newt
) == TYPE_NAME (t
))
3196 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
3199 delete id
.cb
.decl_map
;
3202 scope
= gimple_seq_first_stmt_as_a_bind (gimple_body (root
->context
));
3203 if (gimple_bind_block (scope
))
3204 declare_vars (root
->debug_var_chain
, scope
, true);
3206 BLOCK_VARS (DECL_INITIAL (root
->context
))
3207 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
3208 root
->debug_var_chain
);
3211 /* Fold the rewritten MEM_REF trees. */
3212 root
->mem_refs
->traverse
<void *, fold_mem_refs
> (NULL
);
3214 /* Dump the translated tree function. */
3217 fputs ("\n\n", dump_file
);
3218 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
3223 finalize_nesting_tree (struct nesting_info
*root
)
3225 struct nesting_info
*n
;
3226 FOR_EACH_NEST_INFO (n
, root
)
3227 finalize_nesting_tree_1 (n
);
3230 /* Unnest the nodes and pass them to cgraph. */
3233 unnest_nesting_tree_1 (struct nesting_info
*root
)
3235 struct cgraph_node
*node
= cgraph_node::get (root
->context
);
3237 /* For nested functions update the cgraph to reflect unnesting.
3238 We also delay finalizing of these functions up to this point. */
3242 cgraph_node::finalize_function (root
->context
, true);
3247 unnest_nesting_tree (struct nesting_info
*root
)
3249 struct nesting_info
*n
;
3250 FOR_EACH_NEST_INFO (n
, root
)
3251 unnest_nesting_tree_1 (n
);
3254 /* Free the data structures allocated during this pass. */
3257 free_nesting_tree (struct nesting_info
*root
)
3259 struct nesting_info
*node
, *next
;
3261 node
= iter_nestinfo_start (root
);
3264 next
= iter_nestinfo_next (node
);
3265 delete node
->var_map
;
3266 delete node
->field_map
;
3267 delete node
->mem_refs
;
3274 /* Gimplify a function and all its nested functions. */
3276 gimplify_all_functions (struct cgraph_node
*root
)
3278 struct cgraph_node
*iter
;
3279 if (!gimple_body (root
->decl
))
3280 gimplify_function_tree (root
->decl
);
3281 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
3282 gimplify_all_functions (iter
);
3285 /* Main entry point for this pass. Process FNDECL and all of its nested
3286 subroutines and turn them into something less tightly bound. */
3289 lower_nested_functions (tree fndecl
)
3291 struct cgraph_node
*cgn
;
3292 struct nesting_info
*root
;
3294 /* If there are no nested functions, there's nothing to do. */
3295 cgn
= cgraph_node::get (fndecl
);
3299 gimplify_all_functions (cgn
);
3301 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
3303 fprintf (dump_file
, "\n;; Function %s\n\n",
3304 lang_hooks
.decl_printable_name (fndecl
, 2));
3306 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
3307 root
= create_nesting_tree (cgn
);
3309 walk_all_functions (convert_nonlocal_reference_stmt
,
3310 convert_nonlocal_reference_op
,
3312 walk_all_functions (convert_local_reference_stmt
,
3313 convert_local_reference_op
,
3315 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
3316 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
3318 convert_all_function_calls (root
);
3319 finalize_nesting_tree (root
);
3320 unnest_nesting_tree (root
);
3322 free_nesting_tree (root
);
3323 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
3327 dump_end (TDI_nested
, dump_file
);
3332 #include "gt-tree-nested.h"