* configure.ac: Don't test for [build] __cxa_atexit when building a
[official-gcc.git] / gcc / tree-nested.c
blob3f500f6f138914fcae2f05c2316735ce8ce0cedb
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 (TREE_CODE (base) == REALPART_EXPR || TREE_CODE (base) == IMAGPART_EXPR
159 || handled_component_p (base))
160 base = TREE_OPERAND (base, 0);
162 if (DECL_P (base))
163 TREE_ADDRESSABLE (base) = 1;
165 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
168 /* Insert FIELD into TYPE, sorted by alignment requirements. */
170 static void
171 insert_field_into_struct (tree type, tree field)
173 tree *p;
175 DECL_CONTEXT (field) = type;
177 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
178 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
179 break;
181 TREE_CHAIN (field) = *p;
182 *p = field;
185 /* Build or return the RECORD_TYPE that describes the frame state that is
186 shared between INFO->CONTEXT and its nested functions. This record will
187 not be complete until finalize_nesting_tree; up until that point we'll
188 be adding fields as necessary.
190 We also build the DECL that represents this frame in the function. */
192 static tree
193 get_frame_type (struct nesting_info *info)
195 tree type = info->frame_type;
196 if (!type)
198 char *name;
200 type = make_node (RECORD_TYPE);
202 name = concat ("FRAME.",
203 IDENTIFIER_POINTER (DECL_NAME (info->context)),
204 NULL);
205 TYPE_NAME (type) = get_identifier (name);
206 free (name);
208 info->frame_type = type;
209 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
211 return type;
214 /* Return true if DECL should be referenced by pointer in the non-local
215 frame structure. */
217 static bool
218 use_pointer_in_frame (tree decl)
220 if (TREE_CODE (decl) == PARM_DECL)
222 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
223 sized decls, and inefficient to copy large aggregates. Don't bother
224 moving anything but scalar variables. */
225 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
227 else
229 /* Variable sized types make things "interesting" in the frame. */
230 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
234 /* Given DECL, a non-locally accessed variable, find or create a field
235 in the non-local frame structure for the given nesting context. */
237 static tree
238 lookup_field_for_decl (struct nesting_info *info, tree decl,
239 enum insert_option insert)
241 struct var_map_elt *elt, dummy;
242 void **slot;
243 tree field;
245 dummy.old = decl;
246 slot = htab_find_slot (info->var_map, &dummy, insert);
247 if (!slot)
249 gcc_assert (insert != INSERT);
250 return NULL;
252 elt = *slot;
254 if (!elt && insert == INSERT)
256 field = make_node (FIELD_DECL);
257 DECL_NAME (field) = DECL_NAME (decl);
259 if (use_pointer_in_frame (decl))
261 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
262 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
263 DECL_NONADDRESSABLE_P (field) = 1;
265 else
267 TREE_TYPE (field) = TREE_TYPE (decl);
268 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
269 DECL_ALIGN (field) = DECL_ALIGN (decl);
270 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
271 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
272 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
273 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
276 insert_field_into_struct (get_frame_type (info), field);
278 elt = xmalloc (sizeof (*elt));
279 elt->old = decl;
280 elt->new = field;
281 *slot = elt;
283 if (TREE_CODE (decl) == PARM_DECL)
284 info->any_parm_remapped = true;
286 else
287 field = elt ? elt->new : NULL;
289 return field;
292 /* Build or return the variable that holds the static chain within
293 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
295 static tree
296 get_chain_decl (struct nesting_info *info)
298 tree decl = info->chain_decl;
299 if (!decl)
301 tree type;
303 type = get_frame_type (info->outer);
304 type = build_pointer_type (type);
306 /* Note that this variable is *not* entered into any BIND_EXPR;
307 the construction of this variable is handled specially in
308 expand_function_start and initialize_inlined_parameters.
309 Note also that it's represented as a parameter. This is more
310 close to the truth, since the initial value does come from
311 the caller. */
312 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
313 DECL_ARTIFICIAL (decl) = 1;
314 DECL_IGNORED_P (decl) = 1;
315 TREE_USED (decl) = 1;
316 DECL_CONTEXT (decl) = info->context;
317 DECL_ARG_TYPE (decl) = type;
319 /* Tell tree-inline.c that we never write to this variable, so
320 it can copy-prop the replacement value immediately. */
321 TREE_READONLY (decl) = 1;
323 info->chain_decl = decl;
325 return decl;
328 /* Build or return the field within the non-local frame state that holds
329 the static chain for INFO->CONTEXT. This is the way to walk back up
330 multiple nesting levels. */
332 static tree
333 get_chain_field (struct nesting_info *info)
335 tree field = info->chain_field;
336 if (!field)
338 tree type = build_pointer_type (get_frame_type (info->outer));
340 field = make_node (FIELD_DECL);
341 DECL_NAME (field) = get_identifier ("__chain");
342 TREE_TYPE (field) = type;
343 DECL_ALIGN (field) = TYPE_ALIGN (type);
344 DECL_NONADDRESSABLE_P (field) = 1;
346 insert_field_into_struct (get_frame_type (info), field);
348 info->chain_field = field;
350 return field;
353 /* Copy EXP into a temporary. Allocate the temporary in the context of
354 INFO and insert the initialization statement before TSI. */
356 static tree
357 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
359 tree t, stmt;
361 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
362 stmt = build (MODIFY_EXPR, TREE_TYPE (t), t, exp);
363 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
364 tsi_link_before (tsi, stmt, TSI_SAME_STMT);
366 return t;
369 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
371 static tree
372 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
374 if (is_gimple_val (exp))
375 return exp;
376 else
377 return init_tmp_var (info, exp, tsi);
380 /* Build or return the type used to represent a nested function trampoline. */
382 static GTY(()) tree trampoline_type;
384 static tree
385 get_trampoline_type (void)
387 tree record, t;
388 unsigned align, size;
390 if (trampoline_type)
391 return trampoline_type;
393 align = TRAMPOLINE_ALIGNMENT;
394 size = TRAMPOLINE_SIZE;
396 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
397 then allocate extra space so that we can do dynamic alignment. */
398 if (align > STACK_BOUNDARY)
400 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
401 align = STACK_BOUNDARY;
404 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
405 t = build_array_type (char_type_node, t);
406 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
407 DECL_ALIGN (t) = align;
408 DECL_USER_ALIGN (t) = 1;
410 record = make_node (RECORD_TYPE);
411 TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
412 TYPE_FIELDS (record) = t;
413 layout_type (record);
415 return record;
418 /* Given DECL, a nested function, find or create a field in the non-local
419 frame structure for a trampoline for this function. */
421 static tree
422 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
423 enum insert_option insert)
425 struct var_map_elt *elt, dummy;
426 void **slot;
427 tree field;
429 dummy.old = decl;
430 slot = htab_find_slot (info->var_map, &dummy, insert);
431 if (!slot)
433 gcc_assert (insert != INSERT);
434 return NULL;
436 elt = *slot;
438 if (!elt && insert == INSERT)
440 field = make_node (FIELD_DECL);
441 DECL_NAME (field) = DECL_NAME (decl);
442 TREE_TYPE (field) = get_trampoline_type ();
443 TREE_ADDRESSABLE (field) = 1;
445 insert_field_into_struct (get_frame_type (info), field);
447 elt = xmalloc (sizeof (*elt));
448 elt->old = decl;
449 elt->new = field;
450 *slot = elt;
452 info->any_tramp_created = true;
454 else
455 field = elt ? elt->new : NULL;
457 return field;
460 /* Build or return the field within the non-local frame state that holds
461 the non-local goto "jmp_buf". The buffer itself is maintained by the
462 rtl middle-end as dynamic stack space is allocated. */
464 static tree
465 get_nl_goto_field (struct nesting_info *info)
467 tree field = info->nl_goto_field;
468 if (!field)
470 unsigned size;
471 tree type;
473 /* For __builtin_nonlocal_goto, we need N words. The first is the
474 frame pointer, the rest is for the target's stack pointer save
475 area. The number of words is controlled by STACK_SAVEAREA_MODE;
476 not the best interface, but it'll do for now. */
477 if (Pmode == ptr_mode)
478 type = ptr_type_node;
479 else
480 type = lang_hooks.types.type_for_mode (Pmode, 1);
482 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
483 size = size / GET_MODE_SIZE (Pmode);
484 size = size + 1;
486 type = build_array_type
487 (type, build_index_type (build_int_cst (NULL_TREE, size)));
489 field = make_node (FIELD_DECL);
490 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
491 TREE_TYPE (field) = type;
492 DECL_ALIGN (field) = TYPE_ALIGN (type);
493 TREE_ADDRESSABLE (field) = 1;
495 insert_field_into_struct (get_frame_type (info), field);
497 info->nl_goto_field = field;
500 return field;
503 /* Convenience routines to walk all statements of a gimple function.
505 For each statement, we invoke CALLBACK via walk_tree. The passed
506 data is a walk_stmt_info structure. Of note here is a TSI that
507 points to the current statement being walked. The VAL_ONLY flag
508 that indicates whether the *TP being examined may be replaced
509 with something that matches is_gimple_val (if true) or something
510 slightly more complicated (if false). "Something" technically
511 means the common subset of is_gimple_lvalue and is_gimple_rhs,
512 but we never try to form anything more complicated than that, so
513 we don't bother checking. */
515 struct walk_stmt_info
517 walk_tree_fn callback;
518 tree_stmt_iterator tsi;
519 struct nesting_info *info;
520 bool val_only;
521 bool changed;
524 /* A subroutine of walk_function. Iterate over all sub-statements of *TP. */
526 static void
527 walk_stmts (struct walk_stmt_info *wi, tree *tp)
529 tree t = *tp;
530 if (!t)
531 return;
533 switch (TREE_CODE (t))
535 case STATEMENT_LIST:
537 tree_stmt_iterator i;
538 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
540 wi->tsi = i;
541 walk_stmts (wi, tsi_stmt_ptr (i));
544 break;
546 case COND_EXPR:
547 walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
548 walk_stmts (wi, &COND_EXPR_THEN (t));
549 walk_stmts (wi, &COND_EXPR_ELSE (t));
550 break;
551 case CATCH_EXPR:
552 walk_stmts (wi, &CATCH_BODY (t));
553 break;
554 case EH_FILTER_EXPR:
555 walk_stmts (wi, &EH_FILTER_FAILURE (t));
556 break;
557 case TRY_CATCH_EXPR:
558 case TRY_FINALLY_EXPR:
559 walk_stmts (wi, &TREE_OPERAND (t, 0));
560 walk_stmts (wi, &TREE_OPERAND (t, 1));
561 break;
562 case BIND_EXPR:
563 walk_stmts (wi, &BIND_EXPR_BODY (t));
564 break;
566 case RETURN_EXPR:
567 walk_stmts (wi, &TREE_OPERAND (t, 0));
568 break;
570 case MODIFY_EXPR:
571 /* The immediate arguments of a MODIFY_EXPR may use COMPONENT_REF. */
572 wi->val_only = false;
573 walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
574 wi->val_only = false;
575 walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
576 wi->val_only = true;
577 break;
579 default:
580 wi->val_only = true;
581 walk_tree (tp, wi->callback, wi, NULL);
582 break;
586 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
588 static void
589 walk_function (walk_tree_fn callback, struct nesting_info *info)
591 struct walk_stmt_info wi;
593 memset (&wi, 0, sizeof (wi));
594 wi.callback = callback;
595 wi.info = info;
596 wi.val_only = true;
598 walk_stmts (&wi, &DECL_SAVED_TREE (info->context));
601 /* Similarly for ROOT and all functions nested underneath, depth first. */
603 static void
604 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
608 if (root->inner)
609 walk_all_functions (callback, root->inner);
610 walk_function (callback, root);
611 root = root->next;
613 while (root);
617 /* Construct our local datastructure describing the function nesting
618 tree rooted by CGN. */
620 static struct nesting_info *
621 create_nesting_tree (struct cgraph_node *cgn)
623 struct nesting_info *info = xcalloc (1, sizeof (*info));
624 info->var_map = htab_create (7, var_map_hash, var_map_eq, free);
625 info->context = cgn->decl;
627 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
629 struct nesting_info *sub = create_nesting_tree (cgn);
630 sub->outer = info;
631 sub->next = info->inner;
632 info->inner = sub;
635 return info;
638 /* Return an expression computing the static chain for TARGET_CONTEXT
639 from INFO->CONTEXT. Insert any necessary computations before TSI. */
641 static tree
642 get_static_chain (struct nesting_info *info, tree target_context,
643 tree_stmt_iterator *tsi)
645 struct nesting_info *i;
646 tree x;
648 if (info->context == target_context)
650 x = build_addr (info->frame_decl);
652 else
654 x = get_chain_decl (info);
656 for (i = info->outer; i->context != target_context; i = i->outer)
658 tree field = get_chain_field (i);
660 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
661 x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
662 x = init_tmp_var (info, x, tsi);
666 return x;
669 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
670 frame as seen from INFO->CONTEXT. Insert any necessary computations
671 before TSI. */
673 static tree
674 get_frame_field (struct nesting_info *info, tree target_context,
675 tree field, tree_stmt_iterator *tsi)
677 struct nesting_info *i;
678 tree x;
680 if (info->context == target_context)
682 /* Make sure frame_decl gets created. */
683 (void) get_frame_type (info);
684 x = info->frame_decl;
686 else
688 x = get_chain_decl (info);
690 for (i = info->outer; i->context != target_context; i = i->outer)
692 tree field = get_chain_field (i);
694 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
695 x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
696 x = init_tmp_var (info, x, tsi);
699 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
702 x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
703 return x;
706 /* Called via walk_function+walk_tree, rewrite all references to VAR
707 and PARM_DECLs that belong to outer functions.
709 The rewrite will involve some number of structure accesses back up
710 the static chain. E.g. for a variable FOO up one nesting level it'll
711 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
712 indirections apply to decls for which use_pointer_in_frame is true. */
714 static tree
715 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
717 struct walk_stmt_info *wi = data;
718 struct nesting_info *info = wi->info;
719 tree t = *tp;
721 *walk_subtrees = 0;
722 switch (TREE_CODE (t))
724 case VAR_DECL:
725 /* Non-automatic variables are never processed. */
726 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
727 break;
728 /* FALLTHRU */
730 case PARM_DECL:
731 if (decl_function_context (t) != info->context)
733 tree target_context = decl_function_context (t);
734 struct nesting_info *i;
735 tree x;
736 wi->changed = true;
738 for (i = info->outer; i->context != target_context; i = i->outer)
739 continue;
740 x = lookup_field_for_decl (i, t, INSERT);
741 x = get_frame_field (info, target_context, x, &wi->tsi);
742 if (use_pointer_in_frame (t))
744 x = init_tmp_var (info, x, &wi->tsi);
745 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
747 if (wi->val_only)
748 x = init_tmp_var (info, x, &wi->tsi);
750 *tp = x;
752 break;
754 case GOTO_EXPR:
755 /* Don't walk non-local gotos for now. */
756 if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
758 *walk_subtrees = 1;
759 wi->val_only = true;
761 break;
763 case LABEL_DECL:
764 /* We're taking the address of a label from a parent function, but
765 this is not itself a non-local goto. Mark the label such that it
766 will not be deleted, much as we would with a label address in
767 static storage. */
768 if (decl_function_context (t) != info->context)
769 FORCED_LABEL (t) = 1;
770 break;
772 case ADDR_EXPR:
774 bool save_val_only = wi->val_only;
776 wi->changed = false;
777 wi->val_only = false;
778 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
779 wi->val_only = true;
781 if (wi->changed)
783 /* If we changed anything, then TREE_INVARIANT is be wrong,
784 since we're no longer directly referencing a decl. */
785 recompute_tree_invarant_for_addr_expr (t);
787 /* If the callback converted the address argument in a context
788 where we only accept variables (and min_invariant, presumably),
789 then compute the address into a temporary. */
790 if (save_val_only)
791 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
794 break;
796 case REALPART_EXPR:
797 case IMAGPART_EXPR:
798 case COMPONENT_REF:
799 case ARRAY_REF:
800 case ARRAY_RANGE_REF:
801 case BIT_FIELD_REF:
802 /* Go down this entire nest and just look at the final prefix and
803 anything that describes the references. Otherwise, we lose track
804 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
805 wi->val_only = true;
806 for (; handled_component_p (t)
807 || TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR;
808 tp = &TREE_OPERAND (t, 0), t = *tp)
810 if (TREE_CODE (t) == COMPONENT_REF)
811 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
812 NULL);
813 else if (TREE_CODE (t) == ARRAY_REF
814 || TREE_CODE (t) == ARRAY_RANGE_REF)
816 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
817 NULL);
818 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
819 NULL);
820 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
821 NULL);
823 else if (TREE_CODE (t) == BIT_FIELD_REF)
825 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
826 NULL);
827 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
828 NULL);
831 wi->val_only = false;
832 walk_tree (tp, convert_nonlocal_reference, wi, NULL);
833 break;
835 default:
836 if (!IS_TYPE_OR_DECL_P (t))
838 *walk_subtrees = 1;
839 wi->val_only = true;
841 break;
844 return NULL_TREE;
847 /* Called via walk_function+walk_tree, rewrite all references to VAR
848 and PARM_DECLs that were referenced by inner nested functions.
849 The rewrite will be a structure reference to the local frame variable. */
851 static tree
852 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
854 struct walk_stmt_info *wi = data;
855 struct nesting_info *info = wi->info;
856 tree t = *tp, field, x;
858 switch (TREE_CODE (t))
860 case VAR_DECL:
861 /* Non-automatic variables are never processed. */
862 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
863 break;
864 /* FALLTHRU */
866 case PARM_DECL:
867 if (decl_function_context (t) == info->context)
869 /* If we copied a pointer to the frame, then the original decl
870 is used unchanged in the parent function. */
871 if (use_pointer_in_frame (t))
872 break;
874 /* No need to transform anything if no child references the
875 variable. */
876 field = lookup_field_for_decl (info, t, NO_INSERT);
877 if (!field)
878 break;
879 wi->changed = true;
881 x = get_frame_field (info, info->context, field, &wi->tsi);
882 if (wi->val_only)
883 x = init_tmp_var (info, x, &wi->tsi);
884 *tp = x;
886 break;
888 case ADDR_EXPR:
890 bool save_val_only = wi->val_only;
892 wi->changed = false;
893 wi->val_only = false;
894 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
895 wi->val_only = save_val_only;
897 /* If we converted anything ... */
898 if (wi->changed)
900 /* Then the frame decl is now addressable. */
901 TREE_ADDRESSABLE (info->frame_decl) = 1;
903 recompute_tree_invarant_for_addr_expr (t);
905 /* If we are in a context where we only accept values, then
906 compute the address into a temporary. */
907 if (save_val_only)
908 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
911 break;
913 case REALPART_EXPR:
914 case IMAGPART_EXPR:
915 case COMPONENT_REF:
916 case ARRAY_REF:
917 case ARRAY_RANGE_REF:
918 case BIT_FIELD_REF:
919 /* Go down this entire nest and just look at the final prefix and
920 anything that describes the references. Otherwise, we lose track
921 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
922 wi->val_only = true;
923 for (; handled_component_p (t)
924 || TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR;
925 tp = &TREE_OPERAND (t, 0), t = *tp)
927 if (TREE_CODE (t) == COMPONENT_REF)
928 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
929 NULL);
930 else if (TREE_CODE (t) == ARRAY_REF
931 || TREE_CODE (t) == ARRAY_RANGE_REF)
933 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
934 NULL);
935 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
936 NULL);
937 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
938 NULL);
940 else if (TREE_CODE (t) == BIT_FIELD_REF)
942 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
943 NULL);
944 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
945 NULL);
948 wi->val_only = false;
949 walk_tree (tp, convert_local_reference, wi, NULL);
950 break;
952 default:
953 if (!IS_TYPE_OR_DECL_P (t))
955 *walk_subtrees = 1;
956 wi->val_only = true;
958 break;
961 return NULL_TREE;
964 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
965 reference labels from outer functions. The rewrite will be a
966 call to __builtin_nonlocal_goto. */
968 static tree
969 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
971 struct walk_stmt_info *wi = data;
972 struct nesting_info *info = wi->info, *i;
973 tree t = *tp, label, new_label, target_context, x, arg, field;
974 struct var_map_elt *elt;
975 void **slot;
977 *walk_subtrees = 0;
978 if (TREE_CODE (t) != GOTO_EXPR)
979 return NULL_TREE;
980 label = GOTO_DESTINATION (t);
981 if (TREE_CODE (label) != LABEL_DECL)
982 return NULL_TREE;
983 target_context = decl_function_context (label);
984 if (target_context == info->context)
985 return NULL_TREE;
987 for (i = info->outer; target_context != i->context; i = i->outer)
988 continue;
990 /* The original user label may also be use for a normal goto, therefore
991 we must create a new label that will actually receive the abnormal
992 control transfer. This new label will be marked LABEL_NONLOCAL; this
993 mark will trigger proper behavior in the cfg, as well as cause the
994 (hairy target-specific) non-local goto receiver code to be generated
995 when we expand rtl. */
996 new_label = create_artificial_label ();
997 DECL_NONLOCAL (new_label) = 1;
999 /* Enter this association into var_map so that we can insert the new
1000 label into the IL during a second pass. */
1001 elt = xmalloc (sizeof (*elt));
1002 elt->old = label;
1003 elt->new = new_label;
1004 slot = htab_find_slot (i->var_map, elt, INSERT);
1005 *slot = elt;
1007 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1008 field = get_nl_goto_field (i);
1009 x = get_frame_field (info, target_context, field, &wi->tsi);
1010 x = build_addr (x);
1011 x = tsi_gimplify_val (info, x, &wi->tsi);
1012 arg = tree_cons (NULL, x, NULL);
1013 x = build_addr (new_label);
1014 arg = tree_cons (NULL, x, arg);
1015 x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1016 x = build_function_call_expr (x, arg);
1018 SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1019 *tsi_stmt_ptr (wi->tsi) = x;
1021 return NULL_TREE;
1024 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1025 are referenced via nonlocal goto from a nested function. The rewrite
1026 will involve installing a newly generated DECL_NONLOCAL label, and
1027 (potentially) a branch around the rtl gunk that is assumed to be
1028 attached to such a label. */
1030 static tree
1031 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1033 struct walk_stmt_info *wi = data;
1034 struct nesting_info *info = wi->info;
1035 tree t = *tp, label, new_label, x;
1036 struct var_map_elt *elt, dummy;
1037 tree_stmt_iterator tmp_tsi;
1039 *walk_subtrees = 0;
1040 if (TREE_CODE (t) != LABEL_EXPR)
1041 return NULL_TREE;
1042 label = LABEL_EXPR_LABEL (t);
1044 dummy.old = label;
1045 elt = htab_find (info->var_map, &dummy);
1046 if (!elt)
1047 return NULL_TREE;
1048 new_label = elt->new;
1050 /* If there's any possibility that the previous statement falls through,
1051 then we must branch around the new non-local label. */
1052 tmp_tsi = wi->tsi;
1053 tsi_prev (&tmp_tsi);
1054 if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1056 x = build1 (GOTO_EXPR, void_type_node, label);
1057 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1059 x = build1 (LABEL_EXPR, void_type_node, new_label);
1060 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1062 return NULL_TREE;
1065 /* Called via walk_function+walk_tree, rewrite all references to addresses
1066 of nested functions that require the use of trampolines. The rewrite
1067 will involve a reference a trampoline generated for the occasion. */
1069 static tree
1070 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1072 struct walk_stmt_info *wi = data;
1073 struct nesting_info *info = wi->info, *i;
1074 tree t = *tp, decl, target_context, x, arg;
1076 *walk_subtrees = 0;
1077 switch (TREE_CODE (t))
1079 case ADDR_EXPR:
1080 /* Build
1081 T.1 = &CHAIN->tramp;
1082 T.2 = __builtin_adjust_trampoline (T.1);
1083 T.3 = (func_type)T.2;
1086 decl = TREE_OPERAND (t, 0);
1087 if (TREE_CODE (decl) != FUNCTION_DECL)
1088 break;
1090 /* Only need to process nested functions. */
1091 target_context = decl_function_context (decl);
1092 if (!target_context)
1093 break;
1095 /* If the nested function doesn't use a static chain, then
1096 it doesn't need a trampoline. */
1097 if (DECL_NO_STATIC_CHAIN (decl))
1098 break;
1100 /* Lookup the immediate parent of the callee, as that's where
1101 we need to insert the trampoline. */
1102 for (i = info; i->context != target_context; i = i->outer)
1103 continue;
1104 x = lookup_tramp_for_decl (i, decl, INSERT);
1106 /* Compute the address of the field holding the trampoline. */
1107 x = get_frame_field (info, target_context, x, &wi->tsi);
1108 x = build_addr (x);
1109 x = tsi_gimplify_val (info, x, &wi->tsi);
1110 arg = tree_cons (NULL, x, NULL);
1112 /* Do machine-specific ugliness. Normally this will involve
1113 computing extra alignment, but it can really be anything. */
1114 x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1115 x = build_function_call_expr (x, arg);
1116 x = init_tmp_var (info, x, &wi->tsi);
1118 /* Cast back to the proper function type. */
1119 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1120 x = init_tmp_var (info, x, &wi->tsi);
1122 *tp = x;
1123 break;
1125 case CALL_EXPR:
1126 /* Only walk call arguments, lest we generate trampolines for
1127 direct calls. */
1128 walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1129 break;
1131 default:
1132 if (!IS_TYPE_OR_DECL_P (t))
1133 *walk_subtrees = 1;
1134 break;
1137 return NULL_TREE;
1140 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1141 reference nested functions to make sure that the static chain is
1142 set up properly for the call. */
1144 static tree
1145 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1147 struct walk_stmt_info *wi = data;
1148 struct nesting_info *info = wi->info;
1149 tree t = *tp, decl, target_context;
1151 *walk_subtrees = 0;
1152 switch (TREE_CODE (t))
1154 case CALL_EXPR:
1155 decl = get_callee_fndecl (t);
1156 if (!decl)
1157 break;
1158 target_context = decl_function_context (decl);
1159 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1160 TREE_OPERAND (t, 2)
1161 = get_static_chain (info, target_context, &wi->tsi);
1162 break;
1164 case RETURN_EXPR:
1165 case MODIFY_EXPR:
1166 case WITH_SIZE_EXPR:
1167 /* Only return modify and with_size_expr may contain calls. */
1168 *walk_subtrees = 1;
1169 break;
1171 default:
1172 break;
1175 return NULL_TREE;
1178 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1179 trampolines and call expressions. On the way back up, determine if
1180 a nested function actually uses its static chain; if not, remember that. */
1182 static void
1183 convert_all_function_calls (struct nesting_info *root)
1187 if (root->inner)
1188 convert_all_function_calls (root->inner);
1190 walk_function (convert_tramp_reference, root);
1191 walk_function (convert_call_expr, root);
1193 /* If the function does not use a static chain, then remember that. */
1194 if (root->outer && !root->chain_decl && !root->chain_field)
1195 DECL_NO_STATIC_CHAIN (root->context) = 1;
1196 else
1197 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1199 root = root->next;
1201 while (root);
1204 /* Do "everything else" to clean up or complete state collected by the
1205 various walking passes -- lay out the types and decls, generate code
1206 to initialize the frame decl, store critical expressions in the
1207 struct function for rtl to find. */
1209 static void
1210 finalize_nesting_tree_1 (struct nesting_info *root)
1212 tree stmt_list = NULL;
1213 tree context = root->context;
1214 struct function *sf;
1215 struct cgraph_node *node;
1217 /* If we created a non-local frame type or decl, we need to lay them
1218 out at this time. */
1219 if (root->frame_type)
1221 layout_type (root->frame_type);
1222 layout_decl (root->frame_decl, 0);
1225 /* If any parameters were referenced non-locally, then we need to
1226 insert a copy. Likewise, if any variables were referenced by
1227 pointer, we need to initialize the address. */
1228 if (root->any_parm_remapped)
1230 tree p;
1231 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1233 tree field, x, y;
1235 field = lookup_field_for_decl (root, p, NO_INSERT);
1236 if (!field)
1237 continue;
1239 if (use_pointer_in_frame (p))
1240 x = build_addr (p);
1241 else
1242 x = p;
1244 y = build (COMPONENT_REF, TREE_TYPE (field),
1245 root->frame_decl, field, NULL_TREE);
1246 x = build (MODIFY_EXPR, TREE_TYPE (field), y, x);
1247 append_to_statement_list (x, &stmt_list);
1251 /* If a chain_field was created, then it needs to be initialized
1252 from chain_decl. */
1253 if (root->chain_field)
1255 tree x = build (COMPONENT_REF, TREE_TYPE (root->chain_field),
1256 root->frame_decl, root->chain_field, NULL_TREE);
1257 x = build (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1258 append_to_statement_list (x, &stmt_list);
1261 /* If trampolines were created, then we need to initialize them. */
1262 if (root->any_tramp_created)
1264 struct nesting_info *i;
1265 for (i = root->inner; i ; i = i->next)
1267 tree arg, x, field;
1269 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1270 if (!field)
1271 continue;
1273 if (DECL_NO_STATIC_CHAIN (i->context))
1274 x = null_pointer_node;
1275 else
1276 x = build_addr (root->frame_decl);
1277 arg = tree_cons (NULL, x, NULL);
1279 x = build_addr (i->context);
1280 arg = tree_cons (NULL, x, arg);
1282 x = build (COMPONENT_REF, TREE_TYPE (field),
1283 root->frame_decl, field, NULL_TREE);
1284 x = build_addr (x);
1285 arg = tree_cons (NULL, x, arg);
1287 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1288 x = build_function_call_expr (x, arg);
1290 append_to_statement_list (x, &stmt_list);
1294 /* If we created initialization statements, insert them. */
1295 if (stmt_list)
1297 annotate_all_with_locus (&stmt_list,
1298 DECL_SOURCE_LOCATION (context));
1299 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1300 &stmt_list);
1301 BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1304 /* If a chain_decl was created, then it needs to be registered with
1305 struct function so that it gets initialized from the static chain
1306 register at the beginning of the function. */
1307 sf = DECL_STRUCT_FUNCTION (root->context);
1308 sf->static_chain_decl = root->chain_decl;
1310 /* Similarly for the non-local goto save area. */
1311 if (root->nl_goto_field)
1313 sf->nonlocal_goto_save_area
1314 = get_frame_field (root, context, root->nl_goto_field, NULL);
1315 sf->has_nonlocal_label = 1;
1318 /* Make sure all new local variables get inserted into the
1319 proper BIND_EXPR. */
1320 if (root->new_local_var_chain)
1321 declare_tmp_vars (root->new_local_var_chain,
1322 DECL_SAVED_TREE (root->context));
1324 /* Dump the translated tree function. */
1325 dump_function (TDI_nested, root->context);
1326 node = cgraph_node (root->context);
1328 /* For nested functions update the cgraph to reflect unnesting.
1329 We also delay finalizing of these functions up to this point. */
1330 if (node->origin)
1332 cgraph_unnest_node (cgraph_node (root->context));
1333 cgraph_finalize_function (root->context, true);
1337 static void
1338 finalize_nesting_tree (struct nesting_info *root)
1342 if (root->inner)
1343 finalize_nesting_tree (root->inner);
1344 finalize_nesting_tree_1 (root);
1345 root = root->next;
1347 while (root);
1350 /* Free the data structures allocated during this pass. */
1352 static void
1353 free_nesting_tree (struct nesting_info *root)
1355 struct nesting_info *next;
1358 if (root->inner)
1359 free_nesting_tree (root->inner);
1360 htab_delete (root->var_map);
1361 next = root->next;
1362 free (root);
1363 root = next;
1365 while (root);
1368 /* Main entry point for this pass. Process FNDECL and all of its nested
1369 subroutines and turn them into something less tightly bound. */
1371 void
1372 lower_nested_functions (tree fndecl)
1374 struct nesting_info *root;
1375 struct cgraph_node *cgn;
1377 /* If there are no nested functions, there's nothing to do. */
1378 cgn = cgraph_node (fndecl);
1379 if (!cgn->nested)
1380 return;
1382 root = create_nesting_tree (cgn);
1383 walk_all_functions (convert_nonlocal_reference, root);
1384 walk_all_functions (convert_local_reference, root);
1385 walk_all_functions (convert_nl_goto_reference, root);
1386 walk_all_functions (convert_nl_goto_receiver, root);
1387 convert_all_function_calls (root);
1388 finalize_nesting_tree (root);
1389 free_nesting_tree (root);
1392 #include "gt-tree-nested.h"