* cp-tree.h (note_decl_for_pch): New function.
[official-gcc.git] / gcc / tree-nested.c
blob70b377724706087733faf66cf02ace038dfeffd8
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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "function.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"
34 #include "cgraph.h"
35 #include "expr.h"
36 #include "langhooks.h"
37 #include "ggc.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
66 inlinable.
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
82 tree old;
83 tree new;
86 struct nesting_info
88 struct nesting_info *outer;
89 struct nesting_info *inner;
90 struct nesting_info *next;
92 htab_t var_map;
93 tree context;
94 tree new_local_var_chain;
95 tree frame_type;
96 tree frame_decl;
97 tree chain_field;
98 tree chain_decl;
99 tree nl_goto_field;
101 bool any_parm_remapped;
102 bool any_tramp_created;
106 /* Hashing and equality functions for nesting_info->var_map. */
108 static hashval_t
109 var_map_hash (const void *x)
111 const struct var_map_elt *a = x;
112 return htab_hash_pointer (a->old);
115 static int
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. */
130 static tree
131 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
133 tree tmp_var;
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 info->new_local_var_chain = tmp_var;
148 return tmp_var;
151 /* Take the address of EXP. Mark it for addressability as necessary. */
153 tree
154 build_addr (tree exp)
156 tree base = exp;
158 while (handled_component_p (base))
159 base = TREE_OPERAND (base, 0);
161 if (DECL_P (base))
162 TREE_ADDRESSABLE (base) = 1;
164 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
167 /* Insert FIELD into TYPE, sorted by alignment requirements. */
169 static void
170 insert_field_into_struct (tree type, tree field)
172 tree *p;
174 DECL_CONTEXT (field) = type;
176 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
177 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
178 break;
180 TREE_CHAIN (field) = *p;
181 *p = field;
184 /* Build or return the RECORD_TYPE that describes the frame state that is
185 shared between INFO->CONTEXT and its nested functions. This record will
186 not be complete until finalize_nesting_tree; up until that point we'll
187 be adding fields as necessary.
189 We also build the DECL that represents this frame in the function. */
191 static tree
192 get_frame_type (struct nesting_info *info)
194 tree type = info->frame_type;
195 if (!type)
197 char *name;
199 type = make_node (RECORD_TYPE);
201 name = concat ("FRAME.",
202 IDENTIFIER_POINTER (DECL_NAME (info->context)),
203 NULL);
204 TYPE_NAME (type) = get_identifier (name);
205 free (name);
207 info->frame_type = type;
208 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
210 return type;
213 /* Return true if DECL should be referenced by pointer in the non-local
214 frame structure. */
216 static bool
217 use_pointer_in_frame (tree decl)
219 if (TREE_CODE (decl) == PARM_DECL)
221 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
222 sized decls, and inefficient to copy large aggregates. Don't bother
223 moving anything but scalar variables. */
224 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
226 else
228 /* Variable sized types make things "interesting" in the frame. */
229 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
233 /* Given DECL, a non-locally accessed variable, find or create a field
234 in the non-local frame structure for the given nesting context. */
236 static tree
237 lookup_field_for_decl (struct nesting_info *info, tree decl,
238 enum insert_option insert)
240 struct var_map_elt *elt, dummy;
241 void **slot;
242 tree field;
244 dummy.old = decl;
245 slot = htab_find_slot (info->var_map, &dummy, insert);
246 if (!slot)
248 gcc_assert (insert != INSERT);
249 return NULL;
251 elt = *slot;
253 if (!elt && insert == INSERT)
255 field = make_node (FIELD_DECL);
256 DECL_NAME (field) = DECL_NAME (decl);
258 if (use_pointer_in_frame (decl))
260 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
261 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
262 DECL_NONADDRESSABLE_P (field) = 1;
264 else
266 TREE_TYPE (field) = TREE_TYPE (decl);
267 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
268 DECL_ALIGN (field) = DECL_ALIGN (decl);
269 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
270 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
271 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
272 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
275 insert_field_into_struct (get_frame_type (info), field);
277 elt = xmalloc (sizeof (*elt));
278 elt->old = decl;
279 elt->new = field;
280 *slot = elt;
282 if (TREE_CODE (decl) == PARM_DECL)
283 info->any_parm_remapped = true;
285 else
286 field = elt ? elt->new : NULL;
288 return field;
291 /* Build or return the variable that holds the static chain within
292 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
294 static tree
295 get_chain_decl (struct nesting_info *info)
297 tree decl = info->chain_decl;
298 if (!decl)
300 tree type;
302 type = get_frame_type (info->outer);
303 type = build_pointer_type (type);
305 /* Note that this variable is *not* entered into any BIND_EXPR;
306 the construction of this variable is handled specially in
307 expand_function_start and initialize_inlined_parameters.
308 Note also that it's represented as a parameter. This is more
309 close to the truth, since the initial value does come from
310 the caller. */
311 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
312 DECL_ARTIFICIAL (decl) = 1;
313 DECL_IGNORED_P (decl) = 1;
314 TREE_USED (decl) = 1;
315 DECL_CONTEXT (decl) = info->context;
316 DECL_ARG_TYPE (decl) = type;
318 /* Tell tree-inline.c that we never write to this variable, so
319 it can copy-prop the replacement value immediately. */
320 TREE_READONLY (decl) = 1;
322 info->chain_decl = decl;
324 return decl;
327 /* Build or return the field within the non-local frame state that holds
328 the static chain for INFO->CONTEXT. This is the way to walk back up
329 multiple nesting levels. */
331 static tree
332 get_chain_field (struct nesting_info *info)
334 tree field = info->chain_field;
335 if (!field)
337 tree type = build_pointer_type (get_frame_type (info->outer));
339 field = make_node (FIELD_DECL);
340 DECL_NAME (field) = get_identifier ("__chain");
341 TREE_TYPE (field) = type;
342 DECL_ALIGN (field) = TYPE_ALIGN (type);
343 DECL_NONADDRESSABLE_P (field) = 1;
345 insert_field_into_struct (get_frame_type (info), field);
347 info->chain_field = field;
349 return field;
352 /* Copy EXP into a temporary. Allocate the temporary in the context of
353 INFO and insert the initialization statement before TSI. */
355 static tree
356 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
358 tree t, stmt;
360 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
361 stmt = build (MODIFY_EXPR, TREE_TYPE (t), t, exp);
362 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
363 tsi_link_before (tsi, stmt, TSI_SAME_STMT);
365 return t;
368 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
370 static tree
371 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
373 if (is_gimple_val (exp))
374 return exp;
375 else
376 return init_tmp_var (info, exp, tsi);
379 /* Similarly, but copy from the temporary and insert the statement
380 after the iterator. */
382 static tree
383 save_tmp_var (struct nesting_info *info, tree exp,
384 tree_stmt_iterator *tsi)
386 tree t, stmt;
388 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
389 stmt = build (MODIFY_EXPR, TREE_TYPE (t), exp, t);
390 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
391 tsi_link_after (tsi, stmt, TSI_SAME_STMT);
393 return t;
396 /* Build or return the type used to represent a nested function trampoline. */
398 static GTY(()) tree trampoline_type;
400 static tree
401 get_trampoline_type (void)
403 tree record, t;
404 unsigned align, size;
406 if (trampoline_type)
407 return trampoline_type;
409 align = TRAMPOLINE_ALIGNMENT;
410 size = TRAMPOLINE_SIZE;
412 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
413 then allocate extra space so that we can do dynamic alignment. */
414 if (align > STACK_BOUNDARY)
416 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
417 align = STACK_BOUNDARY;
420 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
421 t = build_array_type (char_type_node, t);
422 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
423 DECL_ALIGN (t) = align;
424 DECL_USER_ALIGN (t) = 1;
426 record = make_node (RECORD_TYPE);
427 TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
428 TYPE_FIELDS (record) = t;
429 layout_type (record);
431 return record;
434 /* Given DECL, a nested function, find or create a field in the non-local
435 frame structure for a trampoline for this function. */
437 static tree
438 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
439 enum insert_option insert)
441 struct var_map_elt *elt, dummy;
442 void **slot;
443 tree field;
445 dummy.old = decl;
446 slot = htab_find_slot (info->var_map, &dummy, insert);
447 if (!slot)
449 gcc_assert (insert != INSERT);
450 return NULL;
452 elt = *slot;
454 if (!elt && insert == INSERT)
456 field = make_node (FIELD_DECL);
457 DECL_NAME (field) = DECL_NAME (decl);
458 TREE_TYPE (field) = get_trampoline_type ();
459 TREE_ADDRESSABLE (field) = 1;
461 insert_field_into_struct (get_frame_type (info), field);
463 elt = xmalloc (sizeof (*elt));
464 elt->old = decl;
465 elt->new = field;
466 *slot = elt;
468 info->any_tramp_created = true;
470 else
471 field = elt ? elt->new : NULL;
473 return field;
476 /* Build or return the field within the non-local frame state that holds
477 the non-local goto "jmp_buf". The buffer itself is maintained by the
478 rtl middle-end as dynamic stack space is allocated. */
480 static tree
481 get_nl_goto_field (struct nesting_info *info)
483 tree field = info->nl_goto_field;
484 if (!field)
486 unsigned size;
487 tree type;
489 /* For __builtin_nonlocal_goto, we need N words. The first is the
490 frame pointer, the rest is for the target's stack pointer save
491 area. The number of words is controlled by STACK_SAVEAREA_MODE;
492 not the best interface, but it'll do for now. */
493 if (Pmode == ptr_mode)
494 type = ptr_type_node;
495 else
496 type = lang_hooks.types.type_for_mode (Pmode, 1);
498 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
499 size = size / GET_MODE_SIZE (Pmode);
500 size = size + 1;
502 type = build_array_type
503 (type, build_index_type (build_int_cst (NULL_TREE, size)));
505 field = make_node (FIELD_DECL);
506 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
507 TREE_TYPE (field) = type;
508 DECL_ALIGN (field) = TYPE_ALIGN (type);
509 TREE_ADDRESSABLE (field) = 1;
511 insert_field_into_struct (get_frame_type (info), field);
513 info->nl_goto_field = field;
516 return field;
519 /* Convenience routines to walk all statements of a gimple function.
521 For each statement, we invoke CALLBACK via walk_tree. The passed
522 data is a walk_stmt_info structure. Of note here is a TSI that
523 points to the current statement being walked. The VAL_ONLY flag
524 that indicates whether the *TP being examined may be replaced
525 with something that matches is_gimple_val (if true) or something
526 slightly more complicated (if false). "Something" technically
527 means the common subset of is_gimple_lvalue and is_gimple_rhs,
528 but we never try to form anything more complicated than that, so
529 we don't bother checking. */
531 struct walk_stmt_info
533 walk_tree_fn callback;
534 tree_stmt_iterator tsi;
535 struct nesting_info *info;
536 bool val_only;
537 bool is_lhs;
538 bool changed;
541 /* A subroutine of walk_function. Iterate over all sub-statements of *TP. */
543 static void
544 walk_stmts (struct walk_stmt_info *wi, tree *tp)
546 tree t = *tp;
547 if (!t)
548 return;
550 switch (TREE_CODE (t))
552 case STATEMENT_LIST:
554 tree_stmt_iterator i;
555 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
557 wi->tsi = i;
558 walk_stmts (wi, tsi_stmt_ptr (i));
561 break;
563 case COND_EXPR:
564 walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
565 walk_stmts (wi, &COND_EXPR_THEN (t));
566 walk_stmts (wi, &COND_EXPR_ELSE (t));
567 break;
568 case CATCH_EXPR:
569 walk_stmts (wi, &CATCH_BODY (t));
570 break;
571 case EH_FILTER_EXPR:
572 walk_stmts (wi, &EH_FILTER_FAILURE (t));
573 break;
574 case TRY_CATCH_EXPR:
575 case TRY_FINALLY_EXPR:
576 walk_stmts (wi, &TREE_OPERAND (t, 0));
577 walk_stmts (wi, &TREE_OPERAND (t, 1));
578 break;
579 case BIND_EXPR:
580 walk_stmts (wi, &BIND_EXPR_BODY (t));
581 break;
583 case RETURN_EXPR:
584 walk_stmts (wi, &TREE_OPERAND (t, 0));
585 break;
587 case MODIFY_EXPR:
588 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
589 wi->val_only = !is_gimple_formal_tmp_var (TREE_OPERAND (t, 0));
590 walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
592 /* If the rhs is appropriate for a memory, we may use a
593 COMPONENT_REF on the lhs. */
594 wi->val_only = !is_gimple_mem_rhs (TREE_OPERAND (t, 1));
595 wi->is_lhs = true;
596 walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
598 wi->val_only = true;
599 wi->is_lhs = false;
600 break;
602 default:
603 wi->val_only = true;
604 walk_tree (tp, wi->callback, wi, NULL);
605 break;
609 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
611 static void
612 walk_function (walk_tree_fn callback, struct nesting_info *info)
614 struct walk_stmt_info wi;
616 memset (&wi, 0, sizeof (wi));
617 wi.callback = callback;
618 wi.info = info;
619 wi.val_only = true;
621 walk_stmts (&wi, &DECL_SAVED_TREE (info->context));
624 /* Similarly for ROOT and all functions nested underneath, depth first. */
626 static void
627 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
631 if (root->inner)
632 walk_all_functions (callback, root->inner);
633 walk_function (callback, root);
634 root = root->next;
636 while (root);
639 /* We have to check for a fairly pathalogical case. The operands of function
640 nested function are to be interpreted in the context of the enclosing
641 function. So if any are variably-sized, they will get remapped when the
642 enclosing function is inlined. But that remapping would also have to be
643 done in the types of the PARM_DECLs of the nested function, meaning the
644 argument types of that function will disagree with the arguments in the
645 calls to that function. So we'd either have to make a copy of the nested
646 function corresponding to each time the enclosing function was inlined or
647 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
648 function. The former is not practical. The latter would still require
649 detecting this case to know when to add the conversions. So, for now at
650 least, we don't inline such an enclosing function.
652 We have to do that check recursively, so here return indicating whether
653 FNDECL has such a nested function. ORIG_FN is the function we were
654 trying to inline to use for checking whether any argument is variably
655 modified by anything in it.
657 It would be better to do this in tree-inline.c so that we could give
658 the appropriate warning for why a function can't be inlined, but that's
659 too late since the nesting structure has already been flattened and
660 adding a flag just to record this fact seems a waste of a flag. */
662 static bool
663 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
665 struct cgraph_node *cgn = cgraph_node (fndecl);
666 tree arg;
668 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
670 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
671 if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
672 return true;
674 if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
675 return true;
678 return false;
681 /* Construct our local datastructure describing the function nesting
682 tree rooted by CGN. */
684 static struct nesting_info *
685 create_nesting_tree (struct cgraph_node *cgn)
687 struct nesting_info *info = xcalloc (1, sizeof (*info));
688 info->var_map = htab_create (7, var_map_hash, var_map_eq, free);
689 info->context = cgn->decl;
691 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
693 struct nesting_info *sub = create_nesting_tree (cgn);
694 sub->outer = info;
695 sub->next = info->inner;
696 info->inner = sub;
699 /* See discussion at check_for_nested_with_variably_modified for a
700 discussion of why this has to be here. */
701 if (check_for_nested_with_variably_modified (info->context, info->context))
702 DECL_UNINLINABLE (info->context) = true;
704 return info;
707 /* Return an expression computing the static chain for TARGET_CONTEXT
708 from INFO->CONTEXT. Insert any necessary computations before TSI. */
710 static tree
711 get_static_chain (struct nesting_info *info, tree target_context,
712 tree_stmt_iterator *tsi)
714 struct nesting_info *i;
715 tree x;
717 if (info->context == target_context)
719 x = build_addr (info->frame_decl);
721 else
723 x = get_chain_decl (info);
725 for (i = info->outer; i->context != target_context; i = i->outer)
727 tree field = get_chain_field (i);
729 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
730 x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
731 x = init_tmp_var (info, x, tsi);
735 return x;
738 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
739 frame as seen from INFO->CONTEXT. Insert any necessary computations
740 before TSI. */
742 static tree
743 get_frame_field (struct nesting_info *info, tree target_context,
744 tree field, tree_stmt_iterator *tsi)
746 struct nesting_info *i;
747 tree x;
749 if (info->context == target_context)
751 /* Make sure frame_decl gets created. */
752 (void) get_frame_type (info);
753 x = info->frame_decl;
755 else
757 x = get_chain_decl (info);
759 for (i = info->outer; i->context != target_context; i = i->outer)
761 tree field = get_chain_field (i);
763 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
764 x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
765 x = init_tmp_var (info, x, tsi);
768 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
771 x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
772 return x;
775 /* Called via walk_function+walk_tree, rewrite all references to VAR
776 and PARM_DECLs that belong to outer functions.
778 The rewrite will involve some number of structure accesses back up
779 the static chain. E.g. for a variable FOO up one nesting level it'll
780 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
781 indirections apply to decls for which use_pointer_in_frame is true. */
783 static tree
784 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
786 struct walk_stmt_info *wi = data;
787 struct nesting_info *info = wi->info;
788 tree t = *tp;
790 *walk_subtrees = 0;
791 switch (TREE_CODE (t))
793 case VAR_DECL:
794 /* Non-automatic variables are never processed. */
795 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
796 break;
797 /* FALLTHRU */
799 case PARM_DECL:
800 if (decl_function_context (t) != info->context)
802 tree target_context = decl_function_context (t);
803 struct nesting_info *i;
804 tree x;
805 wi->changed = true;
807 for (i = info->outer; i->context != target_context; i = i->outer)
808 continue;
809 x = lookup_field_for_decl (i, t, INSERT);
810 x = get_frame_field (info, target_context, x, &wi->tsi);
811 if (use_pointer_in_frame (t))
813 x = init_tmp_var (info, x, &wi->tsi);
814 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
817 if (wi->val_only)
819 if (wi->is_lhs)
820 x = save_tmp_var (info, x, &wi->tsi);
821 else
822 x = init_tmp_var (info, x, &wi->tsi);
825 *tp = x;
827 break;
829 case GOTO_EXPR:
830 /* Don't walk non-local gotos for now. */
831 if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
833 *walk_subtrees = 1;
834 wi->val_only = true;
835 wi->is_lhs = false;
837 break;
839 case LABEL_DECL:
840 /* We're taking the address of a label from a parent function, but
841 this is not itself a non-local goto. Mark the label such that it
842 will not be deleted, much as we would with a label address in
843 static storage. */
844 if (decl_function_context (t) != info->context)
845 FORCED_LABEL (t) = 1;
846 break;
848 case ADDR_EXPR:
850 bool save_val_only = wi->val_only;
852 wi->val_only = false;
853 wi->is_lhs = false;
854 wi->changed = false;
855 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
856 wi->val_only = true;
858 if (wi->changed)
860 /* If we changed anything, then TREE_INVARIANT is be wrong,
861 since we're no longer directly referencing a decl. */
862 recompute_tree_invarant_for_addr_expr (t);
864 /* If the callback converted the address argument in a context
865 where we only accept variables (and min_invariant, presumably),
866 then compute the address into a temporary. */
867 if (save_val_only)
868 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
871 break;
873 case REALPART_EXPR:
874 case IMAGPART_EXPR:
875 case COMPONENT_REF:
876 case ARRAY_REF:
877 case ARRAY_RANGE_REF:
878 case BIT_FIELD_REF:
879 /* Go down this entire nest and just look at the final prefix and
880 anything that describes the references. Otherwise, we lose track
881 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
882 wi->val_only = true;
883 wi->is_lhs = false;
884 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
886 if (TREE_CODE (t) == COMPONENT_REF)
887 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
888 NULL);
889 else if (TREE_CODE (t) == ARRAY_REF
890 || TREE_CODE (t) == ARRAY_RANGE_REF)
892 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
893 NULL);
894 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
895 NULL);
896 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
897 NULL);
899 else if (TREE_CODE (t) == BIT_FIELD_REF)
901 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
902 NULL);
903 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
904 NULL);
907 wi->val_only = false;
908 walk_tree (tp, convert_nonlocal_reference, wi, NULL);
909 break;
911 default:
912 if (!IS_TYPE_OR_DECL_P (t))
914 *walk_subtrees = 1;
915 wi->val_only = true;
916 wi->is_lhs = false;
918 break;
921 return NULL_TREE;
924 /* Called via walk_function+walk_tree, rewrite all references to VAR
925 and PARM_DECLs that were referenced by inner nested functions.
926 The rewrite will be a structure reference to the local frame variable. */
928 static tree
929 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
931 struct walk_stmt_info *wi = data;
932 struct nesting_info *info = wi->info;
933 tree t = *tp, field, x;
935 switch (TREE_CODE (t))
937 case VAR_DECL:
938 /* Non-automatic variables are never processed. */
939 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
940 break;
941 /* FALLTHRU */
943 case PARM_DECL:
944 if (decl_function_context (t) == info->context)
946 /* If we copied a pointer to the frame, then the original decl
947 is used unchanged in the parent function. */
948 if (use_pointer_in_frame (t))
949 break;
951 /* No need to transform anything if no child references the
952 variable. */
953 field = lookup_field_for_decl (info, t, NO_INSERT);
954 if (!field)
955 break;
956 wi->changed = true;
958 x = get_frame_field (info, info->context, field, &wi->tsi);
960 if (wi->val_only)
962 if (wi->is_lhs)
963 x = save_tmp_var (info, x, &wi->tsi);
964 else
965 x = init_tmp_var (info, x, &wi->tsi);
968 *tp = x;
970 break;
972 case ADDR_EXPR:
974 bool save_val_only = wi->val_only;
976 wi->val_only = false;
977 wi->is_lhs = false;
978 wi->changed = false;
979 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
980 wi->val_only = save_val_only;
982 /* If we converted anything ... */
983 if (wi->changed)
985 /* Then the frame decl is now addressable. */
986 TREE_ADDRESSABLE (info->frame_decl) = 1;
988 recompute_tree_invarant_for_addr_expr (t);
990 /* If we are in a context where we only accept values, then
991 compute the address into a temporary. */
992 if (save_val_only)
993 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
996 break;
998 case REALPART_EXPR:
999 case IMAGPART_EXPR:
1000 case COMPONENT_REF:
1001 case ARRAY_REF:
1002 case ARRAY_RANGE_REF:
1003 case BIT_FIELD_REF:
1004 /* Go down this entire nest and just look at the final prefix and
1005 anything that describes the references. Otherwise, we lose track
1006 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1007 wi->val_only = true;
1008 wi->is_lhs = false;
1009 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1011 if (TREE_CODE (t) == COMPONENT_REF)
1012 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1013 NULL);
1014 else if (TREE_CODE (t) == ARRAY_REF
1015 || TREE_CODE (t) == ARRAY_RANGE_REF)
1017 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1018 NULL);
1019 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1020 NULL);
1021 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1022 NULL);
1024 else if (TREE_CODE (t) == BIT_FIELD_REF)
1026 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1027 NULL);
1028 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1029 NULL);
1032 wi->val_only = false;
1033 walk_tree (tp, convert_local_reference, wi, NULL);
1034 break;
1036 default:
1037 if (!IS_TYPE_OR_DECL_P (t))
1039 *walk_subtrees = 1;
1040 wi->val_only = true;
1041 wi->is_lhs = false;
1043 break;
1046 return NULL_TREE;
1049 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1050 reference labels from outer functions. The rewrite will be a
1051 call to __builtin_nonlocal_goto. */
1053 static tree
1054 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1056 struct walk_stmt_info *wi = data;
1057 struct nesting_info *info = wi->info, *i;
1058 tree t = *tp, label, new_label, target_context, x, arg, field;
1059 struct var_map_elt *elt;
1060 void **slot;
1062 *walk_subtrees = 0;
1063 if (TREE_CODE (t) != GOTO_EXPR)
1064 return NULL_TREE;
1065 label = GOTO_DESTINATION (t);
1066 if (TREE_CODE (label) != LABEL_DECL)
1067 return NULL_TREE;
1068 target_context = decl_function_context (label);
1069 if (target_context == info->context)
1070 return NULL_TREE;
1072 for (i = info->outer; target_context != i->context; i = i->outer)
1073 continue;
1075 /* The original user label may also be use for a normal goto, therefore
1076 we must create a new label that will actually receive the abnormal
1077 control transfer. This new label will be marked LABEL_NONLOCAL; this
1078 mark will trigger proper behavior in the cfg, as well as cause the
1079 (hairy target-specific) non-local goto receiver code to be generated
1080 when we expand rtl. */
1081 new_label = create_artificial_label ();
1082 DECL_NONLOCAL (new_label) = 1;
1084 /* Enter this association into var_map so that we can insert the new
1085 label into the IL during a second pass. */
1086 elt = xmalloc (sizeof (*elt));
1087 elt->old = label;
1088 elt->new = new_label;
1089 slot = htab_find_slot (i->var_map, elt, INSERT);
1090 *slot = elt;
1092 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1093 field = get_nl_goto_field (i);
1094 x = get_frame_field (info, target_context, field, &wi->tsi);
1095 x = build_addr (x);
1096 x = tsi_gimplify_val (info, x, &wi->tsi);
1097 arg = tree_cons (NULL, x, NULL);
1098 x = build_addr (new_label);
1099 arg = tree_cons (NULL, x, arg);
1100 x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1101 x = build_function_call_expr (x, arg);
1103 SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1104 *tsi_stmt_ptr (wi->tsi) = x;
1106 return NULL_TREE;
1109 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1110 are referenced via nonlocal goto from a nested function. The rewrite
1111 will involve installing a newly generated DECL_NONLOCAL label, and
1112 (potentially) a branch around the rtl gunk that is assumed to be
1113 attached to such a label. */
1115 static tree
1116 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1118 struct walk_stmt_info *wi = data;
1119 struct nesting_info *info = wi->info;
1120 tree t = *tp, label, new_label, x;
1121 struct var_map_elt *elt, dummy;
1122 tree_stmt_iterator tmp_tsi;
1124 *walk_subtrees = 0;
1125 if (TREE_CODE (t) != LABEL_EXPR)
1126 return NULL_TREE;
1127 label = LABEL_EXPR_LABEL (t);
1129 dummy.old = label;
1130 elt = htab_find (info->var_map, &dummy);
1131 if (!elt)
1132 return NULL_TREE;
1133 new_label = elt->new;
1135 /* If there's any possibility that the previous statement falls through,
1136 then we must branch around the new non-local label. */
1137 tmp_tsi = wi->tsi;
1138 tsi_prev (&tmp_tsi);
1139 if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1141 x = build1 (GOTO_EXPR, void_type_node, label);
1142 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1144 x = build1 (LABEL_EXPR, void_type_node, new_label);
1145 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1147 return NULL_TREE;
1150 /* Called via walk_function+walk_tree, rewrite all references to addresses
1151 of nested functions that require the use of trampolines. The rewrite
1152 will involve a reference a trampoline generated for the occasion. */
1154 static tree
1155 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1157 struct walk_stmt_info *wi = data;
1158 struct nesting_info *info = wi->info, *i;
1159 tree t = *tp, decl, target_context, x, arg;
1161 *walk_subtrees = 0;
1162 switch (TREE_CODE (t))
1164 case ADDR_EXPR:
1165 /* Build
1166 T.1 = &CHAIN->tramp;
1167 T.2 = __builtin_adjust_trampoline (T.1);
1168 T.3 = (func_type)T.2;
1171 decl = TREE_OPERAND (t, 0);
1172 if (TREE_CODE (decl) != FUNCTION_DECL)
1173 break;
1175 /* Only need to process nested functions. */
1176 target_context = decl_function_context (decl);
1177 if (!target_context)
1178 break;
1180 /* If the nested function doesn't use a static chain, then
1181 it doesn't need a trampoline. */
1182 if (DECL_NO_STATIC_CHAIN (decl))
1183 break;
1185 /* Lookup the immediate parent of the callee, as that's where
1186 we need to insert the trampoline. */
1187 for (i = info; i->context != target_context; i = i->outer)
1188 continue;
1189 x = lookup_tramp_for_decl (i, decl, INSERT);
1191 /* Compute the address of the field holding the trampoline. */
1192 x = get_frame_field (info, target_context, x, &wi->tsi);
1193 x = build_addr (x);
1194 x = tsi_gimplify_val (info, x, &wi->tsi);
1195 arg = tree_cons (NULL, x, NULL);
1197 /* Do machine-specific ugliness. Normally this will involve
1198 computing extra alignment, but it can really be anything. */
1199 x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1200 x = build_function_call_expr (x, arg);
1201 x = init_tmp_var (info, x, &wi->tsi);
1203 /* Cast back to the proper function type. */
1204 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1205 x = init_tmp_var (info, x, &wi->tsi);
1207 *tp = x;
1208 break;
1210 case CALL_EXPR:
1211 /* Only walk call arguments, lest we generate trampolines for
1212 direct calls. */
1213 walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1214 break;
1216 default:
1217 if (!IS_TYPE_OR_DECL_P (t))
1218 *walk_subtrees = 1;
1219 break;
1222 return NULL_TREE;
1225 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1226 reference nested functions to make sure that the static chain is
1227 set up properly for the call. */
1229 static tree
1230 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1232 struct walk_stmt_info *wi = data;
1233 struct nesting_info *info = wi->info;
1234 tree t = *tp, decl, target_context;
1236 *walk_subtrees = 0;
1237 switch (TREE_CODE (t))
1239 case CALL_EXPR:
1240 decl = get_callee_fndecl (t);
1241 if (!decl)
1242 break;
1243 target_context = decl_function_context (decl);
1244 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1245 TREE_OPERAND (t, 2)
1246 = get_static_chain (info, target_context, &wi->tsi);
1247 break;
1249 case RETURN_EXPR:
1250 case MODIFY_EXPR:
1251 case WITH_SIZE_EXPR:
1252 /* Only return modify and with_size_expr may contain calls. */
1253 *walk_subtrees = 1;
1254 break;
1256 default:
1257 break;
1260 return NULL_TREE;
1263 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1264 trampolines and call expressions. On the way back up, determine if
1265 a nested function actually uses its static chain; if not, remember that. */
1267 static void
1268 convert_all_function_calls (struct nesting_info *root)
1272 if (root->inner)
1273 convert_all_function_calls (root->inner);
1275 walk_function (convert_tramp_reference, root);
1276 walk_function (convert_call_expr, root);
1278 /* If the function does not use a static chain, then remember that. */
1279 if (root->outer && !root->chain_decl && !root->chain_field)
1280 DECL_NO_STATIC_CHAIN (root->context) = 1;
1281 else
1282 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1284 root = root->next;
1286 while (root);
1289 /* Do "everything else" to clean up or complete state collected by the
1290 various walking passes -- lay out the types and decls, generate code
1291 to initialize the frame decl, store critical expressions in the
1292 struct function for rtl to find. */
1294 static void
1295 finalize_nesting_tree_1 (struct nesting_info *root)
1297 tree stmt_list = NULL;
1298 tree context = root->context;
1299 struct function *sf;
1300 struct cgraph_node *node;
1302 /* If we created a non-local frame type or decl, we need to lay them
1303 out at this time. */
1304 if (root->frame_type)
1306 layout_type (root->frame_type);
1307 layout_decl (root->frame_decl, 0);
1310 /* If any parameters were referenced non-locally, then we need to
1311 insert a copy. Likewise, if any variables were referenced by
1312 pointer, we need to initialize the address. */
1313 if (root->any_parm_remapped)
1315 tree p;
1316 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1318 tree field, x, y;
1320 field = lookup_field_for_decl (root, p, NO_INSERT);
1321 if (!field)
1322 continue;
1324 if (use_pointer_in_frame (p))
1325 x = build_addr (p);
1326 else
1327 x = p;
1329 y = build (COMPONENT_REF, TREE_TYPE (field),
1330 root->frame_decl, field, NULL_TREE);
1331 x = build (MODIFY_EXPR, TREE_TYPE (field), y, x);
1332 append_to_statement_list (x, &stmt_list);
1336 /* If a chain_field was created, then it needs to be initialized
1337 from chain_decl. */
1338 if (root->chain_field)
1340 tree x = build (COMPONENT_REF, TREE_TYPE (root->chain_field),
1341 root->frame_decl, root->chain_field, NULL_TREE);
1342 x = build (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1343 append_to_statement_list (x, &stmt_list);
1346 /* If trampolines were created, then we need to initialize them. */
1347 if (root->any_tramp_created)
1349 struct nesting_info *i;
1350 for (i = root->inner; i ; i = i->next)
1352 tree arg, x, field;
1354 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1355 if (!field)
1356 continue;
1358 if (DECL_NO_STATIC_CHAIN (i->context))
1359 x = null_pointer_node;
1360 else
1361 x = build_addr (root->frame_decl);
1362 arg = tree_cons (NULL, x, NULL);
1364 x = build_addr (i->context);
1365 arg = tree_cons (NULL, x, arg);
1367 x = build (COMPONENT_REF, TREE_TYPE (field),
1368 root->frame_decl, field, NULL_TREE);
1369 x = build_addr (x);
1370 arg = tree_cons (NULL, x, arg);
1372 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1373 x = build_function_call_expr (x, arg);
1375 append_to_statement_list (x, &stmt_list);
1379 /* If we created initialization statements, insert them. */
1380 if (stmt_list)
1382 annotate_all_with_locus (&stmt_list,
1383 DECL_SOURCE_LOCATION (context));
1384 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1385 &stmt_list);
1386 BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1389 /* If a chain_decl was created, then it needs to be registered with
1390 struct function so that it gets initialized from the static chain
1391 register at the beginning of the function. */
1392 sf = DECL_STRUCT_FUNCTION (root->context);
1393 sf->static_chain_decl = root->chain_decl;
1395 /* Similarly for the non-local goto save area. */
1396 if (root->nl_goto_field)
1398 sf->nonlocal_goto_save_area
1399 = get_frame_field (root, context, root->nl_goto_field, NULL);
1400 sf->has_nonlocal_label = 1;
1403 /* Make sure all new local variables get inserted into the
1404 proper BIND_EXPR. */
1405 if (root->new_local_var_chain)
1406 declare_tmp_vars (root->new_local_var_chain,
1407 DECL_SAVED_TREE (root->context));
1409 /* Dump the translated tree function. */
1410 dump_function (TDI_nested, root->context);
1411 node = cgraph_node (root->context);
1413 /* For nested functions update the cgraph to reflect unnesting.
1414 We also delay finalizing of these functions up to this point. */
1415 if (node->origin)
1417 cgraph_unnest_node (cgraph_node (root->context));
1418 cgraph_finalize_function (root->context, true);
1422 static void
1423 finalize_nesting_tree (struct nesting_info *root)
1427 if (root->inner)
1428 finalize_nesting_tree (root->inner);
1429 finalize_nesting_tree_1 (root);
1430 root = root->next;
1432 while (root);
1435 /* Free the data structures allocated during this pass. */
1437 static void
1438 free_nesting_tree (struct nesting_info *root)
1440 struct nesting_info *next;
1443 if (root->inner)
1444 free_nesting_tree (root->inner);
1445 htab_delete (root->var_map);
1446 next = root->next;
1447 free (root);
1448 root = next;
1450 while (root);
1453 /* Main entry point for this pass. Process FNDECL and all of its nested
1454 subroutines and turn them into something less tightly bound. */
1456 void
1457 lower_nested_functions (tree fndecl)
1459 struct nesting_info *root;
1460 struct cgraph_node *cgn;
1462 /* If there are no nested functions, there's nothing to do. */
1463 cgn = cgraph_node (fndecl);
1464 if (!cgn->nested)
1465 return;
1467 root = create_nesting_tree (cgn);
1468 walk_all_functions (convert_nonlocal_reference, root);
1469 walk_all_functions (convert_local_reference, root);
1470 walk_all_functions (convert_nl_goto_reference, root);
1471 walk_all_functions (convert_nl_goto_receiver, root);
1472 convert_all_function_calls (root);
1473 finalize_nesting_tree (root);
1474 free_nesting_tree (root);
1477 #include "gt-tree-nested.h"