1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 #include "coretypes.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "tree-gimple.h"
32 #include "tree-iterator.h"
33 #include "tree-flow.h"
36 #include "langhooks.h"
40 /* The object of this pass is to lower the representation of a set of nested
41 functions in order to expose all of the gory details of the various
42 nonlocal references. We want to do this sooner rather than later, in
43 order to give us more freedom in emitting all of the functions in question.
45 Back in olden times, when gcc was young, we developed an insanely
46 complicated scheme whereby variables which were referenced nonlocally
47 were forced to live in the stack of the declaring function, and then
48 the nested functions magically discovered where these variables were
49 placed. In order for this scheme to function properly, it required
50 that the outer function be partially expanded, then we switch to
51 compiling the inner function, and once done with those we switch back
52 to compiling the outer function. Such delicate ordering requirements
53 makes it difficult to do whole translation unit optimizations
54 involving such functions.
56 The implementation here is much more direct. Everything that can be
57 referenced by an inner function is a member of an explicitly created
58 structure herein called the "nonlocal frame struct". The incoming
59 static chain for a nested function is a pointer to this struct in
60 the parent. In this way, we settle on known offsets from a known
61 base, and so are decoupled from the logic that places objects in the
62 function's stack frame. More importantly, we don't have to wait for
63 that to happen -- since the compilation of the inner function is no
64 longer tied to a real stack frame, the nonlocal frame struct can be
65 allocated anywhere. Which means that the outer function is now
68 Theory of operation here is very simple. Iterate over all the
69 statements in all the functions (depth first) several times,
70 allocating structures and fields on demand. In general we want to
71 examine inner functions first, so that we can avoid making changes
72 to outer functions which are unnecessary.
74 The order of the passes matters a bit, in that later passes will be
75 skipped if it is discovered that the functions don't actually interact
76 at all. That is, they're nested in the lexical sense but could have
77 been written as independent functions without change. */
80 struct var_map_elt GTY(())
86 struct nesting_info GTY ((chain_next ("%h.next")))
88 struct nesting_info *outer;
89 struct nesting_info *inner;
90 struct nesting_info *next;
92 htab_t GTY ((param_is (struct var_map_elt))) field_map;
93 htab_t GTY ((param_is (struct var_map_elt))) var_map;
94 bitmap suppress_expansion;
97 tree new_local_var_chain;
105 bool any_parm_remapped;
106 bool any_tramp_created;
107 char static_chain_added;
111 /* Hashing and equality functions for nesting_info->var_map. */
114 var_map_hash (const void *x)
116 const struct var_map_elt *a = (const struct var_map_elt *) x;
117 return htab_hash_pointer (a->old);
121 var_map_eq (const void *x, const void *y)
123 const struct var_map_elt *a = (const struct var_map_elt *) x;
124 const struct var_map_elt *b = (const struct var_map_elt *) y;
125 return a->old == b->old;
128 /* We're working in so many different function contexts simultaneously,
129 that create_tmp_var is dangerous. Prevent mishap. */
130 #define create_tmp_var cant_use_create_tmp_var_here_dummy
132 /* Like create_tmp_var, except record the variable for registration at
133 the given nesting level. */
136 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
140 /* If the type is of variable size or a type which must be created by the
141 frontend, something is wrong. Note that we explicitly allow
142 incomplete types here, since we create them ourselves here. */
143 gcc_assert (!TREE_ADDRESSABLE (type));
144 gcc_assert (!TYPE_SIZE_UNIT (type)
145 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
147 tmp_var = create_tmp_var_raw (type, prefix);
148 DECL_CONTEXT (tmp_var) = info->context;
149 TREE_CHAIN (tmp_var) = info->new_local_var_chain;
150 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
151 if (TREE_CODE (type) == COMPLEX_TYPE)
152 DECL_COMPLEX_GIMPLE_REG_P (tmp_var) = 1;
154 info->new_local_var_chain = tmp_var;
159 /* Take the address of EXP to be used within function CONTEXT.
160 Mark it for addressability as necessary. */
163 build_addr (tree exp, tree context)
169 while (handled_component_p (base))
170 base = TREE_OPERAND (base, 0);
173 TREE_ADDRESSABLE (base) = 1;
175 /* Building the ADDR_EXPR will compute a set of properties for
176 that ADDR_EXPR. Those properties are unfortunately context
177 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
179 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
180 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
181 way the properties are for the ADDR_EXPR are computed properly. */
182 save_context = current_function_decl;
183 current_function_decl = context;
184 retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
185 current_function_decl = save_context;;
189 /* Insert FIELD into TYPE, sorted by alignment requirements. */
192 insert_field_into_struct (tree type, tree field)
196 DECL_CONTEXT (field) = type;
198 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
199 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
202 TREE_CHAIN (field) = *p;
206 /* Build or return the RECORD_TYPE that describes the frame state that is
207 shared between INFO->CONTEXT and its nested functions. This record will
208 not be complete until finalize_nesting_tree; up until that point we'll
209 be adding fields as necessary.
211 We also build the DECL that represents this frame in the function. */
214 get_frame_type (struct nesting_info *info)
216 tree type = info->frame_type;
221 type = make_node (RECORD_TYPE);
223 name = concat ("FRAME.",
224 IDENTIFIER_POINTER (DECL_NAME (info->context)),
226 TYPE_NAME (type) = get_identifier (name);
229 info->frame_type = type;
230 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
232 /* ??? Always make it addressable for now, since it is meant to
233 be pointed to by the static chain pointer. This pessimizes
234 when it turns out that no static chains are needed because
235 the nested functions referencing non-local variables are not
236 reachable, but the true pessimization is to create the non-
237 local frame structure in the first place. */
238 TREE_ADDRESSABLE (info->frame_decl) = 1;
243 /* Return true if DECL should be referenced by pointer in the non-local
247 use_pointer_in_frame (tree decl)
249 if (TREE_CODE (decl) == PARM_DECL)
251 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
252 sized decls, and inefficient to copy large aggregates. Don't bother
253 moving anything but scalar variables. */
254 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
258 /* Variable sized types make things "interesting" in the frame. */
259 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
263 /* Given DECL, a non-locally accessed variable, find or create a field
264 in the non-local frame structure for the given nesting context. */
267 lookup_field_for_decl (struct nesting_info *info, tree decl,
268 enum insert_option insert)
270 struct var_map_elt *elt, dummy;
275 slot = htab_find_slot (info->field_map, &dummy, insert);
278 gcc_assert (insert != INSERT);
281 elt = (struct var_map_elt *) *slot;
283 if (!elt && insert == INSERT)
285 field = make_node (FIELD_DECL);
286 DECL_NAME (field) = DECL_NAME (decl);
288 if (use_pointer_in_frame (decl))
290 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
291 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
292 DECL_NONADDRESSABLE_P (field) = 1;
296 TREE_TYPE (field) = TREE_TYPE (decl);
297 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
298 DECL_ALIGN (field) = DECL_ALIGN (decl);
299 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
300 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
301 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
302 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
305 insert_field_into_struct (get_frame_type (info), field);
307 elt = GGC_NEW (struct var_map_elt);
312 if (TREE_CODE (decl) == PARM_DECL)
313 info->any_parm_remapped = true;
316 field = elt ? elt->new : NULL;
321 /* Build or return the variable that holds the static chain within
322 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
325 get_chain_decl (struct nesting_info *info)
327 tree decl = info->chain_decl;
332 type = get_frame_type (info->outer);
333 type = build_pointer_type (type);
335 /* Note that this variable is *not* entered into any BIND_EXPR;
336 the construction of this variable is handled specially in
337 expand_function_start and initialize_inlined_parameters.
338 Note also that it's represented as a parameter. This is more
339 close to the truth, since the initial value does come from
341 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
342 DECL_ARTIFICIAL (decl) = 1;
343 DECL_IGNORED_P (decl) = 1;
344 TREE_USED (decl) = 1;
345 DECL_CONTEXT (decl) = info->context;
346 DECL_ARG_TYPE (decl) = type;
348 /* Tell tree-inline.c that we never write to this variable, so
349 it can copy-prop the replacement value immediately. */
350 TREE_READONLY (decl) = 1;
352 info->chain_decl = decl;
357 /* Build or return the field within the non-local frame state that holds
358 the static chain for INFO->CONTEXT. This is the way to walk back up
359 multiple nesting levels. */
362 get_chain_field (struct nesting_info *info)
364 tree field = info->chain_field;
367 tree type = build_pointer_type (get_frame_type (info->outer));
369 field = make_node (FIELD_DECL);
370 DECL_NAME (field) = get_identifier ("__chain");
371 TREE_TYPE (field) = type;
372 DECL_ALIGN (field) = TYPE_ALIGN (type);
373 DECL_NONADDRESSABLE_P (field) = 1;
375 insert_field_into_struct (get_frame_type (info), field);
377 info->chain_field = field;
382 /* Copy EXP into a temporary. Allocate the temporary in the context of
383 INFO and insert the initialization statement before TSI. */
386 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
390 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
391 stmt = build2 (MODIFY_EXPR, TREE_TYPE (t), t, exp);
392 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
393 tsi_link_before (tsi, stmt, TSI_SAME_STMT);
398 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
401 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
403 if (is_gimple_val (exp))
406 return init_tmp_var (info, exp, tsi);
409 /* Similarly, but copy from the temporary and insert the statement
410 after the iterator. */
413 save_tmp_var (struct nesting_info *info, tree exp,
414 tree_stmt_iterator *tsi)
418 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
419 stmt = build2 (MODIFY_EXPR, TREE_TYPE (t), exp, t);
420 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
421 tsi_link_after (tsi, stmt, TSI_SAME_STMT);
426 /* Build or return the type used to represent a nested function trampoline. */
428 static GTY(()) tree trampoline_type;
431 get_trampoline_type (void)
434 unsigned align, size;
437 return trampoline_type;
439 align = TRAMPOLINE_ALIGNMENT;
440 size = TRAMPOLINE_SIZE;
442 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
443 then allocate extra space so that we can do dynamic alignment. */
444 if (align > STACK_BOUNDARY)
446 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
447 align = STACK_BOUNDARY;
450 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
451 t = build_array_type (char_type_node, t);
452 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
453 DECL_ALIGN (t) = align;
454 DECL_USER_ALIGN (t) = 1;
456 record = make_node (RECORD_TYPE);
457 TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
458 TYPE_FIELDS (record) = t;
459 layout_type (record);
464 /* Given DECL, a nested function, find or create a field in the non-local
465 frame structure for a trampoline for this function. */
468 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
469 enum insert_option insert)
471 struct var_map_elt *elt, dummy;
476 slot = htab_find_slot (info->var_map, &dummy, insert);
479 gcc_assert (insert != INSERT);
482 elt = (struct var_map_elt *) *slot;
484 if (!elt && insert == INSERT)
486 field = make_node (FIELD_DECL);
487 DECL_NAME (field) = DECL_NAME (decl);
488 TREE_TYPE (field) = get_trampoline_type ();
489 TREE_ADDRESSABLE (field) = 1;
491 insert_field_into_struct (get_frame_type (info), field);
493 elt = GGC_NEW (struct var_map_elt);
498 info->any_tramp_created = true;
501 field = elt ? elt->new : NULL;
506 /* Build or return the field within the non-local frame state that holds
507 the non-local goto "jmp_buf". The buffer itself is maintained by the
508 rtl middle-end as dynamic stack space is allocated. */
511 get_nl_goto_field (struct nesting_info *info)
513 tree field = info->nl_goto_field;
519 /* For __builtin_nonlocal_goto, we need N words. The first is the
520 frame pointer, the rest is for the target's stack pointer save
521 area. The number of words is controlled by STACK_SAVEAREA_MODE;
522 not the best interface, but it'll do for now. */
523 if (Pmode == ptr_mode)
524 type = ptr_type_node;
526 type = lang_hooks.types.type_for_mode (Pmode, 1);
528 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
529 size = size / GET_MODE_SIZE (Pmode);
532 type = build_array_type
533 (type, build_index_type (build_int_cst (NULL_TREE, size)));
535 field = make_node (FIELD_DECL);
536 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
537 TREE_TYPE (field) = type;
538 DECL_ALIGN (field) = TYPE_ALIGN (type);
539 TREE_ADDRESSABLE (field) = 1;
541 insert_field_into_struct (get_frame_type (info), field);
543 info->nl_goto_field = field;
549 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
552 walk_asm_expr (struct walk_stmt_info *wi, tree stmt)
554 int noutputs = list_length (ASM_OUTPUTS (stmt));
555 const char **oconstraints
556 = (const char **) alloca ((noutputs) * sizeof (const char *));
559 const char *constraint;
560 bool allows_mem, allows_reg, is_inout;
563 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
565 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
566 oconstraints[i] = constraint;
567 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
568 &allows_reg, &is_inout);
570 wi->val_only = (allows_reg || !allows_mem);
571 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
574 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
576 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
577 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
578 oconstraints, &allows_mem, &allows_reg);
580 wi->val_only = (allows_reg || !allows_mem);
581 /* Although input "m" is not really a LHS, we need a lvalue. */
582 wi->is_lhs = !wi->val_only;
583 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
590 /* Iterate over all sub-statements of *TP calling walk_tree with
591 WI->CALLBACK for every sub-expression in each statement found. */
594 walk_stmts (struct walk_stmt_info *wi, tree *tp)
602 if (wi->want_locations && EXPR_HAS_LOCATION (t))
603 input_location = EXPR_LOCATION (t);
605 switch (TREE_CODE (t))
609 tree_stmt_iterator i;
610 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
613 walk_stmts (wi, tsi_stmt_ptr (i));
619 walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
620 walk_stmts (wi, &COND_EXPR_THEN (t));
621 walk_stmts (wi, &COND_EXPR_ELSE (t));
624 walk_stmts (wi, &CATCH_BODY (t));
627 walk_stmts (wi, &EH_FILTER_FAILURE (t));
630 case TRY_FINALLY_EXPR:
631 walk_stmts (wi, &TREE_OPERAND (t, 0));
632 walk_stmts (wi, &TREE_OPERAND (t, 1));
636 if (wi->want_bind_expr)
639 wi->callback (tp, &walk_subtrees, wi);
643 walk_stmts (wi, &BIND_EXPR_BODY (t));
647 if (wi->want_return_expr)
650 wi->callback (tp, &walk_subtrees, wi);
654 walk_stmts (wi, &TREE_OPERAND (t, 0));
658 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
659 wi->val_only = !is_gimple_formal_tmp_var (TREE_OPERAND (t, 0));
660 walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
662 /* If the rhs is appropriate for a memory, we may use a
663 COMPONENT_REF on the lhs. */
664 wi->val_only = !is_gimple_mem_rhs (TREE_OPERAND (t, 1));
666 walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
673 walk_asm_expr (wi, *tp);
678 walk_tree (tp, wi->callback, wi, NULL);
683 /* Invoke CALLBACK on all statements of *STMT_P. */
686 walk_body (walk_tree_fn callback, struct nesting_info *info, tree *stmt_p)
688 struct walk_stmt_info wi;
690 memset (&wi, 0, sizeof (wi));
691 wi.callback = callback;
695 walk_stmts (&wi, stmt_p);
698 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
701 walk_function (walk_tree_fn callback, struct nesting_info *info)
703 walk_body (callback, info, &DECL_SAVED_TREE (info->context));
706 /* Similarly for ROOT and all functions nested underneath, depth first. */
709 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
714 walk_all_functions (callback, root->inner);
715 walk_function (callback, root);
721 /* We have to check for a fairly pathological case. The operands of function
722 nested function are to be interpreted in the context of the enclosing
723 function. So if any are variably-sized, they will get remapped when the
724 enclosing function is inlined. But that remapping would also have to be
725 done in the types of the PARM_DECLs of the nested function, meaning the
726 argument types of that function will disagree with the arguments in the
727 calls to that function. So we'd either have to make a copy of the nested
728 function corresponding to each time the enclosing function was inlined or
729 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
730 function. The former is not practical. The latter would still require
731 detecting this case to know when to add the conversions. So, for now at
732 least, we don't inline such an enclosing function.
734 We have to do that check recursively, so here return indicating whether
735 FNDECL has such a nested function. ORIG_FN is the function we were
736 trying to inline to use for checking whether any argument is variably
737 modified by anything in it.
739 It would be better to do this in tree-inline.c so that we could give
740 the appropriate warning for why a function can't be inlined, but that's
741 too late since the nesting structure has already been flattened and
742 adding a flag just to record this fact seems a waste of a flag. */
745 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
747 struct cgraph_node *cgn = cgraph_node (fndecl);
750 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
752 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
753 if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
756 if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
763 /* Construct our local datastructure describing the function nesting
764 tree rooted by CGN. */
766 static struct nesting_info *
767 create_nesting_tree (struct cgraph_node *cgn)
769 struct nesting_info *info = GGC_CNEW (struct nesting_info);
770 info->field_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
771 info->var_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
772 info->suppress_expansion = BITMAP_GGC_ALLOC ();
773 info->context = cgn->decl;
775 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
777 struct nesting_info *sub = create_nesting_tree (cgn);
779 sub->next = info->inner;
783 /* See discussion at check_for_nested_with_variably_modified for a
784 discussion of why this has to be here. */
785 if (check_for_nested_with_variably_modified (info->context, info->context))
786 DECL_UNINLINABLE (info->context) = true;
791 /* Return an expression computing the static chain for TARGET_CONTEXT
792 from INFO->CONTEXT. Insert any necessary computations before TSI. */
795 get_static_chain (struct nesting_info *info, tree target_context,
796 tree_stmt_iterator *tsi)
798 struct nesting_info *i;
801 if (info->context == target_context)
803 x = build_addr (info->frame_decl, target_context);
807 x = get_chain_decl (info);
809 for (i = info->outer; i->context != target_context; i = i->outer)
811 tree field = get_chain_field (i);
813 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
814 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
815 x = init_tmp_var (info, x, tsi);
822 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
823 frame as seen from INFO->CONTEXT. Insert any necessary computations
827 get_frame_field (struct nesting_info *info, tree target_context,
828 tree field, tree_stmt_iterator *tsi)
830 struct nesting_info *i;
833 if (info->context == target_context)
835 /* Make sure frame_decl gets created. */
836 (void) get_frame_type (info);
837 x = info->frame_decl;
841 x = get_chain_decl (info);
843 for (i = info->outer; i->context != target_context; i = i->outer)
845 tree field = get_chain_field (i);
847 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
848 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
849 x = init_tmp_var (info, x, tsi);
852 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
855 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
859 /* A subroutine of convert_nonlocal_reference. Create a local variable
860 in the nested function with DECL_VALUE_EXPR set to reference the true
861 variable in the parent function. This is used both for debug info
862 and in OpenMP lowering. */
865 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
867 struct var_map_elt *elt, dummy;
869 struct nesting_info *i;
870 tree x, field, new_decl;
874 slot = htab_find_slot (info->var_map, &dummy, INSERT);
880 target_context = decl_function_context (decl);
882 /* A copy of the code in get_frame_field, but without the temporaries. */
883 if (info->context == target_context)
885 /* Make sure frame_decl gets created. */
886 (void) get_frame_type (info);
887 x = info->frame_decl;
892 x = get_chain_decl (info);
893 for (i = info->outer; i->context != target_context; i = i->outer)
895 field = get_chain_field (i);
896 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
897 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
899 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
902 field = lookup_field_for_decl (i, decl, INSERT);
903 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
904 if (use_pointer_in_frame (decl))
905 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
907 /* ??? We should be remapping types as well, surely. */
908 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
909 DECL_CONTEXT (new_decl) = info->context;
910 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
911 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
912 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
913 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
914 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
915 TREE_READONLY (new_decl) = TREE_READONLY (decl);
916 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
917 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
919 SET_DECL_VALUE_EXPR (new_decl, x);
920 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
922 elt = ggc_alloc (sizeof (*elt));
927 TREE_CHAIN (new_decl) = info->debug_var_chain;
928 info->debug_var_chain = new_decl;
933 /* Called via walk_function+walk_tree, rewrite all references to VAR
934 and PARM_DECLs that belong to outer functions.
936 The rewrite will involve some number of structure accesses back up
937 the static chain. E.g. for a variable FOO up one nesting level it'll
938 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
939 indirections apply to decls for which use_pointer_in_frame is true. */
941 static bool convert_nonlocal_omp_clauses (tree *, struct walk_stmt_info *);
944 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
946 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
947 struct nesting_info *info = wi->info;
949 tree save_local_var_chain;
950 bitmap save_suppress;
953 switch (TREE_CODE (t))
956 /* Non-automatic variables are never processed. */
957 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
962 if (decl_function_context (t) != info->context)
967 x = get_nonlocal_debug_decl (info, t);
968 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
970 tree target_context = decl_function_context (t);
971 struct nesting_info *i;
972 for (i = info->outer; i->context != target_context; i = i->outer)
974 x = lookup_field_for_decl (i, t, INSERT);
975 x = get_frame_field (info, target_context, x, &wi->tsi);
976 if (use_pointer_in_frame (t))
978 x = init_tmp_var (info, x, &wi->tsi);
979 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
986 x = save_tmp_var (info, x, &wi->tsi);
988 x = init_tmp_var (info, x, &wi->tsi);
996 /* Don't walk non-local gotos for now. */
997 if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
1000 wi->val_only = true;
1006 /* We're taking the address of a label from a parent function, but
1007 this is not itself a non-local goto. Mark the label such that it
1008 will not be deleted, much as we would with a label address in
1010 if (decl_function_context (t) != info->context)
1011 FORCED_LABEL (t) = 1;
1016 bool save_val_only = wi->val_only;
1018 wi->val_only = false;
1020 wi->changed = false;
1021 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
1022 wi->val_only = true;
1028 /* If we changed anything, then TREE_INVARIANT is be wrong,
1029 since we're no longer directly referencing a decl. */
1030 save_context = current_function_decl;
1031 current_function_decl = info->context;
1032 recompute_tree_invariant_for_addr_expr (t);
1033 current_function_decl = save_context;
1035 /* If the callback converted the address argument in a context
1036 where we only accept variables (and min_invariant, presumably),
1037 then compute the address into a temporary. */
1039 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1048 case ARRAY_RANGE_REF:
1050 /* Go down this entire nest and just look at the final prefix and
1051 anything that describes the references. Otherwise, we lose track
1052 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1053 wi->val_only = true;
1055 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1057 if (TREE_CODE (t) == COMPONENT_REF)
1058 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1060 else if (TREE_CODE (t) == ARRAY_REF
1061 || TREE_CODE (t) == ARRAY_RANGE_REF)
1063 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1065 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1067 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
1070 else if (TREE_CODE (t) == BIT_FIELD_REF)
1072 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1074 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1078 wi->val_only = false;
1079 walk_tree (tp, convert_nonlocal_reference, wi, NULL);
1083 save_suppress = info->suppress_expansion;
1084 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1087 decl = get_chain_decl (info);
1088 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1089 OMP_CLAUSE_DECL (c) = decl;
1090 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1091 OMP_PARALLEL_CLAUSES (t) = c;
1094 save_local_var_chain = info->new_local_var_chain;
1095 info->new_local_var_chain = NULL;
1097 walk_body (convert_nonlocal_reference, info, &OMP_PARALLEL_BODY (t));
1099 if (info->new_local_var_chain)
1100 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1101 info->new_local_var_chain = save_local_var_chain;
1102 info->suppress_expansion = save_suppress;
1108 save_suppress = info->suppress_expansion;
1109 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t), wi);
1110 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1111 info->suppress_expansion = save_suppress;
1117 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1121 if (!IS_TYPE_OR_DECL_P (t))
1124 wi->val_only = true;
1134 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1136 struct nesting_info *info = wi->info;
1137 bool need_chain = false;
1140 bitmap new_suppress;
1142 new_suppress = BITMAP_GGC_ALLOC ();
1143 bitmap_copy (new_suppress, info->suppress_expansion);
1145 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1147 switch (OMP_CLAUSE_CODE (clause))
1149 case OMP_CLAUSE_PRIVATE:
1150 case OMP_CLAUSE_FIRSTPRIVATE:
1151 case OMP_CLAUSE_LASTPRIVATE:
1152 case OMP_CLAUSE_REDUCTION:
1153 case OMP_CLAUSE_COPYPRIVATE:
1154 case OMP_CLAUSE_SHARED:
1155 decl = OMP_CLAUSE_DECL (clause);
1156 if (decl_function_context (decl) != info->context)
1158 bitmap_set_bit (new_suppress, DECL_UID (decl));
1159 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1164 case OMP_CLAUSE_SCHEDULE:
1165 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1169 case OMP_CLAUSE_NUM_THREADS:
1170 wi->val_only = true;
1172 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1176 case OMP_CLAUSE_NOWAIT:
1177 case OMP_CLAUSE_ORDERED:
1178 case OMP_CLAUSE_DEFAULT:
1179 case OMP_CLAUSE_COPYIN:
1187 info->suppress_expansion = new_suppress;
1192 /* A subroutine of convert_local_reference. Create a local variable
1193 in the parent function with DECL_VALUE_EXPR set to reference the
1194 field in FRAME. This is used both for debug info and in OpenMP
1198 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1200 struct var_map_elt *elt, dummy;
1205 slot = htab_find_slot (info->var_map, &dummy, INSERT);
1211 /* Make sure frame_decl gets created. */
1212 (void) get_frame_type (info);
1213 x = info->frame_decl;
1214 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1216 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1217 DECL_CONTEXT (new_decl) = info->context;
1218 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1219 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1220 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1221 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1222 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1223 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1224 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1225 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1227 SET_DECL_VALUE_EXPR (new_decl, x);
1228 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1230 elt = ggc_alloc (sizeof (*elt));
1232 elt->new = new_decl;
1235 TREE_CHAIN (new_decl) = info->debug_var_chain;
1236 info->debug_var_chain = new_decl;
1238 /* Do not emit debug info twice. */
1239 DECL_IGNORED_P (decl) = 1;
1244 /* Called via walk_function+walk_tree, rewrite all references to VAR
1245 and PARM_DECLs that were referenced by inner nested functions.
1246 The rewrite will be a structure reference to the local frame variable. */
1248 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1251 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
1253 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1254 struct nesting_info *info = wi->info;
1255 tree t = *tp, field, x;
1257 tree save_local_var_chain;
1258 bitmap save_suppress;
1261 switch (TREE_CODE (t))
1264 /* Non-automatic variables are never processed. */
1265 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1270 if (decl_function_context (t) == info->context)
1272 /* If we copied a pointer to the frame, then the original decl
1273 is used unchanged in the parent function. */
1274 if (use_pointer_in_frame (t))
1277 /* No need to transform anything if no child references the
1279 field = lookup_field_for_decl (info, t, NO_INSERT);
1284 x = get_local_debug_decl (info, t, field);
1285 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1286 x = get_frame_field (info, info->context, field, &wi->tsi);
1291 x = save_tmp_var (info, x, &wi->tsi);
1293 x = init_tmp_var (info, x, &wi->tsi);
1301 save_val_only = wi->val_only;
1302 wi->val_only = false;
1304 wi->changed = false;
1305 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
1306 wi->val_only = save_val_only;
1308 /* If we converted anything ... */
1313 /* Then the frame decl is now addressable. */
1314 TREE_ADDRESSABLE (info->frame_decl) = 1;
1316 save_context = current_function_decl;
1317 current_function_decl = info->context;
1318 recompute_tree_invariant_for_addr_expr (t);
1319 current_function_decl = save_context;
1321 /* If we are in a context where we only accept values, then
1322 compute the address into a temporary. */
1324 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1332 case ARRAY_RANGE_REF:
1334 /* Go down this entire nest and just look at the final prefix and
1335 anything that describes the references. Otherwise, we lose track
1336 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1337 save_val_only = wi->val_only;
1338 wi->val_only = true;
1340 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1342 if (TREE_CODE (t) == COMPONENT_REF)
1343 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1345 else if (TREE_CODE (t) == ARRAY_REF
1346 || TREE_CODE (t) == ARRAY_RANGE_REF)
1348 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1350 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1352 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1355 else if (TREE_CODE (t) == BIT_FIELD_REF)
1357 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1359 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1363 wi->val_only = false;
1364 walk_tree (tp, convert_local_reference, wi, NULL);
1365 wi->val_only = save_val_only;
1369 save_suppress = info->suppress_expansion;
1370 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1373 (void) get_frame_type (info);
1374 c = build_omp_clause (OMP_CLAUSE_SHARED);
1375 OMP_CLAUSE_DECL (c) = info->frame_decl;
1376 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1377 OMP_PARALLEL_CLAUSES (t) = c;
1380 save_local_var_chain = info->new_local_var_chain;
1381 info->new_local_var_chain = NULL;
1383 walk_body (convert_local_reference, info, &OMP_PARALLEL_BODY (t));
1385 if (info->new_local_var_chain)
1386 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1387 info->new_local_var_chain = save_local_var_chain;
1388 info->suppress_expansion = save_suppress;
1394 save_suppress = info->suppress_expansion;
1395 convert_local_omp_clauses (&OMP_CLAUSES (t), wi);
1396 walk_body (convert_local_reference, info, &OMP_BODY (t));
1397 info->suppress_expansion = save_suppress;
1403 walk_body (convert_local_reference, info, &OMP_BODY (t));
1407 if (!IS_TYPE_OR_DECL_P (t))
1410 wi->val_only = true;
1420 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1422 struct nesting_info *info = wi->info;
1423 bool need_frame = false;
1426 bitmap new_suppress;
1428 new_suppress = BITMAP_GGC_ALLOC ();
1429 bitmap_copy (new_suppress, info->suppress_expansion);
1431 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1433 switch (OMP_CLAUSE_CODE (clause))
1435 case OMP_CLAUSE_PRIVATE:
1436 case OMP_CLAUSE_FIRSTPRIVATE:
1437 case OMP_CLAUSE_LASTPRIVATE:
1438 case OMP_CLAUSE_REDUCTION:
1439 case OMP_CLAUSE_COPYPRIVATE:
1440 case OMP_CLAUSE_SHARED:
1441 decl = OMP_CLAUSE_DECL (clause);
1442 if (decl_function_context (decl) == info->context
1443 && !use_pointer_in_frame (decl))
1445 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1448 bitmap_set_bit (new_suppress, DECL_UID (decl));
1449 OMP_CLAUSE_DECL (clause)
1450 = get_local_debug_decl (info, decl, field);
1456 case OMP_CLAUSE_SCHEDULE:
1457 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1461 case OMP_CLAUSE_NUM_THREADS:
1462 wi->val_only = true;
1464 convert_local_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy, wi);
1467 case OMP_CLAUSE_NOWAIT:
1468 case OMP_CLAUSE_ORDERED:
1469 case OMP_CLAUSE_DEFAULT:
1470 case OMP_CLAUSE_COPYIN:
1478 info->suppress_expansion = new_suppress;
1483 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1484 reference labels from outer functions. The rewrite will be a
1485 call to __builtin_nonlocal_goto. */
1488 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1490 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1491 struct nesting_info *info = wi->info, *i;
1492 tree t = *tp, label, new_label, target_context, x, arg, field;
1493 struct var_map_elt *elt, dummy;
1497 if (TREE_CODE (t) != GOTO_EXPR)
1499 label = GOTO_DESTINATION (t);
1500 if (TREE_CODE (label) != LABEL_DECL)
1502 target_context = decl_function_context (label);
1503 if (target_context == info->context)
1506 for (i = info->outer; target_context != i->context; i = i->outer)
1509 /* The original user label may also be use for a normal goto, therefore
1510 we must create a new label that will actually receive the abnormal
1511 control transfer. This new label will be marked LABEL_NONLOCAL; this
1512 mark will trigger proper behavior in the cfg, as well as cause the
1513 (hairy target-specific) non-local goto receiver code to be generated
1514 when we expand rtl. Enter this association into var_map so that we
1515 can insert the new label into the IL during a second pass. */
1517 slot = htab_find_slot (i->var_map, &dummy, INSERT);
1518 elt = (struct var_map_elt *) *slot;
1521 new_label = create_artificial_label ();
1522 DECL_NONLOCAL (new_label) = 1;
1524 elt = GGC_NEW (struct var_map_elt);
1526 elt->new = new_label;
1530 new_label = elt->new;
1532 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1533 field = get_nl_goto_field (i);
1534 x = get_frame_field (info, target_context, field, &wi->tsi);
1535 x = build_addr (x, target_context);
1536 x = tsi_gimplify_val (info, x, &wi->tsi);
1537 arg = tree_cons (NULL, x, NULL);
1538 x = build_addr (new_label, target_context);
1539 arg = tree_cons (NULL, x, arg);
1540 x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1541 x = build_function_call_expr (x, arg);
1543 SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1544 *tsi_stmt_ptr (wi->tsi) = x;
1549 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1550 are referenced via nonlocal goto from a nested function. The rewrite
1551 will involve installing a newly generated DECL_NONLOCAL label, and
1552 (potentially) a branch around the rtl gunk that is assumed to be
1553 attached to such a label. */
1556 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1558 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1559 struct nesting_info *info = wi->info;
1560 tree t = *tp, label, new_label, x;
1561 struct var_map_elt *elt, dummy;
1562 tree_stmt_iterator tmp_tsi;
1565 if (TREE_CODE (t) != LABEL_EXPR)
1567 label = LABEL_EXPR_LABEL (t);
1570 elt = (struct var_map_elt *) htab_find (info->var_map, &dummy);
1573 new_label = elt->new;
1575 /* If there's any possibility that the previous statement falls through,
1576 then we must branch around the new non-local label. */
1578 tsi_prev (&tmp_tsi);
1579 if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1581 x = build1 (GOTO_EXPR, void_type_node, label);
1582 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1584 x = build1 (LABEL_EXPR, void_type_node, new_label);
1585 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1590 /* Called via walk_function+walk_tree, rewrite all references to addresses
1591 of nested functions that require the use of trampolines. The rewrite
1592 will involve a reference a trampoline generated for the occasion. */
1595 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1597 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1598 struct nesting_info *info = wi->info, *i;
1599 tree t = *tp, decl, target_context, x, arg;
1602 switch (TREE_CODE (t))
1606 T.1 = &CHAIN->tramp;
1607 T.2 = __builtin_adjust_trampoline (T.1);
1608 T.3 = (func_type)T.2;
1611 decl = TREE_OPERAND (t, 0);
1612 if (TREE_CODE (decl) != FUNCTION_DECL)
1615 /* Only need to process nested functions. */
1616 target_context = decl_function_context (decl);
1617 if (!target_context)
1620 /* If the nested function doesn't use a static chain, then
1621 it doesn't need a trampoline. */
1622 if (DECL_NO_STATIC_CHAIN (decl))
1625 /* Lookup the immediate parent of the callee, as that's where
1626 we need to insert the trampoline. */
1627 for (i = info; i->context != target_context; i = i->outer)
1629 x = lookup_tramp_for_decl (i, decl, INSERT);
1631 /* Compute the address of the field holding the trampoline. */
1632 x = get_frame_field (info, target_context, x, &wi->tsi);
1633 x = build_addr (x, target_context);
1634 x = tsi_gimplify_val (info, x, &wi->tsi);
1635 arg = tree_cons (NULL, x, NULL);
1637 /* Do machine-specific ugliness. Normally this will involve
1638 computing extra alignment, but it can really be anything. */
1639 x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1640 x = build_function_call_expr (x, arg);
1641 x = init_tmp_var (info, x, &wi->tsi);
1643 /* Cast back to the proper function type. */
1644 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1645 x = init_tmp_var (info, x, &wi->tsi);
1651 /* Only walk call arguments, lest we generate trampolines for
1653 walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1657 if (!IS_TYPE_OR_DECL_P (t))
1665 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1666 reference nested functions to make sure that the static chain is
1667 set up properly for the call. */
1670 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1672 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1673 struct nesting_info *info = wi->info;
1674 tree t = *tp, decl, target_context;
1675 char save_static_chain_added;
1679 switch (TREE_CODE (t))
1682 decl = get_callee_fndecl (t);
1685 target_context = decl_function_context (decl);
1686 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1689 = get_static_chain (info, target_context, &wi->tsi);
1690 info->static_chain_added
1691 |= (1 << (info->context != target_context));
1697 case WITH_SIZE_EXPR:
1698 /* Only return modify and with_size_expr may contain calls. */
1703 save_static_chain_added = info->static_chain_added;
1704 info->static_chain_added = 0;
1705 walk_body (convert_call_expr, info, &OMP_PARALLEL_BODY (t));
1706 for (i = 0; i < 2; i++)
1709 if ((info->static_chain_added & (1 << i)) == 0)
1711 decl = i ? get_chain_decl (info) : info->frame_decl;
1712 /* Don't add CHAIN.* or FRAME.* twice. */
1713 for (c = OMP_PARALLEL_CLAUSES (t); c; c = OMP_CLAUSE_CHAIN (c))
1714 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1715 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1716 && OMP_CLAUSE_DECL (c) == decl)
1720 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1721 OMP_CLAUSE_DECL (c) = decl;
1722 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1723 OMP_PARALLEL_CLAUSES (t) = c;
1726 info->static_chain_added |= save_static_chain_added;
1736 walk_body (convert_call_expr, info, &OMP_BODY (t));
1746 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1747 trampolines and call expressions. On the way back up, determine if
1748 a nested function actually uses its static chain; if not, remember that. */
1751 convert_all_function_calls (struct nesting_info *root)
1756 convert_all_function_calls (root->inner);
1758 walk_function (convert_tramp_reference, root);
1759 walk_function (convert_call_expr, root);
1761 /* If the function does not use a static chain, then remember that. */
1762 if (root->outer && !root->chain_decl && !root->chain_field)
1763 DECL_NO_STATIC_CHAIN (root->context) = 1;
1765 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1772 /* Do "everything else" to clean up or complete state collected by the
1773 various walking passes -- lay out the types and decls, generate code
1774 to initialize the frame decl, store critical expressions in the
1775 struct function for rtl to find. */
1778 finalize_nesting_tree_1 (struct nesting_info *root)
1780 tree stmt_list = NULL;
1781 tree context = root->context;
1782 struct function *sf;
1784 /* If we created a non-local frame type or decl, we need to lay them
1785 out at this time. */
1786 if (root->frame_type)
1788 /* In some cases the frame type will trigger the -Wpadded warning.
1789 This is not helpful; suppress it. */
1790 int save_warn_padded = warn_padded;
1792 layout_type (root->frame_type);
1793 warn_padded = save_warn_padded;
1794 layout_decl (root->frame_decl, 0);
1797 /* If any parameters were referenced non-locally, then we need to
1798 insert a copy. Likewise, if any variables were referenced by
1799 pointer, we need to initialize the address. */
1800 if (root->any_parm_remapped)
1803 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1807 field = lookup_field_for_decl (root, p, NO_INSERT);
1811 if (use_pointer_in_frame (p))
1812 x = build_addr (p, context);
1816 y = build3 (COMPONENT_REF, TREE_TYPE (field),
1817 root->frame_decl, field, NULL_TREE);
1818 x = build2 (MODIFY_EXPR, TREE_TYPE (field), y, x);
1819 append_to_statement_list (x, &stmt_list);
1823 /* If a chain_field was created, then it needs to be initialized
1825 if (root->chain_field)
1827 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
1828 root->frame_decl, root->chain_field, NULL_TREE);
1829 x = build2 (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1830 append_to_statement_list (x, &stmt_list);
1833 /* If trampolines were created, then we need to initialize them. */
1834 if (root->any_tramp_created)
1836 struct nesting_info *i;
1837 for (i = root->inner; i ; i = i->next)
1841 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1845 if (DECL_NO_STATIC_CHAIN (i->context))
1846 x = null_pointer_node;
1848 x = build_addr (root->frame_decl, context);
1849 arg = tree_cons (NULL, x, NULL);
1851 x = build_addr (i->context, context);
1852 arg = tree_cons (NULL, x, arg);
1854 x = build3 (COMPONENT_REF, TREE_TYPE (field),
1855 root->frame_decl, field, NULL_TREE);
1856 x = build_addr (x, context);
1857 arg = tree_cons (NULL, x, arg);
1859 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1860 x = build_function_call_expr (x, arg);
1862 append_to_statement_list (x, &stmt_list);
1866 /* If we created initialization statements, insert them. */
1869 annotate_all_with_locus (&stmt_list,
1870 DECL_SOURCE_LOCATION (context));
1871 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1873 BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1876 /* If a chain_decl was created, then it needs to be registered with
1877 struct function so that it gets initialized from the static chain
1878 register at the beginning of the function. */
1879 sf = DECL_STRUCT_FUNCTION (root->context);
1880 sf->static_chain_decl = root->chain_decl;
1882 /* Similarly for the non-local goto save area. */
1883 if (root->nl_goto_field)
1885 sf->nonlocal_goto_save_area
1886 = get_frame_field (root, context, root->nl_goto_field, NULL);
1887 sf->has_nonlocal_label = 1;
1890 /* Make sure all new local variables get inserted into the
1891 proper BIND_EXPR. */
1892 if (root->new_local_var_chain)
1893 declare_vars (root->new_local_var_chain, DECL_SAVED_TREE (root->context),
1895 if (root->debug_var_chain)
1896 declare_vars (root->debug_var_chain, DECL_SAVED_TREE (root->context),
1899 /* Dump the translated tree function. */
1900 dump_function (TDI_nested, root->context);
1904 finalize_nesting_tree (struct nesting_info *root)
1909 finalize_nesting_tree (root->inner);
1910 finalize_nesting_tree_1 (root);
1916 /* Unnest the nodes and pass them to cgraph. */
1919 unnest_nesting_tree_1 (struct nesting_info *root)
1921 struct cgraph_node *node = cgraph_node (root->context);
1923 /* For nested functions update the cgraph to reflect unnesting.
1924 We also delay finalizing of these functions up to this point. */
1927 cgraph_unnest_node (cgraph_node (root->context));
1928 cgraph_finalize_function (root->context, true);
1933 unnest_nesting_tree (struct nesting_info *root)
1938 unnest_nesting_tree (root->inner);
1939 unnest_nesting_tree_1 (root);
1945 /* Free the data structures allocated during this pass. */
1948 free_nesting_tree (struct nesting_info *root)
1950 struct nesting_info *next;
1954 free_nesting_tree (root->inner);
1955 htab_delete (root->var_map);
1963 static GTY(()) struct nesting_info *root;
1965 /* Main entry point for this pass. Process FNDECL and all of its nested
1966 subroutines and turn them into something less tightly bound. */
1969 lower_nested_functions (tree fndecl)
1971 struct cgraph_node *cgn;
1973 /* If there are no nested functions, there's nothing to do. */
1974 cgn = cgraph_node (fndecl);
1978 root = create_nesting_tree (cgn);
1979 walk_all_functions (convert_nonlocal_reference, root);
1980 walk_all_functions (convert_local_reference, root);
1981 walk_all_functions (convert_nl_goto_reference, root);
1982 walk_all_functions (convert_nl_goto_receiver, root);
1983 convert_all_function_calls (root);
1984 finalize_nesting_tree (root);
1985 unnest_nesting_tree (root);
1986 free_nesting_tree (root);
1990 #include "gt-tree-nested.h"