1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
34 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
35 #include "langhooks.h"
36 #include "pointer-set.h"
39 /* The object of this pass is to lower the representation of a set of nested
40 functions in order to expose all of the gory details of the various
41 nonlocal references. We want to do this sooner rather than later, in
42 order to give us more freedom in emitting all of the functions in question.
44 Back in olden times, when gcc was young, we developed an insanely
45 complicated scheme whereby variables which were referenced nonlocally
46 were forced to live in the stack of the declaring function, and then
47 the nested functions magically discovered where these variables were
48 placed. In order for this scheme to function properly, it required
49 that the outer function be partially expanded, then we switch to
50 compiling the inner function, and once done with those we switch back
51 to compiling the outer function. Such delicate ordering requirements
52 makes it difficult to do whole translation unit optimizations
53 involving such functions.
55 The implementation here is much more direct. Everything that can be
56 referenced by an inner function is a member of an explicitly created
57 structure herein called the "nonlocal frame struct". The incoming
58 static chain for a nested function is a pointer to this struct in
59 the parent. In this way, we settle on known offsets from a known
60 base, and so are decoupled from the logic that places objects in the
61 function's stack frame. More importantly, we don't have to wait for
62 that to happen -- since the compilation of the inner function is no
63 longer tied to a real stack frame, the nonlocal frame struct can be
64 allocated anywhere. Which means that the outer function is now
67 Theory of operation here is very simple. Iterate over all the
68 statements in all the functions (depth first) several times,
69 allocating structures and fields on demand. In general we want to
70 examine inner functions first, so that we can avoid making changes
71 to outer functions which are unnecessary.
73 The order of the passes matters a bit, in that later passes will be
74 skipped if it is discovered that the functions don't actually interact
75 at all. That is, they're nested in the lexical sense but could have
76 been written as independent functions without change. */
81 struct nesting_info
*outer
;
82 struct nesting_info
*inner
;
83 struct nesting_info
*next
;
85 struct pointer_map_t
*field_map
;
86 struct pointer_map_t
*var_map
;
87 struct pointer_set_t
*mem_refs
;
88 bitmap suppress_expansion
;
91 tree new_local_var_chain
;
99 bool any_parm_remapped
;
100 bool any_tramp_created
;
101 char static_chain_added
;
105 /* Iterate over the nesting tree, starting with ROOT, depth first. */
107 static inline struct nesting_info
*
108 iter_nestinfo_start (struct nesting_info
*root
)
115 static inline struct nesting_info
*
116 iter_nestinfo_next (struct nesting_info
*node
)
119 return iter_nestinfo_start (node
->next
);
123 #define FOR_EACH_NEST_INFO(I, ROOT) \
124 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
126 /* Obstack used for the bitmaps in the struct above. */
127 static struct bitmap_obstack nesting_info_bitmap_obstack
;
130 /* We're working in so many different function contexts simultaneously,
131 that create_tmp_var is dangerous. Prevent mishap. */
132 #define create_tmp_var cant_use_create_tmp_var_here_dummy
134 /* Like create_tmp_var, except record the variable for registration at
135 the given nesting level. */
138 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
142 /* If the type is of variable size or a type which must be created by the
143 frontend, something is wrong. Note that we explicitly allow
144 incomplete types here, since we create them ourselves here. */
145 gcc_assert (!TREE_ADDRESSABLE (type
));
146 gcc_assert (!TYPE_SIZE_UNIT (type
)
147 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
149 tmp_var
= create_tmp_var_raw (type
, prefix
);
150 DECL_CONTEXT (tmp_var
) = info
->context
;
151 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
152 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
153 if (TREE_CODE (type
) == COMPLEX_TYPE
154 || TREE_CODE (type
) == VECTOR_TYPE
)
155 DECL_GIMPLE_REG_P (tmp_var
) = 1;
157 info
->new_local_var_chain
= tmp_var
;
162 /* Take the address of EXP to be used within function CONTEXT.
163 Mark it for addressability as necessary. */
166 build_addr (tree exp
, tree context
)
172 while (handled_component_p (base
))
173 base
= TREE_OPERAND (base
, 0);
176 TREE_ADDRESSABLE (base
) = 1;
178 /* Building the ADDR_EXPR will compute a set of properties for
179 that ADDR_EXPR. Those properties are unfortunately context
180 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
182 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
183 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
184 way the properties are for the ADDR_EXPR are computed properly. */
185 save_context
= current_function_decl
;
186 current_function_decl
= context
;
187 retval
= build_fold_addr_expr (exp
);
188 current_function_decl
= save_context
;
192 /* Insert FIELD into TYPE, sorted by alignment requirements. */
195 insert_field_into_struct (tree type
, tree field
)
199 DECL_CONTEXT (field
) = type
;
201 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
202 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
205 DECL_CHAIN (field
) = *p
;
208 /* Set correct alignment for frame struct type. */
209 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
210 TYPE_ALIGN (type
) = DECL_ALIGN (field
);
213 /* Build or return the RECORD_TYPE that describes the frame state that is
214 shared between INFO->CONTEXT and its nested functions. This record will
215 not be complete until finalize_nesting_tree; up until that point we'll
216 be adding fields as necessary.
218 We also build the DECL that represents this frame in the function. */
221 get_frame_type (struct nesting_info
*info
)
223 tree type
= info
->frame_type
;
228 type
= make_node (RECORD_TYPE
);
230 name
= concat ("FRAME.",
231 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
233 TYPE_NAME (type
) = get_identifier (name
);
236 info
->frame_type
= type
;
237 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
239 /* ??? Always make it addressable for now, since it is meant to
240 be pointed to by the static chain pointer. This pessimizes
241 when it turns out that no static chains are needed because
242 the nested functions referencing non-local variables are not
243 reachable, but the true pessimization is to create the non-
244 local frame structure in the first place. */
245 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
250 /* Return true if DECL should be referenced by pointer in the non-local
254 use_pointer_in_frame (tree decl
)
256 if (TREE_CODE (decl
) == PARM_DECL
)
258 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
259 sized decls, and inefficient to copy large aggregates. Don't bother
260 moving anything but scalar variables. */
261 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
265 /* Variable sized types make things "interesting" in the frame. */
266 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
270 /* Given DECL, a non-locally accessed variable, find or create a field
271 in the non-local frame structure for the given nesting context. */
274 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
275 enum insert_option insert
)
279 if (insert
== NO_INSERT
)
281 slot
= pointer_map_contains (info
->field_map
, decl
);
282 return slot
? (tree
) *slot
: NULL_TREE
;
285 slot
= pointer_map_insert (info
->field_map
, decl
);
288 tree field
= make_node (FIELD_DECL
);
289 DECL_NAME (field
) = DECL_NAME (decl
);
291 if (use_pointer_in_frame (decl
))
293 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
294 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
295 DECL_NONADDRESSABLE_P (field
) = 1;
299 TREE_TYPE (field
) = TREE_TYPE (decl
);
300 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
301 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
302 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
303 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
304 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
305 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
308 insert_field_into_struct (get_frame_type (info
), field
);
311 if (TREE_CODE (decl
) == PARM_DECL
)
312 info
->any_parm_remapped
= true;
318 /* Build or return the variable that holds the static chain within
319 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
322 get_chain_decl (struct nesting_info
*info
)
324 tree decl
= info
->chain_decl
;
330 type
= get_frame_type (info
->outer
);
331 type
= build_pointer_type (type
);
333 /* Note that this variable is *not* entered into any BIND_EXPR;
334 the construction of this variable is handled specially in
335 expand_function_start and initialize_inlined_parameters.
336 Note also that it's represented as a parameter. This is more
337 close to the truth, since the initial value does come from
339 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
340 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
341 DECL_ARTIFICIAL (decl
) = 1;
342 DECL_IGNORED_P (decl
) = 1;
343 TREE_USED (decl
) = 1;
344 DECL_CONTEXT (decl
) = info
->context
;
345 DECL_ARG_TYPE (decl
) = type
;
347 /* Tell tree-inline.c that we never write to this variable, so
348 it can copy-prop the replacement value immediately. */
349 TREE_READONLY (decl
) = 1;
351 info
->chain_decl
= decl
;
354 && (dump_flags
& TDF_DETAILS
)
355 && !DECL_STATIC_CHAIN (info
->context
))
356 fprintf (dump_file
, "Setting static-chain for %s\n",
357 lang_hooks
.decl_printable_name (info
->context
, 2));
359 DECL_STATIC_CHAIN (info
->context
) = 1;
364 /* Build or return the field within the non-local frame state that holds
365 the static chain for INFO->CONTEXT. This is the way to walk back up
366 multiple nesting levels. */
369 get_chain_field (struct nesting_info
*info
)
371 tree field
= info
->chain_field
;
375 tree type
= build_pointer_type (get_frame_type (info
->outer
));
377 field
= make_node (FIELD_DECL
);
378 DECL_NAME (field
) = get_identifier ("__chain");
379 TREE_TYPE (field
) = type
;
380 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
381 DECL_NONADDRESSABLE_P (field
) = 1;
383 insert_field_into_struct (get_frame_type (info
), field
);
385 info
->chain_field
= field
;
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 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
401 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
406 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
407 gimple_call_set_lhs (call
, t
);
408 if (! gsi_end_p (*gsi
))
409 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
410 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
416 /* Copy EXP into a temporary. Allocate the temporary in the context of
417 INFO and insert the initialization statement before GSI. */
420 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
425 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
426 stmt
= gimple_build_assign (t
, exp
);
427 if (! gsi_end_p (*gsi
))
428 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
429 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
435 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
438 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
439 gimple_stmt_iterator
*gsi
)
441 if (is_gimple_val (exp
))
444 return init_tmp_var (info
, exp
, gsi
);
447 /* Similarly, but copy from the temporary and insert the statement
448 after the iterator. */
451 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
456 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
457 stmt
= gimple_build_assign (exp
, t
);
458 if (! gsi_end_p (*gsi
))
459 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
460 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
465 /* Build or return the type used to represent a nested function trampoline. */
467 static GTY(()) tree trampoline_type
;
470 get_trampoline_type (struct nesting_info
*info
)
472 unsigned align
, size
;
476 return trampoline_type
;
478 align
= TRAMPOLINE_ALIGNMENT
;
479 size
= TRAMPOLINE_SIZE
;
481 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
482 then allocate extra space so that we can do dynamic alignment. */
483 if (align
> STACK_BOUNDARY
)
485 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
486 align
= STACK_BOUNDARY
;
489 t
= build_index_type (size_int (size
- 1));
490 t
= build_array_type (char_type_node
, t
);
491 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
492 FIELD_DECL
, get_identifier ("__data"), t
);
493 DECL_ALIGN (t
) = align
;
494 DECL_USER_ALIGN (t
) = 1;
496 trampoline_type
= make_node (RECORD_TYPE
);
497 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
498 TYPE_FIELDS (trampoline_type
) = t
;
499 layout_type (trampoline_type
);
500 DECL_CONTEXT (t
) = trampoline_type
;
502 return trampoline_type
;
505 /* Given DECL, a nested function, find or create a field in the non-local
506 frame structure for a trampoline for this function. */
509 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
510 enum insert_option insert
)
514 if (insert
== NO_INSERT
)
516 slot
= pointer_map_contains (info
->var_map
, decl
);
517 return slot
? (tree
) *slot
: NULL_TREE
;
520 slot
= pointer_map_insert (info
->var_map
, decl
);
523 tree field
= make_node (FIELD_DECL
);
524 DECL_NAME (field
) = DECL_NAME (decl
);
525 TREE_TYPE (field
) = get_trampoline_type (info
);
526 TREE_ADDRESSABLE (field
) = 1;
528 insert_field_into_struct (get_frame_type (info
), field
);
531 info
->any_tramp_created
= true;
537 /* Build or return the field within the non-local frame state that holds
538 the non-local goto "jmp_buf". The buffer itself is maintained by the
539 rtl middle-end as dynamic stack space is allocated. */
542 get_nl_goto_field (struct nesting_info
*info
)
544 tree field
= info
->nl_goto_field
;
550 /* For __builtin_nonlocal_goto, we need N words. The first is the
551 frame pointer, the rest is for the target's stack pointer save
552 area. The number of words is controlled by STACK_SAVEAREA_MODE;
553 not the best interface, but it'll do for now. */
554 if (Pmode
== ptr_mode
)
555 type
= ptr_type_node
;
557 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
559 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
560 size
= size
/ GET_MODE_SIZE (Pmode
);
563 type
= build_array_type
564 (type
, build_index_type (size_int (size
)));
566 field
= make_node (FIELD_DECL
);
567 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
568 TREE_TYPE (field
) = type
;
569 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
570 TREE_ADDRESSABLE (field
) = 1;
572 insert_field_into_struct (get_frame_type (info
), field
);
574 info
->nl_goto_field
= field
;
580 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
583 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
584 struct nesting_info
*info
, gimple_seq
*pseq
)
586 struct walk_stmt_info wi
;
588 memset (&wi
, 0, sizeof (wi
));
591 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
595 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
598 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
599 struct nesting_info
*info
)
601 gimple_seq body
= gimple_body (info
->context
);
602 walk_body (callback_stmt
, callback_op
, info
, &body
);
603 gimple_set_body (info
->context
, body
);
606 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
609 walk_gimple_omp_for (gimple for_stmt
,
610 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
611 struct nesting_info
*info
)
613 struct walk_stmt_info wi
;
618 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
621 memset (&wi
, 0, sizeof (wi
));
623 wi
.gsi
= gsi_last (seq
);
625 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
628 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
632 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
637 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
640 t
= gimple_omp_for_incr (for_stmt
, i
);
641 gcc_assert (BINARY_CLASS_P (t
));
643 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
646 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
649 seq
= gsi_seq (wi
.gsi
);
650 if (!gimple_seq_empty_p (seq
))
652 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
653 annotate_all_with_location (seq
, gimple_location (for_stmt
));
654 gimple_seq_add_seq (&pre_body
, seq
);
655 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
659 /* Similarly for ROOT and all functions nested underneath, depth first. */
662 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
663 struct nesting_info
*root
)
665 struct nesting_info
*n
;
666 FOR_EACH_NEST_INFO (n
, root
)
667 walk_function (callback_stmt
, callback_op
, n
);
671 /* We have to check for a fairly pathological case. The operands of function
672 nested function are to be interpreted in the context of the enclosing
673 function. So if any are variably-sized, they will get remapped when the
674 enclosing function is inlined. But that remapping would also have to be
675 done in the types of the PARM_DECLs of the nested function, meaning the
676 argument types of that function will disagree with the arguments in the
677 calls to that function. So we'd either have to make a copy of the nested
678 function corresponding to each time the enclosing function was inlined or
679 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
680 function. The former is not practical. The latter would still require
681 detecting this case to know when to add the conversions. So, for now at
682 least, we don't inline such an enclosing function.
684 We have to do that check recursively, so here return indicating whether
685 FNDECL has such a nested function. ORIG_FN is the function we were
686 trying to inline to use for checking whether any argument is variably
687 modified by anything in it.
689 It would be better to do this in tree-inline.c so that we could give
690 the appropriate warning for why a function can't be inlined, but that's
691 too late since the nesting structure has already been flattened and
692 adding a flag just to record this fact seems a waste of a flag. */
695 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
697 struct cgraph_node
*cgn
= cgraph_get_node (fndecl
);
700 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
702 for (arg
= DECL_ARGUMENTS (cgn
->symbol
.decl
); arg
; arg
= DECL_CHAIN (arg
))
703 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
706 if (check_for_nested_with_variably_modified (cgn
->symbol
.decl
,
714 /* Construct our local datastructure describing the function nesting
715 tree rooted by CGN. */
717 static struct nesting_info
*
718 create_nesting_tree (struct cgraph_node
*cgn
)
720 struct nesting_info
*info
= XCNEW (struct nesting_info
);
721 info
->field_map
= pointer_map_create ();
722 info
->var_map
= pointer_map_create ();
723 info
->mem_refs
= pointer_set_create ();
724 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
725 info
->context
= cgn
->symbol
.decl
;
727 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
729 struct nesting_info
*sub
= create_nesting_tree (cgn
);
731 sub
->next
= info
->inner
;
735 /* See discussion at check_for_nested_with_variably_modified for a
736 discussion of why this has to be here. */
737 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
738 DECL_UNINLINABLE (info
->context
) = true;
743 /* Return an expression computing the static chain for TARGET_CONTEXT
744 from INFO->CONTEXT. Insert any necessary computations before TSI. */
747 get_static_chain (struct nesting_info
*info
, tree target_context
,
748 gimple_stmt_iterator
*gsi
)
750 struct nesting_info
*i
;
753 if (info
->context
== target_context
)
755 x
= build_addr (info
->frame_decl
, target_context
);
759 x
= get_chain_decl (info
);
761 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
763 tree field
= get_chain_field (i
);
765 x
= build_simple_mem_ref (x
);
766 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
767 x
= init_tmp_var (info
, x
, gsi
);
775 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
776 frame as seen from INFO->CONTEXT. Insert any necessary computations
780 get_frame_field (struct nesting_info
*info
, tree target_context
,
781 tree field
, gimple_stmt_iterator
*gsi
)
783 struct nesting_info
*i
;
786 if (info
->context
== target_context
)
788 /* Make sure frame_decl gets created. */
789 (void) get_frame_type (info
);
790 x
= info
->frame_decl
;
794 x
= get_chain_decl (info
);
796 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
798 tree field
= get_chain_field (i
);
800 x
= build_simple_mem_ref (x
);
801 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
802 x
= init_tmp_var (info
, x
, gsi
);
805 x
= build_simple_mem_ref (x
);
808 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
812 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
814 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
815 in the nested function with DECL_VALUE_EXPR set to reference the true
816 variable in the parent function. This is used both for debug info
817 and in OpenMP lowering. */
820 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
823 struct nesting_info
*i
;
824 tree x
, field
, new_decl
;
827 slot
= pointer_map_insert (info
->var_map
, decl
);
832 target_context
= decl_function_context (decl
);
834 /* A copy of the code in get_frame_field, but without the temporaries. */
835 if (info
->context
== target_context
)
837 /* Make sure frame_decl gets created. */
838 (void) get_frame_type (info
);
839 x
= info
->frame_decl
;
844 x
= get_chain_decl (info
);
845 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
847 field
= get_chain_field (i
);
848 x
= build_simple_mem_ref (x
);
849 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
851 x
= build_simple_mem_ref (x
);
854 field
= lookup_field_for_decl (i
, decl
, INSERT
);
855 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
856 if (use_pointer_in_frame (decl
))
857 x
= build_simple_mem_ref (x
);
859 /* ??? We should be remapping types as well, surely. */
860 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
861 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
862 DECL_CONTEXT (new_decl
) = info
->context
;
863 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
864 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
865 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
866 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
867 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
868 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
869 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
870 if ((TREE_CODE (decl
) == PARM_DECL
871 || TREE_CODE (decl
) == RESULT_DECL
872 || TREE_CODE (decl
) == VAR_DECL
)
873 && DECL_BY_REFERENCE (decl
))
874 DECL_BY_REFERENCE (new_decl
) = 1;
876 SET_DECL_VALUE_EXPR (new_decl
, x
);
877 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
880 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
881 info
->debug_var_chain
= new_decl
;
884 && info
->context
!= target_context
885 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
886 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
892 /* Callback for walk_gimple_stmt, rewrite all references to VAR
893 and PARM_DECLs that belong to outer functions.
895 The rewrite will involve some number of structure accesses back up
896 the static chain. E.g. for a variable FOO up one nesting level it'll
897 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
898 indirections apply to decls for which use_pointer_in_frame is true. */
901 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
903 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
904 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
908 switch (TREE_CODE (t
))
911 /* Non-automatic variables are never processed. */
912 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
917 if (decl_function_context (t
) != info
->context
)
922 x
= get_nonlocal_debug_decl (info
, t
);
923 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
925 tree target_context
= decl_function_context (t
);
926 struct nesting_info
*i
;
927 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
929 x
= lookup_field_for_decl (i
, t
, INSERT
);
930 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
931 if (use_pointer_in_frame (t
))
933 x
= init_tmp_var (info
, x
, &wi
->gsi
);
934 x
= build_simple_mem_ref (x
);
941 x
= save_tmp_var (info
, x
, &wi
->gsi
);
943 x
= init_tmp_var (info
, x
, &wi
->gsi
);
951 /* We're taking the address of a label from a parent function, but
952 this is not itself a non-local goto. Mark the label such that it
953 will not be deleted, much as we would with a label address in
955 if (decl_function_context (t
) != info
->context
)
956 FORCED_LABEL (t
) = 1;
961 bool save_val_only
= wi
->val_only
;
963 wi
->val_only
= false;
966 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
973 /* If we changed anything, we might no longer be directly
974 referencing a decl. */
975 save_context
= current_function_decl
;
976 current_function_decl
= info
->context
;
977 recompute_tree_invariant_for_addr_expr (t
);
978 current_function_decl
= save_context
;
980 /* If the callback converted the address argument in a context
981 where we only accept variables (and min_invariant, presumably),
982 then compute the address into a temporary. */
984 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
994 case ARRAY_RANGE_REF
:
996 /* Go down this entire nest and just look at the final prefix and
997 anything that describes the references. Otherwise, we lose track
998 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1001 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1003 if (TREE_CODE (t
) == COMPONENT_REF
)
1004 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1006 else if (TREE_CODE (t
) == ARRAY_REF
1007 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1009 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1011 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1013 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1017 wi
->val_only
= false;
1018 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1021 case VIEW_CONVERT_EXPR
:
1022 /* Just request to look at the subtrees, leaving val_only and lhs
1023 untouched. This might actually be for !val_only + lhs, in which
1024 case we don't want to force a replacement by a temporary. */
1029 if (!IS_TYPE_OR_DECL_P (t
))
1032 wi
->val_only
= true;
1041 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1042 struct walk_stmt_info
*);
1044 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1045 and PARM_DECLs that belong to outer functions. */
1048 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1050 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1051 bool need_chain
= false, need_stmts
= false;
1054 bitmap new_suppress
;
1056 new_suppress
= BITMAP_GGC_ALLOC ();
1057 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1059 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1061 switch (OMP_CLAUSE_CODE (clause
))
1063 case OMP_CLAUSE_REDUCTION
:
1064 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1066 goto do_decl_clause
;
1068 case OMP_CLAUSE_LASTPRIVATE
:
1069 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1071 goto do_decl_clause
;
1073 case OMP_CLAUSE_PRIVATE
:
1074 case OMP_CLAUSE_FIRSTPRIVATE
:
1075 case OMP_CLAUSE_COPYPRIVATE
:
1076 case OMP_CLAUSE_SHARED
:
1078 decl
= OMP_CLAUSE_DECL (clause
);
1079 if (TREE_CODE (decl
) == VAR_DECL
1080 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1082 if (decl_function_context (decl
) != info
->context
)
1084 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1085 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1086 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1091 case OMP_CLAUSE_SCHEDULE
:
1092 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1095 case OMP_CLAUSE_FINAL
:
1097 case OMP_CLAUSE_NUM_THREADS
:
1098 wi
->val_only
= true;
1100 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1104 case OMP_CLAUSE_NOWAIT
:
1105 case OMP_CLAUSE_ORDERED
:
1106 case OMP_CLAUSE_DEFAULT
:
1107 case OMP_CLAUSE_COPYIN
:
1108 case OMP_CLAUSE_COLLAPSE
:
1109 case OMP_CLAUSE_UNTIED
:
1110 case OMP_CLAUSE_MERGEABLE
:
1118 info
->suppress_expansion
= new_suppress
;
1121 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1122 switch (OMP_CLAUSE_CODE (clause
))
1124 case OMP_CLAUSE_REDUCTION
:
1125 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1128 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1129 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1131 walk_body (convert_nonlocal_reference_stmt
,
1132 convert_nonlocal_reference_op
, info
,
1133 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1134 walk_body (convert_nonlocal_reference_stmt
,
1135 convert_nonlocal_reference_op
, info
,
1136 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1137 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1142 case OMP_CLAUSE_LASTPRIVATE
:
1143 walk_body (convert_nonlocal_reference_stmt
,
1144 convert_nonlocal_reference_op
, info
,
1145 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1155 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1158 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1160 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1161 type
= TREE_TYPE (type
);
1163 if (TYPE_NAME (type
)
1164 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1165 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1166 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1168 while (POINTER_TYPE_P (type
)
1169 || TREE_CODE (type
) == VECTOR_TYPE
1170 || TREE_CODE (type
) == FUNCTION_TYPE
1171 || TREE_CODE (type
) == METHOD_TYPE
)
1172 type
= TREE_TYPE (type
);
1174 if (TREE_CODE (type
) == ARRAY_TYPE
)
1178 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1179 domain
= TYPE_DOMAIN (type
);
1182 t
= TYPE_MIN_VALUE (domain
);
1183 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1184 && decl_function_context (t
) != info
->context
)
1185 get_nonlocal_debug_decl (info
, t
);
1186 t
= TYPE_MAX_VALUE (domain
);
1187 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1188 && decl_function_context (t
) != info
->context
)
1189 get_nonlocal_debug_decl (info
, t
);
1194 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1198 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1202 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
1203 if (TREE_CODE (var
) == VAR_DECL
1204 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1205 && DECL_HAS_VALUE_EXPR_P (var
)
1206 && decl_function_context (var
) != info
->context
)
1207 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1210 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1211 PARM_DECLs that belong to outer functions. This handles statements
1212 that are not handled via the standard recursion done in
1213 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1214 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1215 operands of STMT have been handled by this function. */
1218 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1219 struct walk_stmt_info
*wi
)
1221 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1222 tree save_local_var_chain
;
1223 bitmap save_suppress
;
1224 gimple stmt
= gsi_stmt (*gsi
);
1226 switch (gimple_code (stmt
))
1229 /* Don't walk non-local gotos for now. */
1230 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1232 wi
->val_only
= true;
1234 *handled_ops_p
= true;
1239 case GIMPLE_OMP_PARALLEL
:
1240 case GIMPLE_OMP_TASK
:
1241 save_suppress
= info
->suppress_expansion
;
1242 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1246 decl
= get_chain_decl (info
);
1247 c
= build_omp_clause (gimple_location (stmt
),
1248 OMP_CLAUSE_FIRSTPRIVATE
);
1249 OMP_CLAUSE_DECL (c
) = decl
;
1250 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1251 gimple_omp_taskreg_set_clauses (stmt
, c
);
1254 save_local_var_chain
= info
->new_local_var_chain
;
1255 info
->new_local_var_chain
= NULL
;
1257 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1258 info
, gimple_omp_body_ptr (stmt
));
1260 if (info
->new_local_var_chain
)
1261 declare_vars (info
->new_local_var_chain
,
1262 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1264 info
->new_local_var_chain
= save_local_var_chain
;
1265 info
->suppress_expansion
= save_suppress
;
1268 case GIMPLE_OMP_FOR
:
1269 save_suppress
= info
->suppress_expansion
;
1270 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1271 walk_gimple_omp_for (stmt
, convert_nonlocal_reference_stmt
,
1272 convert_nonlocal_reference_op
, info
);
1273 walk_body (convert_nonlocal_reference_stmt
,
1274 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1275 info
->suppress_expansion
= save_suppress
;
1278 case GIMPLE_OMP_SECTIONS
:
1279 save_suppress
= info
->suppress_expansion
;
1280 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1281 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1282 info
, gimple_omp_body_ptr (stmt
));
1283 info
->suppress_expansion
= save_suppress
;
1286 case GIMPLE_OMP_SINGLE
:
1287 save_suppress
= info
->suppress_expansion
;
1288 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1289 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1290 info
, gimple_omp_body_ptr (stmt
));
1291 info
->suppress_expansion
= save_suppress
;
1294 case GIMPLE_OMP_SECTION
:
1295 case GIMPLE_OMP_MASTER
:
1296 case GIMPLE_OMP_ORDERED
:
1297 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1298 info
, gimple_omp_body_ptr (stmt
));
1302 if (!optimize
&& gimple_bind_block (stmt
))
1303 note_nonlocal_block_vlas (info
, gimple_bind_block (stmt
));
1305 *handled_ops_p
= false;
1309 wi
->val_only
= true;
1311 *handled_ops_p
= false;
1315 /* For every other statement that we are not interested in
1316 handling here, let the walker traverse the operands. */
1317 *handled_ops_p
= false;
1321 /* We have handled all of STMT operands, no need to traverse the operands. */
1322 *handled_ops_p
= true;
1327 /* A subroutine of convert_local_reference. Create a local variable
1328 in the parent function with DECL_VALUE_EXPR set to reference the
1329 field in FRAME. This is used both for debug info and in OpenMP
1333 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1338 slot
= pointer_map_insert (info
->var_map
, decl
);
1340 return (tree
) *slot
;
1342 /* Make sure frame_decl gets created. */
1343 (void) get_frame_type (info
);
1344 x
= info
->frame_decl
;
1345 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1347 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1348 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1349 DECL_CONTEXT (new_decl
) = info
->context
;
1350 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1351 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1352 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1353 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1354 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1355 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1356 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1357 if ((TREE_CODE (decl
) == PARM_DECL
1358 || TREE_CODE (decl
) == RESULT_DECL
1359 || TREE_CODE (decl
) == VAR_DECL
)
1360 && DECL_BY_REFERENCE (decl
))
1361 DECL_BY_REFERENCE (new_decl
) = 1;
1363 SET_DECL_VALUE_EXPR (new_decl
, x
);
1364 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1367 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1368 info
->debug_var_chain
= new_decl
;
1370 /* Do not emit debug info twice. */
1371 DECL_IGNORED_P (decl
) = 1;
1377 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1378 and PARM_DECLs that were referenced by inner nested functions.
1379 The rewrite will be a structure reference to the local frame variable. */
1381 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1384 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1386 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1387 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1388 tree t
= *tp
, field
, x
;
1392 switch (TREE_CODE (t
))
1395 /* Non-automatic variables are never processed. */
1396 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1401 if (decl_function_context (t
) == info
->context
)
1403 /* If we copied a pointer to the frame, then the original decl
1404 is used unchanged in the parent function. */
1405 if (use_pointer_in_frame (t
))
1408 /* No need to transform anything if no child references the
1410 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1415 x
= get_local_debug_decl (info
, t
, field
);
1416 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1417 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1422 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1424 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1432 save_val_only
= wi
->val_only
;
1433 wi
->val_only
= false;
1435 wi
->changed
= false;
1436 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1437 wi
->val_only
= save_val_only
;
1439 /* If we converted anything ... */
1444 /* Then the frame decl is now addressable. */
1445 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1447 save_context
= current_function_decl
;
1448 current_function_decl
= info
->context
;
1449 recompute_tree_invariant_for_addr_expr (t
);
1450 current_function_decl
= save_context
;
1452 /* If we are in a context where we only accept values, then
1453 compute the address into a temporary. */
1455 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1464 case ARRAY_RANGE_REF
:
1466 /* Go down this entire nest and just look at the final prefix and
1467 anything that describes the references. Otherwise, we lose track
1468 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1469 save_val_only
= wi
->val_only
;
1470 wi
->val_only
= true;
1472 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1474 if (TREE_CODE (t
) == COMPONENT_REF
)
1475 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1477 else if (TREE_CODE (t
) == ARRAY_REF
1478 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1480 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1482 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1484 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1488 wi
->val_only
= false;
1489 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1490 wi
->val_only
= save_val_only
;
1494 save_val_only
= wi
->val_only
;
1495 wi
->val_only
= true;
1497 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1499 /* We need to re-fold the MEM_REF as component references as
1500 part of a ADDR_EXPR address are not allowed. But we cannot
1501 fold here, as the chain record type is not yet finalized. */
1502 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1503 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1504 pointer_set_insert (info
->mem_refs
, tp
);
1505 wi
->val_only
= save_val_only
;
1508 case VIEW_CONVERT_EXPR
:
1509 /* Just request to look at the subtrees, leaving val_only and lhs
1510 untouched. This might actually be for !val_only + lhs, in which
1511 case we don't want to force a replacement by a temporary. */
1516 if (!IS_TYPE_OR_DECL_P (t
))
1519 wi
->val_only
= true;
1528 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1529 struct walk_stmt_info
*);
1531 /* Helper for convert_local_reference. Convert all the references in
1532 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1535 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1537 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1538 bool need_frame
= false, need_stmts
= false;
1541 bitmap new_suppress
;
1543 new_suppress
= BITMAP_GGC_ALLOC ();
1544 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1546 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1548 switch (OMP_CLAUSE_CODE (clause
))
1550 case OMP_CLAUSE_REDUCTION
:
1551 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1553 goto do_decl_clause
;
1555 case OMP_CLAUSE_LASTPRIVATE
:
1556 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1558 goto do_decl_clause
;
1560 case OMP_CLAUSE_PRIVATE
:
1561 case OMP_CLAUSE_FIRSTPRIVATE
:
1562 case OMP_CLAUSE_COPYPRIVATE
:
1563 case OMP_CLAUSE_SHARED
:
1565 decl
= OMP_CLAUSE_DECL (clause
);
1566 if (TREE_CODE (decl
) == VAR_DECL
1567 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1569 if (decl_function_context (decl
) == info
->context
1570 && !use_pointer_in_frame (decl
))
1572 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1575 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1576 OMP_CLAUSE_DECL (clause
)
1577 = get_local_debug_decl (info
, decl
, field
);
1583 case OMP_CLAUSE_SCHEDULE
:
1584 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1587 case OMP_CLAUSE_FINAL
:
1589 case OMP_CLAUSE_NUM_THREADS
:
1590 wi
->val_only
= true;
1592 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1596 case OMP_CLAUSE_NOWAIT
:
1597 case OMP_CLAUSE_ORDERED
:
1598 case OMP_CLAUSE_DEFAULT
:
1599 case OMP_CLAUSE_COPYIN
:
1600 case OMP_CLAUSE_COLLAPSE
:
1601 case OMP_CLAUSE_UNTIED
:
1602 case OMP_CLAUSE_MERGEABLE
:
1610 info
->suppress_expansion
= new_suppress
;
1613 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1614 switch (OMP_CLAUSE_CODE (clause
))
1616 case OMP_CLAUSE_REDUCTION
:
1617 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1620 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1621 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1623 walk_body (convert_local_reference_stmt
,
1624 convert_local_reference_op
, info
,
1625 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1626 walk_body (convert_local_reference_stmt
,
1627 convert_local_reference_op
, info
,
1628 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1629 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1634 case OMP_CLAUSE_LASTPRIVATE
:
1635 walk_body (convert_local_reference_stmt
,
1636 convert_local_reference_op
, info
,
1637 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1648 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1649 and PARM_DECLs that were referenced by inner nested functions.
1650 The rewrite will be a structure reference to the local frame variable. */
1653 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1654 struct walk_stmt_info
*wi
)
1656 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1657 tree save_local_var_chain
;
1658 bitmap save_suppress
;
1659 gimple stmt
= gsi_stmt (*gsi
);
1661 switch (gimple_code (stmt
))
1663 case GIMPLE_OMP_PARALLEL
:
1664 case GIMPLE_OMP_TASK
:
1665 save_suppress
= info
->suppress_expansion
;
1666 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1670 (void) get_frame_type (info
);
1671 c
= build_omp_clause (gimple_location (stmt
),
1673 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1674 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1675 gimple_omp_taskreg_set_clauses (stmt
, c
);
1678 save_local_var_chain
= info
->new_local_var_chain
;
1679 info
->new_local_var_chain
= NULL
;
1681 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1682 gimple_omp_body_ptr (stmt
));
1684 if (info
->new_local_var_chain
)
1685 declare_vars (info
->new_local_var_chain
,
1686 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1687 info
->new_local_var_chain
= save_local_var_chain
;
1688 info
->suppress_expansion
= save_suppress
;
1691 case GIMPLE_OMP_FOR
:
1692 save_suppress
= info
->suppress_expansion
;
1693 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1694 walk_gimple_omp_for (stmt
, convert_local_reference_stmt
,
1695 convert_local_reference_op
, info
);
1696 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1697 info
, gimple_omp_body_ptr (stmt
));
1698 info
->suppress_expansion
= save_suppress
;
1701 case GIMPLE_OMP_SECTIONS
:
1702 save_suppress
= info
->suppress_expansion
;
1703 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1704 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1705 info
, gimple_omp_body_ptr (stmt
));
1706 info
->suppress_expansion
= save_suppress
;
1709 case GIMPLE_OMP_SINGLE
:
1710 save_suppress
= info
->suppress_expansion
;
1711 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1712 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1713 info
, gimple_omp_body_ptr (stmt
));
1714 info
->suppress_expansion
= save_suppress
;
1717 case GIMPLE_OMP_SECTION
:
1718 case GIMPLE_OMP_MASTER
:
1719 case GIMPLE_OMP_ORDERED
:
1720 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1721 info
, gimple_omp_body_ptr (stmt
));
1725 wi
->val_only
= true;
1727 *handled_ops_p
= false;
1731 /* For every other statement that we are not interested in
1732 handling here, let the walker traverse the operands. */
1733 *handled_ops_p
= false;
1737 /* Indicate that we have handled all the operands ourselves. */
1738 *handled_ops_p
= true;
1743 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1744 that reference labels from outer functions. The rewrite will be a
1745 call to __builtin_nonlocal_goto. */
1748 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1749 struct walk_stmt_info
*wi
)
1751 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
1752 tree label
, new_label
, target_context
, x
, field
;
1755 gimple stmt
= gsi_stmt (*gsi
);
1757 if (gimple_code (stmt
) != GIMPLE_GOTO
)
1759 *handled_ops_p
= false;
1763 label
= gimple_goto_dest (stmt
);
1764 if (TREE_CODE (label
) != LABEL_DECL
)
1766 *handled_ops_p
= false;
1770 target_context
= decl_function_context (label
);
1771 if (target_context
== info
->context
)
1773 *handled_ops_p
= false;
1777 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1780 /* The original user label may also be use for a normal goto, therefore
1781 we must create a new label that will actually receive the abnormal
1782 control transfer. This new label will be marked LABEL_NONLOCAL; this
1783 mark will trigger proper behavior in the cfg, as well as cause the
1784 (hairy target-specific) non-local goto receiver code to be generated
1785 when we expand rtl. Enter this association into var_map so that we
1786 can insert the new label into the IL during a second pass. */
1787 slot
= pointer_map_insert (i
->var_map
, label
);
1790 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
1791 DECL_NONLOCAL (new_label
) = 1;
1795 new_label
= (tree
) *slot
;
1797 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1798 field
= get_nl_goto_field (i
);
1799 x
= get_frame_field (info
, target_context
, field
, gsi
);
1800 x
= build_addr (x
, target_context
);
1801 x
= gsi_gimplify_val (info
, x
, gsi
);
1802 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
1803 2, build_addr (new_label
, target_context
), x
);
1804 gsi_replace (gsi
, call
, false);
1806 /* We have handled all of STMT's operands, no need to keep going. */
1807 *handled_ops_p
= true;
1812 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1813 are referenced via nonlocal goto from a nested function. The rewrite
1814 will involve installing a newly generated DECL_NONLOCAL label, and
1815 (potentially) a branch around the rtl gunk that is assumed to be
1816 attached to such a label. */
1819 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1820 struct walk_stmt_info
*wi
)
1822 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1823 tree label
, new_label
;
1824 gimple_stmt_iterator tmp_gsi
;
1826 gimple stmt
= gsi_stmt (*gsi
);
1828 if (gimple_code (stmt
) != GIMPLE_LABEL
)
1830 *handled_ops_p
= false;
1834 label
= gimple_label_label (stmt
);
1836 slot
= pointer_map_contains (info
->var_map
, label
);
1839 *handled_ops_p
= false;
1843 /* If there's any possibility that the previous statement falls through,
1844 then we must branch around the new non-local label. */
1846 gsi_prev (&tmp_gsi
);
1847 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
1849 gimple stmt
= gimple_build_goto (label
);
1850 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1853 new_label
= (tree
) *slot
;
1854 stmt
= gimple_build_label (new_label
);
1855 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1857 *handled_ops_p
= true;
1862 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1863 of nested functions that require the use of trampolines. The rewrite
1864 will involve a reference a trampoline generated for the occasion. */
1867 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1869 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1870 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
1871 tree t
= *tp
, decl
, target_context
, x
, builtin
;
1875 switch (TREE_CODE (t
))
1879 T.1 = &CHAIN->tramp;
1880 T.2 = __builtin_adjust_trampoline (T.1);
1881 T.3 = (func_type)T.2;
1884 decl
= TREE_OPERAND (t
, 0);
1885 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1888 /* Only need to process nested functions. */
1889 target_context
= decl_function_context (decl
);
1890 if (!target_context
)
1893 /* If the nested function doesn't use a static chain, then
1894 it doesn't need a trampoline. */
1895 if (!DECL_STATIC_CHAIN (decl
))
1898 /* If we don't want a trampoline, then don't build one. */
1899 if (TREE_NO_TRAMPOLINE (t
))
1902 /* Lookup the immediate parent of the callee, as that's where
1903 we need to insert the trampoline. */
1904 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1906 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1908 /* Compute the address of the field holding the trampoline. */
1909 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1910 x
= build_addr (x
, target_context
);
1911 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
1913 /* Do machine-specific ugliness. Normally this will involve
1914 computing extra alignment, but it can really be anything. */
1915 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
1916 call
= gimple_build_call (builtin
, 1, x
);
1917 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
1919 /* Cast back to the proper function type. */
1920 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1921 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1927 if (!IS_TYPE_OR_DECL_P (t
))
1936 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1937 to addresses of nested functions that require the use of
1938 trampolines. The rewrite will involve a reference a trampoline
1939 generated for the occasion. */
1942 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1943 struct walk_stmt_info
*wi
)
1945 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1946 gimple stmt
= gsi_stmt (*gsi
);
1948 switch (gimple_code (stmt
))
1952 /* Only walk call arguments, lest we generate trampolines for
1954 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
1955 for (i
= 0; i
< nargs
; i
++)
1956 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
1961 case GIMPLE_OMP_PARALLEL
:
1962 case GIMPLE_OMP_TASK
:
1964 tree save_local_var_chain
;
1965 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
1966 save_local_var_chain
= info
->new_local_var_chain
;
1967 info
->new_local_var_chain
= NULL
;
1968 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
1969 info
, 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
)),
1974 info
->new_local_var_chain
= save_local_var_chain
;
1979 *handled_ops_p
= false;
1984 *handled_ops_p
= true;
1990 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
1991 that reference nested functions to make sure that the static chain
1992 is set up properly for the call. */
1995 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1996 struct walk_stmt_info
*wi
)
1998 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1999 tree decl
, target_context
;
2000 char save_static_chain_added
;
2002 gimple stmt
= gsi_stmt (*gsi
);
2004 switch (gimple_code (stmt
))
2007 if (gimple_call_chain (stmt
))
2009 decl
= gimple_call_fndecl (stmt
);
2012 target_context
= decl_function_context (decl
);
2013 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2015 gimple_call_set_chain (stmt
, get_static_chain (info
, target_context
,
2017 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2021 case GIMPLE_OMP_PARALLEL
:
2022 case GIMPLE_OMP_TASK
:
2023 save_static_chain_added
= info
->static_chain_added
;
2024 info
->static_chain_added
= 0;
2025 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2026 for (i
= 0; i
< 2; i
++)
2029 if ((info
->static_chain_added
& (1 << i
)) == 0)
2031 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2032 /* Don't add CHAIN.* or FRAME.* twice. */
2033 for (c
= gimple_omp_taskreg_clauses (stmt
);
2035 c
= OMP_CLAUSE_CHAIN (c
))
2036 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2037 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2038 && OMP_CLAUSE_DECL (c
) == decl
)
2042 c
= build_omp_clause (gimple_location (stmt
),
2043 i
? OMP_CLAUSE_FIRSTPRIVATE
2044 : OMP_CLAUSE_SHARED
);
2045 OMP_CLAUSE_DECL (c
) = decl
;
2046 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2047 gimple_omp_taskreg_set_clauses (stmt
, c
);
2050 info
->static_chain_added
|= save_static_chain_added
;
2053 case GIMPLE_OMP_FOR
:
2054 walk_body (convert_gimple_call
, NULL
, info
,
2055 gimple_omp_for_pre_body_ptr (stmt
));
2057 case GIMPLE_OMP_SECTIONS
:
2058 case GIMPLE_OMP_SECTION
:
2059 case GIMPLE_OMP_SINGLE
:
2060 case GIMPLE_OMP_MASTER
:
2061 case GIMPLE_OMP_ORDERED
:
2062 case GIMPLE_OMP_CRITICAL
:
2063 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2067 /* Keep looking for other operands. */
2068 *handled_ops_p
= false;
2072 *handled_ops_p
= true;
2076 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2077 call expressions. At the same time, determine if a nested function
2078 actually uses its static chain; if not, remember that. */
2081 convert_all_function_calls (struct nesting_info
*root
)
2083 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2084 struct nesting_info
*n
;
2086 /* First, optimistically clear static_chain for all decls that haven't
2087 used the static chain already for variable access. */
2088 FOR_EACH_NEST_INFO (n
, root
)
2090 tree decl
= n
->context
;
2091 if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2093 DECL_STATIC_CHAIN (decl
) = 0;
2094 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2095 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2096 lang_hooks
.decl_printable_name (decl
, 2));
2099 DECL_STATIC_CHAIN (decl
) = 1;
2100 chain_count
+= DECL_STATIC_CHAIN (decl
);
2103 /* Walk the functions and perform transformations. Note that these
2104 transformations can induce new uses of the static chain, which in turn
2105 require re-examining all users of the decl. */
2106 /* ??? It would make sense to try to use the call graph to speed this up,
2107 but the call graph hasn't really been built yet. Even if it did, we
2108 would still need to iterate in this loop since address-of references
2109 wouldn't show up in the callgraph anyway. */
2113 old_chain_count
= chain_count
;
2117 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2118 fputc ('\n', dump_file
);
2120 FOR_EACH_NEST_INFO (n
, root
)
2122 tree decl
= n
->context
;
2123 walk_function (convert_tramp_reference_stmt
,
2124 convert_tramp_reference_op
, n
);
2125 walk_function (convert_gimple_call
, NULL
, n
);
2126 chain_count
+= DECL_STATIC_CHAIN (decl
);
2129 while (chain_count
!= old_chain_count
);
2131 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2132 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2136 struct nesting_copy_body_data
2139 struct nesting_info
*root
;
2142 /* A helper subroutine for debug_var_chain type remapping. */
2145 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2147 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2148 void **slot
= pointer_map_contains (nid
->root
->var_map
, decl
);
2151 return (tree
) *slot
;
2153 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2155 tree new_decl
= copy_decl_no_change (decl
, id
);
2156 DECL_ORIGINAL_TYPE (new_decl
)
2157 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2161 if (TREE_CODE (decl
) == VAR_DECL
2162 || TREE_CODE (decl
) == PARM_DECL
2163 || TREE_CODE (decl
) == RESULT_DECL
)
2166 return copy_decl_no_change (decl
, id
);
2169 /* A helper function for remap_vla_decls. See if *TP contains
2170 some remapped variables. */
2173 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2175 struct nesting_info
*root
= (struct nesting_info
*) data
;
2182 slot
= pointer_map_contains (root
->var_map
, t
);
2185 return (tree
) *slot
;
2190 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2194 remap_vla_decls (tree block
, struct nesting_info
*root
)
2196 tree var
, subblock
, val
, type
;
2197 struct nesting_copy_body_data id
;
2199 for (subblock
= BLOCK_SUBBLOCKS (block
);
2201 subblock
= BLOCK_CHAIN (subblock
))
2202 remap_vla_decls (subblock
, root
);
2204 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
2205 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2207 val
= DECL_VALUE_EXPR (var
);
2208 type
= TREE_TYPE (var
);
2210 if (!(TREE_CODE (val
) == INDIRECT_REF
2211 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2212 && variably_modified_type_p (type
, NULL
)))
2215 if (pointer_map_contains (root
->var_map
, TREE_OPERAND (val
, 0))
2216 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2220 if (var
== NULL_TREE
)
2223 memset (&id
, 0, sizeof (id
));
2224 id
.cb
.copy_decl
= nesting_copy_decl
;
2225 id
.cb
.decl_map
= pointer_map_create ();
2228 for (; var
; var
= DECL_CHAIN (var
))
2229 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2231 struct nesting_info
*i
;
2235 val
= DECL_VALUE_EXPR (var
);
2236 type
= TREE_TYPE (var
);
2238 if (!(TREE_CODE (val
) == INDIRECT_REF
2239 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2240 && variably_modified_type_p (type
, NULL
)))
2243 slot
= pointer_map_contains (root
->var_map
, TREE_OPERAND (val
, 0));
2244 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2247 context
= decl_function_context (var
);
2248 for (i
= root
; i
; i
= i
->outer
)
2249 if (i
->context
== context
)
2255 /* Fully expand value expressions. This avoids having debug variables
2256 only referenced from them and that can be swept during GC. */
2259 tree t
= (tree
) *slot
;
2260 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
2261 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
2264 id
.cb
.src_fn
= i
->context
;
2265 id
.cb
.dst_fn
= i
->context
;
2266 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2268 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2269 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2271 newt
= TREE_TYPE (newt
);
2272 type
= TREE_TYPE (type
);
2274 if (TYPE_NAME (newt
)
2275 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2276 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2278 && TYPE_NAME (newt
) == TYPE_NAME (type
))
2279 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2281 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2282 if (val
!= DECL_VALUE_EXPR (var
))
2283 SET_DECL_VALUE_EXPR (var
, val
);
2286 pointer_map_destroy (id
.cb
.decl_map
);
2289 /* Fold the MEM_REF *E. */
2291 fold_mem_refs (const void *e
, void *data ATTRIBUTE_UNUSED
)
2293 tree
*ref_p
= CONST_CAST2(tree
*, const tree
*, (const tree
*)e
);
2294 *ref_p
= fold (*ref_p
);
2298 /* Do "everything else" to clean up or complete state collected by the
2299 various walking passes -- lay out the types and decls, generate code
2300 to initialize the frame decl, store critical expressions in the
2301 struct function for rtl to find. */
2304 finalize_nesting_tree_1 (struct nesting_info
*root
)
2306 gimple_seq stmt_list
;
2308 tree context
= root
->context
;
2309 struct function
*sf
;
2313 /* If we created a non-local frame type or decl, we need to lay them
2314 out at this time. */
2315 if (root
->frame_type
)
2317 /* In some cases the frame type will trigger the -Wpadded warning.
2318 This is not helpful; suppress it. */
2319 int save_warn_padded
= warn_padded
;
2323 layout_type (root
->frame_type
);
2324 warn_padded
= save_warn_padded
;
2325 layout_decl (root
->frame_decl
, 0);
2327 /* Remove root->frame_decl from root->new_local_var_chain, so
2328 that we can declare it also in the lexical blocks, which
2329 helps ensure virtual regs that end up appearing in its RTL
2330 expression get substituted in instantiate_virtual_regs(). */
2331 for (adjust
= &root
->new_local_var_chain
;
2332 *adjust
!= root
->frame_decl
;
2333 adjust
= &DECL_CHAIN (*adjust
))
2334 gcc_assert (DECL_CHAIN (*adjust
));
2335 *adjust
= DECL_CHAIN (*adjust
);
2337 DECL_CHAIN (root
->frame_decl
) = NULL_TREE
;
2338 declare_vars (root
->frame_decl
,
2339 gimple_seq_first_stmt (gimple_body (context
)), true);
2342 /* If any parameters were referenced non-locally, then we need to
2343 insert a copy. Likewise, if any variables were referenced by
2344 pointer, we need to initialize the address. */
2345 if (root
->any_parm_remapped
)
2348 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
2352 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
2356 if (use_pointer_in_frame (p
))
2357 x
= build_addr (p
, context
);
2361 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2362 root
->frame_decl
, field
, NULL_TREE
);
2363 stmt
= gimple_build_assign (y
, x
);
2364 gimple_seq_add_stmt (&stmt_list
, stmt
);
2365 /* If the assignment is from a non-register the stmt is
2366 not valid gimple. Make it so by using a temporary instead. */
2367 if (!is_gimple_reg (x
)
2368 && is_gimple_reg_type (TREE_TYPE (x
)))
2370 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
2371 x
= init_tmp_var (root
, x
, &gsi
);
2372 gimple_assign_set_rhs1 (stmt
, x
);
2377 /* If a chain_field was created, then it needs to be initialized
2379 if (root
->chain_field
)
2381 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
2382 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
2383 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
2384 gimple_seq_add_stmt (&stmt_list
, stmt
);
2387 /* If trampolines were created, then we need to initialize them. */
2388 if (root
->any_tramp_created
)
2390 struct nesting_info
*i
;
2391 for (i
= root
->inner
; i
; i
= i
->next
)
2393 tree arg1
, arg2
, arg3
, x
, field
;
2395 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
2399 gcc_assert (DECL_STATIC_CHAIN (i
->context
));
2400 arg3
= build_addr (root
->frame_decl
, context
);
2402 arg2
= build_addr (i
->context
, context
);
2404 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2405 root
->frame_decl
, field
, NULL_TREE
);
2406 arg1
= build_addr (x
, context
);
2408 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
2409 stmt
= gimple_build_call (x
, 3, arg1
, arg2
, arg3
);
2410 gimple_seq_add_stmt (&stmt_list
, stmt
);
2414 /* If we created initialization statements, insert them. */
2418 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
2419 bind
= gimple_seq_first_stmt (gimple_body (context
));
2420 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
2421 gimple_bind_set_body (bind
, stmt_list
);
2424 /* If a chain_decl was created, then it needs to be registered with
2425 struct function so that it gets initialized from the static chain
2426 register at the beginning of the function. */
2427 sf
= DECL_STRUCT_FUNCTION (root
->context
);
2428 sf
->static_chain_decl
= root
->chain_decl
;
2430 /* Similarly for the non-local goto save area. */
2431 if (root
->nl_goto_field
)
2433 sf
->nonlocal_goto_save_area
2434 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
2435 sf
->has_nonlocal_label
= 1;
2438 /* Make sure all new local variables get inserted into the
2439 proper BIND_EXPR. */
2440 if (root
->new_local_var_chain
)
2441 declare_vars (root
->new_local_var_chain
,
2442 gimple_seq_first_stmt (gimple_body (root
->context
)),
2445 if (root
->debug_var_chain
)
2450 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
2452 for (debug_var
= root
->debug_var_chain
; debug_var
;
2453 debug_var
= DECL_CHAIN (debug_var
))
2454 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2457 /* If there are any debug decls with variable length types,
2458 remap those types using other debug_var_chain variables. */
2461 struct nesting_copy_body_data id
;
2463 memset (&id
, 0, sizeof (id
));
2464 id
.cb
.copy_decl
= nesting_copy_decl
;
2465 id
.cb
.decl_map
= pointer_map_create ();
2468 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
2469 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2471 tree type
= TREE_TYPE (debug_var
);
2472 tree newt
, t
= type
;
2473 struct nesting_info
*i
;
2475 for (i
= root
; i
; i
= i
->outer
)
2476 if (variably_modified_type_p (type
, i
->context
))
2482 id
.cb
.src_fn
= i
->context
;
2483 id
.cb
.dst_fn
= i
->context
;
2484 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2486 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
2487 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2489 newt
= TREE_TYPE (newt
);
2492 if (TYPE_NAME (newt
)
2493 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2494 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2496 && TYPE_NAME (newt
) == TYPE_NAME (t
))
2497 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2500 pointer_map_destroy (id
.cb
.decl_map
);
2503 scope
= gimple_seq_first_stmt (gimple_body (root
->context
));
2504 if (gimple_bind_block (scope
))
2505 declare_vars (root
->debug_var_chain
, scope
, true);
2507 BLOCK_VARS (DECL_INITIAL (root
->context
))
2508 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
2509 root
->debug_var_chain
);
2512 /* Fold the rewritten MEM_REF trees. */
2513 pointer_set_traverse (root
->mem_refs
, fold_mem_refs
, NULL
);
2515 /* Dump the translated tree function. */
2518 fputs ("\n\n", dump_file
);
2519 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
2524 finalize_nesting_tree (struct nesting_info
*root
)
2526 struct nesting_info
*n
;
2527 FOR_EACH_NEST_INFO (n
, root
)
2528 finalize_nesting_tree_1 (n
);
2531 /* Unnest the nodes and pass them to cgraph. */
2534 unnest_nesting_tree_1 (struct nesting_info
*root
)
2536 struct cgraph_node
*node
= cgraph_get_node (root
->context
);
2538 /* For nested functions update the cgraph to reflect unnesting.
2539 We also delay finalizing of these functions up to this point. */
2542 cgraph_unnest_node (node
);
2543 cgraph_finalize_function (root
->context
, true);
2548 unnest_nesting_tree (struct nesting_info
*root
)
2550 struct nesting_info
*n
;
2551 FOR_EACH_NEST_INFO (n
, root
)
2552 unnest_nesting_tree_1 (n
);
2555 /* Free the data structures allocated during this pass. */
2558 free_nesting_tree (struct nesting_info
*root
)
2560 struct nesting_info
*node
, *next
;
2562 node
= iter_nestinfo_start (root
);
2565 next
= iter_nestinfo_next (node
);
2566 pointer_map_destroy (node
->var_map
);
2567 pointer_map_destroy (node
->field_map
);
2568 pointer_set_destroy (node
->mem_refs
);
2575 /* Gimplify a function and all its nested functions. */
2577 gimplify_all_functions (struct cgraph_node
*root
)
2579 struct cgraph_node
*iter
;
2580 if (!gimple_body (root
->symbol
.decl
))
2581 gimplify_function_tree (root
->symbol
.decl
);
2582 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
2583 gimplify_all_functions (iter
);
2586 /* Main entry point for this pass. Process FNDECL and all of its nested
2587 subroutines and turn them into something less tightly bound. */
2590 lower_nested_functions (tree fndecl
)
2592 struct cgraph_node
*cgn
;
2593 struct nesting_info
*root
;
2595 /* If there are no nested functions, there's nothing to do. */
2596 cgn
= cgraph_get_node (fndecl
);
2600 gimplify_all_functions (cgn
);
2602 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
2604 fprintf (dump_file
, "\n;; Function %s\n\n",
2605 lang_hooks
.decl_printable_name (fndecl
, 2));
2607 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
2608 root
= create_nesting_tree (cgn
);
2610 walk_all_functions (convert_nonlocal_reference_stmt
,
2611 convert_nonlocal_reference_op
,
2613 walk_all_functions (convert_local_reference_stmt
,
2614 convert_local_reference_op
,
2616 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
2617 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
2619 convert_all_function_calls (root
);
2620 finalize_nesting_tree (root
);
2621 unnest_nesting_tree (root
);
2623 free_nesting_tree (root
);
2624 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
2628 dump_end (TDI_nested
, dump_file
);
2633 #include "gt-tree-nested.h"