1 /* Nested function decomposition for trees.
2 Copyright (C) 2004 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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 incomming
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. */
88 struct nesting_info
*outer
;
89 struct nesting_info
*inner
;
90 struct nesting_info
*next
;
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 defined ENABLE_CHECKING
136 /* If the type is of variable size or a type which must be created by the
137 frontend, something is wrong. Note that we explicitly allow
138 incomplete types here, since we create them ourselves here. */
139 if (TREE_ADDRESSABLE (type
)
140 || (TYPE_SIZE_UNIT (type
)
141 && TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
))
145 tmp_var
= create_tmp_var_raw (type
, prefix
);
146 DECL_CONTEXT (tmp_var
) = info
->context
;
147 TREE_CHAIN (tmp_var
) = info
->new_local_var_chain
;
148 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
149 info
->new_local_var_chain
= tmp_var
;
154 /* Take the address of EXP. Mark it for addressability as necessary. */
157 build_addr (tree exp
)
161 while (TREE_CODE (base
) == REALPART_EXPR
|| TREE_CODE (base
) == IMAGPART_EXPR
162 || handled_component_p (base
))
163 base
= TREE_OPERAND (base
, 0);
166 TREE_ADDRESSABLE (base
) = 1;
168 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
171 /* Insert FIELD into TYPE, sorted by alignment requirements. */
174 insert_field_into_struct (tree type
, tree field
)
178 DECL_CONTEXT (field
) = type
;
180 for (p
= &TYPE_FIELDS (type
); *p
; p
= &TREE_CHAIN (*p
))
181 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
184 TREE_CHAIN (field
) = *p
;
188 /* Build or return the RECORD_TYPE that describes the frame state that is
189 shared between INFO->CONTEXT and its nested functions. This record will
190 not be complete until finalize_nesting_tree; up until that point we'll
191 be adding fields as necessary.
193 We also build the DECL that represents this frame in the function. */
196 get_frame_type (struct nesting_info
*info
)
198 tree type
= info
->frame_type
;
203 type
= make_node (RECORD_TYPE
);
205 name
= concat ("FRAME.",
206 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
208 TYPE_NAME (type
) = get_identifier (name
);
211 info
->frame_type
= type
;
212 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
217 /* Return true if DECL should be referenced by pointer in the non-local
221 use_pointer_in_frame (tree decl
)
223 if (TREE_CODE (decl
) == PARM_DECL
)
225 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
226 sized decls, and inefficient to copy large aggregates. Don't bother
227 moving anything but scalar variables. */
228 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
232 /* Variable sized types make things "interesting" in the frame. */
233 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
237 /* Given DECL, a non-locally accessed variable, find or create a field
238 in the non-local frame structure for the given nesting context. */
241 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
242 enum insert_option insert
)
244 struct var_map_elt
*elt
, dummy
;
249 slot
= htab_find_slot (info
->var_map
, &dummy
, insert
);
252 if (insert
== INSERT
)
258 if (!elt
&& insert
== INSERT
)
260 field
= make_node (FIELD_DECL
);
261 DECL_NAME (field
) = DECL_NAME (decl
);
263 if (use_pointer_in_frame (decl
))
265 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
266 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
267 DECL_NONADDRESSABLE_P (field
) = 1;
271 TREE_TYPE (field
) = TREE_TYPE (decl
);
272 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
273 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
274 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
275 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
276 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
277 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
280 insert_field_into_struct (get_frame_type (info
), field
);
282 elt
= xmalloc (sizeof (*elt
));
287 if (TREE_CODE (decl
) == PARM_DECL
)
288 info
->any_parm_remapped
= true;
291 field
= elt
? elt
->new : NULL
;
296 /* Build or return the variable that holds the static chain within
297 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
300 get_chain_decl (struct nesting_info
*info
)
302 tree decl
= info
->chain_decl
;
307 type
= get_frame_type (info
->outer
);
308 type
= build_pointer_type (type
);
310 /* Note that this variable is *not* entered into any BIND_EXPR;
311 the construction of this variable is handled specially in
312 expand_function_start and initialize_inlined_parameters.
313 Note also that it's represented as a parameter. This is more
314 close to the truth, since the initial value does come from
316 decl
= build_decl (PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
317 DECL_ARTIFICIAL (decl
) = 1;
318 DECL_IGNORED_P (decl
) = 1;
319 TREE_USED (decl
) = 1;
320 DECL_CONTEXT (decl
) = info
->context
;
321 DECL_ARG_TYPE (decl
) = type
;
323 /* Tell tree-inline.c that we never write to this variable, so
324 it can copy-prop the replacement value immediately. */
325 TREE_READONLY (decl
) = 1;
327 info
->chain_decl
= decl
;
332 /* Build or return the field within the non-local frame state that holds
333 the static chain for INFO->CONTEXT. This is the way to walk back up
334 multiple nesting levels. */
337 get_chain_field (struct nesting_info
*info
)
339 tree field
= info
->chain_field
;
342 tree type
= build_pointer_type (get_frame_type (info
->outer
));
344 field
= make_node (FIELD_DECL
);
345 DECL_NAME (field
) = get_identifier ("__chain");
346 TREE_TYPE (field
) = type
;
347 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
348 DECL_NONADDRESSABLE_P (field
) = 1;
350 insert_field_into_struct (get_frame_type (info
), field
);
352 info
->chain_field
= field
;
357 /* Copy EXP into a temporary. Allocate the temporary in the context of
358 INFO and insert the initialization statement before TSI. */
361 init_tmp_var (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
365 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
366 stmt
= build (MODIFY_EXPR
, TREE_TYPE (t
), t
, exp
);
367 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
368 tsi_link_before (tsi
, stmt
, TSI_SAME_STMT
);
373 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
376 tsi_gimplify_val (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
378 if (is_gimple_val (exp
))
381 return init_tmp_var (info
, exp
, tsi
);
384 /* Build or return the type used to represent a nested function trampoline. */
386 static GTY(()) tree trampoline_type
;
389 get_trampoline_type (void)
392 unsigned align
, size
;
395 return trampoline_type
;
397 align
= TRAMPOLINE_ALIGNMENT
;
398 size
= TRAMPOLINE_SIZE
;
400 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
401 then allocate extra space so that we can do dynamic alignment. */
402 if (align
> STACK_BOUNDARY
)
404 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
405 align
= STACK_BOUNDARY
;
408 t
= build_index_type (build_int_2 (size
- 1, 0));
409 t
= build_array_type (char_type_node
, t
);
410 t
= build_decl (FIELD_DECL
, get_identifier ("__data"), t
);
411 DECL_ALIGN (t
) = align
;
412 DECL_USER_ALIGN (t
) = 1;
414 record
= make_node (RECORD_TYPE
);
415 TYPE_NAME (record
) = get_identifier ("__builtin_trampoline");
416 TYPE_FIELDS (record
) = t
;
417 layout_type (record
);
422 /* Given DECL, a nested function, find or create a field in the non-local
423 frame structure for a trampoline for this function. */
426 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
427 enum insert_option insert
)
429 struct var_map_elt
*elt
, dummy
;
434 slot
= htab_find_slot (info
->var_map
, &dummy
, insert
);
437 if (insert
== INSERT
)
443 if (!elt
&& insert
== INSERT
)
445 field
= make_node (FIELD_DECL
);
446 DECL_NAME (field
) = DECL_NAME (decl
);
447 TREE_TYPE (field
) = get_trampoline_type ();
448 TREE_ADDRESSABLE (field
) = 1;
450 insert_field_into_struct (get_frame_type (info
), field
);
452 elt
= xmalloc (sizeof (*elt
));
457 info
->any_tramp_created
= true;
460 field
= elt
? elt
->new : NULL
;
465 /* Build or return the field within the non-local frame state that holds
466 the non-local goto "jmp_buf". The buffer itself is maintained by the
467 rtl middle-end as dynamic stack space is allocated. */
470 get_nl_goto_field (struct nesting_info
*info
)
472 tree field
= info
->nl_goto_field
;
478 /* For __builtin_nonlocal_goto, we need N words. The first is the
479 frame pointer, the rest is for the target's stack pointer save
480 area. The number of words is controlled by STACK_SAVEAREA_MODE;
481 not the best interface, but it'll do for now. */
482 if (Pmode
== ptr_mode
)
483 type
= ptr_type_node
;
485 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
487 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
488 size
= size
/ GET_MODE_SIZE (Pmode
);
491 type
= build_array_type (type
, build_index_type (build_int_2 (size
, 0)));
493 field
= make_node (FIELD_DECL
);
494 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
495 TREE_TYPE (field
) = type
;
496 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
497 TREE_ADDRESSABLE (field
) = 1;
499 insert_field_into_struct (get_frame_type (info
), field
);
501 info
->nl_goto_field
= field
;
507 /* Convenience routines to walk all statements of a gimple function.
509 For each statement, we invoke CALLBACK via walk_tree. The passed
510 data is a walk_stmt_info structure. Of note here is a TSI that
511 points to the current statement being walked. The VAL_ONLY flag
512 that indicates whether the *TP being examined may be replaced
513 with something that matches is_gimple_val (if true) or something
514 slightly more complicated (if false). "Something" technically
515 means the common subset of is_gimple_lvalue and is_gimple_rhs,
516 but we never try to form anything more complicated than that, so
517 we don't bother checking. */
519 struct walk_stmt_info
521 walk_tree_fn callback
;
522 tree_stmt_iterator tsi
;
523 struct nesting_info
*info
;
527 /* A subroutine of walk_function. Iterate over all sub-statements of *TP. */
530 walk_stmts (struct walk_stmt_info
*wi
, tree
*tp
)
536 switch (TREE_CODE (t
))
540 tree_stmt_iterator i
;
541 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
544 walk_stmts (wi
, tsi_stmt_ptr (i
));
550 walk_tree (&COND_EXPR_COND (t
), wi
->callback
, wi
, NULL
);
551 walk_stmts (wi
, &COND_EXPR_THEN (t
));
552 walk_stmts (wi
, &COND_EXPR_ELSE (t
));
555 walk_stmts (wi
, &CATCH_BODY (t
));
558 walk_stmts (wi
, &EH_FILTER_FAILURE (t
));
561 case TRY_FINALLY_EXPR
:
562 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
563 walk_stmts (wi
, &TREE_OPERAND (t
, 1));
566 walk_stmts (wi
, &BIND_EXPR_BODY (t
));
570 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
574 /* The immediate arguments of a MODIFY_EXPR may use COMPONENT_REF. */
575 wi
->val_only
= false;
576 walk_tree (&TREE_OPERAND (t
, 0), wi
->callback
, wi
, NULL
);
577 wi
->val_only
= false;
578 walk_tree (&TREE_OPERAND (t
, 1), wi
->callback
, wi
, NULL
);
584 walk_tree (tp
, wi
->callback
, wi
, NULL
);
589 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
592 walk_function (walk_tree_fn callback
, struct nesting_info
*info
)
594 struct walk_stmt_info wi
;
596 memset (&wi
, 0, sizeof (wi
));
597 wi
.callback
= callback
;
601 walk_stmts (&wi
, &DECL_SAVED_TREE (info
->context
));
604 /* Similarly for ROOT and all functions nested underneath, depth first. */
607 walk_all_functions (walk_tree_fn callback
, struct nesting_info
*root
)
612 walk_all_functions (callback
, root
->inner
);
613 walk_function (callback
, root
);
620 /* Construct our local datastructure describing the function nesting
621 tree rooted by CGN. */
623 static struct nesting_info
*
624 create_nesting_tree (struct cgraph_node
*cgn
)
626 struct nesting_info
*info
= xcalloc (1, sizeof (*info
));
627 info
->var_map
= htab_create (7, var_map_hash
, var_map_eq
, free
);
628 info
->context
= cgn
->decl
;
630 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
632 struct nesting_info
*sub
= create_nesting_tree (cgn
);
634 sub
->next
= info
->inner
;
641 /* Return an expression computing the static chain for TARGET_CONTEXT
642 from INFO->CONTEXT. Insert any necessary computations before TSI. */
645 get_static_chain (struct nesting_info
*info
, tree target_context
,
646 tree_stmt_iterator
*tsi
)
648 struct nesting_info
*i
;
651 if (info
->context
== target_context
)
653 x
= build_addr (info
->frame_decl
);
657 x
= get_chain_decl (info
);
659 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
661 tree field
= get_chain_field (i
);
663 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
664 x
= build (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
665 x
= init_tmp_var (info
, x
, tsi
);
672 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
673 frame as seen from INFO->CONTEXT. Insert any necessary computations
677 get_frame_field (struct nesting_info
*info
, tree target_context
,
678 tree field
, tree_stmt_iterator
*tsi
)
680 struct nesting_info
*i
;
683 if (info
->context
== target_context
)
685 /* Make sure frame_decl gets created. */
686 (void) get_frame_type (info
);
687 x
= info
->frame_decl
;
691 x
= get_chain_decl (info
);
693 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
695 tree field
= get_chain_field (i
);
697 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
698 x
= build (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
699 x
= init_tmp_var (info
, x
, tsi
);
702 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
705 x
= build (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
709 /* Called via walk_function+walk_tree, rewrite all references to VAR
710 and PARM_DECLs that belong to outer functions.
712 The rewrite will involve some number of structure accesses back up
713 the static chain. E.g. for a variable FOO up one nesting level it'll
714 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
715 indirections apply to decls for which use_pointer_in_frame is true. */
718 convert_nonlocal_reference (tree
*tp
, int *walk_subtrees
, void *data
)
720 struct walk_stmt_info
*wi
= data
;
721 struct nesting_info
*info
= wi
->info
;
725 switch (TREE_CODE (t
))
728 /* Non-automatic variables are never processed. */
729 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
734 if (decl_function_context (t
) != info
->context
)
736 tree target_context
= decl_function_context (t
);
737 struct nesting_info
*i
;
740 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
742 x
= lookup_field_for_decl (i
, t
, INSERT
);
743 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
744 if (use_pointer_in_frame (t
))
746 x
= init_tmp_var (info
, x
, &wi
->tsi
);
747 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
750 x
= init_tmp_var (info
, x
, &wi
->tsi
);
757 /* Don't walk non-local gotos for now. */
758 if (TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
)
766 /* We're taking the address of a label from a parent function, but
767 this is not itself a non-local goto. Mark the label such that it
768 will not be deleted, much as we would with a label address in
770 if (decl_function_context (t
) != info
->context
)
771 FORCED_LABEL (t
) = 1;
776 bool save_val_only
= wi
->val_only
;
777 tree save_sub
= TREE_OPERAND (t
, 0);
779 wi
->val_only
= false;
780 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference
, wi
, NULL
);
783 if (save_sub
!= TREE_OPERAND (t
, 0))
785 /* If we changed anything, then TREE_INVARIANT is be wrong,
786 since we're no longer directly referencing a decl. */
787 TREE_INVARIANT (t
) = 0;
789 /* If the callback converted the address argument in a context
790 where we only accept variables (and min_invariant, presumably),
791 then compute the address into a temporary. */
793 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
802 case ARRAY_RANGE_REF
:
804 /* Go down this entire nest and just look at the final prefix and
805 anything that describes the references. Otherwise, we lose track
806 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
808 for (; handled_component_p (t
)
809 || TREE_CODE (t
) == REALPART_EXPR
|| TREE_CODE (t
) == IMAGPART_EXPR
;
810 tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
812 if (TREE_CODE (t
) == COMPONENT_REF
)
813 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
815 else if (TREE_CODE (t
) == ARRAY_REF
816 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
818 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
820 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
822 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference
, wi
,
825 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
827 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
829 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
833 wi
->val_only
= false;
834 walk_tree (tp
, convert_nonlocal_reference
, wi
, NULL
);
838 if (!DECL_P (t
) && !TYPE_P (t
))
849 /* Called via walk_function+walk_tree, rewrite all references to VAR
850 and PARM_DECLs that were referenced by inner nested functions.
851 The rewrite will be a structure reference to the local frame variable. */
854 convert_local_reference (tree
*tp
, int *walk_subtrees
, void *data
)
856 struct walk_stmt_info
*wi
= data
;
857 struct nesting_info
*info
= wi
->info
;
858 tree t
= *tp
, field
, x
, y
;
860 switch (TREE_CODE (t
))
863 /* Non-automatic variables are never processed. */
864 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
869 if (decl_function_context (t
) == info
->context
)
871 /* If we copied a pointer to the frame, then the original decl
872 is used unchanged in the parent function. */
873 if (use_pointer_in_frame (t
))
876 /* No need to transform anything if no child references the
878 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
882 x
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
884 x
= init_tmp_var (info
, x
, &wi
->tsi
);
891 bool save_val_only
= wi
->val_only
;
892 tree save_sub
= TREE_OPERAND (t
, 0);
894 wi
->val_only
= false;
895 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference
, wi
, NULL
);
896 wi
->val_only
= save_val_only
;
898 /* If we converted anything ... */
899 if (TREE_OPERAND (t
, 0) != save_sub
)
901 /* Then the frame decl is now addressable. */
902 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
904 /* If we are in a context where we only accept values, then
905 compute the address into a temporary. */
907 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
915 /* Ready for some fun? We need to recognize
916 __builtin_stack_alloc (&x, n)
919 after that. X should have use_pointer_in_frame set. We can't
920 do this any earlier, since we can't meaningfully evaluate &x. */
922 x
= get_callee_fndecl (t
);
923 if (!x
|| DECL_BUILT_IN_CLASS (x
) != BUILT_IN_NORMAL
)
925 if (DECL_FUNCTION_CODE (x
) != BUILT_IN_STACK_ALLOC
)
927 t
= TREE_VALUE (TREE_OPERAND (t
, 1));
928 if (TREE_CODE (t
) != ADDR_EXPR
)
930 t
= TREE_OPERAND (t
, 0);
931 if (TREE_CODE (t
) != VAR_DECL
)
933 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
936 if (!use_pointer_in_frame (t
))
940 y
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
941 x
= build (MODIFY_EXPR
, void_type_node
, y
, x
);
942 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
943 tsi_link_after (&wi
->tsi
, x
, TSI_SAME_STMT
);
950 case ARRAY_RANGE_REF
:
952 /* Go down this entire nest and just look at the final prefix and
953 anything that describes the references. Otherwise, we lose track
954 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
956 for (; handled_component_p (t
)
957 || TREE_CODE (t
) == REALPART_EXPR
|| TREE_CODE (t
) == IMAGPART_EXPR
;
958 tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
960 if (TREE_CODE (t
) == COMPONENT_REF
)
961 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
963 else if (TREE_CODE (t
) == ARRAY_REF
964 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
966 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
968 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
970 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference
, wi
,
973 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
975 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
977 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
981 wi
->val_only
= false;
982 walk_tree (tp
, convert_local_reference
, wi
, NULL
);
986 if (!DECL_P (t
) && !TYPE_P (t
))
997 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
998 reference labels from outer functions. The rewrite will be a
999 call to __builtin_nonlocal_goto. */
1002 convert_nl_goto_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1004 struct walk_stmt_info
*wi
= data
;
1005 struct nesting_info
*info
= wi
->info
, *i
;
1006 tree t
= *tp
, label
, new_label
, target_context
, x
, arg
, field
;
1007 struct var_map_elt
*elt
;
1011 if (TREE_CODE (t
) != GOTO_EXPR
)
1013 label
= GOTO_DESTINATION (t
);
1014 if (TREE_CODE (label
) != LABEL_DECL
)
1016 target_context
= decl_function_context (label
);
1017 if (target_context
== info
->context
)
1020 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1023 /* The original user label may also be use for a normal goto, therefore
1024 we must create a new label that will actually receive the abnormal
1025 control transfer. This new label will be marked LABEL_NONLOCAL; this
1026 mark will trigger proper behavior in the cfg, as well as cause the
1027 (hairy target-specific) non-local goto receiver code to be generated
1028 when we expand rtl. */
1029 new_label
= create_artificial_label ();
1030 DECL_NONLOCAL (new_label
) = 1;
1032 /* Enter this association into var_map so that we can insert the new
1033 label into the IL during a second pass. */
1034 elt
= xmalloc (sizeof (*elt
));
1036 elt
->new = new_label
;
1037 slot
= htab_find_slot (i
->var_map
, elt
, INSERT
);
1040 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1041 field
= get_nl_goto_field (i
);
1042 x
= get_frame_field (info
, target_context
, field
, &wi
->tsi
);
1044 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1045 arg
= tree_cons (NULL
, x
, NULL
);
1046 x
= build_addr (new_label
);
1047 arg
= tree_cons (NULL
, x
, arg
);
1048 x
= implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
];
1049 x
= build_function_call_expr (x
, arg
);
1051 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
1052 *tsi_stmt_ptr (wi
->tsi
) = x
;
1057 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1058 are referenced via nonlocal goto from a nested function. The rewrite
1059 will involve installing a newly generated DECL_NONLOCAL label, and
1060 (potentially) a branch around the rtl gunk that is assumed to be
1061 attached to such a label. */
1064 convert_nl_goto_receiver (tree
*tp
, int *walk_subtrees
, void *data
)
1066 struct walk_stmt_info
*wi
= data
;
1067 struct nesting_info
*info
= wi
->info
;
1068 tree t
= *tp
, label
, new_label
, x
;
1069 struct var_map_elt
*elt
, dummy
;
1070 tree_stmt_iterator tmp_tsi
;
1073 if (TREE_CODE (t
) != LABEL_EXPR
)
1075 label
= LABEL_EXPR_LABEL (t
);
1078 elt
= htab_find (info
->var_map
, &dummy
);
1081 new_label
= elt
->new;
1083 /* If there's any possibility that the previous statement falls through,
1084 then we must branch around the new non-local label. */
1086 tsi_prev (&tmp_tsi
);
1087 if (tsi_end_p (tmp_tsi
) || block_may_fallthru (tsi_stmt (tmp_tsi
)))
1089 x
= build1 (GOTO_EXPR
, void_type_node
, label
);
1090 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1092 x
= build1 (LABEL_EXPR
, void_type_node
, new_label
);
1093 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1098 /* Called via walk_function+walk_tree, rewrite all references to addresses
1099 of nested functions that require the use of trampolines. The rewrite
1100 will involve a reference a trampoline generated for the occasion. */
1103 convert_tramp_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1105 struct walk_stmt_info
*wi
= data
;
1106 struct nesting_info
*info
= wi
->info
, *i
;
1107 tree t
= *tp
, decl
, target_context
, x
, arg
;
1110 switch (TREE_CODE (t
))
1114 T.1 = &CHAIN->tramp;
1115 T.2 = __builtin_adjust_trampoline (T.1);
1116 T.3 = (func_type)T.2;
1119 decl
= TREE_OPERAND (t
, 0);
1120 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1123 /* Only need to process nested functions. */
1124 target_context
= decl_function_context (decl
);
1125 if (!target_context
)
1128 /* If the nested function doesn't use a static chain, then
1129 it doesn't need a trampoline. */
1130 if (DECL_NO_STATIC_CHAIN (decl
))
1133 /* Lookup the immediate parent of the callee, as that's where
1134 we need to insert the trampoline. */
1135 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1137 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1139 /* Compute the address of the field holding the trampoline. */
1140 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
1142 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1143 arg
= tree_cons (NULL
, x
, NULL
);
1145 /* Do machine-specific ugliness. Normally this will involve
1146 computing extra alignment, but it can really be anything. */
1147 x
= implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
];
1148 x
= build_function_call_expr (x
, arg
);
1149 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1151 /* Cast back to the proper function type. */
1152 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1153 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1159 /* Only walk call arguments, lest we generate trampolines for
1161 walk_tree (&TREE_OPERAND (t
, 1), convert_tramp_reference
, wi
, NULL
);
1165 if (!DECL_P (t
) && !TYPE_P (t
))
1173 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1174 reference nested functions to make sure that the static chain is
1175 set up properly for the call. */
1178 convert_call_expr (tree
*tp
, int *walk_subtrees
, void *data
)
1180 struct walk_stmt_info
*wi
= data
;
1181 struct nesting_info
*info
= wi
->info
;
1182 tree t
= *tp
, decl
, target_context
;
1185 switch (TREE_CODE (t
))
1188 decl
= get_callee_fndecl (t
);
1191 target_context
= decl_function_context (decl
);
1192 if (target_context
&& !DECL_NO_STATIC_CHAIN (decl
))
1194 = get_static_chain (info
, target_context
, &wi
->tsi
);
1199 case WITH_SIZE_EXPR
:
1200 /* Only return modify and with_size_expr may contain calls. */
1211 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1212 trampolines and call expressions. On the way back up, determine if
1213 a nested function actually uses its static chain; if not, remember that. */
1216 convert_all_function_calls (struct nesting_info
*root
)
1221 convert_all_function_calls (root
->inner
);
1223 walk_function (convert_tramp_reference
, root
);
1224 walk_function (convert_call_expr
, root
);
1226 /* If the function does not use a static chain, then remember that. */
1227 if (root
->outer
&& !root
->chain_decl
&& !root
->chain_field
)
1228 DECL_NO_STATIC_CHAIN (root
->context
) = 1;
1231 #ifdef ENABLE_CHECKING
1232 if (DECL_NO_STATIC_CHAIN (root
->context
))
1242 /* Do "everything else" to clean up or complete state collected by the
1243 various walking passes -- lay out the types and decls, generate code
1244 to initialize the frame decl, store critical expressions in the
1245 struct function for rtl to find. */
1248 finalize_nesting_tree_1 (struct nesting_info
*root
)
1250 tree stmt_list
= NULL
;
1251 tree context
= root
->context
;
1252 struct function
*sf
;
1254 /* If we created a non-local frame type or decl, we need to lay them
1255 out at this time. */
1256 if (root
->frame_type
)
1258 layout_type (root
->frame_type
);
1259 layout_decl (root
->frame_decl
, 0);
1262 /* If any parameters were referenced non-locally, then we need to
1263 insert a copy. Likewise, if any variables were referenced by
1264 pointer, we need to initialize the address. */
1265 if (root
->any_parm_remapped
)
1268 for (p
= DECL_ARGUMENTS (context
); p
; p
= TREE_CHAIN (p
))
1272 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
1276 if (use_pointer_in_frame (p
))
1281 y
= build (COMPONENT_REF
, TREE_TYPE (field
),
1282 root
->frame_decl
, field
, NULL_TREE
);
1283 x
= build (MODIFY_EXPR
, TREE_TYPE (field
), y
, x
);
1284 append_to_statement_list (x
, &stmt_list
);
1288 /* If a chain_field was created, then it needs to be initialized
1290 if (root
->chain_field
)
1292 tree x
= build (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
1293 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
1294 x
= build (MODIFY_EXPR
, TREE_TYPE (x
), x
, get_chain_decl (root
));
1295 append_to_statement_list (x
, &stmt_list
);
1298 /* If trampolines were created, then we need to initialize them. */
1299 if (root
->any_tramp_created
)
1301 struct nesting_info
*i
;
1302 for (i
= root
->inner
; i
; i
= i
->next
)
1306 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
1310 if (DECL_NO_STATIC_CHAIN (i
->context
))
1311 x
= null_pointer_node
;
1313 x
= build_addr (root
->frame_decl
);
1314 arg
= tree_cons (NULL
, x
, NULL
);
1316 x
= build_addr (i
->context
);
1317 arg
= tree_cons (NULL
, x
, arg
);
1319 x
= build (COMPONENT_REF
, TREE_TYPE (field
),
1320 root
->frame_decl
, field
, NULL_TREE
);
1322 arg
= tree_cons (NULL
, x
, arg
);
1324 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
1325 x
= build_function_call_expr (x
, arg
);
1327 append_to_statement_list (x
, &stmt_list
);
1331 /* If we created initialization statements, insert them. */
1334 annotate_all_with_locus (&stmt_list
,
1335 DECL_SOURCE_LOCATION (context
));
1336 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context
)),
1338 BIND_EXPR_BODY (DECL_SAVED_TREE (context
)) = stmt_list
;
1341 /* If a chain_decl was created, then it needs to be registered with
1342 struct function so that it gets initialized from the static chain
1343 register at the beginning of the function. */
1344 sf
= DECL_STRUCT_FUNCTION (root
->context
);
1345 sf
->static_chain_decl
= root
->chain_decl
;
1347 /* Similarly for the non-local goto save area. */
1348 if (root
->nl_goto_field
)
1350 sf
->nonlocal_goto_save_area
1351 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
1352 sf
->has_nonlocal_label
= 1;
1355 /* Make sure all new local variables get inserted into the
1356 proper BIND_EXPR. */
1357 if (root
->new_local_var_chain
)
1358 declare_tmp_vars (root
->new_local_var_chain
,
1359 DECL_SAVED_TREE (root
->context
));
1361 /* Dump the translated tree function. */
1362 dump_function (TDI_nested
, root
->context
);
1366 finalize_nesting_tree (struct nesting_info
*root
)
1371 finalize_nesting_tree (root
->inner
);
1372 finalize_nesting_tree_1 (root
);
1378 /* Free the data structures allocated during this pass. */
1381 free_nesting_tree (struct nesting_info
*root
)
1383 struct nesting_info
*next
;
1387 free_nesting_tree (root
->inner
);
1388 htab_delete (root
->var_map
);
1396 /* Main entry point for this pass. Process FNDECL and all of its nested
1397 subroutines and turn them into something less tightly bound. */
1400 lower_nested_functions (tree fndecl
)
1402 struct nesting_info
*root
;
1403 struct cgraph_node
*cgn
;
1405 /* If there are no nested functions, there's nothing to do. */
1406 cgn
= cgraph_node (fndecl
);
1410 root
= create_nesting_tree (cgn
);
1411 walk_all_functions (convert_nonlocal_reference
, root
);
1412 walk_all_functions (convert_local_reference
, root
);
1413 walk_all_functions (convert_nl_goto_reference
, root
);
1414 walk_all_functions (convert_nl_goto_receiver
, root
);
1415 convert_all_function_calls (root
);
1416 finalize_nesting_tree (root
);
1417 free_nesting_tree (root
);
1420 #include "gt-tree-nested.h"