1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2015 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"
31 #include "fold-const.h"
32 #include "stringpool.h"
33 #include "stor-layout.h"
35 #include "hard-reg-set.h"
37 #include "tree-dump.h"
38 #include "tree-inline.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
47 #include "gimple-iterator.h"
48 #include "gimple-walk.h"
49 #include "tree-iterator.h"
52 #include "plugin-api.h"
59 #include "statistics.h"
60 #include "insn-config.h"
68 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
69 #include "langhooks.h"
70 #include "gimple-low.h"
71 #include "gomp-constants.h"
74 /* The object of this pass is to lower the representation of a set of nested
75 functions in order to expose all of the gory details of the various
76 nonlocal references. We want to do this sooner rather than later, in
77 order to give us more freedom in emitting all of the functions in question.
79 Back in olden times, when gcc was young, we developed an insanely
80 complicated scheme whereby variables which were referenced nonlocally
81 were forced to live in the stack of the declaring function, and then
82 the nested functions magically discovered where these variables were
83 placed. In order for this scheme to function properly, it required
84 that the outer function be partially expanded, then we switch to
85 compiling the inner function, and once done with those we switch back
86 to compiling the outer function. Such delicate ordering requirements
87 makes it difficult to do whole translation unit optimizations
88 involving such functions.
90 The implementation here is much more direct. Everything that can be
91 referenced by an inner function is a member of an explicitly created
92 structure herein called the "nonlocal frame struct". The incoming
93 static chain for a nested function is a pointer to this struct in
94 the parent. In this way, we settle on known offsets from a known
95 base, and so are decoupled from the logic that places objects in the
96 function's stack frame. More importantly, we don't have to wait for
97 that to happen -- since the compilation of the inner function is no
98 longer tied to a real stack frame, the nonlocal frame struct can be
99 allocated anywhere. Which means that the outer function is now
102 Theory of operation here is very simple. Iterate over all the
103 statements in all the functions (depth first) several times,
104 allocating structures and fields on demand. In general we want to
105 examine inner functions first, so that we can avoid making changes
106 to outer functions which are unnecessary.
108 The order of the passes matters a bit, in that later passes will be
109 skipped if it is discovered that the functions don't actually interact
110 at all. That is, they're nested in the lexical sense but could have
111 been written as independent functions without change. */
116 struct nesting_info
*outer
;
117 struct nesting_info
*inner
;
118 struct nesting_info
*next
;
120 hash_map
<tree
, tree
> *field_map
;
121 hash_map
<tree
, tree
> *var_map
;
122 hash_set
<tree
*> *mem_refs
;
123 bitmap suppress_expansion
;
126 tree new_local_var_chain
;
127 tree debug_var_chain
;
134 bool any_parm_remapped
;
135 bool any_tramp_created
;
136 char static_chain_added
;
140 /* Iterate over the nesting tree, starting with ROOT, depth first. */
142 static inline struct nesting_info
*
143 iter_nestinfo_start (struct nesting_info
*root
)
150 static inline struct nesting_info
*
151 iter_nestinfo_next (struct nesting_info
*node
)
154 return iter_nestinfo_start (node
->next
);
158 #define FOR_EACH_NEST_INFO(I, ROOT) \
159 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
161 /* Obstack used for the bitmaps in the struct above. */
162 static struct bitmap_obstack nesting_info_bitmap_obstack
;
165 /* We're working in so many different function contexts simultaneously,
166 that create_tmp_var is dangerous. Prevent mishap. */
167 #define create_tmp_var cant_use_create_tmp_var_here_dummy
169 /* Like create_tmp_var, except record the variable for registration at
170 the given nesting level. */
173 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
177 /* If the type is of variable size or a type which must be created by the
178 frontend, something is wrong. Note that we explicitly allow
179 incomplete types here, since we create them ourselves here. */
180 gcc_assert (!TREE_ADDRESSABLE (type
));
181 gcc_assert (!TYPE_SIZE_UNIT (type
)
182 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
184 tmp_var
= create_tmp_var_raw (type
, prefix
);
185 DECL_CONTEXT (tmp_var
) = info
->context
;
186 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
187 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
188 if (TREE_CODE (type
) == COMPLEX_TYPE
189 || TREE_CODE (type
) == VECTOR_TYPE
)
190 DECL_GIMPLE_REG_P (tmp_var
) = 1;
192 info
->new_local_var_chain
= tmp_var
;
197 /* Take the address of EXP to be used within function CONTEXT.
198 Mark it for addressability as necessary. */
201 build_addr (tree exp
, tree context
)
207 while (handled_component_p (base
))
208 base
= TREE_OPERAND (base
, 0);
211 TREE_ADDRESSABLE (base
) = 1;
213 /* Building the ADDR_EXPR will compute a set of properties for
214 that ADDR_EXPR. Those properties are unfortunately context
215 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
217 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
218 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
219 way the properties are for the ADDR_EXPR are computed properly. */
220 save_context
= current_function_decl
;
221 current_function_decl
= context
;
222 retval
= build_fold_addr_expr (exp
);
223 current_function_decl
= save_context
;
227 /* Insert FIELD into TYPE, sorted by alignment requirements. */
230 insert_field_into_struct (tree type
, tree field
)
234 DECL_CONTEXT (field
) = type
;
236 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
237 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
240 DECL_CHAIN (field
) = *p
;
243 /* Set correct alignment for frame struct type. */
244 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
245 TYPE_ALIGN (type
) = DECL_ALIGN (field
);
248 /* Build or return the RECORD_TYPE that describes the frame state that is
249 shared between INFO->CONTEXT and its nested functions. This record will
250 not be complete until finalize_nesting_tree; up until that point we'll
251 be adding fields as necessary.
253 We also build the DECL that represents this frame in the function. */
256 get_frame_type (struct nesting_info
*info
)
258 tree type
= info
->frame_type
;
263 type
= make_node (RECORD_TYPE
);
265 name
= concat ("FRAME.",
266 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
268 TYPE_NAME (type
) = get_identifier (name
);
271 info
->frame_type
= type
;
272 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
273 DECL_NONLOCAL_FRAME (info
->frame_decl
) = 1;
275 /* ??? Always make it addressable for now, since it is meant to
276 be pointed to by the static chain pointer. This pessimizes
277 when it turns out that no static chains are needed because
278 the nested functions referencing non-local variables are not
279 reachable, but the true pessimization is to create the non-
280 local frame structure in the first place. */
281 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
286 /* Return true if DECL should be referenced by pointer in the non-local
290 use_pointer_in_frame (tree decl
)
292 if (TREE_CODE (decl
) == PARM_DECL
)
294 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
295 sized decls, and inefficient to copy large aggregates. Don't bother
296 moving anything but scalar variables. */
297 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
301 /* Variable sized types make things "interesting" in the frame. */
302 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
306 /* Given DECL, a non-locally accessed variable, find or create a field
307 in the non-local frame structure for the given nesting context. */
310 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
311 enum insert_option insert
)
313 if (insert
== NO_INSERT
)
315 tree
*slot
= info
->field_map
->get (decl
);
316 return slot
? *slot
: NULL_TREE
;
319 tree
*slot
= &info
->field_map
->get_or_insert (decl
);
322 tree field
= make_node (FIELD_DECL
);
323 DECL_NAME (field
) = DECL_NAME (decl
);
325 if (use_pointer_in_frame (decl
))
327 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
328 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
329 DECL_NONADDRESSABLE_P (field
) = 1;
333 TREE_TYPE (field
) = TREE_TYPE (decl
);
334 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
335 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
336 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
337 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
338 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
339 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
342 insert_field_into_struct (get_frame_type (info
), field
);
345 if (TREE_CODE (decl
) == PARM_DECL
)
346 info
->any_parm_remapped
= true;
352 /* Build or return the variable that holds the static chain within
353 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
356 get_chain_decl (struct nesting_info
*info
)
358 tree decl
= info
->chain_decl
;
364 type
= get_frame_type (info
->outer
);
365 type
= build_pointer_type (type
);
367 /* Note that this variable is *not* entered into any BIND_EXPR;
368 the construction of this variable is handled specially in
369 expand_function_start and initialize_inlined_parameters.
370 Note also that it's represented as a parameter. This is more
371 close to the truth, since the initial value does come from
373 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
374 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
375 DECL_ARTIFICIAL (decl
) = 1;
376 DECL_IGNORED_P (decl
) = 1;
377 TREE_USED (decl
) = 1;
378 DECL_CONTEXT (decl
) = info
->context
;
379 DECL_ARG_TYPE (decl
) = type
;
381 /* Tell tree-inline.c that we never write to this variable, so
382 it can copy-prop the replacement value immediately. */
383 TREE_READONLY (decl
) = 1;
385 info
->chain_decl
= decl
;
388 && (dump_flags
& TDF_DETAILS
)
389 && !DECL_STATIC_CHAIN (info
->context
))
390 fprintf (dump_file
, "Setting static-chain for %s\n",
391 lang_hooks
.decl_printable_name (info
->context
, 2));
393 DECL_STATIC_CHAIN (info
->context
) = 1;
398 /* Build or return the field within the non-local frame state that holds
399 the static chain for INFO->CONTEXT. This is the way to walk back up
400 multiple nesting levels. */
403 get_chain_field (struct nesting_info
*info
)
405 tree field
= info
->chain_field
;
409 tree type
= build_pointer_type (get_frame_type (info
->outer
));
411 field
= make_node (FIELD_DECL
);
412 DECL_NAME (field
) = get_identifier ("__chain");
413 TREE_TYPE (field
) = type
;
414 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
415 DECL_NONADDRESSABLE_P (field
) = 1;
417 insert_field_into_struct (get_frame_type (info
), field
);
419 info
->chain_field
= field
;
422 && (dump_flags
& TDF_DETAILS
)
423 && !DECL_STATIC_CHAIN (info
->context
))
424 fprintf (dump_file
, "Setting static-chain for %s\n",
425 lang_hooks
.decl_printable_name (info
->context
, 2));
427 DECL_STATIC_CHAIN (info
->context
) = 1;
432 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
435 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
440 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
441 gimple_call_set_lhs (call
, t
);
442 if (! gsi_end_p (*gsi
))
443 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
444 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
450 /* Copy EXP into a temporary. Allocate the temporary in the context of
451 INFO and insert the initialization statement before GSI. */
454 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
459 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
460 stmt
= gimple_build_assign (t
, exp
);
461 if (! gsi_end_p (*gsi
))
462 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
463 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
469 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
472 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
473 gimple_stmt_iterator
*gsi
)
475 if (is_gimple_val (exp
))
478 return init_tmp_var (info
, exp
, gsi
);
481 /* Similarly, but copy from the temporary and insert the statement
482 after the iterator. */
485 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
490 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
491 stmt
= gimple_build_assign (exp
, t
);
492 if (! gsi_end_p (*gsi
))
493 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
494 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
499 /* Build or return the type used to represent a nested function trampoline. */
501 static GTY(()) tree trampoline_type
;
504 get_trampoline_type (struct nesting_info
*info
)
506 unsigned align
, size
;
510 return trampoline_type
;
512 align
= TRAMPOLINE_ALIGNMENT
;
513 size
= TRAMPOLINE_SIZE
;
515 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
516 then allocate extra space so that we can do dynamic alignment. */
517 if (align
> STACK_BOUNDARY
)
519 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
520 align
= STACK_BOUNDARY
;
523 t
= build_index_type (size_int (size
- 1));
524 t
= build_array_type (char_type_node
, t
);
525 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
526 FIELD_DECL
, get_identifier ("__data"), t
);
527 DECL_ALIGN (t
) = align
;
528 DECL_USER_ALIGN (t
) = 1;
530 trampoline_type
= make_node (RECORD_TYPE
);
531 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
532 TYPE_FIELDS (trampoline_type
) = t
;
533 layout_type (trampoline_type
);
534 DECL_CONTEXT (t
) = trampoline_type
;
536 return trampoline_type
;
539 /* Given DECL, a nested function, find or create a field in the non-local
540 frame structure for a trampoline for this function. */
543 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
544 enum insert_option insert
)
546 if (insert
== NO_INSERT
)
548 tree
*slot
= info
->var_map
->get (decl
);
549 return slot
? *slot
: NULL_TREE
;
552 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
555 tree field
= make_node (FIELD_DECL
);
556 DECL_NAME (field
) = DECL_NAME (decl
);
557 TREE_TYPE (field
) = get_trampoline_type (info
);
558 TREE_ADDRESSABLE (field
) = 1;
560 insert_field_into_struct (get_frame_type (info
), field
);
563 info
->any_tramp_created
= true;
569 /* Build or return the field within the non-local frame state that holds
570 the non-local goto "jmp_buf". The buffer itself is maintained by the
571 rtl middle-end as dynamic stack space is allocated. */
574 get_nl_goto_field (struct nesting_info
*info
)
576 tree field
= info
->nl_goto_field
;
582 /* For __builtin_nonlocal_goto, we need N words. The first is the
583 frame pointer, the rest is for the target's stack pointer save
584 area. The number of words is controlled by STACK_SAVEAREA_MODE;
585 not the best interface, but it'll do for now. */
586 if (Pmode
== ptr_mode
)
587 type
= ptr_type_node
;
589 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
591 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
592 size
= size
/ GET_MODE_SIZE (Pmode
);
595 type
= build_array_type
596 (type
, build_index_type (size_int (size
)));
598 field
= make_node (FIELD_DECL
);
599 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
600 TREE_TYPE (field
) = type
;
601 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
602 TREE_ADDRESSABLE (field
) = 1;
604 insert_field_into_struct (get_frame_type (info
), field
);
606 info
->nl_goto_field
= field
;
612 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
615 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
616 struct nesting_info
*info
, gimple_seq
*pseq
)
618 struct walk_stmt_info wi
;
620 memset (&wi
, 0, sizeof (wi
));
623 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
627 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
630 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
631 struct nesting_info
*info
)
633 gimple_seq body
= gimple_body (info
->context
);
634 walk_body (callback_stmt
, callback_op
, info
, &body
);
635 gimple_set_body (info
->context
, body
);
638 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
641 walk_gimple_omp_for (gomp_for
*for_stmt
,
642 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
643 struct nesting_info
*info
)
645 struct walk_stmt_info wi
;
650 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
653 memset (&wi
, 0, sizeof (wi
));
655 wi
.gsi
= gsi_last (seq
);
657 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
660 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
664 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
669 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
672 t
= gimple_omp_for_incr (for_stmt
, i
);
673 gcc_assert (BINARY_CLASS_P (t
));
675 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
678 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
681 seq
= gsi_seq (wi
.gsi
);
682 if (!gimple_seq_empty_p (seq
))
684 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
685 annotate_all_with_location (seq
, gimple_location (for_stmt
));
686 gimple_seq_add_seq (&pre_body
, seq
);
687 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
691 /* Similarly for ROOT and all functions nested underneath, depth first. */
694 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
695 struct nesting_info
*root
)
697 struct nesting_info
*n
;
698 FOR_EACH_NEST_INFO (n
, root
)
699 walk_function (callback_stmt
, callback_op
, n
);
703 /* We have to check for a fairly pathological case. The operands of function
704 nested function are to be interpreted in the context of the enclosing
705 function. So if any are variably-sized, they will get remapped when the
706 enclosing function is inlined. But that remapping would also have to be
707 done in the types of the PARM_DECLs of the nested function, meaning the
708 argument types of that function will disagree with the arguments in the
709 calls to that function. So we'd either have to make a copy of the nested
710 function corresponding to each time the enclosing function was inlined or
711 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
712 function. The former is not practical. The latter would still require
713 detecting this case to know when to add the conversions. So, for now at
714 least, we don't inline such an enclosing function.
716 We have to do that check recursively, so here return indicating whether
717 FNDECL has such a nested function. ORIG_FN is the function we were
718 trying to inline to use for checking whether any argument is variably
719 modified by anything in it.
721 It would be better to do this in tree-inline.c so that we could give
722 the appropriate warning for why a function can't be inlined, but that's
723 too late since the nesting structure has already been flattened and
724 adding a flag just to record this fact seems a waste of a flag. */
727 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
729 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
732 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
734 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
735 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
738 if (check_for_nested_with_variably_modified (cgn
->decl
,
746 /* Construct our local datastructure describing the function nesting
747 tree rooted by CGN. */
749 static struct nesting_info
*
750 create_nesting_tree (struct cgraph_node
*cgn
)
752 struct nesting_info
*info
= XCNEW (struct nesting_info
);
753 info
->field_map
= new hash_map
<tree
, tree
>;
754 info
->var_map
= new hash_map
<tree
, tree
>;
755 info
->mem_refs
= new hash_set
<tree
*>;
756 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
757 info
->context
= cgn
->decl
;
759 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
761 struct nesting_info
*sub
= create_nesting_tree (cgn
);
763 sub
->next
= info
->inner
;
767 /* See discussion at check_for_nested_with_variably_modified for a
768 discussion of why this has to be here. */
769 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
770 DECL_UNINLINABLE (info
->context
) = true;
775 /* Return an expression computing the static chain for TARGET_CONTEXT
776 from INFO->CONTEXT. Insert any necessary computations before TSI. */
779 get_static_chain (struct nesting_info
*info
, tree target_context
,
780 gimple_stmt_iterator
*gsi
)
782 struct nesting_info
*i
;
785 if (info
->context
== target_context
)
787 x
= build_addr (info
->frame_decl
, target_context
);
791 x
= get_chain_decl (info
);
793 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
795 tree field
= get_chain_field (i
);
797 x
= build_simple_mem_ref (x
);
798 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
799 x
= init_tmp_var (info
, x
, gsi
);
807 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
808 frame as seen from INFO->CONTEXT. Insert any necessary computations
812 get_frame_field (struct nesting_info
*info
, tree target_context
,
813 tree field
, gimple_stmt_iterator
*gsi
)
815 struct nesting_info
*i
;
818 if (info
->context
== target_context
)
820 /* Make sure frame_decl gets created. */
821 (void) get_frame_type (info
);
822 x
= info
->frame_decl
;
826 x
= get_chain_decl (info
);
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
);
837 x
= build_simple_mem_ref (x
);
840 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
844 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
846 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
847 in the nested function with DECL_VALUE_EXPR set to reference the true
848 variable in the parent function. This is used both for debug info
849 and in OMP lowering. */
852 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
855 struct nesting_info
*i
;
856 tree x
, field
, new_decl
;
858 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
863 target_context
= decl_function_context (decl
);
865 /* A copy of the code in get_frame_field, but without the temporaries. */
866 if (info
->context
== target_context
)
868 /* Make sure frame_decl gets created. */
869 (void) get_frame_type (info
);
870 x
= info
->frame_decl
;
875 x
= get_chain_decl (info
);
876 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
878 field
= get_chain_field (i
);
879 x
= build_simple_mem_ref (x
);
880 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
882 x
= build_simple_mem_ref (x
);
885 field
= lookup_field_for_decl (i
, decl
, INSERT
);
886 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
887 if (use_pointer_in_frame (decl
))
888 x
= build_simple_mem_ref (x
);
890 /* ??? We should be remapping types as well, surely. */
891 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
892 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
893 DECL_CONTEXT (new_decl
) = info
->context
;
894 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
895 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
896 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
897 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
898 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
899 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
900 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
901 if ((TREE_CODE (decl
) == PARM_DECL
902 || TREE_CODE (decl
) == RESULT_DECL
903 || TREE_CODE (decl
) == VAR_DECL
)
904 && DECL_BY_REFERENCE (decl
))
905 DECL_BY_REFERENCE (new_decl
) = 1;
907 SET_DECL_VALUE_EXPR (new_decl
, x
);
908 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
911 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
912 info
->debug_var_chain
= new_decl
;
915 && info
->context
!= target_context
916 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
917 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
923 /* Callback for walk_gimple_stmt, rewrite all references to VAR
924 and PARM_DECLs that belong to outer functions.
926 The rewrite will involve some number of structure accesses back up
927 the static chain. E.g. for a variable FOO up one nesting level it'll
928 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
929 indirections apply to decls for which use_pointer_in_frame is true. */
932 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
934 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
935 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
939 switch (TREE_CODE (t
))
942 /* Non-automatic variables are never processed. */
943 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
948 if (decl_function_context (t
) != info
->context
)
953 x
= get_nonlocal_debug_decl (info
, t
);
954 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
956 tree target_context
= decl_function_context (t
);
957 struct nesting_info
*i
;
958 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
960 x
= lookup_field_for_decl (i
, t
, INSERT
);
961 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
962 if (use_pointer_in_frame (t
))
964 x
= init_tmp_var (info
, x
, &wi
->gsi
);
965 x
= build_simple_mem_ref (x
);
972 x
= save_tmp_var (info
, x
, &wi
->gsi
);
974 x
= init_tmp_var (info
, x
, &wi
->gsi
);
982 /* We're taking the address of a label from a parent function, but
983 this is not itself a non-local goto. Mark the label such that it
984 will not be deleted, much as we would with a label address in
986 if (decl_function_context (t
) != info
->context
)
987 FORCED_LABEL (t
) = 1;
992 bool save_val_only
= wi
->val_only
;
994 wi
->val_only
= false;
997 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
1004 /* If we changed anything, we might no longer be directly
1005 referencing a decl. */
1006 save_context
= current_function_decl
;
1007 current_function_decl
= info
->context
;
1008 recompute_tree_invariant_for_addr_expr (t
);
1009 current_function_decl
= save_context
;
1011 /* If the callback converted the address argument in a context
1012 where we only accept variables (and min_invariant, presumably),
1013 then compute the address into a temporary. */
1015 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1025 case ARRAY_RANGE_REF
:
1027 /* Go down this entire nest and just look at the final prefix and
1028 anything that describes the references. Otherwise, we lose track
1029 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1030 wi
->val_only
= true;
1032 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1034 if (TREE_CODE (t
) == COMPONENT_REF
)
1035 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1037 else if (TREE_CODE (t
) == ARRAY_REF
1038 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1040 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1042 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1044 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1048 wi
->val_only
= false;
1049 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1052 case VIEW_CONVERT_EXPR
:
1053 /* Just request to look at the subtrees, leaving val_only and lhs
1054 untouched. This might actually be for !val_only + lhs, in which
1055 case we don't want to force a replacement by a temporary. */
1060 if (!IS_TYPE_OR_DECL_P (t
))
1063 wi
->val_only
= true;
1072 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1073 struct walk_stmt_info
*);
1075 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1076 and PARM_DECLs that belong to outer functions. */
1079 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1081 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1082 bool need_chain
= false, need_stmts
= false;
1085 bitmap new_suppress
;
1087 new_suppress
= BITMAP_GGC_ALLOC ();
1088 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1090 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1092 switch (OMP_CLAUSE_CODE (clause
))
1094 case OMP_CLAUSE_REDUCTION
:
1095 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1097 goto do_decl_clause
;
1099 case OMP_CLAUSE_LASTPRIVATE
:
1100 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1102 goto do_decl_clause
;
1104 case OMP_CLAUSE_LINEAR
:
1105 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1107 wi
->val_only
= true;
1109 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
),
1111 goto do_decl_clause
;
1113 case OMP_CLAUSE_PRIVATE
:
1114 case OMP_CLAUSE_FIRSTPRIVATE
:
1115 case OMP_CLAUSE_COPYPRIVATE
:
1116 case OMP_CLAUSE_SHARED
:
1118 decl
= OMP_CLAUSE_DECL (clause
);
1119 if (TREE_CODE (decl
) == VAR_DECL
1120 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1122 if (decl_function_context (decl
) != info
->context
)
1124 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1125 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1126 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1131 case OMP_CLAUSE_SCHEDULE
:
1132 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1135 case OMP_CLAUSE_FINAL
:
1137 case OMP_CLAUSE_NUM_THREADS
:
1138 case OMP_CLAUSE_DEPEND
:
1139 case OMP_CLAUSE_DEVICE
:
1140 case OMP_CLAUSE_NUM_TEAMS
:
1141 case OMP_CLAUSE_THREAD_LIMIT
:
1142 case OMP_CLAUSE_SAFELEN
:
1143 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1144 wi
->val_only
= true;
1146 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1150 case OMP_CLAUSE_DIST_SCHEDULE
:
1151 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1153 wi
->val_only
= true;
1155 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1160 case OMP_CLAUSE_MAP
:
1162 case OMP_CLAUSE_FROM
:
1163 if (OMP_CLAUSE_SIZE (clause
))
1165 wi
->val_only
= true;
1167 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause
),
1170 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1171 goto do_decl_clause
;
1172 wi
->val_only
= true;
1174 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_nonlocal_reference_op
,
1178 case OMP_CLAUSE_ALIGNED
:
1179 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1181 wi
->val_only
= true;
1183 convert_nonlocal_reference_op
1184 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1186 /* Like do_decl_clause, but don't add any suppression. */
1187 decl
= OMP_CLAUSE_DECL (clause
);
1188 if (TREE_CODE (decl
) == VAR_DECL
1189 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1191 if (decl_function_context (decl
) != info
->context
)
1193 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1194 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1199 case OMP_CLAUSE_NOWAIT
:
1200 case OMP_CLAUSE_ORDERED
:
1201 case OMP_CLAUSE_DEFAULT
:
1202 case OMP_CLAUSE_COPYIN
:
1203 case OMP_CLAUSE_COLLAPSE
:
1204 case OMP_CLAUSE_UNTIED
:
1205 case OMP_CLAUSE_MERGEABLE
:
1206 case OMP_CLAUSE_PROC_BIND
:
1214 info
->suppress_expansion
= new_suppress
;
1217 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1218 switch (OMP_CLAUSE_CODE (clause
))
1220 case OMP_CLAUSE_REDUCTION
:
1221 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1224 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1225 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1227 walk_body (convert_nonlocal_reference_stmt
,
1228 convert_nonlocal_reference_op
, info
,
1229 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1230 walk_body (convert_nonlocal_reference_stmt
,
1231 convert_nonlocal_reference_op
, info
,
1232 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1233 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1238 case OMP_CLAUSE_LASTPRIVATE
:
1239 walk_body (convert_nonlocal_reference_stmt
,
1240 convert_nonlocal_reference_op
, info
,
1241 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1244 case OMP_CLAUSE_LINEAR
:
1245 walk_body (convert_nonlocal_reference_stmt
,
1246 convert_nonlocal_reference_op
, info
,
1247 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1257 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1260 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1262 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1263 type
= TREE_TYPE (type
);
1265 if (TYPE_NAME (type
)
1266 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1267 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1268 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1270 while (POINTER_TYPE_P (type
)
1271 || TREE_CODE (type
) == VECTOR_TYPE
1272 || TREE_CODE (type
) == FUNCTION_TYPE
1273 || TREE_CODE (type
) == METHOD_TYPE
)
1274 type
= TREE_TYPE (type
);
1276 if (TREE_CODE (type
) == ARRAY_TYPE
)
1280 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1281 domain
= TYPE_DOMAIN (type
);
1284 t
= TYPE_MIN_VALUE (domain
);
1285 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1286 && decl_function_context (t
) != info
->context
)
1287 get_nonlocal_debug_decl (info
, t
);
1288 t
= TYPE_MAX_VALUE (domain
);
1289 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1290 && decl_function_context (t
) != info
->context
)
1291 get_nonlocal_debug_decl (info
, t
);
1296 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1300 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1304 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
1305 if (TREE_CODE (var
) == VAR_DECL
1306 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1307 && DECL_HAS_VALUE_EXPR_P (var
)
1308 && decl_function_context (var
) != info
->context
)
1309 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1312 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1313 PARM_DECLs that belong to outer functions. This handles statements
1314 that are not handled via the standard recursion done in
1315 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1316 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1317 operands of STMT have been handled by this function. */
1320 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1321 struct walk_stmt_info
*wi
)
1323 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1324 tree save_local_var_chain
;
1325 bitmap save_suppress
;
1326 gimple stmt
= gsi_stmt (*gsi
);
1328 switch (gimple_code (stmt
))
1331 /* Don't walk non-local gotos for now. */
1332 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1334 wi
->val_only
= true;
1336 *handled_ops_p
= true;
1341 case GIMPLE_OMP_PARALLEL
:
1342 case GIMPLE_OMP_TASK
:
1343 save_suppress
= info
->suppress_expansion
;
1344 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1348 decl
= get_chain_decl (info
);
1349 c
= build_omp_clause (gimple_location (stmt
),
1350 OMP_CLAUSE_FIRSTPRIVATE
);
1351 OMP_CLAUSE_DECL (c
) = decl
;
1352 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1353 gimple_omp_taskreg_set_clauses (stmt
, c
);
1356 save_local_var_chain
= info
->new_local_var_chain
;
1357 info
->new_local_var_chain
= NULL
;
1359 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1360 info
, gimple_omp_body_ptr (stmt
));
1362 if (info
->new_local_var_chain
)
1363 declare_vars (info
->new_local_var_chain
,
1364 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1366 info
->new_local_var_chain
= save_local_var_chain
;
1367 info
->suppress_expansion
= save_suppress
;
1370 case GIMPLE_OMP_FOR
:
1371 save_suppress
= info
->suppress_expansion
;
1372 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1373 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
1374 convert_nonlocal_reference_stmt
,
1375 convert_nonlocal_reference_op
, info
);
1376 walk_body (convert_nonlocal_reference_stmt
,
1377 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1378 info
->suppress_expansion
= save_suppress
;
1381 case GIMPLE_OMP_SECTIONS
:
1382 save_suppress
= info
->suppress_expansion
;
1383 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1384 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1385 info
, gimple_omp_body_ptr (stmt
));
1386 info
->suppress_expansion
= save_suppress
;
1389 case GIMPLE_OMP_SINGLE
:
1390 save_suppress
= info
->suppress_expansion
;
1391 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1392 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1393 info
, gimple_omp_body_ptr (stmt
));
1394 info
->suppress_expansion
= save_suppress
;
1397 case GIMPLE_OMP_TARGET
:
1398 if (!is_gimple_omp_offloaded (stmt
))
1400 save_suppress
= info
->suppress_expansion
;
1401 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1403 info
->suppress_expansion
= save_suppress
;
1404 walk_body (convert_nonlocal_reference_stmt
,
1405 convert_nonlocal_reference_op
, info
,
1406 gimple_omp_body_ptr (stmt
));
1409 save_suppress
= info
->suppress_expansion
;
1410 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1414 decl
= get_chain_decl (info
);
1415 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1416 OMP_CLAUSE_DECL (c
) = decl
;
1417 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TO
);
1418 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
1419 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1420 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
1423 save_local_var_chain
= info
->new_local_var_chain
;
1424 info
->new_local_var_chain
= NULL
;
1426 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1427 info
, gimple_omp_body_ptr (stmt
));
1429 if (info
->new_local_var_chain
)
1430 declare_vars (info
->new_local_var_chain
,
1431 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1433 info
->new_local_var_chain
= save_local_var_chain
;
1434 info
->suppress_expansion
= save_suppress
;
1437 case GIMPLE_OMP_TEAMS
:
1438 save_suppress
= info
->suppress_expansion
;
1439 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1440 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1441 info
, gimple_omp_body_ptr (stmt
));
1442 info
->suppress_expansion
= save_suppress
;
1445 case GIMPLE_OMP_SECTION
:
1446 case GIMPLE_OMP_MASTER
:
1447 case GIMPLE_OMP_TASKGROUP
:
1448 case GIMPLE_OMP_ORDERED
:
1449 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1450 info
, gimple_omp_body_ptr (stmt
));
1455 gbind
*bind_stmt
= as_a
<gbind
*> (stmt
);
1456 if (!optimize
&& gimple_bind_block (bind_stmt
))
1457 note_nonlocal_block_vlas (info
, gimple_bind_block (bind_stmt
));
1459 for (tree var
= gimple_bind_vars (bind_stmt
); var
; var
= DECL_CHAIN (var
))
1460 if (TREE_CODE (var
) == NAMELIST_DECL
)
1462 /* Adjust decls mentioned in NAMELIST_DECL. */
1463 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
1467 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
1469 if (TREE_CODE (decl
) == VAR_DECL
1470 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1472 if (decl_function_context (decl
) != info
->context
)
1473 CONSTRUCTOR_ELT (decls
, i
)->value
1474 = get_nonlocal_debug_decl (info
, decl
);
1478 *handled_ops_p
= false;
1482 wi
->val_only
= true;
1484 *handled_ops_p
= false;
1488 /* For every other statement that we are not interested in
1489 handling here, let the walker traverse the operands. */
1490 *handled_ops_p
= false;
1494 /* We have handled all of STMT operands, no need to traverse the operands. */
1495 *handled_ops_p
= true;
1500 /* A subroutine of convert_local_reference. Create a local variable
1501 in the parent function with DECL_VALUE_EXPR set to reference the
1502 field in FRAME. This is used both for debug info and in OMP
1506 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1510 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1514 /* Make sure frame_decl gets created. */
1515 (void) get_frame_type (info
);
1516 x
= info
->frame_decl
;
1517 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1519 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1520 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1521 DECL_CONTEXT (new_decl
) = info
->context
;
1522 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1523 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1524 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1525 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1526 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1527 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1528 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1529 if ((TREE_CODE (decl
) == PARM_DECL
1530 || TREE_CODE (decl
) == RESULT_DECL
1531 || TREE_CODE (decl
) == VAR_DECL
)
1532 && DECL_BY_REFERENCE (decl
))
1533 DECL_BY_REFERENCE (new_decl
) = 1;
1535 SET_DECL_VALUE_EXPR (new_decl
, x
);
1536 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1539 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1540 info
->debug_var_chain
= new_decl
;
1542 /* Do not emit debug info twice. */
1543 DECL_IGNORED_P (decl
) = 1;
1549 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1550 and PARM_DECLs that were referenced by inner nested functions.
1551 The rewrite will be a structure reference to the local frame variable. */
1553 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1556 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1558 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1559 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1560 tree t
= *tp
, field
, x
;
1564 switch (TREE_CODE (t
))
1567 /* Non-automatic variables are never processed. */
1568 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1573 if (decl_function_context (t
) == info
->context
)
1575 /* If we copied a pointer to the frame, then the original decl
1576 is used unchanged in the parent function. */
1577 if (use_pointer_in_frame (t
))
1580 /* No need to transform anything if no child references the
1582 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1587 x
= get_local_debug_decl (info
, t
, field
);
1588 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1589 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1594 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1596 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1604 save_val_only
= wi
->val_only
;
1605 wi
->val_only
= false;
1607 wi
->changed
= false;
1608 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1609 wi
->val_only
= save_val_only
;
1611 /* If we converted anything ... */
1616 /* Then the frame decl is now addressable. */
1617 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1619 save_context
= current_function_decl
;
1620 current_function_decl
= info
->context
;
1621 recompute_tree_invariant_for_addr_expr (t
);
1622 current_function_decl
= save_context
;
1624 /* If we are in a context where we only accept values, then
1625 compute the address into a temporary. */
1627 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1636 case ARRAY_RANGE_REF
:
1638 /* Go down this entire nest and just look at the final prefix and
1639 anything that describes the references. Otherwise, we lose track
1640 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1641 save_val_only
= wi
->val_only
;
1642 wi
->val_only
= true;
1644 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1646 if (TREE_CODE (t
) == COMPONENT_REF
)
1647 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1649 else if (TREE_CODE (t
) == ARRAY_REF
1650 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1652 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1654 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1656 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1660 wi
->val_only
= false;
1661 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1662 wi
->val_only
= save_val_only
;
1666 save_val_only
= wi
->val_only
;
1667 wi
->val_only
= true;
1669 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1671 /* We need to re-fold the MEM_REF as component references as
1672 part of a ADDR_EXPR address are not allowed. But we cannot
1673 fold here, as the chain record type is not yet finalized. */
1674 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1675 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1676 info
->mem_refs
->add (tp
);
1677 wi
->val_only
= save_val_only
;
1680 case VIEW_CONVERT_EXPR
:
1681 /* Just request to look at the subtrees, leaving val_only and lhs
1682 untouched. This might actually be for !val_only + lhs, in which
1683 case we don't want to force a replacement by a temporary. */
1688 if (!IS_TYPE_OR_DECL_P (t
))
1691 wi
->val_only
= true;
1700 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1701 struct walk_stmt_info
*);
1703 /* Helper for convert_local_reference. Convert all the references in
1704 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1707 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1709 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1710 bool need_frame
= false, need_stmts
= false;
1713 bitmap new_suppress
;
1715 new_suppress
= BITMAP_GGC_ALLOC ();
1716 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1718 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1720 switch (OMP_CLAUSE_CODE (clause
))
1722 case OMP_CLAUSE_REDUCTION
:
1723 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1725 goto do_decl_clause
;
1727 case OMP_CLAUSE_LASTPRIVATE
:
1728 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1730 goto do_decl_clause
;
1732 case OMP_CLAUSE_LINEAR
:
1733 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1735 wi
->val_only
= true;
1737 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
), &dummy
,
1739 goto do_decl_clause
;
1741 case OMP_CLAUSE_PRIVATE
:
1742 case OMP_CLAUSE_FIRSTPRIVATE
:
1743 case OMP_CLAUSE_COPYPRIVATE
:
1744 case OMP_CLAUSE_SHARED
:
1746 decl
= OMP_CLAUSE_DECL (clause
);
1747 if (TREE_CODE (decl
) == VAR_DECL
1748 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1750 if (decl_function_context (decl
) == info
->context
1751 && !use_pointer_in_frame (decl
))
1753 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1756 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1757 OMP_CLAUSE_DECL (clause
)
1758 = get_local_debug_decl (info
, decl
, field
);
1764 case OMP_CLAUSE_SCHEDULE
:
1765 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1768 case OMP_CLAUSE_FINAL
:
1770 case OMP_CLAUSE_NUM_THREADS
:
1771 case OMP_CLAUSE_DEPEND
:
1772 case OMP_CLAUSE_DEVICE
:
1773 case OMP_CLAUSE_NUM_TEAMS
:
1774 case OMP_CLAUSE_THREAD_LIMIT
:
1775 case OMP_CLAUSE_SAFELEN
:
1776 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1777 wi
->val_only
= true;
1779 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1783 case OMP_CLAUSE_DIST_SCHEDULE
:
1784 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1786 wi
->val_only
= true;
1788 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1793 case OMP_CLAUSE_MAP
:
1795 case OMP_CLAUSE_FROM
:
1796 if (OMP_CLAUSE_SIZE (clause
))
1798 wi
->val_only
= true;
1800 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause
),
1803 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1804 goto do_decl_clause
;
1805 wi
->val_only
= true;
1807 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_local_reference_op
,
1811 case OMP_CLAUSE_ALIGNED
:
1812 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1814 wi
->val_only
= true;
1816 convert_local_reference_op
1817 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1819 /* Like do_decl_clause, but don't add any suppression. */
1820 decl
= OMP_CLAUSE_DECL (clause
);
1821 if (TREE_CODE (decl
) == VAR_DECL
1822 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1824 if (decl_function_context (decl
) == info
->context
1825 && !use_pointer_in_frame (decl
))
1827 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1830 OMP_CLAUSE_DECL (clause
)
1831 = get_local_debug_decl (info
, decl
, field
);
1837 case OMP_CLAUSE_NOWAIT
:
1838 case OMP_CLAUSE_ORDERED
:
1839 case OMP_CLAUSE_DEFAULT
:
1840 case OMP_CLAUSE_COPYIN
:
1841 case OMP_CLAUSE_COLLAPSE
:
1842 case OMP_CLAUSE_UNTIED
:
1843 case OMP_CLAUSE_MERGEABLE
:
1844 case OMP_CLAUSE_PROC_BIND
:
1852 info
->suppress_expansion
= new_suppress
;
1855 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1856 switch (OMP_CLAUSE_CODE (clause
))
1858 case OMP_CLAUSE_REDUCTION
:
1859 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1862 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1863 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1865 walk_body (convert_local_reference_stmt
,
1866 convert_local_reference_op
, info
,
1867 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1868 walk_body (convert_local_reference_stmt
,
1869 convert_local_reference_op
, info
,
1870 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1871 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1876 case OMP_CLAUSE_LASTPRIVATE
:
1877 walk_body (convert_local_reference_stmt
,
1878 convert_local_reference_op
, info
,
1879 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1882 case OMP_CLAUSE_LINEAR
:
1883 walk_body (convert_local_reference_stmt
,
1884 convert_local_reference_op
, info
,
1885 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1896 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1897 and PARM_DECLs that were referenced by inner nested functions.
1898 The rewrite will be a structure reference to the local frame variable. */
1901 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1902 struct walk_stmt_info
*wi
)
1904 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1905 tree save_local_var_chain
;
1906 bitmap save_suppress
;
1907 gimple stmt
= gsi_stmt (*gsi
);
1909 switch (gimple_code (stmt
))
1911 case GIMPLE_OMP_PARALLEL
:
1912 case GIMPLE_OMP_TASK
:
1913 save_suppress
= info
->suppress_expansion
;
1914 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1918 (void) get_frame_type (info
);
1919 c
= build_omp_clause (gimple_location (stmt
),
1921 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1922 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1923 gimple_omp_taskreg_set_clauses (stmt
, c
);
1926 save_local_var_chain
= info
->new_local_var_chain
;
1927 info
->new_local_var_chain
= NULL
;
1929 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1930 gimple_omp_body_ptr (stmt
));
1932 if (info
->new_local_var_chain
)
1933 declare_vars (info
->new_local_var_chain
,
1934 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1935 info
->new_local_var_chain
= save_local_var_chain
;
1936 info
->suppress_expansion
= save_suppress
;
1939 case GIMPLE_OMP_FOR
:
1940 save_suppress
= info
->suppress_expansion
;
1941 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1942 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
1943 convert_local_reference_stmt
,
1944 convert_local_reference_op
, info
);
1945 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1946 info
, gimple_omp_body_ptr (stmt
));
1947 info
->suppress_expansion
= save_suppress
;
1950 case GIMPLE_OMP_SECTIONS
:
1951 save_suppress
= info
->suppress_expansion
;
1952 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1953 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1954 info
, gimple_omp_body_ptr (stmt
));
1955 info
->suppress_expansion
= save_suppress
;
1958 case GIMPLE_OMP_SINGLE
:
1959 save_suppress
= info
->suppress_expansion
;
1960 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1961 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1962 info
, gimple_omp_body_ptr (stmt
));
1963 info
->suppress_expansion
= save_suppress
;
1966 case GIMPLE_OMP_TARGET
:
1967 if (!is_gimple_omp_offloaded (stmt
))
1969 save_suppress
= info
->suppress_expansion
;
1970 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
1971 info
->suppress_expansion
= save_suppress
;
1972 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1973 info
, gimple_omp_body_ptr (stmt
));
1976 save_suppress
= info
->suppress_expansion
;
1977 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
))
1980 (void) get_frame_type (info
);
1981 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1982 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1983 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
1984 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
1985 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1986 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
1989 save_local_var_chain
= info
->new_local_var_chain
;
1990 info
->new_local_var_chain
= NULL
;
1992 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1993 gimple_omp_body_ptr (stmt
));
1995 if (info
->new_local_var_chain
)
1996 declare_vars (info
->new_local_var_chain
,
1997 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1998 info
->new_local_var_chain
= save_local_var_chain
;
1999 info
->suppress_expansion
= save_suppress
;
2002 case GIMPLE_OMP_TEAMS
:
2003 save_suppress
= info
->suppress_expansion
;
2004 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
2005 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2006 info
, gimple_omp_body_ptr (stmt
));
2007 info
->suppress_expansion
= save_suppress
;
2010 case GIMPLE_OMP_SECTION
:
2011 case GIMPLE_OMP_MASTER
:
2012 case GIMPLE_OMP_TASKGROUP
:
2013 case GIMPLE_OMP_ORDERED
:
2014 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2015 info
, gimple_omp_body_ptr (stmt
));
2019 wi
->val_only
= true;
2021 *handled_ops_p
= false;
2025 if (gimple_clobber_p (stmt
))
2027 tree lhs
= gimple_assign_lhs (stmt
);
2028 if (!use_pointer_in_frame (lhs
)
2029 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
2031 gsi_replace (gsi
, gimple_build_nop (), true);
2035 *handled_ops_p
= false;
2039 for (tree var
= gimple_bind_vars (as_a
<gbind
*> (stmt
));
2041 var
= DECL_CHAIN (var
))
2042 if (TREE_CODE (var
) == NAMELIST_DECL
)
2044 /* Adjust decls mentioned in NAMELIST_DECL. */
2045 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
2049 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
2051 if (TREE_CODE (decl
) == VAR_DECL
2052 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2054 if (decl_function_context (decl
) == info
->context
2055 && !use_pointer_in_frame (decl
))
2057 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2060 CONSTRUCTOR_ELT (decls
, i
)->value
2061 = get_local_debug_decl (info
, decl
, field
);
2067 *handled_ops_p
= false;
2071 /* For every other statement that we are not interested in
2072 handling here, let the walker traverse the operands. */
2073 *handled_ops_p
= false;
2077 /* Indicate that we have handled all the operands ourselves. */
2078 *handled_ops_p
= true;
2083 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2084 that reference labels from outer functions. The rewrite will be a
2085 call to __builtin_nonlocal_goto. */
2088 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2089 struct walk_stmt_info
*wi
)
2091 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2092 tree label
, new_label
, target_context
, x
, field
;
2094 gimple stmt
= gsi_stmt (*gsi
);
2096 if (gimple_code (stmt
) != GIMPLE_GOTO
)
2098 *handled_ops_p
= false;
2102 label
= gimple_goto_dest (stmt
);
2103 if (TREE_CODE (label
) != LABEL_DECL
)
2105 *handled_ops_p
= false;
2109 target_context
= decl_function_context (label
);
2110 if (target_context
== info
->context
)
2112 *handled_ops_p
= false;
2116 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
2119 /* The original user label may also be use for a normal goto, therefore
2120 we must create a new label that will actually receive the abnormal
2121 control transfer. This new label will be marked LABEL_NONLOCAL; this
2122 mark will trigger proper behavior in the cfg, as well as cause the
2123 (hairy target-specific) non-local goto receiver code to be generated
2124 when we expand rtl. Enter this association into var_map so that we
2125 can insert the new label into the IL during a second pass. */
2126 tree
*slot
= &i
->var_map
->get_or_insert (label
);
2129 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
2130 DECL_NONLOCAL (new_label
) = 1;
2136 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2137 field
= get_nl_goto_field (i
);
2138 x
= get_frame_field (info
, target_context
, field
, gsi
);
2139 x
= build_addr (x
, target_context
);
2140 x
= gsi_gimplify_val (info
, x
, gsi
);
2141 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
2142 2, build_addr (new_label
, target_context
), x
);
2143 gsi_replace (gsi
, call
, false);
2145 /* We have handled all of STMT's operands, no need to keep going. */
2146 *handled_ops_p
= true;
2151 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2152 are referenced via nonlocal goto from a nested function. The rewrite
2153 will involve installing a newly generated DECL_NONLOCAL label, and
2154 (potentially) a branch around the rtl gunk that is assumed to be
2155 attached to such a label. */
2158 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2159 struct walk_stmt_info
*wi
)
2161 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2162 tree label
, new_label
;
2163 gimple_stmt_iterator tmp_gsi
;
2164 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (*gsi
));
2168 *handled_ops_p
= false;
2172 label
= gimple_label_label (stmt
);
2174 tree
*slot
= info
->var_map
->get (label
);
2177 *handled_ops_p
= false;
2181 /* If there's any possibility that the previous statement falls through,
2182 then we must branch around the new non-local label. */
2184 gsi_prev (&tmp_gsi
);
2185 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
2187 gimple stmt
= gimple_build_goto (label
);
2188 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2191 new_label
= (tree
) *slot
;
2192 stmt
= gimple_build_label (new_label
);
2193 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2195 *handled_ops_p
= true;
2200 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2201 of nested functions that require the use of trampolines. The rewrite
2202 will involve a reference a trampoline generated for the occasion. */
2205 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
2207 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2208 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2209 tree t
= *tp
, decl
, target_context
, x
, builtin
;
2213 switch (TREE_CODE (t
))
2217 T.1 = &CHAIN->tramp;
2218 T.2 = __builtin_adjust_trampoline (T.1);
2219 T.3 = (func_type)T.2;
2222 decl
= TREE_OPERAND (t
, 0);
2223 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2226 /* Only need to process nested functions. */
2227 target_context
= decl_function_context (decl
);
2228 if (!target_context
)
2231 /* If the nested function doesn't use a static chain, then
2232 it doesn't need a trampoline. */
2233 if (!DECL_STATIC_CHAIN (decl
))
2236 /* If we don't want a trampoline, then don't build one. */
2237 if (TREE_NO_TRAMPOLINE (t
))
2240 /* Lookup the immediate parent of the callee, as that's where
2241 we need to insert the trampoline. */
2242 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
2244 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
2246 /* Compute the address of the field holding the trampoline. */
2247 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
2248 x
= build_addr (x
, target_context
);
2249 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
2251 /* Do machine-specific ugliness. Normally this will involve
2252 computing extra alignment, but it can really be anything. */
2253 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
2254 call
= gimple_build_call (builtin
, 1, x
);
2255 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
2257 /* Cast back to the proper function type. */
2258 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
2259 x
= init_tmp_var (info
, x
, &wi
->gsi
);
2265 if (!IS_TYPE_OR_DECL_P (t
))
2274 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2275 to addresses of nested functions that require the use of
2276 trampolines. The rewrite will involve a reference a trampoline
2277 generated for the occasion. */
2280 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2281 struct walk_stmt_info
*wi
)
2283 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2284 gimple stmt
= gsi_stmt (*gsi
);
2286 switch (gimple_code (stmt
))
2290 /* Only walk call arguments, lest we generate trampolines for
2292 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2293 for (i
= 0; i
< nargs
; i
++)
2294 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2299 case GIMPLE_OMP_TARGET
:
2300 if (!is_gimple_omp_offloaded (stmt
))
2302 *handled_ops_p
= false;
2306 case GIMPLE_OMP_PARALLEL
:
2307 case GIMPLE_OMP_TASK
:
2309 tree save_local_var_chain
;
2310 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2311 save_local_var_chain
= info
->new_local_var_chain
;
2312 info
->new_local_var_chain
= NULL
;
2313 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2314 info
, gimple_omp_body_ptr (stmt
));
2315 if (info
->new_local_var_chain
)
2316 declare_vars (info
->new_local_var_chain
,
2317 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2319 info
->new_local_var_chain
= save_local_var_chain
;
2324 *handled_ops_p
= false;
2328 *handled_ops_p
= true;
2334 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2335 that reference nested functions to make sure that the static chain
2336 is set up properly for the call. */
2339 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2340 struct walk_stmt_info
*wi
)
2342 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2343 tree decl
, target_context
;
2344 char save_static_chain_added
;
2346 gimple stmt
= gsi_stmt (*gsi
);
2348 switch (gimple_code (stmt
))
2351 if (gimple_call_chain (stmt
))
2353 decl
= gimple_call_fndecl (stmt
);
2356 target_context
= decl_function_context (decl
);
2357 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2359 gimple_call_set_chain (as_a
<gcall
*> (stmt
),
2360 get_static_chain (info
, target_context
,
2362 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2366 case GIMPLE_OMP_PARALLEL
:
2367 case GIMPLE_OMP_TASK
:
2368 save_static_chain_added
= info
->static_chain_added
;
2369 info
->static_chain_added
= 0;
2370 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2371 for (i
= 0; i
< 2; i
++)
2374 if ((info
->static_chain_added
& (1 << i
)) == 0)
2376 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2377 /* Don't add CHAIN.* or FRAME.* twice. */
2378 for (c
= gimple_omp_taskreg_clauses (stmt
);
2380 c
= OMP_CLAUSE_CHAIN (c
))
2381 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2382 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2383 && OMP_CLAUSE_DECL (c
) == decl
)
2387 c
= build_omp_clause (gimple_location (stmt
),
2388 i
? OMP_CLAUSE_FIRSTPRIVATE
2389 : OMP_CLAUSE_SHARED
);
2390 OMP_CLAUSE_DECL (c
) = decl
;
2391 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2392 gimple_omp_taskreg_set_clauses (stmt
, c
);
2395 info
->static_chain_added
|= save_static_chain_added
;
2398 case GIMPLE_OMP_TARGET
:
2399 if (!is_gimple_omp_offloaded (stmt
))
2401 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2404 save_static_chain_added
= info
->static_chain_added
;
2405 info
->static_chain_added
= 0;
2406 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2407 for (i
= 0; i
< 2; i
++)
2410 if ((info
->static_chain_added
& (1 << i
)) == 0)
2412 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2413 /* Don't add CHAIN.* or FRAME.* twice. */
2414 for (c
= gimple_omp_target_clauses (stmt
);
2416 c
= OMP_CLAUSE_CHAIN (c
))
2417 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
2418 && OMP_CLAUSE_DECL (c
) == decl
)
2422 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2423 OMP_CLAUSE_DECL (c
) = decl
;
2424 OMP_CLAUSE_SET_MAP_KIND (c
, i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2425 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2426 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2427 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2431 info
->static_chain_added
|= save_static_chain_added
;
2434 case GIMPLE_OMP_FOR
:
2435 walk_body (convert_gimple_call
, NULL
, info
,
2436 gimple_omp_for_pre_body_ptr (stmt
));
2438 case GIMPLE_OMP_SECTIONS
:
2439 case GIMPLE_OMP_SECTION
:
2440 case GIMPLE_OMP_SINGLE
:
2441 case GIMPLE_OMP_TEAMS
:
2442 case GIMPLE_OMP_MASTER
:
2443 case GIMPLE_OMP_TASKGROUP
:
2444 case GIMPLE_OMP_ORDERED
:
2445 case GIMPLE_OMP_CRITICAL
:
2446 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2450 /* Keep looking for other operands. */
2451 *handled_ops_p
= false;
2455 *handled_ops_p
= true;
2459 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2460 call expressions. At the same time, determine if a nested function
2461 actually uses its static chain; if not, remember that. */
2464 convert_all_function_calls (struct nesting_info
*root
)
2466 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2467 struct nesting_info
*n
;
2469 /* First, optimistically clear static_chain for all decls that haven't
2470 used the static chain already for variable access. But always create
2471 it if not optimizing. This makes it possible to reconstruct the static
2472 nesting tree at run time and thus to resolve up-level references from
2473 within the debugger. */
2474 FOR_EACH_NEST_INFO (n
, root
)
2476 tree decl
= n
->context
;
2480 (void) get_frame_type (n
);
2482 (void) get_chain_decl (n
);
2484 else if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2486 DECL_STATIC_CHAIN (decl
) = 0;
2487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2488 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2489 lang_hooks
.decl_printable_name (decl
, 2));
2492 DECL_STATIC_CHAIN (decl
) = 1;
2493 chain_count
+= DECL_STATIC_CHAIN (decl
);
2496 /* Walk the functions and perform transformations. Note that these
2497 transformations can induce new uses of the static chain, which in turn
2498 require re-examining all users of the decl. */
2499 /* ??? It would make sense to try to use the call graph to speed this up,
2500 but the call graph hasn't really been built yet. Even if it did, we
2501 would still need to iterate in this loop since address-of references
2502 wouldn't show up in the callgraph anyway. */
2506 old_chain_count
= chain_count
;
2510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2511 fputc ('\n', dump_file
);
2513 FOR_EACH_NEST_INFO (n
, root
)
2515 tree decl
= n
->context
;
2516 walk_function (convert_tramp_reference_stmt
,
2517 convert_tramp_reference_op
, n
);
2518 walk_function (convert_gimple_call
, NULL
, n
);
2519 chain_count
+= DECL_STATIC_CHAIN (decl
);
2522 while (chain_count
!= old_chain_count
);
2524 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2525 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2529 struct nesting_copy_body_data
2532 struct nesting_info
*root
;
2535 /* A helper subroutine for debug_var_chain type remapping. */
2538 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2540 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2541 tree
*slot
= nid
->root
->var_map
->get (decl
);
2544 return (tree
) *slot
;
2546 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2548 tree new_decl
= copy_decl_no_change (decl
, id
);
2549 DECL_ORIGINAL_TYPE (new_decl
)
2550 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2554 if (TREE_CODE (decl
) == VAR_DECL
2555 || TREE_CODE (decl
) == PARM_DECL
2556 || TREE_CODE (decl
) == RESULT_DECL
)
2559 return copy_decl_no_change (decl
, id
);
2562 /* A helper function for remap_vla_decls. See if *TP contains
2563 some remapped variables. */
2566 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2568 struct nesting_info
*root
= (struct nesting_info
*) data
;
2574 tree
*slot
= root
->var_map
->get (t
);
2582 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2586 remap_vla_decls (tree block
, struct nesting_info
*root
)
2588 tree var
, subblock
, val
, type
;
2589 struct nesting_copy_body_data id
;
2591 for (subblock
= BLOCK_SUBBLOCKS (block
);
2593 subblock
= BLOCK_CHAIN (subblock
))
2594 remap_vla_decls (subblock
, root
);
2596 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
2597 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2599 val
= DECL_VALUE_EXPR (var
);
2600 type
= TREE_TYPE (var
);
2602 if (!(TREE_CODE (val
) == INDIRECT_REF
2603 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2604 && variably_modified_type_p (type
, NULL
)))
2607 if (root
->var_map
->get (TREE_OPERAND (val
, 0))
2608 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2612 if (var
== NULL_TREE
)
2615 memset (&id
, 0, sizeof (id
));
2616 id
.cb
.copy_decl
= nesting_copy_decl
;
2617 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
2620 for (; var
; var
= DECL_CHAIN (var
))
2621 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2623 struct nesting_info
*i
;
2626 val
= DECL_VALUE_EXPR (var
);
2627 type
= TREE_TYPE (var
);
2629 if (!(TREE_CODE (val
) == INDIRECT_REF
2630 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2631 && variably_modified_type_p (type
, NULL
)))
2634 tree
*slot
= root
->var_map
->get (TREE_OPERAND (val
, 0));
2635 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2638 context
= decl_function_context (var
);
2639 for (i
= root
; i
; i
= i
->outer
)
2640 if (i
->context
== context
)
2646 /* Fully expand value expressions. This avoids having debug variables
2647 only referenced from them and that can be swept during GC. */
2650 tree t
= (tree
) *slot
;
2651 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
2652 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
2655 id
.cb
.src_fn
= i
->context
;
2656 id
.cb
.dst_fn
= i
->context
;
2657 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2659 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2660 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2662 newt
= TREE_TYPE (newt
);
2663 type
= TREE_TYPE (type
);
2665 if (TYPE_NAME (newt
)
2666 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2667 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2669 && TYPE_NAME (newt
) == TYPE_NAME (type
))
2670 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2672 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2673 if (val
!= DECL_VALUE_EXPR (var
))
2674 SET_DECL_VALUE_EXPR (var
, val
);
2677 delete id
.cb
.decl_map
;
2680 /* Fold the MEM_REF *E. */
2682 fold_mem_refs (tree
*const &e
, void *data ATTRIBUTE_UNUSED
)
2684 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
2685 *ref_p
= fold (*ref_p
);
2689 /* Do "everything else" to clean up or complete state collected by the
2690 various walking passes -- lay out the types and decls, generate code
2691 to initialize the frame decl, store critical expressions in the
2692 struct function for rtl to find. */
2695 finalize_nesting_tree_1 (struct nesting_info
*root
)
2697 gimple_seq stmt_list
;
2699 tree context
= root
->context
;
2700 struct function
*sf
;
2704 /* If we created a non-local frame type or decl, we need to lay them
2705 out at this time. */
2706 if (root
->frame_type
)
2708 /* In some cases the frame type will trigger the -Wpadded warning.
2709 This is not helpful; suppress it. */
2710 int save_warn_padded
= warn_padded
;
2714 layout_type (root
->frame_type
);
2715 warn_padded
= save_warn_padded
;
2716 layout_decl (root
->frame_decl
, 0);
2718 /* Remove root->frame_decl from root->new_local_var_chain, so
2719 that we can declare it also in the lexical blocks, which
2720 helps ensure virtual regs that end up appearing in its RTL
2721 expression get substituted in instantiate_virtual_regs(). */
2722 for (adjust
= &root
->new_local_var_chain
;
2723 *adjust
!= root
->frame_decl
;
2724 adjust
= &DECL_CHAIN (*adjust
))
2725 gcc_assert (DECL_CHAIN (*adjust
));
2726 *adjust
= DECL_CHAIN (*adjust
);
2728 DECL_CHAIN (root
->frame_decl
) = NULL_TREE
;
2729 declare_vars (root
->frame_decl
,
2730 gimple_seq_first_stmt (gimple_body (context
)), true);
2733 /* If any parameters were referenced non-locally, then we need to
2734 insert a copy. Likewise, if any variables were referenced by
2735 pointer, we need to initialize the address. */
2736 if (root
->any_parm_remapped
)
2739 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
2743 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
2747 if (use_pointer_in_frame (p
))
2748 x
= build_addr (p
, context
);
2752 /* If the assignment is from a non-register the stmt is
2753 not valid gimple. Make it so by using a temporary instead. */
2754 if (!is_gimple_reg (x
)
2755 && is_gimple_reg_type (TREE_TYPE (x
)))
2757 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
2758 x
= init_tmp_var (root
, x
, &gsi
);
2761 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2762 root
->frame_decl
, field
, NULL_TREE
);
2763 stmt
= gimple_build_assign (y
, x
);
2764 gimple_seq_add_stmt (&stmt_list
, stmt
);
2768 /* If a chain_field was created, then it needs to be initialized
2770 if (root
->chain_field
)
2772 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
2773 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
2774 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
2775 gimple_seq_add_stmt (&stmt_list
, stmt
);
2778 /* If trampolines were created, then we need to initialize them. */
2779 if (root
->any_tramp_created
)
2781 struct nesting_info
*i
;
2782 for (i
= root
->inner
; i
; i
= i
->next
)
2784 tree arg1
, arg2
, arg3
, x
, field
;
2786 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
2790 gcc_assert (DECL_STATIC_CHAIN (i
->context
));
2791 arg3
= build_addr (root
->frame_decl
, context
);
2793 arg2
= build_addr (i
->context
, context
);
2795 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2796 root
->frame_decl
, field
, NULL_TREE
);
2797 arg1
= build_addr (x
, context
);
2799 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
2800 stmt
= gimple_build_call (x
, 3, arg1
, arg2
, arg3
);
2801 gimple_seq_add_stmt (&stmt_list
, stmt
);
2805 /* If we created initialization statements, insert them. */
2809 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
2810 bind
= gimple_seq_first_stmt_as_a_bind (gimple_body (context
));
2811 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
2812 gimple_bind_set_body (bind
, stmt_list
);
2815 /* If a chain_decl was created, then it needs to be registered with
2816 struct function so that it gets initialized from the static chain
2817 register at the beginning of the function. */
2818 sf
= DECL_STRUCT_FUNCTION (root
->context
);
2819 sf
->static_chain_decl
= root
->chain_decl
;
2821 /* Similarly for the non-local goto save area. */
2822 if (root
->nl_goto_field
)
2824 sf
->nonlocal_goto_save_area
2825 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
2826 sf
->has_nonlocal_label
= 1;
2829 /* Make sure all new local variables get inserted into the
2830 proper BIND_EXPR. */
2831 if (root
->new_local_var_chain
)
2832 declare_vars (root
->new_local_var_chain
,
2833 gimple_seq_first_stmt (gimple_body (root
->context
)),
2836 if (root
->debug_var_chain
)
2841 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
2843 for (debug_var
= root
->debug_var_chain
; debug_var
;
2844 debug_var
= DECL_CHAIN (debug_var
))
2845 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2848 /* If there are any debug decls with variable length types,
2849 remap those types using other debug_var_chain variables. */
2852 struct nesting_copy_body_data id
;
2854 memset (&id
, 0, sizeof (id
));
2855 id
.cb
.copy_decl
= nesting_copy_decl
;
2856 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
2859 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
2860 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2862 tree type
= TREE_TYPE (debug_var
);
2863 tree newt
, t
= type
;
2864 struct nesting_info
*i
;
2866 for (i
= root
; i
; i
= i
->outer
)
2867 if (variably_modified_type_p (type
, i
->context
))
2873 id
.cb
.src_fn
= i
->context
;
2874 id
.cb
.dst_fn
= i
->context
;
2875 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2877 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
2878 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2880 newt
= TREE_TYPE (newt
);
2883 if (TYPE_NAME (newt
)
2884 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2885 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2887 && TYPE_NAME (newt
) == TYPE_NAME (t
))
2888 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2891 delete id
.cb
.decl_map
;
2894 scope
= gimple_seq_first_stmt_as_a_bind (gimple_body (root
->context
));
2895 if (gimple_bind_block (scope
))
2896 declare_vars (root
->debug_var_chain
, scope
, true);
2898 BLOCK_VARS (DECL_INITIAL (root
->context
))
2899 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
2900 root
->debug_var_chain
);
2903 /* Fold the rewritten MEM_REF trees. */
2904 root
->mem_refs
->traverse
<void *, fold_mem_refs
> (NULL
);
2906 /* Dump the translated tree function. */
2909 fputs ("\n\n", dump_file
);
2910 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
2915 finalize_nesting_tree (struct nesting_info
*root
)
2917 struct nesting_info
*n
;
2918 FOR_EACH_NEST_INFO (n
, root
)
2919 finalize_nesting_tree_1 (n
);
2922 /* Unnest the nodes and pass them to cgraph. */
2925 unnest_nesting_tree_1 (struct nesting_info
*root
)
2927 struct cgraph_node
*node
= cgraph_node::get (root
->context
);
2929 /* For nested functions update the cgraph to reflect unnesting.
2930 We also delay finalizing of these functions up to this point. */
2934 cgraph_node::finalize_function (root
->context
, true);
2939 unnest_nesting_tree (struct nesting_info
*root
)
2941 struct nesting_info
*n
;
2942 FOR_EACH_NEST_INFO (n
, root
)
2943 unnest_nesting_tree_1 (n
);
2946 /* Free the data structures allocated during this pass. */
2949 free_nesting_tree (struct nesting_info
*root
)
2951 struct nesting_info
*node
, *next
;
2953 node
= iter_nestinfo_start (root
);
2956 next
= iter_nestinfo_next (node
);
2957 delete node
->var_map
;
2958 delete node
->field_map
;
2959 delete node
->mem_refs
;
2966 /* Gimplify a function and all its nested functions. */
2968 gimplify_all_functions (struct cgraph_node
*root
)
2970 struct cgraph_node
*iter
;
2971 if (!gimple_body (root
->decl
))
2972 gimplify_function_tree (root
->decl
);
2973 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
2974 gimplify_all_functions (iter
);
2977 /* Main entry point for this pass. Process FNDECL and all of its nested
2978 subroutines and turn them into something less tightly bound. */
2981 lower_nested_functions (tree fndecl
)
2983 struct cgraph_node
*cgn
;
2984 struct nesting_info
*root
;
2986 /* If there are no nested functions, there's nothing to do. */
2987 cgn
= cgraph_node::get (fndecl
);
2991 gimplify_all_functions (cgn
);
2993 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
2995 fprintf (dump_file
, "\n;; Function %s\n\n",
2996 lang_hooks
.decl_printable_name (fndecl
, 2));
2998 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
2999 root
= create_nesting_tree (cgn
);
3001 walk_all_functions (convert_nonlocal_reference_stmt
,
3002 convert_nonlocal_reference_op
,
3004 walk_all_functions (convert_local_reference_stmt
,
3005 convert_local_reference_op
,
3007 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
3008 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
3010 convert_all_function_calls (root
);
3011 finalize_nesting_tree (root
);
3012 unnest_nesting_tree (root
);
3014 free_nesting_tree (root
);
3015 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
3019 dump_end (TDI_nested
, dump_file
);
3024 #include "gt-tree-nested.h"