1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2014 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"
25 #include "stringpool.h"
26 #include "stor-layout.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
38 #include "gimple-iterator.h"
39 #include "gimple-walk.h"
40 #include "tree-iterator.h"
44 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
45 #include "langhooks.h"
46 #include "gimple-low.h"
49 /* The object of this pass is to lower the representation of a set of nested
50 functions in order to expose all of the gory details of the various
51 nonlocal references. We want to do this sooner rather than later, in
52 order to give us more freedom in emitting all of the functions in question.
54 Back in olden times, when gcc was young, we developed an insanely
55 complicated scheme whereby variables which were referenced nonlocally
56 were forced to live in the stack of the declaring function, and then
57 the nested functions magically discovered where these variables were
58 placed. In order for this scheme to function properly, it required
59 that the outer function be partially expanded, then we switch to
60 compiling the inner function, and once done with those we switch back
61 to compiling the outer function. Such delicate ordering requirements
62 makes it difficult to do whole translation unit optimizations
63 involving such functions.
65 The implementation here is much more direct. Everything that can be
66 referenced by an inner function is a member of an explicitly created
67 structure herein called the "nonlocal frame struct". The incoming
68 static chain for a nested function is a pointer to this struct in
69 the parent. In this way, we settle on known offsets from a known
70 base, and so are decoupled from the logic that places objects in the
71 function's stack frame. More importantly, we don't have to wait for
72 that to happen -- since the compilation of the inner function is no
73 longer tied to a real stack frame, the nonlocal frame struct can be
74 allocated anywhere. Which means that the outer function is now
77 Theory of operation here is very simple. Iterate over all the
78 statements in all the functions (depth first) several times,
79 allocating structures and fields on demand. In general we want to
80 examine inner functions first, so that we can avoid making changes
81 to outer functions which are unnecessary.
83 The order of the passes matters a bit, in that later passes will be
84 skipped if it is discovered that the functions don't actually interact
85 at all. That is, they're nested in the lexical sense but could have
86 been written as independent functions without change. */
91 struct nesting_info
*outer
;
92 struct nesting_info
*inner
;
93 struct nesting_info
*next
;
95 hash_map
<tree
, tree
> *field_map
;
96 hash_map
<tree
, tree
> *var_map
;
97 hash_set
<tree
*> *mem_refs
;
98 bitmap suppress_expansion
;
101 tree new_local_var_chain
;
102 tree debug_var_chain
;
109 bool any_parm_remapped
;
110 bool any_tramp_created
;
111 char static_chain_added
;
115 /* Iterate over the nesting tree, starting with ROOT, depth first. */
117 static inline struct nesting_info
*
118 iter_nestinfo_start (struct nesting_info
*root
)
125 static inline struct nesting_info
*
126 iter_nestinfo_next (struct nesting_info
*node
)
129 return iter_nestinfo_start (node
->next
);
133 #define FOR_EACH_NEST_INFO(I, ROOT) \
134 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
136 /* Obstack used for the bitmaps in the struct above. */
137 static struct bitmap_obstack nesting_info_bitmap_obstack
;
140 /* We're working in so many different function contexts simultaneously,
141 that create_tmp_var is dangerous. Prevent mishap. */
142 #define create_tmp_var cant_use_create_tmp_var_here_dummy
144 /* Like create_tmp_var, except record the variable for registration at
145 the given nesting level. */
148 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
152 /* If the type is of variable size or a type which must be created by the
153 frontend, something is wrong. Note that we explicitly allow
154 incomplete types here, since we create them ourselves here. */
155 gcc_assert (!TREE_ADDRESSABLE (type
));
156 gcc_assert (!TYPE_SIZE_UNIT (type
)
157 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
159 tmp_var
= create_tmp_var_raw (type
, prefix
);
160 DECL_CONTEXT (tmp_var
) = info
->context
;
161 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
162 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
163 if (TREE_CODE (type
) == COMPLEX_TYPE
164 || TREE_CODE (type
) == VECTOR_TYPE
)
165 DECL_GIMPLE_REG_P (tmp_var
) = 1;
167 info
->new_local_var_chain
= tmp_var
;
172 /* Take the address of EXP to be used within function CONTEXT.
173 Mark it for addressability as necessary. */
176 build_addr (tree exp
, tree context
)
182 while (handled_component_p (base
))
183 base
= TREE_OPERAND (base
, 0);
186 TREE_ADDRESSABLE (base
) = 1;
188 /* Building the ADDR_EXPR will compute a set of properties for
189 that ADDR_EXPR. Those properties are unfortunately context
190 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
192 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
193 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
194 way the properties are for the ADDR_EXPR are computed properly. */
195 save_context
= current_function_decl
;
196 current_function_decl
= context
;
197 retval
= build_fold_addr_expr (exp
);
198 current_function_decl
= save_context
;
202 /* Insert FIELD into TYPE, sorted by alignment requirements. */
205 insert_field_into_struct (tree type
, tree field
)
209 DECL_CONTEXT (field
) = type
;
211 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
212 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
215 DECL_CHAIN (field
) = *p
;
218 /* Set correct alignment for frame struct type. */
219 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
220 TYPE_ALIGN (type
) = DECL_ALIGN (field
);
223 /* Build or return the RECORD_TYPE that describes the frame state that is
224 shared between INFO->CONTEXT and its nested functions. This record will
225 not be complete until finalize_nesting_tree; up until that point we'll
226 be adding fields as necessary.
228 We also build the DECL that represents this frame in the function. */
231 get_frame_type (struct nesting_info
*info
)
233 tree type
= info
->frame_type
;
238 type
= make_node (RECORD_TYPE
);
240 name
= concat ("FRAME.",
241 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
243 TYPE_NAME (type
) = get_identifier (name
);
246 info
->frame_type
= type
;
247 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
248 DECL_NONLOCAL_FRAME (info
->frame_decl
) = 1;
250 /* ??? Always make it addressable for now, since it is meant to
251 be pointed to by the static chain pointer. This pessimizes
252 when it turns out that no static chains are needed because
253 the nested functions referencing non-local variables are not
254 reachable, but the true pessimization is to create the non-
255 local frame structure in the first place. */
256 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
261 /* Return true if DECL should be referenced by pointer in the non-local
265 use_pointer_in_frame (tree decl
)
267 if (TREE_CODE (decl
) == PARM_DECL
)
269 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
270 sized decls, and inefficient to copy large aggregates. Don't bother
271 moving anything but scalar variables. */
272 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
276 /* Variable sized types make things "interesting" in the frame. */
277 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
281 /* Given DECL, a non-locally accessed variable, find or create a field
282 in the non-local frame structure for the given nesting context. */
285 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
286 enum insert_option insert
)
288 if (insert
== NO_INSERT
)
290 tree
*slot
= info
->field_map
->get (decl
);
291 return slot
? *slot
: NULL_TREE
;
294 tree
*slot
= &info
->field_map
->get_or_insert (decl
);
297 tree field
= make_node (FIELD_DECL
);
298 DECL_NAME (field
) = DECL_NAME (decl
);
300 if (use_pointer_in_frame (decl
))
302 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
303 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
304 DECL_NONADDRESSABLE_P (field
) = 1;
308 TREE_TYPE (field
) = TREE_TYPE (decl
);
309 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
310 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
311 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
312 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
313 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
314 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
317 insert_field_into_struct (get_frame_type (info
), field
);
320 if (TREE_CODE (decl
) == PARM_DECL
)
321 info
->any_parm_remapped
= true;
327 /* Build or return the variable that holds the static chain within
328 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
331 get_chain_decl (struct nesting_info
*info
)
333 tree decl
= info
->chain_decl
;
339 type
= get_frame_type (info
->outer
);
340 type
= build_pointer_type (type
);
342 /* Note that this variable is *not* entered into any BIND_EXPR;
343 the construction of this variable is handled specially in
344 expand_function_start and initialize_inlined_parameters.
345 Note also that it's represented as a parameter. This is more
346 close to the truth, since the initial value does come from
348 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
349 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
350 DECL_ARTIFICIAL (decl
) = 1;
351 DECL_IGNORED_P (decl
) = 1;
352 TREE_USED (decl
) = 1;
353 DECL_CONTEXT (decl
) = info
->context
;
354 DECL_ARG_TYPE (decl
) = type
;
356 /* Tell tree-inline.c that we never write to this variable, so
357 it can copy-prop the replacement value immediately. */
358 TREE_READONLY (decl
) = 1;
360 info
->chain_decl
= decl
;
363 && (dump_flags
& TDF_DETAILS
)
364 && !DECL_STATIC_CHAIN (info
->context
))
365 fprintf (dump_file
, "Setting static-chain for %s\n",
366 lang_hooks
.decl_printable_name (info
->context
, 2));
368 DECL_STATIC_CHAIN (info
->context
) = 1;
373 /* Build or return the field within the non-local frame state that holds
374 the static chain for INFO->CONTEXT. This is the way to walk back up
375 multiple nesting levels. */
378 get_chain_field (struct nesting_info
*info
)
380 tree field
= info
->chain_field
;
384 tree type
= build_pointer_type (get_frame_type (info
->outer
));
386 field
= make_node (FIELD_DECL
);
387 DECL_NAME (field
) = get_identifier ("__chain");
388 TREE_TYPE (field
) = type
;
389 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
390 DECL_NONADDRESSABLE_P (field
) = 1;
392 insert_field_into_struct (get_frame_type (info
), field
);
394 info
->chain_field
= field
;
397 && (dump_flags
& TDF_DETAILS
)
398 && !DECL_STATIC_CHAIN (info
->context
))
399 fprintf (dump_file
, "Setting static-chain for %s\n",
400 lang_hooks
.decl_printable_name (info
->context
, 2));
402 DECL_STATIC_CHAIN (info
->context
) = 1;
407 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
410 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
415 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
416 gimple_call_set_lhs (call
, t
);
417 if (! gsi_end_p (*gsi
))
418 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
419 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
425 /* Copy EXP into a temporary. Allocate the temporary in the context of
426 INFO and insert the initialization statement before GSI. */
429 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
434 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
435 stmt
= gimple_build_assign (t
, exp
);
436 if (! gsi_end_p (*gsi
))
437 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
438 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
444 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
447 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
448 gimple_stmt_iterator
*gsi
)
450 if (is_gimple_val (exp
))
453 return init_tmp_var (info
, exp
, gsi
);
456 /* Similarly, but copy from the temporary and insert the statement
457 after the iterator. */
460 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
465 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
466 stmt
= gimple_build_assign (exp
, t
);
467 if (! gsi_end_p (*gsi
))
468 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
469 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
474 /* Build or return the type used to represent a nested function trampoline. */
476 static GTY(()) tree trampoline_type
;
479 get_trampoline_type (struct nesting_info
*info
)
481 unsigned align
, size
;
485 return trampoline_type
;
487 align
= TRAMPOLINE_ALIGNMENT
;
488 size
= TRAMPOLINE_SIZE
;
490 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
491 then allocate extra space so that we can do dynamic alignment. */
492 if (align
> STACK_BOUNDARY
)
494 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
495 align
= STACK_BOUNDARY
;
498 t
= build_index_type (size_int (size
- 1));
499 t
= build_array_type (char_type_node
, t
);
500 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
501 FIELD_DECL
, get_identifier ("__data"), t
);
502 DECL_ALIGN (t
) = align
;
503 DECL_USER_ALIGN (t
) = 1;
505 trampoline_type
= make_node (RECORD_TYPE
);
506 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
507 TYPE_FIELDS (trampoline_type
) = t
;
508 layout_type (trampoline_type
);
509 DECL_CONTEXT (t
) = trampoline_type
;
511 return trampoline_type
;
514 /* Given DECL, a nested function, find or create a field in the non-local
515 frame structure for a trampoline for this function. */
518 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
519 enum insert_option insert
)
521 if (insert
== NO_INSERT
)
523 tree
*slot
= info
->var_map
->get (decl
);
524 return slot
? *slot
: NULL_TREE
;
527 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
530 tree field
= make_node (FIELD_DECL
);
531 DECL_NAME (field
) = DECL_NAME (decl
);
532 TREE_TYPE (field
) = get_trampoline_type (info
);
533 TREE_ADDRESSABLE (field
) = 1;
535 insert_field_into_struct (get_frame_type (info
), field
);
538 info
->any_tramp_created
= true;
544 /* Build or return the field within the non-local frame state that holds
545 the non-local goto "jmp_buf". The buffer itself is maintained by the
546 rtl middle-end as dynamic stack space is allocated. */
549 get_nl_goto_field (struct nesting_info
*info
)
551 tree field
= info
->nl_goto_field
;
557 /* For __builtin_nonlocal_goto, we need N words. The first is the
558 frame pointer, the rest is for the target's stack pointer save
559 area. The number of words is controlled by STACK_SAVEAREA_MODE;
560 not the best interface, but it'll do for now. */
561 if (Pmode
== ptr_mode
)
562 type
= ptr_type_node
;
564 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
566 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
567 size
= size
/ GET_MODE_SIZE (Pmode
);
570 type
= build_array_type
571 (type
, build_index_type (size_int (size
)));
573 field
= make_node (FIELD_DECL
);
574 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
575 TREE_TYPE (field
) = type
;
576 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
577 TREE_ADDRESSABLE (field
) = 1;
579 insert_field_into_struct (get_frame_type (info
), field
);
581 info
->nl_goto_field
= field
;
587 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
590 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
591 struct nesting_info
*info
, gimple_seq
*pseq
)
593 struct walk_stmt_info wi
;
595 memset (&wi
, 0, sizeof (wi
));
598 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
602 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
605 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
606 struct nesting_info
*info
)
608 gimple_seq body
= gimple_body (info
->context
);
609 walk_body (callback_stmt
, callback_op
, info
, &body
);
610 gimple_set_body (info
->context
, body
);
613 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
616 walk_gimple_omp_for (gimple_omp_for for_stmt
,
617 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
618 struct nesting_info
*info
)
620 struct walk_stmt_info wi
;
625 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
628 memset (&wi
, 0, sizeof (wi
));
630 wi
.gsi
= gsi_last (seq
);
632 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
635 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
639 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
644 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
647 t
= gimple_omp_for_incr (for_stmt
, i
);
648 gcc_assert (BINARY_CLASS_P (t
));
650 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
653 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
656 seq
= gsi_seq (wi
.gsi
);
657 if (!gimple_seq_empty_p (seq
))
659 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
660 annotate_all_with_location (seq
, gimple_location (for_stmt
));
661 gimple_seq_add_seq (&pre_body
, seq
);
662 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
666 /* Similarly for ROOT and all functions nested underneath, depth first. */
669 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
670 struct nesting_info
*root
)
672 struct nesting_info
*n
;
673 FOR_EACH_NEST_INFO (n
, root
)
674 walk_function (callback_stmt
, callback_op
, n
);
678 /* We have to check for a fairly pathological case. The operands of function
679 nested function are to be interpreted in the context of the enclosing
680 function. So if any are variably-sized, they will get remapped when the
681 enclosing function is inlined. But that remapping would also have to be
682 done in the types of the PARM_DECLs of the nested function, meaning the
683 argument types of that function will disagree with the arguments in the
684 calls to that function. So we'd either have to make a copy of the nested
685 function corresponding to each time the enclosing function was inlined or
686 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
687 function. The former is not practical. The latter would still require
688 detecting this case to know when to add the conversions. So, for now at
689 least, we don't inline such an enclosing function.
691 We have to do that check recursively, so here return indicating whether
692 FNDECL has such a nested function. ORIG_FN is the function we were
693 trying to inline to use for checking whether any argument is variably
694 modified by anything in it.
696 It would be better to do this in tree-inline.c so that we could give
697 the appropriate warning for why a function can't be inlined, but that's
698 too late since the nesting structure has already been flattened and
699 adding a flag just to record this fact seems a waste of a flag. */
702 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
704 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
707 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
709 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
710 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
713 if (check_for_nested_with_variably_modified (cgn
->decl
,
721 /* Construct our local datastructure describing the function nesting
722 tree rooted by CGN. */
724 static struct nesting_info
*
725 create_nesting_tree (struct cgraph_node
*cgn
)
727 struct nesting_info
*info
= XCNEW (struct nesting_info
);
728 info
->field_map
= new hash_map
<tree
, tree
>;
729 info
->var_map
= new hash_map
<tree
, tree
>;
730 info
->mem_refs
= new hash_set
<tree
*>;
731 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
732 info
->context
= cgn
->decl
;
734 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
736 struct nesting_info
*sub
= create_nesting_tree (cgn
);
738 sub
->next
= info
->inner
;
742 /* See discussion at check_for_nested_with_variably_modified for a
743 discussion of why this has to be here. */
744 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
745 DECL_UNINLINABLE (info
->context
) = true;
750 /* Return an expression computing the static chain for TARGET_CONTEXT
751 from INFO->CONTEXT. Insert any necessary computations before TSI. */
754 get_static_chain (struct nesting_info
*info
, tree target_context
,
755 gimple_stmt_iterator
*gsi
)
757 struct nesting_info
*i
;
760 if (info
->context
== target_context
)
762 x
= build_addr (info
->frame_decl
, target_context
);
766 x
= get_chain_decl (info
);
768 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
770 tree field
= get_chain_field (i
);
772 x
= build_simple_mem_ref (x
);
773 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
774 x
= init_tmp_var (info
, x
, gsi
);
782 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
783 frame as seen from INFO->CONTEXT. Insert any necessary computations
787 get_frame_field (struct nesting_info
*info
, tree target_context
,
788 tree field
, gimple_stmt_iterator
*gsi
)
790 struct nesting_info
*i
;
793 if (info
->context
== target_context
)
795 /* Make sure frame_decl gets created. */
796 (void) get_frame_type (info
);
797 x
= info
->frame_decl
;
801 x
= get_chain_decl (info
);
803 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
805 tree field
= get_chain_field (i
);
807 x
= build_simple_mem_ref (x
);
808 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
809 x
= init_tmp_var (info
, x
, gsi
);
812 x
= build_simple_mem_ref (x
);
815 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
819 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
821 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
822 in the nested function with DECL_VALUE_EXPR set to reference the true
823 variable in the parent function. This is used both for debug info
824 and in OpenMP lowering. */
827 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
830 struct nesting_info
*i
;
831 tree x
, field
, new_decl
;
833 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
838 target_context
= decl_function_context (decl
);
840 /* A copy of the code in get_frame_field, but without the temporaries. */
841 if (info
->context
== target_context
)
843 /* Make sure frame_decl gets created. */
844 (void) get_frame_type (info
);
845 x
= info
->frame_decl
;
850 x
= get_chain_decl (info
);
851 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
853 field
= get_chain_field (i
);
854 x
= build_simple_mem_ref (x
);
855 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
857 x
= build_simple_mem_ref (x
);
860 field
= lookup_field_for_decl (i
, decl
, INSERT
);
861 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
862 if (use_pointer_in_frame (decl
))
863 x
= build_simple_mem_ref (x
);
865 /* ??? We should be remapping types as well, surely. */
866 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
867 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
868 DECL_CONTEXT (new_decl
) = info
->context
;
869 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
870 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
871 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
872 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
873 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
874 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
875 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
876 if ((TREE_CODE (decl
) == PARM_DECL
877 || TREE_CODE (decl
) == RESULT_DECL
878 || TREE_CODE (decl
) == VAR_DECL
)
879 && DECL_BY_REFERENCE (decl
))
880 DECL_BY_REFERENCE (new_decl
) = 1;
882 SET_DECL_VALUE_EXPR (new_decl
, x
);
883 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
886 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
887 info
->debug_var_chain
= new_decl
;
890 && info
->context
!= target_context
891 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
892 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
898 /* Callback for walk_gimple_stmt, rewrite all references to VAR
899 and PARM_DECLs that belong to outer functions.
901 The rewrite will involve some number of structure accesses back up
902 the static chain. E.g. for a variable FOO up one nesting level it'll
903 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
904 indirections apply to decls for which use_pointer_in_frame is true. */
907 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
909 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
910 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
914 switch (TREE_CODE (t
))
917 /* Non-automatic variables are never processed. */
918 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
923 if (decl_function_context (t
) != info
->context
)
928 x
= get_nonlocal_debug_decl (info
, t
);
929 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
931 tree target_context
= decl_function_context (t
);
932 struct nesting_info
*i
;
933 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
935 x
= lookup_field_for_decl (i
, t
, INSERT
);
936 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
937 if (use_pointer_in_frame (t
))
939 x
= init_tmp_var (info
, x
, &wi
->gsi
);
940 x
= build_simple_mem_ref (x
);
947 x
= save_tmp_var (info
, x
, &wi
->gsi
);
949 x
= init_tmp_var (info
, x
, &wi
->gsi
);
957 /* We're taking the address of a label from a parent function, but
958 this is not itself a non-local goto. Mark the label such that it
959 will not be deleted, much as we would with a label address in
961 if (decl_function_context (t
) != info
->context
)
962 FORCED_LABEL (t
) = 1;
967 bool save_val_only
= wi
->val_only
;
969 wi
->val_only
= false;
972 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
979 /* If we changed anything, we might no longer be directly
980 referencing a decl. */
981 save_context
= current_function_decl
;
982 current_function_decl
= info
->context
;
983 recompute_tree_invariant_for_addr_expr (t
);
984 current_function_decl
= save_context
;
986 /* If the callback converted the address argument in a context
987 where we only accept variables (and min_invariant, presumably),
988 then compute the address into a temporary. */
990 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1000 case ARRAY_RANGE_REF
:
1002 /* Go down this entire nest and just look at the final prefix and
1003 anything that describes the references. Otherwise, we lose track
1004 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1005 wi
->val_only
= true;
1007 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1009 if (TREE_CODE (t
) == COMPONENT_REF
)
1010 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1012 else if (TREE_CODE (t
) == ARRAY_REF
1013 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1015 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1017 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1019 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1023 wi
->val_only
= false;
1024 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1027 case VIEW_CONVERT_EXPR
:
1028 /* Just request to look at the subtrees, leaving val_only and lhs
1029 untouched. This might actually be for !val_only + lhs, in which
1030 case we don't want to force a replacement by a temporary. */
1035 if (!IS_TYPE_OR_DECL_P (t
))
1038 wi
->val_only
= true;
1047 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1048 struct walk_stmt_info
*);
1050 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1051 and PARM_DECLs that belong to outer functions. */
1054 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1056 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1057 bool need_chain
= false, need_stmts
= false;
1060 bitmap new_suppress
;
1062 new_suppress
= BITMAP_GGC_ALLOC ();
1063 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1065 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1067 switch (OMP_CLAUSE_CODE (clause
))
1069 case OMP_CLAUSE_REDUCTION
:
1070 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1072 goto do_decl_clause
;
1074 case OMP_CLAUSE_LASTPRIVATE
:
1075 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1077 goto do_decl_clause
;
1079 case OMP_CLAUSE_LINEAR
:
1080 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1082 wi
->val_only
= true;
1084 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
),
1086 goto do_decl_clause
;
1088 case OMP_CLAUSE_PRIVATE
:
1089 case OMP_CLAUSE_FIRSTPRIVATE
:
1090 case OMP_CLAUSE_COPYPRIVATE
:
1091 case OMP_CLAUSE_SHARED
:
1093 decl
= OMP_CLAUSE_DECL (clause
);
1094 if (TREE_CODE (decl
) == VAR_DECL
1095 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1097 if (decl_function_context (decl
) != info
->context
)
1099 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1100 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1101 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1106 case OMP_CLAUSE_SCHEDULE
:
1107 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1110 case OMP_CLAUSE_FINAL
:
1112 case OMP_CLAUSE_NUM_THREADS
:
1113 case OMP_CLAUSE_DEPEND
:
1114 case OMP_CLAUSE_DEVICE
:
1115 case OMP_CLAUSE_NUM_TEAMS
:
1116 case OMP_CLAUSE_THREAD_LIMIT
:
1117 case OMP_CLAUSE_SAFELEN
:
1118 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1119 wi
->val_only
= true;
1121 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1125 case OMP_CLAUSE_DIST_SCHEDULE
:
1126 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1128 wi
->val_only
= true;
1130 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1135 case OMP_CLAUSE_MAP
:
1137 case OMP_CLAUSE_FROM
:
1138 if (OMP_CLAUSE_SIZE (clause
))
1140 wi
->val_only
= true;
1142 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause
),
1145 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1146 goto do_decl_clause
;
1147 wi
->val_only
= true;
1149 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_nonlocal_reference_op
,
1153 case OMP_CLAUSE_ALIGNED
:
1154 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1156 wi
->val_only
= true;
1158 convert_nonlocal_reference_op
1159 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1161 /* Like do_decl_clause, but don't add any suppression. */
1162 decl
= OMP_CLAUSE_DECL (clause
);
1163 if (TREE_CODE (decl
) == VAR_DECL
1164 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1166 if (decl_function_context (decl
) != info
->context
)
1168 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1169 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1174 case OMP_CLAUSE_NOWAIT
:
1175 case OMP_CLAUSE_ORDERED
:
1176 case OMP_CLAUSE_DEFAULT
:
1177 case OMP_CLAUSE_COPYIN
:
1178 case OMP_CLAUSE_COLLAPSE
:
1179 case OMP_CLAUSE_UNTIED
:
1180 case OMP_CLAUSE_MERGEABLE
:
1181 case OMP_CLAUSE_PROC_BIND
:
1189 info
->suppress_expansion
= new_suppress
;
1192 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1193 switch (OMP_CLAUSE_CODE (clause
))
1195 case OMP_CLAUSE_REDUCTION
:
1196 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1199 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1200 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1202 walk_body (convert_nonlocal_reference_stmt
,
1203 convert_nonlocal_reference_op
, info
,
1204 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1205 walk_body (convert_nonlocal_reference_stmt
,
1206 convert_nonlocal_reference_op
, info
,
1207 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1208 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1213 case OMP_CLAUSE_LASTPRIVATE
:
1214 walk_body (convert_nonlocal_reference_stmt
,
1215 convert_nonlocal_reference_op
, info
,
1216 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1219 case OMP_CLAUSE_LINEAR
:
1220 walk_body (convert_nonlocal_reference_stmt
,
1221 convert_nonlocal_reference_op
, info
,
1222 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1232 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1235 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1237 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1238 type
= TREE_TYPE (type
);
1240 if (TYPE_NAME (type
)
1241 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1242 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1243 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1245 while (POINTER_TYPE_P (type
)
1246 || TREE_CODE (type
) == VECTOR_TYPE
1247 || TREE_CODE (type
) == FUNCTION_TYPE
1248 || TREE_CODE (type
) == METHOD_TYPE
)
1249 type
= TREE_TYPE (type
);
1251 if (TREE_CODE (type
) == ARRAY_TYPE
)
1255 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1256 domain
= TYPE_DOMAIN (type
);
1259 t
= TYPE_MIN_VALUE (domain
);
1260 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1261 && decl_function_context (t
) != info
->context
)
1262 get_nonlocal_debug_decl (info
, t
);
1263 t
= TYPE_MAX_VALUE (domain
);
1264 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1265 && decl_function_context (t
) != info
->context
)
1266 get_nonlocal_debug_decl (info
, t
);
1271 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1275 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1279 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
1280 if (TREE_CODE (var
) == VAR_DECL
1281 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1282 && DECL_HAS_VALUE_EXPR_P (var
)
1283 && decl_function_context (var
) != info
->context
)
1284 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1287 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1288 PARM_DECLs that belong to outer functions. This handles statements
1289 that are not handled via the standard recursion done in
1290 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1291 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1292 operands of STMT have been handled by this function. */
1295 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1296 struct walk_stmt_info
*wi
)
1298 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1299 tree save_local_var_chain
;
1300 bitmap save_suppress
;
1301 gimple stmt
= gsi_stmt (*gsi
);
1303 switch (gimple_code (stmt
))
1306 /* Don't walk non-local gotos for now. */
1307 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1309 wi
->val_only
= true;
1311 *handled_ops_p
= true;
1316 case GIMPLE_OMP_PARALLEL
:
1317 case GIMPLE_OMP_TASK
:
1318 save_suppress
= info
->suppress_expansion
;
1319 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1323 decl
= get_chain_decl (info
);
1324 c
= build_omp_clause (gimple_location (stmt
),
1325 OMP_CLAUSE_FIRSTPRIVATE
);
1326 OMP_CLAUSE_DECL (c
) = decl
;
1327 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1328 gimple_omp_taskreg_set_clauses (stmt
, c
);
1331 save_local_var_chain
= info
->new_local_var_chain
;
1332 info
->new_local_var_chain
= NULL
;
1334 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1335 info
, gimple_omp_body_ptr (stmt
));
1337 if (info
->new_local_var_chain
)
1338 declare_vars (info
->new_local_var_chain
,
1339 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1341 info
->new_local_var_chain
= save_local_var_chain
;
1342 info
->suppress_expansion
= save_suppress
;
1345 case GIMPLE_OMP_FOR
:
1346 save_suppress
= info
->suppress_expansion
;
1347 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1348 walk_gimple_omp_for (as_a
<gimple_omp_for
> (stmt
),
1349 convert_nonlocal_reference_stmt
,
1350 convert_nonlocal_reference_op
, info
);
1351 walk_body (convert_nonlocal_reference_stmt
,
1352 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1353 info
->suppress_expansion
= save_suppress
;
1356 case GIMPLE_OMP_SECTIONS
:
1357 save_suppress
= info
->suppress_expansion
;
1358 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1359 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1360 info
, gimple_omp_body_ptr (stmt
));
1361 info
->suppress_expansion
= save_suppress
;
1364 case GIMPLE_OMP_SINGLE
:
1365 save_suppress
= info
->suppress_expansion
;
1366 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1367 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1368 info
, gimple_omp_body_ptr (stmt
));
1369 info
->suppress_expansion
= save_suppress
;
1372 case GIMPLE_OMP_TARGET
:
1373 if (gimple_omp_target_kind (stmt
) != GF_OMP_TARGET_KIND_REGION
)
1375 save_suppress
= info
->suppress_expansion
;
1376 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1378 info
->suppress_expansion
= save_suppress
;
1379 walk_body (convert_nonlocal_reference_stmt
,
1380 convert_nonlocal_reference_op
, info
,
1381 gimple_omp_body_ptr (stmt
));
1384 save_suppress
= info
->suppress_expansion
;
1385 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1389 decl
= get_chain_decl (info
);
1390 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1391 OMP_CLAUSE_DECL (c
) = decl
;
1392 OMP_CLAUSE_MAP_KIND (c
) = OMP_CLAUSE_MAP_TO
;
1393 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
1394 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1395 gimple_omp_target_set_clauses (as_a
<gimple_omp_target
> (stmt
), c
);
1398 save_local_var_chain
= info
->new_local_var_chain
;
1399 info
->new_local_var_chain
= NULL
;
1401 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1402 info
, gimple_omp_body_ptr (stmt
));
1404 if (info
->new_local_var_chain
)
1405 declare_vars (info
->new_local_var_chain
,
1406 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1408 info
->new_local_var_chain
= save_local_var_chain
;
1409 info
->suppress_expansion
= save_suppress
;
1412 case GIMPLE_OMP_TEAMS
:
1413 save_suppress
= info
->suppress_expansion
;
1414 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1415 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1416 info
, gimple_omp_body_ptr (stmt
));
1417 info
->suppress_expansion
= save_suppress
;
1420 case GIMPLE_OMP_SECTION
:
1421 case GIMPLE_OMP_MASTER
:
1422 case GIMPLE_OMP_TASKGROUP
:
1423 case GIMPLE_OMP_ORDERED
:
1424 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1425 info
, gimple_omp_body_ptr (stmt
));
1430 gimple_bind bind_stmt
= as_a
<gimple_bind
> (stmt
);
1431 if (!optimize
&& gimple_bind_block (bind_stmt
))
1432 note_nonlocal_block_vlas (info
, gimple_bind_block (bind_stmt
));
1434 for (tree var
= gimple_bind_vars (bind_stmt
); var
; var
= DECL_CHAIN (var
))
1435 if (TREE_CODE (var
) == NAMELIST_DECL
)
1437 /* Adjust decls mentioned in NAMELIST_DECL. */
1438 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
1442 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
1444 if (TREE_CODE (decl
) == VAR_DECL
1445 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1447 if (decl_function_context (decl
) != info
->context
)
1448 CONSTRUCTOR_ELT (decls
, i
)->value
1449 = get_nonlocal_debug_decl (info
, decl
);
1453 *handled_ops_p
= false;
1457 wi
->val_only
= true;
1459 *handled_ops_p
= false;
1463 /* For every other statement that we are not interested in
1464 handling here, let the walker traverse the operands. */
1465 *handled_ops_p
= false;
1469 /* We have handled all of STMT operands, no need to traverse the operands. */
1470 *handled_ops_p
= true;
1475 /* A subroutine of convert_local_reference. Create a local variable
1476 in the parent function with DECL_VALUE_EXPR set to reference the
1477 field in FRAME. This is used both for debug info and in OpenMP
1481 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1485 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1489 /* Make sure frame_decl gets created. */
1490 (void) get_frame_type (info
);
1491 x
= info
->frame_decl
;
1492 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1494 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1495 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1496 DECL_CONTEXT (new_decl
) = info
->context
;
1497 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1498 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1499 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1500 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1501 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1502 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1503 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1504 if ((TREE_CODE (decl
) == PARM_DECL
1505 || TREE_CODE (decl
) == RESULT_DECL
1506 || TREE_CODE (decl
) == VAR_DECL
)
1507 && DECL_BY_REFERENCE (decl
))
1508 DECL_BY_REFERENCE (new_decl
) = 1;
1510 SET_DECL_VALUE_EXPR (new_decl
, x
);
1511 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1514 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1515 info
->debug_var_chain
= new_decl
;
1517 /* Do not emit debug info twice. */
1518 DECL_IGNORED_P (decl
) = 1;
1524 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1525 and PARM_DECLs that were referenced by inner nested functions.
1526 The rewrite will be a structure reference to the local frame variable. */
1528 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1531 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1533 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1534 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1535 tree t
= *tp
, field
, x
;
1539 switch (TREE_CODE (t
))
1542 /* Non-automatic variables are never processed. */
1543 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1548 if (decl_function_context (t
) == info
->context
)
1550 /* If we copied a pointer to the frame, then the original decl
1551 is used unchanged in the parent function. */
1552 if (use_pointer_in_frame (t
))
1555 /* No need to transform anything if no child references the
1557 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1562 x
= get_local_debug_decl (info
, t
, field
);
1563 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1564 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1569 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1571 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1579 save_val_only
= wi
->val_only
;
1580 wi
->val_only
= false;
1582 wi
->changed
= false;
1583 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1584 wi
->val_only
= save_val_only
;
1586 /* If we converted anything ... */
1591 /* Then the frame decl is now addressable. */
1592 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1594 save_context
= current_function_decl
;
1595 current_function_decl
= info
->context
;
1596 recompute_tree_invariant_for_addr_expr (t
);
1597 current_function_decl
= save_context
;
1599 /* If we are in a context where we only accept values, then
1600 compute the address into a temporary. */
1602 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1611 case ARRAY_RANGE_REF
:
1613 /* Go down this entire nest and just look at the final prefix and
1614 anything that describes the references. Otherwise, we lose track
1615 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1616 save_val_only
= wi
->val_only
;
1617 wi
->val_only
= true;
1619 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1621 if (TREE_CODE (t
) == COMPONENT_REF
)
1622 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1624 else if (TREE_CODE (t
) == ARRAY_REF
1625 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1627 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1629 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1631 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1635 wi
->val_only
= false;
1636 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1637 wi
->val_only
= save_val_only
;
1641 save_val_only
= wi
->val_only
;
1642 wi
->val_only
= true;
1644 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1646 /* We need to re-fold the MEM_REF as component references as
1647 part of a ADDR_EXPR address are not allowed. But we cannot
1648 fold here, as the chain record type is not yet finalized. */
1649 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1650 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1651 info
->mem_refs
->add (tp
);
1652 wi
->val_only
= save_val_only
;
1655 case VIEW_CONVERT_EXPR
:
1656 /* Just request to look at the subtrees, leaving val_only and lhs
1657 untouched. This might actually be for !val_only + lhs, in which
1658 case we don't want to force a replacement by a temporary. */
1663 if (!IS_TYPE_OR_DECL_P (t
))
1666 wi
->val_only
= true;
1675 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1676 struct walk_stmt_info
*);
1678 /* Helper for convert_local_reference. Convert all the references in
1679 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1682 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1684 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1685 bool need_frame
= false, need_stmts
= false;
1688 bitmap new_suppress
;
1690 new_suppress
= BITMAP_GGC_ALLOC ();
1691 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1693 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1695 switch (OMP_CLAUSE_CODE (clause
))
1697 case OMP_CLAUSE_REDUCTION
:
1698 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1700 goto do_decl_clause
;
1702 case OMP_CLAUSE_LASTPRIVATE
:
1703 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1705 goto do_decl_clause
;
1707 case OMP_CLAUSE_LINEAR
:
1708 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1710 wi
->val_only
= true;
1712 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
), &dummy
,
1714 goto do_decl_clause
;
1716 case OMP_CLAUSE_PRIVATE
:
1717 case OMP_CLAUSE_FIRSTPRIVATE
:
1718 case OMP_CLAUSE_COPYPRIVATE
:
1719 case OMP_CLAUSE_SHARED
:
1721 decl
= OMP_CLAUSE_DECL (clause
);
1722 if (TREE_CODE (decl
) == VAR_DECL
1723 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1725 if (decl_function_context (decl
) == info
->context
1726 && !use_pointer_in_frame (decl
))
1728 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1731 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1732 OMP_CLAUSE_DECL (clause
)
1733 = get_local_debug_decl (info
, decl
, field
);
1739 case OMP_CLAUSE_SCHEDULE
:
1740 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1743 case OMP_CLAUSE_FINAL
:
1745 case OMP_CLAUSE_NUM_THREADS
:
1746 case OMP_CLAUSE_DEPEND
:
1747 case OMP_CLAUSE_DEVICE
:
1748 case OMP_CLAUSE_NUM_TEAMS
:
1749 case OMP_CLAUSE_THREAD_LIMIT
:
1750 case OMP_CLAUSE_SAFELEN
:
1751 case OMP_CLAUSE__CILK_FOR_COUNT_
:
1752 wi
->val_only
= true;
1754 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1758 case OMP_CLAUSE_DIST_SCHEDULE
:
1759 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1761 wi
->val_only
= true;
1763 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1768 case OMP_CLAUSE_MAP
:
1770 case OMP_CLAUSE_FROM
:
1771 if (OMP_CLAUSE_SIZE (clause
))
1773 wi
->val_only
= true;
1775 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause
),
1778 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1779 goto do_decl_clause
;
1780 wi
->val_only
= true;
1782 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_local_reference_op
,
1786 case OMP_CLAUSE_ALIGNED
:
1787 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1789 wi
->val_only
= true;
1791 convert_local_reference_op
1792 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1794 /* Like do_decl_clause, but don't add any suppression. */
1795 decl
= OMP_CLAUSE_DECL (clause
);
1796 if (TREE_CODE (decl
) == VAR_DECL
1797 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1799 if (decl_function_context (decl
) == info
->context
1800 && !use_pointer_in_frame (decl
))
1802 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1805 OMP_CLAUSE_DECL (clause
)
1806 = get_local_debug_decl (info
, decl
, field
);
1812 case OMP_CLAUSE_NOWAIT
:
1813 case OMP_CLAUSE_ORDERED
:
1814 case OMP_CLAUSE_DEFAULT
:
1815 case OMP_CLAUSE_COPYIN
:
1816 case OMP_CLAUSE_COLLAPSE
:
1817 case OMP_CLAUSE_UNTIED
:
1818 case OMP_CLAUSE_MERGEABLE
:
1819 case OMP_CLAUSE_PROC_BIND
:
1827 info
->suppress_expansion
= new_suppress
;
1830 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1831 switch (OMP_CLAUSE_CODE (clause
))
1833 case OMP_CLAUSE_REDUCTION
:
1834 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1837 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1838 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1840 walk_body (convert_local_reference_stmt
,
1841 convert_local_reference_op
, info
,
1842 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1843 walk_body (convert_local_reference_stmt
,
1844 convert_local_reference_op
, info
,
1845 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1846 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1851 case OMP_CLAUSE_LASTPRIVATE
:
1852 walk_body (convert_local_reference_stmt
,
1853 convert_local_reference_op
, info
,
1854 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1857 case OMP_CLAUSE_LINEAR
:
1858 walk_body (convert_local_reference_stmt
,
1859 convert_local_reference_op
, info
,
1860 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1871 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1872 and PARM_DECLs that were referenced by inner nested functions.
1873 The rewrite will be a structure reference to the local frame variable. */
1876 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1877 struct walk_stmt_info
*wi
)
1879 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1880 tree save_local_var_chain
;
1881 bitmap save_suppress
;
1882 gimple stmt
= gsi_stmt (*gsi
);
1884 switch (gimple_code (stmt
))
1886 case GIMPLE_OMP_PARALLEL
:
1887 case GIMPLE_OMP_TASK
:
1888 save_suppress
= info
->suppress_expansion
;
1889 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1893 (void) get_frame_type (info
);
1894 c
= build_omp_clause (gimple_location (stmt
),
1896 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1897 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1898 gimple_omp_taskreg_set_clauses (stmt
, c
);
1901 save_local_var_chain
= info
->new_local_var_chain
;
1902 info
->new_local_var_chain
= NULL
;
1904 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1905 gimple_omp_body_ptr (stmt
));
1907 if (info
->new_local_var_chain
)
1908 declare_vars (info
->new_local_var_chain
,
1909 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1910 info
->new_local_var_chain
= save_local_var_chain
;
1911 info
->suppress_expansion
= save_suppress
;
1914 case GIMPLE_OMP_FOR
:
1915 save_suppress
= info
->suppress_expansion
;
1916 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1917 walk_gimple_omp_for (as_a
<gimple_omp_for
> (stmt
),
1918 convert_local_reference_stmt
,
1919 convert_local_reference_op
, info
);
1920 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1921 info
, gimple_omp_body_ptr (stmt
));
1922 info
->suppress_expansion
= save_suppress
;
1925 case GIMPLE_OMP_SECTIONS
:
1926 save_suppress
= info
->suppress_expansion
;
1927 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1928 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1929 info
, gimple_omp_body_ptr (stmt
));
1930 info
->suppress_expansion
= save_suppress
;
1933 case GIMPLE_OMP_SINGLE
:
1934 save_suppress
= info
->suppress_expansion
;
1935 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1936 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1937 info
, gimple_omp_body_ptr (stmt
));
1938 info
->suppress_expansion
= save_suppress
;
1941 case GIMPLE_OMP_TARGET
:
1942 if (gimple_omp_target_kind (stmt
) != GF_OMP_TARGET_KIND_REGION
)
1944 save_suppress
= info
->suppress_expansion
;
1945 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
1946 info
->suppress_expansion
= save_suppress
;
1947 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1948 info
, gimple_omp_body_ptr (stmt
));
1951 save_suppress
= info
->suppress_expansion
;
1952 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
))
1955 (void) get_frame_type (info
);
1956 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1957 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1958 OMP_CLAUSE_MAP_KIND (c
) = OMP_CLAUSE_MAP_TOFROM
;
1959 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
1960 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1961 gimple_omp_target_set_clauses (as_a
<gimple_omp_target
> (stmt
), c
);
1964 save_local_var_chain
= info
->new_local_var_chain
;
1965 info
->new_local_var_chain
= NULL
;
1967 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1968 gimple_omp_body_ptr (stmt
));
1970 if (info
->new_local_var_chain
)
1971 declare_vars (info
->new_local_var_chain
,
1972 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1973 info
->new_local_var_chain
= save_local_var_chain
;
1974 info
->suppress_expansion
= save_suppress
;
1977 case GIMPLE_OMP_TEAMS
:
1978 save_suppress
= info
->suppress_expansion
;
1979 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1980 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1981 info
, gimple_omp_body_ptr (stmt
));
1982 info
->suppress_expansion
= save_suppress
;
1985 case GIMPLE_OMP_SECTION
:
1986 case GIMPLE_OMP_MASTER
:
1987 case GIMPLE_OMP_TASKGROUP
:
1988 case GIMPLE_OMP_ORDERED
:
1989 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1990 info
, gimple_omp_body_ptr (stmt
));
1994 wi
->val_only
= true;
1996 *handled_ops_p
= false;
2000 if (gimple_clobber_p (stmt
))
2002 tree lhs
= gimple_assign_lhs (stmt
);
2003 if (!use_pointer_in_frame (lhs
)
2004 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
2006 gsi_replace (gsi
, gimple_build_nop (), true);
2010 *handled_ops_p
= false;
2014 for (tree var
= gimple_bind_vars (as_a
<gimple_bind
> (stmt
));
2016 var
= DECL_CHAIN (var
))
2017 if (TREE_CODE (var
) == NAMELIST_DECL
)
2019 /* Adjust decls mentioned in NAMELIST_DECL. */
2020 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
2024 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
2026 if (TREE_CODE (decl
) == VAR_DECL
2027 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2029 if (decl_function_context (decl
) == info
->context
2030 && !use_pointer_in_frame (decl
))
2032 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2035 CONSTRUCTOR_ELT (decls
, i
)->value
2036 = get_local_debug_decl (info
, decl
, field
);
2042 *handled_ops_p
= false;
2046 /* For every other statement that we are not interested in
2047 handling here, let the walker traverse the operands. */
2048 *handled_ops_p
= false;
2052 /* Indicate that we have handled all the operands ourselves. */
2053 *handled_ops_p
= true;
2058 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2059 that reference labels from outer functions. The rewrite will be a
2060 call to __builtin_nonlocal_goto. */
2063 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2064 struct walk_stmt_info
*wi
)
2066 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2067 tree label
, new_label
, target_context
, x
, field
;
2069 gimple stmt
= gsi_stmt (*gsi
);
2071 if (gimple_code (stmt
) != GIMPLE_GOTO
)
2073 *handled_ops_p
= false;
2077 label
= gimple_goto_dest (stmt
);
2078 if (TREE_CODE (label
) != LABEL_DECL
)
2080 *handled_ops_p
= false;
2084 target_context
= decl_function_context (label
);
2085 if (target_context
== info
->context
)
2087 *handled_ops_p
= false;
2091 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
2094 /* The original user label may also be use for a normal goto, therefore
2095 we must create a new label that will actually receive the abnormal
2096 control transfer. This new label will be marked LABEL_NONLOCAL; this
2097 mark will trigger proper behavior in the cfg, as well as cause the
2098 (hairy target-specific) non-local goto receiver code to be generated
2099 when we expand rtl. Enter this association into var_map so that we
2100 can insert the new label into the IL during a second pass. */
2101 tree
*slot
= &i
->var_map
->get_or_insert (label
);
2104 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
2105 DECL_NONLOCAL (new_label
) = 1;
2111 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2112 field
= get_nl_goto_field (i
);
2113 x
= get_frame_field (info
, target_context
, field
, gsi
);
2114 x
= build_addr (x
, target_context
);
2115 x
= gsi_gimplify_val (info
, x
, gsi
);
2116 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
2117 2, build_addr (new_label
, target_context
), x
);
2118 gsi_replace (gsi
, call
, false);
2120 /* We have handled all of STMT's operands, no need to keep going. */
2121 *handled_ops_p
= true;
2126 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2127 are referenced via nonlocal goto from a nested function. The rewrite
2128 will involve installing a newly generated DECL_NONLOCAL label, and
2129 (potentially) a branch around the rtl gunk that is assumed to be
2130 attached to such a label. */
2133 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2134 struct walk_stmt_info
*wi
)
2136 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2137 tree label
, new_label
;
2138 gimple_stmt_iterator tmp_gsi
;
2139 gimple_label stmt
= dyn_cast
<gimple_label
> (gsi_stmt (*gsi
));
2143 *handled_ops_p
= false;
2147 label
= gimple_label_label (stmt
);
2149 tree
*slot
= info
->var_map
->get (label
);
2152 *handled_ops_p
= false;
2156 /* If there's any possibility that the previous statement falls through,
2157 then we must branch around the new non-local label. */
2159 gsi_prev (&tmp_gsi
);
2160 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
2162 gimple stmt
= gimple_build_goto (label
);
2163 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2166 new_label
= (tree
) *slot
;
2167 stmt
= gimple_build_label (new_label
);
2168 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2170 *handled_ops_p
= true;
2175 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2176 of nested functions that require the use of trampolines. The rewrite
2177 will involve a reference a trampoline generated for the occasion. */
2180 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
2182 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2183 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2184 tree t
= *tp
, decl
, target_context
, x
, builtin
;
2188 switch (TREE_CODE (t
))
2192 T.1 = &CHAIN->tramp;
2193 T.2 = __builtin_adjust_trampoline (T.1);
2194 T.3 = (func_type)T.2;
2197 decl
= TREE_OPERAND (t
, 0);
2198 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2201 /* Only need to process nested functions. */
2202 target_context
= decl_function_context (decl
);
2203 if (!target_context
)
2206 /* If the nested function doesn't use a static chain, then
2207 it doesn't need a trampoline. */
2208 if (!DECL_STATIC_CHAIN (decl
))
2211 /* If we don't want a trampoline, then don't build one. */
2212 if (TREE_NO_TRAMPOLINE (t
))
2215 /* Lookup the immediate parent of the callee, as that's where
2216 we need to insert the trampoline. */
2217 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
2219 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
2221 /* Compute the address of the field holding the trampoline. */
2222 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
2223 x
= build_addr (x
, target_context
);
2224 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
2226 /* Do machine-specific ugliness. Normally this will involve
2227 computing extra alignment, but it can really be anything. */
2228 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
2229 call
= gimple_build_call (builtin
, 1, x
);
2230 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
2232 /* Cast back to the proper function type. */
2233 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
2234 x
= init_tmp_var (info
, x
, &wi
->gsi
);
2240 if (!IS_TYPE_OR_DECL_P (t
))
2249 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2250 to addresses of nested functions that require the use of
2251 trampolines. The rewrite will involve a reference a trampoline
2252 generated for the occasion. */
2255 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2256 struct walk_stmt_info
*wi
)
2258 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2259 gimple stmt
= gsi_stmt (*gsi
);
2261 switch (gimple_code (stmt
))
2265 /* Only walk call arguments, lest we generate trampolines for
2267 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2268 for (i
= 0; i
< nargs
; i
++)
2269 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2274 case GIMPLE_OMP_TARGET
:
2275 if (gimple_omp_target_kind (stmt
) != GF_OMP_TARGET_KIND_REGION
)
2277 *handled_ops_p
= false;
2281 case GIMPLE_OMP_PARALLEL
:
2282 case GIMPLE_OMP_TASK
:
2284 tree save_local_var_chain
;
2285 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2286 save_local_var_chain
= info
->new_local_var_chain
;
2287 info
->new_local_var_chain
= NULL
;
2288 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2289 info
, gimple_omp_body_ptr (stmt
));
2290 if (info
->new_local_var_chain
)
2291 declare_vars (info
->new_local_var_chain
,
2292 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2294 info
->new_local_var_chain
= save_local_var_chain
;
2299 *handled_ops_p
= false;
2303 *handled_ops_p
= true;
2309 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2310 that reference nested functions to make sure that the static chain
2311 is set up properly for the call. */
2314 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2315 struct walk_stmt_info
*wi
)
2317 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2318 tree decl
, target_context
;
2319 char save_static_chain_added
;
2321 gimple stmt
= gsi_stmt (*gsi
);
2323 switch (gimple_code (stmt
))
2326 if (gimple_call_chain (stmt
))
2328 decl
= gimple_call_fndecl (stmt
);
2331 target_context
= decl_function_context (decl
);
2332 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2334 gimple_call_set_chain (as_a
<gimple_call
> (stmt
),
2335 get_static_chain (info
, target_context
,
2337 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2341 case GIMPLE_OMP_PARALLEL
:
2342 case GIMPLE_OMP_TASK
:
2343 save_static_chain_added
= info
->static_chain_added
;
2344 info
->static_chain_added
= 0;
2345 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2346 for (i
= 0; i
< 2; i
++)
2349 if ((info
->static_chain_added
& (1 << i
)) == 0)
2351 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2352 /* Don't add CHAIN.* or FRAME.* twice. */
2353 for (c
= gimple_omp_taskreg_clauses (stmt
);
2355 c
= OMP_CLAUSE_CHAIN (c
))
2356 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2357 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2358 && OMP_CLAUSE_DECL (c
) == decl
)
2362 c
= build_omp_clause (gimple_location (stmt
),
2363 i
? OMP_CLAUSE_FIRSTPRIVATE
2364 : OMP_CLAUSE_SHARED
);
2365 OMP_CLAUSE_DECL (c
) = decl
;
2366 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2367 gimple_omp_taskreg_set_clauses (stmt
, c
);
2370 info
->static_chain_added
|= save_static_chain_added
;
2373 case GIMPLE_OMP_TARGET
:
2374 if (gimple_omp_target_kind (stmt
) != GF_OMP_TARGET_KIND_REGION
)
2376 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2379 save_static_chain_added
= info
->static_chain_added
;
2380 info
->static_chain_added
= 0;
2381 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2382 for (i
= 0; i
< 2; i
++)
2385 if ((info
->static_chain_added
& (1 << i
)) == 0)
2387 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2388 /* Don't add CHAIN.* or FRAME.* twice. */
2389 for (c
= gimple_omp_target_clauses (stmt
);
2391 c
= OMP_CLAUSE_CHAIN (c
))
2392 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
2393 && OMP_CLAUSE_DECL (c
) == decl
)
2397 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2398 OMP_CLAUSE_DECL (c
) = decl
;
2399 OMP_CLAUSE_MAP_KIND (c
)
2400 = i
? OMP_CLAUSE_MAP_TO
: OMP_CLAUSE_MAP_TOFROM
;
2401 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2402 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2403 gimple_omp_target_set_clauses (as_a
<gimple_omp_target
> (stmt
),
2407 info
->static_chain_added
|= save_static_chain_added
;
2410 case GIMPLE_OMP_FOR
:
2411 walk_body (convert_gimple_call
, NULL
, info
,
2412 gimple_omp_for_pre_body_ptr (stmt
));
2414 case GIMPLE_OMP_SECTIONS
:
2415 case GIMPLE_OMP_SECTION
:
2416 case GIMPLE_OMP_SINGLE
:
2417 case GIMPLE_OMP_TEAMS
:
2418 case GIMPLE_OMP_MASTER
:
2419 case GIMPLE_OMP_TASKGROUP
:
2420 case GIMPLE_OMP_ORDERED
:
2421 case GIMPLE_OMP_CRITICAL
:
2422 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2426 /* Keep looking for other operands. */
2427 *handled_ops_p
= false;
2431 *handled_ops_p
= true;
2435 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2436 call expressions. At the same time, determine if a nested function
2437 actually uses its static chain; if not, remember that. */
2440 convert_all_function_calls (struct nesting_info
*root
)
2442 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2443 struct nesting_info
*n
;
2445 /* First, optimistically clear static_chain for all decls that haven't
2446 used the static chain already for variable access. But always create
2447 it if not optimizing. This makes it possible to reconstruct the static
2448 nesting tree at run time and thus to resolve up-level references from
2449 within the debugger. */
2450 FOR_EACH_NEST_INFO (n
, root
)
2452 tree decl
= n
->context
;
2456 (void) get_frame_type (n
);
2458 (void) get_chain_decl (n
);
2460 else if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2462 DECL_STATIC_CHAIN (decl
) = 0;
2463 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2464 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2465 lang_hooks
.decl_printable_name (decl
, 2));
2468 DECL_STATIC_CHAIN (decl
) = 1;
2469 chain_count
+= DECL_STATIC_CHAIN (decl
);
2472 /* Walk the functions and perform transformations. Note that these
2473 transformations can induce new uses of the static chain, which in turn
2474 require re-examining all users of the decl. */
2475 /* ??? It would make sense to try to use the call graph to speed this up,
2476 but the call graph hasn't really been built yet. Even if it did, we
2477 would still need to iterate in this loop since address-of references
2478 wouldn't show up in the callgraph anyway. */
2482 old_chain_count
= chain_count
;
2486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2487 fputc ('\n', dump_file
);
2489 FOR_EACH_NEST_INFO (n
, root
)
2491 tree decl
= n
->context
;
2492 walk_function (convert_tramp_reference_stmt
,
2493 convert_tramp_reference_op
, n
);
2494 walk_function (convert_gimple_call
, NULL
, n
);
2495 chain_count
+= DECL_STATIC_CHAIN (decl
);
2498 while (chain_count
!= old_chain_count
);
2500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2501 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2505 struct nesting_copy_body_data
2508 struct nesting_info
*root
;
2511 /* A helper subroutine for debug_var_chain type remapping. */
2514 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2516 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2517 tree
*slot
= nid
->root
->var_map
->get (decl
);
2520 return (tree
) *slot
;
2522 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2524 tree new_decl
= copy_decl_no_change (decl
, id
);
2525 DECL_ORIGINAL_TYPE (new_decl
)
2526 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2530 if (TREE_CODE (decl
) == VAR_DECL
2531 || TREE_CODE (decl
) == PARM_DECL
2532 || TREE_CODE (decl
) == RESULT_DECL
)
2535 return copy_decl_no_change (decl
, id
);
2538 /* A helper function for remap_vla_decls. See if *TP contains
2539 some remapped variables. */
2542 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2544 struct nesting_info
*root
= (struct nesting_info
*) data
;
2550 tree
*slot
= root
->var_map
->get (t
);
2558 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2562 remap_vla_decls (tree block
, struct nesting_info
*root
)
2564 tree var
, subblock
, val
, type
;
2565 struct nesting_copy_body_data id
;
2567 for (subblock
= BLOCK_SUBBLOCKS (block
);
2569 subblock
= BLOCK_CHAIN (subblock
))
2570 remap_vla_decls (subblock
, root
);
2572 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
2573 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2575 val
= DECL_VALUE_EXPR (var
);
2576 type
= TREE_TYPE (var
);
2578 if (!(TREE_CODE (val
) == INDIRECT_REF
2579 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2580 && variably_modified_type_p (type
, NULL
)))
2583 if (root
->var_map
->get (TREE_OPERAND (val
, 0))
2584 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2588 if (var
== NULL_TREE
)
2591 memset (&id
, 0, sizeof (id
));
2592 id
.cb
.copy_decl
= nesting_copy_decl
;
2593 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
2596 for (; var
; var
= DECL_CHAIN (var
))
2597 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2599 struct nesting_info
*i
;
2602 val
= DECL_VALUE_EXPR (var
);
2603 type
= TREE_TYPE (var
);
2605 if (!(TREE_CODE (val
) == INDIRECT_REF
2606 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2607 && variably_modified_type_p (type
, NULL
)))
2610 tree
*slot
= root
->var_map
->get (TREE_OPERAND (val
, 0));
2611 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2614 context
= decl_function_context (var
);
2615 for (i
= root
; i
; i
= i
->outer
)
2616 if (i
->context
== context
)
2622 /* Fully expand value expressions. This avoids having debug variables
2623 only referenced from them and that can be swept during GC. */
2626 tree t
= (tree
) *slot
;
2627 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
2628 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
2631 id
.cb
.src_fn
= i
->context
;
2632 id
.cb
.dst_fn
= i
->context
;
2633 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2635 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2636 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2638 newt
= TREE_TYPE (newt
);
2639 type
= TREE_TYPE (type
);
2641 if (TYPE_NAME (newt
)
2642 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2643 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2645 && TYPE_NAME (newt
) == TYPE_NAME (type
))
2646 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2648 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2649 if (val
!= DECL_VALUE_EXPR (var
))
2650 SET_DECL_VALUE_EXPR (var
, val
);
2653 delete id
.cb
.decl_map
;
2656 /* Fold the MEM_REF *E. */
2658 fold_mem_refs (tree
*const &e
, void *data ATTRIBUTE_UNUSED
)
2660 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
2661 *ref_p
= fold (*ref_p
);
2665 /* Do "everything else" to clean up or complete state collected by the
2666 various walking passes -- lay out the types and decls, generate code
2667 to initialize the frame decl, store critical expressions in the
2668 struct function for rtl to find. */
2671 finalize_nesting_tree_1 (struct nesting_info
*root
)
2673 gimple_seq stmt_list
;
2675 tree context
= root
->context
;
2676 struct function
*sf
;
2680 /* If we created a non-local frame type or decl, we need to lay them
2681 out at this time. */
2682 if (root
->frame_type
)
2684 /* In some cases the frame type will trigger the -Wpadded warning.
2685 This is not helpful; suppress it. */
2686 int save_warn_padded
= warn_padded
;
2690 layout_type (root
->frame_type
);
2691 warn_padded
= save_warn_padded
;
2692 layout_decl (root
->frame_decl
, 0);
2694 /* Remove root->frame_decl from root->new_local_var_chain, so
2695 that we can declare it also in the lexical blocks, which
2696 helps ensure virtual regs that end up appearing in its RTL
2697 expression get substituted in instantiate_virtual_regs(). */
2698 for (adjust
= &root
->new_local_var_chain
;
2699 *adjust
!= root
->frame_decl
;
2700 adjust
= &DECL_CHAIN (*adjust
))
2701 gcc_assert (DECL_CHAIN (*adjust
));
2702 *adjust
= DECL_CHAIN (*adjust
);
2704 DECL_CHAIN (root
->frame_decl
) = NULL_TREE
;
2705 declare_vars (root
->frame_decl
,
2706 gimple_seq_first_stmt (gimple_body (context
)), true);
2709 /* If any parameters were referenced non-locally, then we need to
2710 insert a copy. Likewise, if any variables were referenced by
2711 pointer, we need to initialize the address. */
2712 if (root
->any_parm_remapped
)
2715 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
2719 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
2723 if (use_pointer_in_frame (p
))
2724 x
= build_addr (p
, context
);
2728 /* If the assignment is from a non-register the stmt is
2729 not valid gimple. Make it so by using a temporary instead. */
2730 if (!is_gimple_reg (x
)
2731 && is_gimple_reg_type (TREE_TYPE (x
)))
2733 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
2734 x
= init_tmp_var (root
, x
, &gsi
);
2737 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2738 root
->frame_decl
, field
, NULL_TREE
);
2739 stmt
= gimple_build_assign (y
, x
);
2740 gimple_seq_add_stmt (&stmt_list
, stmt
);
2744 /* If a chain_field was created, then it needs to be initialized
2746 if (root
->chain_field
)
2748 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
2749 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
2750 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
2751 gimple_seq_add_stmt (&stmt_list
, stmt
);
2754 /* If trampolines were created, then we need to initialize them. */
2755 if (root
->any_tramp_created
)
2757 struct nesting_info
*i
;
2758 for (i
= root
->inner
; i
; i
= i
->next
)
2760 tree arg1
, arg2
, arg3
, x
, field
;
2762 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
2766 gcc_assert (DECL_STATIC_CHAIN (i
->context
));
2767 arg3
= build_addr (root
->frame_decl
, context
);
2769 arg2
= build_addr (i
->context
, context
);
2771 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2772 root
->frame_decl
, field
, NULL_TREE
);
2773 arg1
= build_addr (x
, context
);
2775 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
2776 stmt
= gimple_build_call (x
, 3, arg1
, arg2
, arg3
);
2777 gimple_seq_add_stmt (&stmt_list
, stmt
);
2781 /* If we created initialization statements, insert them. */
2785 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
2786 bind
= gimple_seq_first_stmt_as_a_bind (gimple_body (context
));
2787 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
2788 gimple_bind_set_body (bind
, stmt_list
);
2791 /* If a chain_decl was created, then it needs to be registered with
2792 struct function so that it gets initialized from the static chain
2793 register at the beginning of the function. */
2794 sf
= DECL_STRUCT_FUNCTION (root
->context
);
2795 sf
->static_chain_decl
= root
->chain_decl
;
2797 /* Similarly for the non-local goto save area. */
2798 if (root
->nl_goto_field
)
2800 sf
->nonlocal_goto_save_area
2801 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
2802 sf
->has_nonlocal_label
= 1;
2805 /* Make sure all new local variables get inserted into the
2806 proper BIND_EXPR. */
2807 if (root
->new_local_var_chain
)
2808 declare_vars (root
->new_local_var_chain
,
2809 gimple_seq_first_stmt (gimple_body (root
->context
)),
2812 if (root
->debug_var_chain
)
2817 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
2819 for (debug_var
= root
->debug_var_chain
; debug_var
;
2820 debug_var
= DECL_CHAIN (debug_var
))
2821 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2824 /* If there are any debug decls with variable length types,
2825 remap those types using other debug_var_chain variables. */
2828 struct nesting_copy_body_data id
;
2830 memset (&id
, 0, sizeof (id
));
2831 id
.cb
.copy_decl
= nesting_copy_decl
;
2832 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
2835 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
2836 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2838 tree type
= TREE_TYPE (debug_var
);
2839 tree newt
, t
= type
;
2840 struct nesting_info
*i
;
2842 for (i
= root
; i
; i
= i
->outer
)
2843 if (variably_modified_type_p (type
, i
->context
))
2849 id
.cb
.src_fn
= i
->context
;
2850 id
.cb
.dst_fn
= i
->context
;
2851 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2853 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
2854 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2856 newt
= TREE_TYPE (newt
);
2859 if (TYPE_NAME (newt
)
2860 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2861 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2863 && TYPE_NAME (newt
) == TYPE_NAME (t
))
2864 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2867 delete id
.cb
.decl_map
;
2870 scope
= gimple_seq_first_stmt_as_a_bind (gimple_body (root
->context
));
2871 if (gimple_bind_block (scope
))
2872 declare_vars (root
->debug_var_chain
, scope
, true);
2874 BLOCK_VARS (DECL_INITIAL (root
->context
))
2875 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
2876 root
->debug_var_chain
);
2879 /* Fold the rewritten MEM_REF trees. */
2880 root
->mem_refs
->traverse
<void *, fold_mem_refs
> (NULL
);
2882 /* Dump the translated tree function. */
2885 fputs ("\n\n", dump_file
);
2886 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
2891 finalize_nesting_tree (struct nesting_info
*root
)
2893 struct nesting_info
*n
;
2894 FOR_EACH_NEST_INFO (n
, root
)
2895 finalize_nesting_tree_1 (n
);
2898 /* Unnest the nodes and pass them to cgraph. */
2901 unnest_nesting_tree_1 (struct nesting_info
*root
)
2903 struct cgraph_node
*node
= cgraph_node::get (root
->context
);
2905 /* For nested functions update the cgraph to reflect unnesting.
2906 We also delay finalizing of these functions up to this point. */
2910 cgraph_node::finalize_function (root
->context
, true);
2915 unnest_nesting_tree (struct nesting_info
*root
)
2917 struct nesting_info
*n
;
2918 FOR_EACH_NEST_INFO (n
, root
)
2919 unnest_nesting_tree_1 (n
);
2922 /* Free the data structures allocated during this pass. */
2925 free_nesting_tree (struct nesting_info
*root
)
2927 struct nesting_info
*node
, *next
;
2929 node
= iter_nestinfo_start (root
);
2932 next
= iter_nestinfo_next (node
);
2933 delete node
->var_map
;
2934 delete node
->field_map
;
2935 delete node
->mem_refs
;
2942 /* Gimplify a function and all its nested functions. */
2944 gimplify_all_functions (struct cgraph_node
*root
)
2946 struct cgraph_node
*iter
;
2947 if (!gimple_body (root
->decl
))
2948 gimplify_function_tree (root
->decl
);
2949 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
2950 gimplify_all_functions (iter
);
2953 /* Main entry point for this pass. Process FNDECL and all of its nested
2954 subroutines and turn them into something less tightly bound. */
2957 lower_nested_functions (tree fndecl
)
2959 struct cgraph_node
*cgn
;
2960 struct nesting_info
*root
;
2962 /* If there are no nested functions, there's nothing to do. */
2963 cgn
= cgraph_node::get (fndecl
);
2967 gimplify_all_functions (cgn
);
2969 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
2971 fprintf (dump_file
, "\n;; Function %s\n\n",
2972 lang_hooks
.decl_printable_name (fndecl
, 2));
2974 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
2975 root
= create_nesting_tree (cgn
);
2977 walk_all_functions (convert_nonlocal_reference_stmt
,
2978 convert_nonlocal_reference_op
,
2980 walk_all_functions (convert_local_reference_stmt
,
2981 convert_local_reference_op
,
2983 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
2984 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
2986 convert_all_function_calls (root
);
2987 finalize_nesting_tree (root
);
2988 unnest_nesting_tree (root
);
2990 free_nesting_tree (root
);
2991 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
2995 dump_end (TDI_nested
, dump_file
);
3000 #include "gt-tree-nested.h"