1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 #include "coretypes.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "tree-gimple.h"
32 #include "tree-iterator.h"
33 #include "tree-flow.h"
36 #include "langhooks.h"
40 /* The object of this pass is to lower the representation of a set of nested
41 functions in order to expose all of the gory details of the various
42 nonlocal references. We want to do this sooner rather than later, in
43 order to give us more freedom in emitting all of the functions in question.
45 Back in olden times, when gcc was young, we developed an insanely
46 complicated scheme whereby variables which were referenced nonlocally
47 were forced to live in the stack of the declaring function, and then
48 the nested functions magically discovered where these variables were
49 placed. In order for this scheme to function properly, it required
50 that the outer function be partially expanded, then we switch to
51 compiling the inner function, and once done with those we switch back
52 to compiling the outer function. Such delicate ordering requirements
53 makes it difficult to do whole translation unit optimizations
54 involving such functions.
56 The implementation here is much more direct. Everything that can be
57 referenced by an inner function is a member of an explicitly created
58 structure herein called the "nonlocal frame struct". The incoming
59 static chain for a nested function is a pointer to this struct in
60 the parent. In this way, we settle on known offsets from a known
61 base, and so are decoupled from the logic that places objects in the
62 function's stack frame. More importantly, we don't have to wait for
63 that to happen -- since the compilation of the inner function is no
64 longer tied to a real stack frame, the nonlocal frame struct can be
65 allocated anywhere. Which means that the outer function is now
68 Theory of operation here is very simple. Iterate over all the
69 statements in all the functions (depth first) several times,
70 allocating structures and fields on demand. In general we want to
71 examine inner functions first, so that we can avoid making changes
72 to outer functions which are unnecessary.
74 The order of the passes matters a bit, in that later passes will be
75 skipped if it is discovered that the functions don't actually interact
76 at all. That is, they're nested in the lexical sense but could have
77 been written as independent functions without change. */
80 struct var_map_elt
GTY(())
86 struct nesting_info
GTY ((chain_next ("%h.next")))
88 struct nesting_info
*outer
;
89 struct nesting_info
*inner
;
90 struct nesting_info
*next
;
92 htab_t
GTY ((param_is (struct var_map_elt
))) var_map
;
94 tree new_local_var_chain
;
101 bool any_parm_remapped
;
102 bool any_tramp_created
;
106 /* Hashing and equality functions for nesting_info->var_map. */
109 var_map_hash (const void *x
)
111 const struct var_map_elt
*a
= x
;
112 return htab_hash_pointer (a
->old
);
116 var_map_eq (const void *x
, const void *y
)
118 const struct var_map_elt
*a
= x
;
119 const struct var_map_elt
*b
= y
;
120 return a
->old
== b
->old
;
123 /* We're working in so many different function contexts simultaneously,
124 that create_tmp_var is dangerous. Prevent mishap. */
125 #define create_tmp_var cant_use_create_tmp_var_here_dummy
127 /* Like create_tmp_var, except record the variable for registration at
128 the given nesting level. */
131 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
135 /* If the type is of variable size or a type which must be created by the
136 frontend, something is wrong. Note that we explicitly allow
137 incomplete types here, since we create them ourselves here. */
138 gcc_assert (!TREE_ADDRESSABLE (type
));
139 gcc_assert (!TYPE_SIZE_UNIT (type
)
140 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
142 tmp_var
= create_tmp_var_raw (type
, prefix
);
143 DECL_CONTEXT (tmp_var
) = info
->context
;
144 TREE_CHAIN (tmp_var
) = info
->new_local_var_chain
;
145 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
146 if (TREE_CODE (type
) == COMPLEX_TYPE
)
147 DECL_COMPLEX_GIMPLE_REG_P (tmp_var
) = 1;
149 info
->new_local_var_chain
= tmp_var
;
154 /* Take the address of EXP to be used within function CONTEXT.
155 Mark it for addressability as necessary. */
158 build_addr (tree exp
, tree context
)
164 while (handled_component_p (base
))
165 base
= TREE_OPERAND (base
, 0);
168 TREE_ADDRESSABLE (base
) = 1;
170 /* Building the ADDR_EXPR will compute a set of properties for
171 that ADDR_EXPR. Those properties are unfortunately context
172 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
174 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
175 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
176 way the properties are for the ADDR_EXPR are computed properly. */
177 save_context
= current_function_decl
;
178 current_function_decl
= context
;
179 retval
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
180 current_function_decl
= save_context
;;
184 /* Insert FIELD into TYPE, sorted by alignment requirements. */
187 insert_field_into_struct (tree type
, tree field
)
191 DECL_CONTEXT (field
) = type
;
193 for (p
= &TYPE_FIELDS (type
); *p
; p
= &TREE_CHAIN (*p
))
194 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
197 TREE_CHAIN (field
) = *p
;
201 /* Build or return the RECORD_TYPE that describes the frame state that is
202 shared between INFO->CONTEXT and its nested functions. This record will
203 not be complete until finalize_nesting_tree; up until that point we'll
204 be adding fields as necessary.
206 We also build the DECL that represents this frame in the function. */
209 get_frame_type (struct nesting_info
*info
)
211 tree type
= info
->frame_type
;
216 type
= make_node (RECORD_TYPE
);
218 name
= concat ("FRAME.",
219 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
221 TYPE_NAME (type
) = get_identifier (name
);
224 info
->frame_type
= type
;
225 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
227 /* ??? Always make it addressable for now, since it is meant to
228 be pointed to by the static chain pointer. This pessimizes
229 when it turns out that no static chains are needed because
230 the nested functions referencing non-local variables are not
231 reachable, but the true pessimization is to create the non-
232 local frame structure in the first place. */
233 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
238 /* Return true if DECL should be referenced by pointer in the non-local
242 use_pointer_in_frame (tree decl
)
244 if (TREE_CODE (decl
) == PARM_DECL
)
246 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
247 sized decls, and inefficient to copy large aggregates. Don't bother
248 moving anything but scalar variables. */
249 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
253 /* Variable sized types make things "interesting" in the frame. */
254 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
258 /* Given DECL, a non-locally accessed variable, find or create a field
259 in the non-local frame structure for the given nesting context. */
262 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
263 enum insert_option insert
)
265 struct var_map_elt
*elt
, dummy
;
270 slot
= htab_find_slot (info
->var_map
, &dummy
, insert
);
273 gcc_assert (insert
!= INSERT
);
278 if (!elt
&& insert
== INSERT
)
280 field
= make_node (FIELD_DECL
);
281 DECL_NAME (field
) = DECL_NAME (decl
);
283 if (use_pointer_in_frame (decl
))
285 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
286 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
287 DECL_NONADDRESSABLE_P (field
) = 1;
291 TREE_TYPE (field
) = TREE_TYPE (decl
);
292 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
293 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
294 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
295 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
296 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
297 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
300 insert_field_into_struct (get_frame_type (info
), field
);
302 elt
= ggc_alloc (sizeof (*elt
));
307 if (TREE_CODE (decl
) == PARM_DECL
)
308 info
->any_parm_remapped
= true;
311 field
= elt
? elt
->new : NULL
;
316 /* Build or return the variable that holds the static chain within
317 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
320 get_chain_decl (struct nesting_info
*info
)
322 tree decl
= info
->chain_decl
;
327 type
= get_frame_type (info
->outer
);
328 type
= build_pointer_type (type
);
330 /* Note that this variable is *not* entered into any BIND_EXPR;
331 the construction of this variable is handled specially in
332 expand_function_start and initialize_inlined_parameters.
333 Note also that it's represented as a parameter. This is more
334 close to the truth, since the initial value does come from
336 decl
= build_decl (PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
337 DECL_ARTIFICIAL (decl
) = 1;
338 DECL_IGNORED_P (decl
) = 1;
339 TREE_USED (decl
) = 1;
340 DECL_CONTEXT (decl
) = info
->context
;
341 DECL_ARG_TYPE (decl
) = type
;
343 /* Tell tree-inline.c that we never write to this variable, so
344 it can copy-prop the replacement value immediately. */
345 TREE_READONLY (decl
) = 1;
347 info
->chain_decl
= decl
;
352 /* Build or return the field within the non-local frame state that holds
353 the static chain for INFO->CONTEXT. This is the way to walk back up
354 multiple nesting levels. */
357 get_chain_field (struct nesting_info
*info
)
359 tree field
= info
->chain_field
;
362 tree type
= build_pointer_type (get_frame_type (info
->outer
));
364 field
= make_node (FIELD_DECL
);
365 DECL_NAME (field
) = get_identifier ("__chain");
366 TREE_TYPE (field
) = type
;
367 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
368 DECL_NONADDRESSABLE_P (field
) = 1;
370 insert_field_into_struct (get_frame_type (info
), field
);
372 info
->chain_field
= field
;
377 /* Copy EXP into a temporary. Allocate the temporary in the context of
378 INFO and insert the initialization statement before TSI. */
381 init_tmp_var (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
385 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
386 stmt
= build (MODIFY_EXPR
, TREE_TYPE (t
), t
, exp
);
387 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
388 tsi_link_before (tsi
, stmt
, TSI_SAME_STMT
);
393 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
396 tsi_gimplify_val (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
398 if (is_gimple_val (exp
))
401 return init_tmp_var (info
, exp
, tsi
);
404 /* Similarly, but copy from the temporary and insert the statement
405 after the iterator. */
408 save_tmp_var (struct nesting_info
*info
, tree exp
,
409 tree_stmt_iterator
*tsi
)
413 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
414 stmt
= build (MODIFY_EXPR
, TREE_TYPE (t
), exp
, t
);
415 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
416 tsi_link_after (tsi
, stmt
, TSI_SAME_STMT
);
421 /* Build or return the type used to represent a nested function trampoline. */
423 static GTY(()) tree trampoline_type
;
426 get_trampoline_type (void)
429 unsigned align
, size
;
432 return trampoline_type
;
434 align
= TRAMPOLINE_ALIGNMENT
;
435 size
= TRAMPOLINE_SIZE
;
437 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
438 then allocate extra space so that we can do dynamic alignment. */
439 if (align
> STACK_BOUNDARY
)
441 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
442 align
= STACK_BOUNDARY
;
445 t
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
446 t
= build_array_type (char_type_node
, t
);
447 t
= build_decl (FIELD_DECL
, get_identifier ("__data"), t
);
448 DECL_ALIGN (t
) = align
;
449 DECL_USER_ALIGN (t
) = 1;
451 record
= make_node (RECORD_TYPE
);
452 TYPE_NAME (record
) = get_identifier ("__builtin_trampoline");
453 TYPE_FIELDS (record
) = t
;
454 layout_type (record
);
459 /* Given DECL, a nested function, find or create a field in the non-local
460 frame structure for a trampoline for this function. */
463 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
464 enum insert_option insert
)
466 struct var_map_elt
*elt
, dummy
;
471 slot
= htab_find_slot (info
->var_map
, &dummy
, insert
);
474 gcc_assert (insert
!= INSERT
);
479 if (!elt
&& insert
== INSERT
)
481 field
= make_node (FIELD_DECL
);
482 DECL_NAME (field
) = DECL_NAME (decl
);
483 TREE_TYPE (field
) = get_trampoline_type ();
484 TREE_ADDRESSABLE (field
) = 1;
486 insert_field_into_struct (get_frame_type (info
), field
);
488 elt
= ggc_alloc (sizeof (*elt
));
493 info
->any_tramp_created
= true;
496 field
= elt
? elt
->new : NULL
;
501 /* Build or return the field within the non-local frame state that holds
502 the non-local goto "jmp_buf". The buffer itself is maintained by the
503 rtl middle-end as dynamic stack space is allocated. */
506 get_nl_goto_field (struct nesting_info
*info
)
508 tree field
= info
->nl_goto_field
;
514 /* For __builtin_nonlocal_goto, we need N words. The first is the
515 frame pointer, the rest is for the target's stack pointer save
516 area. The number of words is controlled by STACK_SAVEAREA_MODE;
517 not the best interface, but it'll do for now. */
518 if (Pmode
== ptr_mode
)
519 type
= ptr_type_node
;
521 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
523 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
524 size
= size
/ GET_MODE_SIZE (Pmode
);
527 type
= build_array_type
528 (type
, build_index_type (build_int_cst (NULL_TREE
, size
)));
530 field
= make_node (FIELD_DECL
);
531 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
532 TREE_TYPE (field
) = type
;
533 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
534 TREE_ADDRESSABLE (field
) = 1;
536 insert_field_into_struct (get_frame_type (info
), field
);
538 info
->nl_goto_field
= field
;
544 /* Convenience routines to walk all statements of a gimple function.
546 For each statement, we invoke CALLBACK via walk_tree. The passed
547 data is a walk_stmt_info structure. Of note here is a TSI that
548 points to the current statement being walked. The VAL_ONLY flag
549 that indicates whether the *TP being examined may be replaced
550 with something that matches is_gimple_val (if true) or something
551 slightly more complicated (if false). "Something" technically
552 means the common subset of is_gimple_lvalue and is_gimple_rhs,
553 but we never try to form anything more complicated than that, so
554 we don't bother checking. */
556 struct walk_stmt_info
558 walk_tree_fn callback
;
559 tree_stmt_iterator tsi
;
560 struct nesting_info
*info
;
566 /* A subroutine of walk_function. Iterate over all sub-statements of *TP. */
569 walk_stmts (struct walk_stmt_info
*wi
, tree
*tp
)
575 switch (TREE_CODE (t
))
579 tree_stmt_iterator i
;
580 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
583 walk_stmts (wi
, tsi_stmt_ptr (i
));
589 walk_tree (&COND_EXPR_COND (t
), wi
->callback
, wi
, NULL
);
590 walk_stmts (wi
, &COND_EXPR_THEN (t
));
591 walk_stmts (wi
, &COND_EXPR_ELSE (t
));
594 walk_stmts (wi
, &CATCH_BODY (t
));
597 walk_stmts (wi
, &EH_FILTER_FAILURE (t
));
600 case TRY_FINALLY_EXPR
:
601 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
602 walk_stmts (wi
, &TREE_OPERAND (t
, 1));
605 walk_stmts (wi
, &BIND_EXPR_BODY (t
));
609 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
613 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
614 wi
->val_only
= !is_gimple_formal_tmp_var (TREE_OPERAND (t
, 0));
615 walk_tree (&TREE_OPERAND (t
, 1), wi
->callback
, wi
, NULL
);
617 /* If the rhs is appropriate for a memory, we may use a
618 COMPONENT_REF on the lhs. */
619 wi
->val_only
= !is_gimple_mem_rhs (TREE_OPERAND (t
, 1));
621 walk_tree (&TREE_OPERAND (t
, 0), wi
->callback
, wi
, NULL
);
629 walk_tree (tp
, wi
->callback
, wi
, NULL
);
634 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
637 walk_function (walk_tree_fn callback
, struct nesting_info
*info
)
639 struct walk_stmt_info wi
;
641 memset (&wi
, 0, sizeof (wi
));
642 wi
.callback
= callback
;
646 walk_stmts (&wi
, &DECL_SAVED_TREE (info
->context
));
649 /* Similarly for ROOT and all functions nested underneath, depth first. */
652 walk_all_functions (walk_tree_fn callback
, struct nesting_info
*root
)
657 walk_all_functions (callback
, root
->inner
);
658 walk_function (callback
, root
);
664 /* We have to check for a fairly pathological case. The operands of function
665 nested function are to be interpreted in the context of the enclosing
666 function. So if any are variably-sized, they will get remapped when the
667 enclosing function is inlined. But that remapping would also have to be
668 done in the types of the PARM_DECLs of the nested function, meaning the
669 argument types of that function will disagree with the arguments in the
670 calls to that function. So we'd either have to make a copy of the nested
671 function corresponding to each time the enclosing function was inlined or
672 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
673 function. The former is not practical. The latter would still require
674 detecting this case to know when to add the conversions. So, for now at
675 least, we don't inline such an enclosing function.
677 We have to do that check recursively, so here return indicating whether
678 FNDECL has such a nested function. ORIG_FN is the function we were
679 trying to inline to use for checking whether any argument is variably
680 modified by anything in it.
682 It would be better to do this in tree-inline.c so that we could give
683 the appropriate warning for why a function can't be inlined, but that's
684 too late since the nesting structure has already been flattened and
685 adding a flag just to record this fact seems a waste of a flag. */
688 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
690 struct cgraph_node
*cgn
= cgraph_node (fndecl
);
693 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
695 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= TREE_CHAIN (arg
))
696 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
699 if (check_for_nested_with_variably_modified (cgn
->decl
, orig_fndecl
))
706 /* Construct our local datastructure describing the function nesting
707 tree rooted by CGN. */
709 static struct nesting_info
*
710 create_nesting_tree (struct cgraph_node
*cgn
)
712 struct nesting_info
*info
= ggc_calloc (1, sizeof (*info
));
713 info
->var_map
= htab_create_ggc (7, var_map_hash
, var_map_eq
, ggc_free
);
714 info
->context
= cgn
->decl
;
716 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
718 struct nesting_info
*sub
= create_nesting_tree (cgn
);
720 sub
->next
= info
->inner
;
724 /* See discussion at check_for_nested_with_variably_modified for a
725 discussion of why this has to be here. */
726 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
727 DECL_UNINLINABLE (info
->context
) = true;
732 /* Return an expression computing the static chain for TARGET_CONTEXT
733 from INFO->CONTEXT. Insert any necessary computations before TSI. */
736 get_static_chain (struct nesting_info
*info
, tree target_context
,
737 tree_stmt_iterator
*tsi
)
739 struct nesting_info
*i
;
742 if (info
->context
== target_context
)
744 x
= build_addr (info
->frame_decl
, target_context
);
748 x
= get_chain_decl (info
);
750 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
752 tree field
= get_chain_field (i
);
754 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
755 x
= build (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
756 x
= init_tmp_var (info
, x
, tsi
);
763 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
764 frame as seen from INFO->CONTEXT. Insert any necessary computations
768 get_frame_field (struct nesting_info
*info
, tree target_context
,
769 tree field
, tree_stmt_iterator
*tsi
)
771 struct nesting_info
*i
;
774 if (info
->context
== target_context
)
776 /* Make sure frame_decl gets created. */
777 (void) get_frame_type (info
);
778 x
= info
->frame_decl
;
782 x
= get_chain_decl (info
);
784 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
786 tree field
= get_chain_field (i
);
788 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
789 x
= build (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
790 x
= init_tmp_var (info
, x
, tsi
);
793 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
796 x
= build (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
800 /* Called via walk_function+walk_tree, rewrite all references to VAR
801 and PARM_DECLs that belong to outer functions.
803 The rewrite will involve some number of structure accesses back up
804 the static chain. E.g. for a variable FOO up one nesting level it'll
805 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
806 indirections apply to decls for which use_pointer_in_frame is true. */
809 convert_nonlocal_reference (tree
*tp
, int *walk_subtrees
, void *data
)
811 struct walk_stmt_info
*wi
= data
;
812 struct nesting_info
*info
= wi
->info
;
816 switch (TREE_CODE (t
))
819 /* Non-automatic variables are never processed. */
820 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
825 if (decl_function_context (t
) != info
->context
)
827 tree target_context
= decl_function_context (t
);
828 struct nesting_info
*i
;
832 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
834 x
= lookup_field_for_decl (i
, t
, INSERT
);
835 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
836 if (use_pointer_in_frame (t
))
838 x
= init_tmp_var (info
, x
, &wi
->tsi
);
839 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
845 x
= save_tmp_var (info
, x
, &wi
->tsi
);
847 x
= init_tmp_var (info
, x
, &wi
->tsi
);
855 /* Don't walk non-local gotos for now. */
856 if (TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
)
865 /* We're taking the address of a label from a parent function, but
866 this is not itself a non-local goto. Mark the label such that it
867 will not be deleted, much as we would with a label address in
869 if (decl_function_context (t
) != info
->context
)
870 FORCED_LABEL (t
) = 1;
875 bool save_val_only
= wi
->val_only
;
877 wi
->val_only
= false;
880 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference
, wi
, NULL
);
887 /* If we changed anything, then TREE_INVARIANT is be wrong,
888 since we're no longer directly referencing a decl. */
889 save_context
= current_function_decl
;
890 current_function_decl
= info
->context
;
891 recompute_tree_invarant_for_addr_expr (t
);
892 current_function_decl
= save_context
;
894 /* If the callback converted the address argument in a context
895 where we only accept variables (and min_invariant, presumably),
896 then compute the address into a temporary. */
898 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
907 case ARRAY_RANGE_REF
:
909 /* Go down this entire nest and just look at the final prefix and
910 anything that describes the references. Otherwise, we lose track
911 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
914 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
916 if (TREE_CODE (t
) == COMPONENT_REF
)
917 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
919 else if (TREE_CODE (t
) == ARRAY_REF
920 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
922 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
924 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
926 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference
, wi
,
929 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
931 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
933 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
937 wi
->val_only
= false;
938 walk_tree (tp
, convert_nonlocal_reference
, wi
, NULL
);
942 if (!IS_TYPE_OR_DECL_P (t
))
954 /* Called via walk_function+walk_tree, rewrite all references to VAR
955 and PARM_DECLs that were referenced by inner nested functions.
956 The rewrite will be a structure reference to the local frame variable. */
959 convert_local_reference (tree
*tp
, int *walk_subtrees
, void *data
)
961 struct walk_stmt_info
*wi
= data
;
962 struct nesting_info
*info
= wi
->info
;
963 tree t
= *tp
, field
, x
;
967 switch (TREE_CODE (t
))
970 /* Non-automatic variables are never processed. */
971 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
976 if (decl_function_context (t
) == info
->context
)
978 /* If we copied a pointer to the frame, then the original decl
979 is used unchanged in the parent function. */
980 if (use_pointer_in_frame (t
))
983 /* No need to transform anything if no child references the
985 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
990 x
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
995 x
= save_tmp_var (info
, x
, &wi
->tsi
);
997 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1005 save_val_only
= wi
->val_only
;
1006 wi
->val_only
= false;
1008 wi
->changed
= false;
1009 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference
, wi
, NULL
);
1010 wi
->val_only
= save_val_only
;
1012 /* If we converted anything ... */
1017 /* Then the frame decl is now addressable. */
1018 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1020 save_context
= current_function_decl
;
1021 current_function_decl
= info
->context
;
1022 recompute_tree_invarant_for_addr_expr (t
);
1023 current_function_decl
= save_context
;
1025 /* If we are in a context where we only accept values, then
1026 compute the address into a temporary. */
1028 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1036 case ARRAY_RANGE_REF
:
1038 /* Go down this entire nest and just look at the final prefix and
1039 anything that describes the references. Otherwise, we lose track
1040 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1041 save_val_only
= wi
->val_only
;
1042 wi
->val_only
= true;
1044 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1046 if (TREE_CODE (t
) == COMPONENT_REF
)
1047 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1049 else if (TREE_CODE (t
) == ARRAY_REF
1050 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1052 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1054 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1056 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference
, wi
,
1059 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1061 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1063 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1067 wi
->val_only
= false;
1068 walk_tree (tp
, convert_local_reference
, wi
, NULL
);
1069 wi
->val_only
= save_val_only
;
1073 if (!IS_TYPE_OR_DECL_P (t
))
1076 wi
->val_only
= true;
1085 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1086 reference labels from outer functions. The rewrite will be a
1087 call to __builtin_nonlocal_goto. */
1090 convert_nl_goto_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1092 struct walk_stmt_info
*wi
= data
;
1093 struct nesting_info
*info
= wi
->info
, *i
;
1094 tree t
= *tp
, label
, new_label
, target_context
, x
, arg
, field
;
1095 struct var_map_elt
*elt
, dummy
;
1099 if (TREE_CODE (t
) != GOTO_EXPR
)
1101 label
= GOTO_DESTINATION (t
);
1102 if (TREE_CODE (label
) != LABEL_DECL
)
1104 target_context
= decl_function_context (label
);
1105 if (target_context
== info
->context
)
1108 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1111 /* The original user label may also be use for a normal goto, therefore
1112 we must create a new label that will actually receive the abnormal
1113 control transfer. This new label will be marked LABEL_NONLOCAL; this
1114 mark will trigger proper behavior in the cfg, as well as cause the
1115 (hairy target-specific) non-local goto receiver code to be generated
1116 when we expand rtl. Enter this association into var_map so that we
1117 can insert the new label into the IL during a second pass. */
1119 slot
= htab_find_slot (i
->var_map
, &dummy
, INSERT
);
1123 new_label
= create_artificial_label ();
1124 DECL_NONLOCAL (new_label
) = 1;
1126 elt
= ggc_alloc (sizeof (*elt
));
1128 elt
->new = new_label
;
1132 new_label
= elt
->new;
1134 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1135 field
= get_nl_goto_field (i
);
1136 x
= get_frame_field (info
, target_context
, field
, &wi
->tsi
);
1137 x
= build_addr (x
, target_context
);
1138 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1139 arg
= tree_cons (NULL
, x
, NULL
);
1140 x
= build_addr (new_label
, target_context
);
1141 arg
= tree_cons (NULL
, x
, arg
);
1142 x
= implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
];
1143 x
= build_function_call_expr (x
, arg
);
1145 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
1146 *tsi_stmt_ptr (wi
->tsi
) = x
;
1151 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1152 are referenced via nonlocal goto from a nested function. The rewrite
1153 will involve installing a newly generated DECL_NONLOCAL label, and
1154 (potentially) a branch around the rtl gunk that is assumed to be
1155 attached to such a label. */
1158 convert_nl_goto_receiver (tree
*tp
, int *walk_subtrees
, void *data
)
1160 struct walk_stmt_info
*wi
= data
;
1161 struct nesting_info
*info
= wi
->info
;
1162 tree t
= *tp
, label
, new_label
, x
;
1163 struct var_map_elt
*elt
, dummy
;
1164 tree_stmt_iterator tmp_tsi
;
1167 if (TREE_CODE (t
) != LABEL_EXPR
)
1169 label
= LABEL_EXPR_LABEL (t
);
1172 elt
= htab_find (info
->var_map
, &dummy
);
1175 new_label
= elt
->new;
1177 /* If there's any possibility that the previous statement falls through,
1178 then we must branch around the new non-local label. */
1180 tsi_prev (&tmp_tsi
);
1181 if (tsi_end_p (tmp_tsi
) || block_may_fallthru (tsi_stmt (tmp_tsi
)))
1183 x
= build1 (GOTO_EXPR
, void_type_node
, label
);
1184 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1186 x
= build1 (LABEL_EXPR
, void_type_node
, new_label
);
1187 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1192 /* Called via walk_function+walk_tree, rewrite all references to addresses
1193 of nested functions that require the use of trampolines. The rewrite
1194 will involve a reference a trampoline generated for the occasion. */
1197 convert_tramp_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1199 struct walk_stmt_info
*wi
= data
;
1200 struct nesting_info
*info
= wi
->info
, *i
;
1201 tree t
= *tp
, decl
, target_context
, x
, arg
;
1204 switch (TREE_CODE (t
))
1208 T.1 = &CHAIN->tramp;
1209 T.2 = __builtin_adjust_trampoline (T.1);
1210 T.3 = (func_type)T.2;
1213 decl
= TREE_OPERAND (t
, 0);
1214 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1217 /* Only need to process nested functions. */
1218 target_context
= decl_function_context (decl
);
1219 if (!target_context
)
1222 /* If the nested function doesn't use a static chain, then
1223 it doesn't need a trampoline. */
1224 if (DECL_NO_STATIC_CHAIN (decl
))
1227 /* Lookup the immediate parent of the callee, as that's where
1228 we need to insert the trampoline. */
1229 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1231 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1233 /* Compute the address of the field holding the trampoline. */
1234 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
1235 x
= build_addr (x
, target_context
);
1236 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1237 arg
= tree_cons (NULL
, x
, NULL
);
1239 /* Do machine-specific ugliness. Normally this will involve
1240 computing extra alignment, but it can really be anything. */
1241 x
= implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
];
1242 x
= build_function_call_expr (x
, arg
);
1243 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1245 /* Cast back to the proper function type. */
1246 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1247 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1253 /* Only walk call arguments, lest we generate trampolines for
1255 walk_tree (&TREE_OPERAND (t
, 1), convert_tramp_reference
, wi
, NULL
);
1259 if (!IS_TYPE_OR_DECL_P (t
))
1267 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1268 reference nested functions to make sure that the static chain is
1269 set up properly for the call. */
1272 convert_call_expr (tree
*tp
, int *walk_subtrees
, void *data
)
1274 struct walk_stmt_info
*wi
= data
;
1275 struct nesting_info
*info
= wi
->info
;
1276 tree t
= *tp
, decl
, target_context
;
1279 switch (TREE_CODE (t
))
1282 decl
= get_callee_fndecl (t
);
1285 target_context
= decl_function_context (decl
);
1286 if (target_context
&& !DECL_NO_STATIC_CHAIN (decl
))
1288 = get_static_chain (info
, target_context
, &wi
->tsi
);
1293 case WITH_SIZE_EXPR
:
1294 /* Only return modify and with_size_expr may contain calls. */
1305 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1306 trampolines and call expressions. On the way back up, determine if
1307 a nested function actually uses its static chain; if not, remember that. */
1310 convert_all_function_calls (struct nesting_info
*root
)
1315 convert_all_function_calls (root
->inner
);
1317 walk_function (convert_tramp_reference
, root
);
1318 walk_function (convert_call_expr
, root
);
1320 /* If the function does not use a static chain, then remember that. */
1321 if (root
->outer
&& !root
->chain_decl
&& !root
->chain_field
)
1322 DECL_NO_STATIC_CHAIN (root
->context
) = 1;
1324 gcc_assert (!DECL_NO_STATIC_CHAIN (root
->context
));
1331 /* Do "everything else" to clean up or complete state collected by the
1332 various walking passes -- lay out the types and decls, generate code
1333 to initialize the frame decl, store critical expressions in the
1334 struct function for rtl to find. */
1337 finalize_nesting_tree_1 (struct nesting_info
*root
)
1339 tree stmt_list
= NULL
;
1340 tree context
= root
->context
;
1341 struct function
*sf
;
1342 struct cgraph_node
*node
;
1344 /* If we created a non-local frame type or decl, we need to lay them
1345 out at this time. */
1346 if (root
->frame_type
)
1348 /* In some cases the frame type will trigger the -Wpadded warning.
1349 This is not helpful; suppress it. */
1350 int save_warn_padded
= warn_padded
;
1352 layout_type (root
->frame_type
);
1353 warn_padded
= save_warn_padded
;
1354 layout_decl (root
->frame_decl
, 0);
1357 /* If any parameters were referenced non-locally, then we need to
1358 insert a copy. Likewise, if any variables were referenced by
1359 pointer, we need to initialize the address. */
1360 if (root
->any_parm_remapped
)
1363 for (p
= DECL_ARGUMENTS (context
); p
; p
= TREE_CHAIN (p
))
1367 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
1371 if (use_pointer_in_frame (p
))
1372 x
= build_addr (p
, context
);
1376 y
= build (COMPONENT_REF
, TREE_TYPE (field
),
1377 root
->frame_decl
, field
, NULL_TREE
);
1378 x
= build (MODIFY_EXPR
, TREE_TYPE (field
), y
, x
);
1379 append_to_statement_list (x
, &stmt_list
);
1383 /* If a chain_field was created, then it needs to be initialized
1385 if (root
->chain_field
)
1387 tree x
= build (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
1388 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
1389 x
= build (MODIFY_EXPR
, TREE_TYPE (x
), x
, get_chain_decl (root
));
1390 append_to_statement_list (x
, &stmt_list
);
1393 /* If trampolines were created, then we need to initialize them. */
1394 if (root
->any_tramp_created
)
1396 struct nesting_info
*i
;
1397 for (i
= root
->inner
; i
; i
= i
->next
)
1401 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
1405 if (DECL_NO_STATIC_CHAIN (i
->context
))
1406 x
= null_pointer_node
;
1408 x
= build_addr (root
->frame_decl
, context
);
1409 arg
= tree_cons (NULL
, x
, NULL
);
1411 x
= build_addr (i
->context
, context
);
1412 arg
= tree_cons (NULL
, x
, arg
);
1414 x
= build (COMPONENT_REF
, TREE_TYPE (field
),
1415 root
->frame_decl
, field
, NULL_TREE
);
1416 x
= build_addr (x
, context
);
1417 arg
= tree_cons (NULL
, x
, arg
);
1419 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
1420 x
= build_function_call_expr (x
, arg
);
1422 append_to_statement_list (x
, &stmt_list
);
1426 /* If we created initialization statements, insert them. */
1429 annotate_all_with_locus (&stmt_list
,
1430 DECL_SOURCE_LOCATION (context
));
1431 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context
)),
1433 BIND_EXPR_BODY (DECL_SAVED_TREE (context
)) = stmt_list
;
1436 /* If a chain_decl was created, then it needs to be registered with
1437 struct function so that it gets initialized from the static chain
1438 register at the beginning of the function. */
1439 sf
= DECL_STRUCT_FUNCTION (root
->context
);
1440 sf
->static_chain_decl
= root
->chain_decl
;
1442 /* Similarly for the non-local goto save area. */
1443 if (root
->nl_goto_field
)
1445 sf
->nonlocal_goto_save_area
1446 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
1447 sf
->has_nonlocal_label
= 1;
1450 /* Make sure all new local variables get inserted into the
1451 proper BIND_EXPR. */
1452 if (root
->new_local_var_chain
)
1453 declare_tmp_vars (root
->new_local_var_chain
,
1454 DECL_SAVED_TREE (root
->context
));
1456 /* Dump the translated tree function. */
1457 dump_function (TDI_nested
, root
->context
);
1458 node
= cgraph_node (root
->context
);
1460 /* For nested functions update the cgraph to reflect unnesting.
1461 We also delay finalizing of these functions up to this point. */
1464 cgraph_unnest_node (cgraph_node (root
->context
));
1465 cgraph_finalize_function (root
->context
, true);
1470 finalize_nesting_tree (struct nesting_info
*root
)
1475 finalize_nesting_tree (root
->inner
);
1476 finalize_nesting_tree_1 (root
);
1482 /* Free the data structures allocated during this pass. */
1485 free_nesting_tree (struct nesting_info
*root
)
1487 struct nesting_info
*next
;
1491 free_nesting_tree (root
->inner
);
1492 htab_delete (root
->var_map
);
1500 static GTY(()) struct nesting_info
*root
;
1502 /* Main entry point for this pass. Process FNDECL and all of its nested
1503 subroutines and turn them into something less tightly bound. */
1506 lower_nested_functions (tree fndecl
)
1508 struct cgraph_node
*cgn
;
1510 /* If there are no nested functions, there's nothing to do. */
1511 cgn
= cgraph_node (fndecl
);
1515 root
= create_nesting_tree (cgn
);
1516 walk_all_functions (convert_nonlocal_reference
, root
);
1517 walk_all_functions (convert_local_reference
, root
);
1518 walk_all_functions (convert_nl_goto_reference
, root
);
1519 walk_all_functions (convert_nl_goto_receiver
, root
);
1520 convert_all_function_calls (root
);
1521 finalize_nesting_tree (root
);
1522 free_nesting_tree (root
);
1526 #include "gt-tree-nested.h"