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 /* Iterate over all sub-statements of *TP calling walk_tree with
550 WI->CALLBACK for every sub-expression in each statement found. */
553 walk_stmts (struct walk_stmt_info
*wi
, tree
*tp
)
561 if (wi
->want_locations
&& EXPR_HAS_LOCATION (t
))
562 input_location
= EXPR_LOCATION (t
);
564 switch (TREE_CODE (t
))
568 tree_stmt_iterator i
;
569 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
572 walk_stmts (wi
, tsi_stmt_ptr (i
));
578 walk_tree (&COND_EXPR_COND (t
), wi
->callback
, wi
, NULL
);
579 walk_stmts (wi
, &COND_EXPR_THEN (t
));
580 walk_stmts (wi
, &COND_EXPR_ELSE (t
));
583 walk_stmts (wi
, &CATCH_BODY (t
));
586 walk_stmts (wi
, &EH_FILTER_FAILURE (t
));
589 case TRY_FINALLY_EXPR
:
590 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
591 walk_stmts (wi
, &TREE_OPERAND (t
, 1));
595 if (wi
->want_bind_expr
)
598 wi
->callback (tp
, &walk_subtrees
, wi
);
602 walk_stmts (wi
, &BIND_EXPR_BODY (t
));
606 if (wi
->want_return_expr
)
609 wi
->callback (tp
, &walk_subtrees
, wi
);
613 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
617 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
618 wi
->val_only
= !is_gimple_formal_tmp_var (TREE_OPERAND (t
, 0));
619 walk_tree (&TREE_OPERAND (t
, 1), wi
->callback
, wi
, NULL
);
621 /* If the rhs is appropriate for a memory, we may use a
622 COMPONENT_REF on the lhs. */
623 wi
->val_only
= !is_gimple_mem_rhs (TREE_OPERAND (t
, 1));
625 walk_tree (&TREE_OPERAND (t
, 0), wi
->callback
, wi
, NULL
);
633 walk_tree (tp
, wi
->callback
, wi
, NULL
);
638 /* Invoke CALLBACK on all statements of *STMT_P. */
641 walk_body (walk_tree_fn callback
, struct nesting_info
*info
, tree
*stmt_p
)
643 struct walk_stmt_info wi
;
645 memset (&wi
, 0, sizeof (wi
));
646 wi
.callback
= callback
;
650 walk_stmts (&wi
, stmt_p
);
653 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
656 walk_function (walk_tree_fn callback
, struct nesting_info
*info
)
658 walk_body (callback
, info
, &DECL_SAVED_TREE (info
->context
));
661 /* Similarly for ROOT and all functions nested underneath, depth first. */
664 walk_all_functions (walk_tree_fn callback
, struct nesting_info
*root
)
669 walk_all_functions (callback
, root
->inner
);
670 walk_function (callback
, root
);
676 /* We have to check for a fairly pathological case. The operands of function
677 nested function are to be interpreted in the context of the enclosing
678 function. So if any are variably-sized, they will get remapped when the
679 enclosing function is inlined. But that remapping would also have to be
680 done in the types of the PARM_DECLs of the nested function, meaning the
681 argument types of that function will disagree with the arguments in the
682 calls to that function. So we'd either have to make a copy of the nested
683 function corresponding to each time the enclosing function was inlined or
684 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
685 function. The former is not practical. The latter would still require
686 detecting this case to know when to add the conversions. So, for now at
687 least, we don't inline such an enclosing function.
689 We have to do that check recursively, so here return indicating whether
690 FNDECL has such a nested function. ORIG_FN is the function we were
691 trying to inline to use for checking whether any argument is variably
692 modified by anything in it.
694 It would be better to do this in tree-inline.c so that we could give
695 the appropriate warning for why a function can't be inlined, but that's
696 too late since the nesting structure has already been flattened and
697 adding a flag just to record this fact seems a waste of a flag. */
700 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
702 struct cgraph_node
*cgn
= cgraph_node (fndecl
);
705 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
707 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= TREE_CHAIN (arg
))
708 if (variably_modified_type_p (TREE_TYPE (arg
), 0), orig_fndecl
)
711 if (check_for_nested_with_variably_modified (cgn
->decl
, orig_fndecl
))
718 /* Construct our local datastructure describing the function nesting
719 tree rooted by CGN. */
721 static struct nesting_info
*
722 create_nesting_tree (struct cgraph_node
*cgn
)
724 struct nesting_info
*info
= GGC_CNEW (struct nesting_info
);
725 info
->field_map
= htab_create_ggc (7, var_map_hash
, var_map_eq
, ggc_free
);
726 info
->var_map
= htab_create_ggc (7, var_map_hash
, var_map_eq
, ggc_free
);
727 info
->suppress_expansion
= BITMAP_GGC_ALLOC ();
728 info
->context
= cgn
->decl
;
730 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
732 struct nesting_info
*sub
= create_nesting_tree (cgn
);
734 sub
->next
= info
->inner
;
738 /* See discussion at check_for_nested_with_variably_modified for a
739 discussion of why this has to be here. */
740 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
741 DECL_UNINLINABLE (info
->context
) = true;
746 /* Return an expression computing the static chain for TARGET_CONTEXT
747 from INFO->CONTEXT. Insert any necessary computations before TSI. */
750 get_static_chain (struct nesting_info
*info
, tree target_context
,
751 tree_stmt_iterator
*tsi
)
753 struct nesting_info
*i
;
756 if (info
->context
== target_context
)
758 x
= build_addr (info
->frame_decl
, target_context
);
762 x
= get_chain_decl (info
);
764 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
766 tree field
= get_chain_field (i
);
768 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
769 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
770 x
= init_tmp_var (info
, x
, tsi
);
777 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
778 frame as seen from INFO->CONTEXT. Insert any necessary computations
782 get_frame_field (struct nesting_info
*info
, tree target_context
,
783 tree field
, tree_stmt_iterator
*tsi
)
785 struct nesting_info
*i
;
788 if (info
->context
== target_context
)
790 /* Make sure frame_decl gets created. */
791 (void) get_frame_type (info
);
792 x
= info
->frame_decl
;
796 x
= get_chain_decl (info
);
798 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
800 tree field
= get_chain_field (i
);
802 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
803 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
804 x
= init_tmp_var (info
, x
, tsi
);
807 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
810 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
814 /* A subroutine of convert_nonlocal_reference. Create a local variable
815 in the nested function with DECL_VALUE_EXPR set to reference the true
816 variable in the parent function. This is used both for debug info
817 and in OpenMP lowering. */
820 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
822 struct var_map_elt
*elt
, dummy
;
824 struct nesting_info
*i
;
825 tree x
, field
, new_decl
;
829 slot
= htab_find_slot (info
->var_map
, &dummy
, INSERT
);
835 target_context
= decl_function_context (decl
);
837 /* A copy of the code in get_frame_field, but without the temporaries. */
838 if (info
->context
== target_context
)
840 /* Make sure frame_decl gets created. */
841 (void) get_frame_type (info
);
842 x
= info
->frame_decl
;
847 x
= get_chain_decl (info
);
848 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
850 field
= get_chain_field (i
);
851 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
852 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
854 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
857 field
= lookup_field_for_decl (i
, decl
, INSERT
);
858 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
859 if (use_pointer_in_frame (decl
))
860 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
862 /* ??? We should be remapping types as well, surely. */
863 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
864 DECL_CONTEXT (new_decl
) = info
->context
;
865 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
866 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
867 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
868 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
869 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
870 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
871 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
872 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
874 SET_DECL_VALUE_EXPR (new_decl
, x
);
875 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
877 elt
= ggc_alloc (sizeof (*elt
));
882 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
883 info
->debug_var_chain
= new_decl
;
888 /* Called via walk_function+walk_tree, rewrite all references to VAR
889 and PARM_DECLs that belong to outer functions.
891 The rewrite will involve some number of structure accesses back up
892 the static chain. E.g. for a variable FOO up one nesting level it'll
893 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
894 indirections apply to decls for which use_pointer_in_frame is true. */
896 static bool convert_nonlocal_omp_clauses (tree
*, struct walk_stmt_info
*);
899 convert_nonlocal_reference (tree
*tp
, int *walk_subtrees
, void *data
)
901 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
902 struct nesting_info
*info
= wi
->info
;
904 tree save_local_var_chain
;
905 bitmap save_suppress
;
908 switch (TREE_CODE (t
))
911 /* Non-automatic variables are never processed. */
912 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
917 if (decl_function_context (t
) != info
->context
)
922 x
= get_nonlocal_debug_decl (info
, t
);
923 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
925 tree target_context
= decl_function_context (t
);
926 struct nesting_info
*i
;
927 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
929 x
= lookup_field_for_decl (i
, t
, INSERT
);
930 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
931 if (use_pointer_in_frame (t
))
933 x
= init_tmp_var (info
, x
, &wi
->tsi
);
934 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
941 x
= save_tmp_var (info
, x
, &wi
->tsi
);
943 x
= init_tmp_var (info
, x
, &wi
->tsi
);
951 /* Don't walk non-local gotos for now. */
952 if (TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
)
961 /* We're taking the address of a label from a parent function, but
962 this is not itself a non-local goto. Mark the label such that it
963 will not be deleted, much as we would with a label address in
965 if (decl_function_context (t
) != info
->context
)
966 FORCED_LABEL (t
) = 1;
971 bool save_val_only
= wi
->val_only
;
973 wi
->val_only
= false;
976 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference
, wi
, NULL
);
983 /* If we changed anything, then TREE_INVARIANT is be wrong,
984 since we're no longer directly referencing a decl. */
985 save_context
= current_function_decl
;
986 current_function_decl
= info
->context
;
987 recompute_tree_invariant_for_addr_expr (t
);
988 current_function_decl
= save_context
;
990 /* If the callback converted the address argument in a context
991 where we only accept variables (and min_invariant, presumably),
992 then compute the address into a temporary. */
994 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1003 case ARRAY_RANGE_REF
:
1005 /* Go down this entire nest and just look at the final prefix and
1006 anything that describes the references. Otherwise, we lose track
1007 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1008 wi
->val_only
= true;
1010 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1012 if (TREE_CODE (t
) == COMPONENT_REF
)
1013 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1015 else if (TREE_CODE (t
) == ARRAY_REF
1016 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1018 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
1020 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1022 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference
, wi
,
1025 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1027 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
1029 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1033 wi
->val_only
= false;
1034 walk_tree (tp
, convert_nonlocal_reference
, wi
, NULL
);
1038 save_suppress
= info
->suppress_expansion
;
1039 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1042 decl
= get_chain_decl (info
);
1043 c
= build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE
);
1044 OMP_CLAUSE_DECL (c
) = decl
;
1045 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1046 OMP_PARALLEL_CLAUSES (t
) = c
;
1049 save_local_var_chain
= info
->new_local_var_chain
;
1050 info
->new_local_var_chain
= NULL
;
1052 walk_body (convert_nonlocal_reference
, info
, &OMP_PARALLEL_BODY (t
));
1054 if (info
->new_local_var_chain
)
1055 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1056 info
->new_local_var_chain
= save_local_var_chain
;
1057 info
->suppress_expansion
= save_suppress
;
1063 save_suppress
= info
->suppress_expansion
;
1064 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t
), wi
);
1065 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1066 info
->suppress_expansion
= save_suppress
;
1072 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1076 if (!IS_TYPE_OR_DECL_P (t
))
1079 wi
->val_only
= true;
1089 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1091 struct nesting_info
*info
= wi
->info
;
1092 bool need_chain
= false;
1095 bitmap new_suppress
;
1097 new_suppress
= BITMAP_GGC_ALLOC ();
1098 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1100 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1102 switch (OMP_CLAUSE_CODE (clause
))
1104 case OMP_CLAUSE_PRIVATE
:
1105 case OMP_CLAUSE_FIRSTPRIVATE
:
1106 case OMP_CLAUSE_LASTPRIVATE
:
1107 case OMP_CLAUSE_REDUCTION
:
1108 case OMP_CLAUSE_COPYPRIVATE
:
1109 case OMP_CLAUSE_SHARED
:
1110 decl
= OMP_CLAUSE_DECL (clause
);
1111 if (decl_function_context (decl
) != info
->context
)
1113 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1114 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1119 case OMP_CLAUSE_SCHEDULE
:
1120 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1124 case OMP_CLAUSE_NUM_THREADS
:
1125 wi
->val_only
= true;
1127 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1131 case OMP_CLAUSE_NOWAIT
:
1132 case OMP_CLAUSE_ORDERED
:
1133 case OMP_CLAUSE_DEFAULT
:
1134 case OMP_CLAUSE_COPYIN
:
1142 info
->suppress_expansion
= new_suppress
;
1147 /* A subroutine of convert_local_reference. Create a local variable
1148 in the parent function with DECL_VALUE_EXPR set to reference the
1149 field in FRAME. This is used both for debug info and in OpenMP
1153 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1155 struct var_map_elt
*elt
, dummy
;
1160 slot
= htab_find_slot (info
->var_map
, &dummy
, INSERT
);
1166 /* Make sure frame_decl gets created. */
1167 (void) get_frame_type (info
);
1168 x
= info
->frame_decl
;
1169 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1171 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1172 DECL_CONTEXT (new_decl
) = info
->context
;
1173 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
1174 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1175 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1176 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1177 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1178 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1179 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1180 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1182 SET_DECL_VALUE_EXPR (new_decl
, x
);
1183 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1185 elt
= ggc_alloc (sizeof (*elt
));
1187 elt
->new = new_decl
;
1190 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
1191 info
->debug_var_chain
= new_decl
;
1193 /* Do not emit debug info twice. */
1194 DECL_IGNORED_P (decl
) = 1;
1199 /* Called via walk_function+walk_tree, rewrite all references to VAR
1200 and PARM_DECLs that were referenced by inner nested functions.
1201 The rewrite will be a structure reference to the local frame variable. */
1203 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1206 convert_local_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1208 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1209 struct nesting_info
*info
= wi
->info
;
1210 tree t
= *tp
, field
, x
;
1212 tree save_local_var_chain
;
1213 bitmap save_suppress
;
1216 switch (TREE_CODE (t
))
1219 /* Non-automatic variables are never processed. */
1220 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1225 if (decl_function_context (t
) == info
->context
)
1227 /* If we copied a pointer to the frame, then the original decl
1228 is used unchanged in the parent function. */
1229 if (use_pointer_in_frame (t
))
1232 /* No need to transform anything if no child references the
1234 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1239 x
= get_local_debug_decl (info
, t
, field
);
1240 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1241 x
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
1246 x
= save_tmp_var (info
, x
, &wi
->tsi
);
1248 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1256 save_val_only
= wi
->val_only
;
1257 wi
->val_only
= false;
1259 wi
->changed
= false;
1260 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference
, wi
, NULL
);
1261 wi
->val_only
= save_val_only
;
1263 /* If we converted anything ... */
1268 /* Then the frame decl is now addressable. */
1269 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1271 save_context
= current_function_decl
;
1272 current_function_decl
= info
->context
;
1273 recompute_tree_invariant_for_addr_expr (t
);
1274 current_function_decl
= save_context
;
1276 /* If we are in a context where we only accept values, then
1277 compute the address into a temporary. */
1279 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1287 case ARRAY_RANGE_REF
:
1289 /* Go down this entire nest and just look at the final prefix and
1290 anything that describes the references. Otherwise, we lose track
1291 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1292 save_val_only
= wi
->val_only
;
1293 wi
->val_only
= true;
1295 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1297 if (TREE_CODE (t
) == COMPONENT_REF
)
1298 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1300 else if (TREE_CODE (t
) == ARRAY_REF
1301 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1303 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1305 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1307 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference
, wi
,
1310 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1312 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1314 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1318 wi
->val_only
= false;
1319 walk_tree (tp
, convert_local_reference
, wi
, NULL
);
1320 wi
->val_only
= save_val_only
;
1324 save_suppress
= info
->suppress_expansion
;
1325 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1328 (void) get_frame_type (info
);
1329 c
= build_omp_clause (OMP_CLAUSE_SHARED
);
1330 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1331 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1332 OMP_PARALLEL_CLAUSES (t
) = c
;
1335 save_local_var_chain
= info
->new_local_var_chain
;
1336 info
->new_local_var_chain
= NULL
;
1338 walk_body (convert_local_reference
, info
, &OMP_PARALLEL_BODY (t
));
1340 if (info
->new_local_var_chain
)
1341 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1342 info
->new_local_var_chain
= save_local_var_chain
;
1343 info
->suppress_expansion
= save_suppress
;
1349 save_suppress
= info
->suppress_expansion
;
1350 convert_local_omp_clauses (&OMP_CLAUSES (t
), wi
);
1351 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1352 info
->suppress_expansion
= save_suppress
;
1358 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1362 if (!IS_TYPE_OR_DECL_P (t
))
1365 wi
->val_only
= true;
1375 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1377 struct nesting_info
*info
= wi
->info
;
1378 bool need_frame
= false;
1381 bitmap new_suppress
;
1383 new_suppress
= BITMAP_GGC_ALLOC ();
1384 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1386 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1388 switch (OMP_CLAUSE_CODE (clause
))
1390 case OMP_CLAUSE_PRIVATE
:
1391 case OMP_CLAUSE_FIRSTPRIVATE
:
1392 case OMP_CLAUSE_LASTPRIVATE
:
1393 case OMP_CLAUSE_REDUCTION
:
1394 case OMP_CLAUSE_COPYPRIVATE
:
1395 case OMP_CLAUSE_SHARED
:
1396 decl
= OMP_CLAUSE_DECL (clause
);
1397 if (decl_function_context (decl
) == info
->context
1398 && !use_pointer_in_frame (decl
))
1400 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1403 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1404 OMP_CLAUSE_DECL (clause
)
1405 = get_local_debug_decl (info
, decl
, field
);
1411 case OMP_CLAUSE_SCHEDULE
:
1412 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1416 case OMP_CLAUSE_NUM_THREADS
:
1417 wi
->val_only
= true;
1419 convert_local_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
, wi
);
1422 case OMP_CLAUSE_NOWAIT
:
1423 case OMP_CLAUSE_ORDERED
:
1424 case OMP_CLAUSE_DEFAULT
:
1425 case OMP_CLAUSE_COPYIN
:
1433 info
->suppress_expansion
= new_suppress
;
1438 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1439 reference labels from outer functions. The rewrite will be a
1440 call to __builtin_nonlocal_goto. */
1443 convert_nl_goto_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1445 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1446 struct nesting_info
*info
= wi
->info
, *i
;
1447 tree t
= *tp
, label
, new_label
, target_context
, x
, arg
, field
;
1448 struct var_map_elt
*elt
, dummy
;
1452 if (TREE_CODE (t
) != GOTO_EXPR
)
1454 label
= GOTO_DESTINATION (t
);
1455 if (TREE_CODE (label
) != LABEL_DECL
)
1457 target_context
= decl_function_context (label
);
1458 if (target_context
== info
->context
)
1461 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1464 /* The original user label may also be use for a normal goto, therefore
1465 we must create a new label that will actually receive the abnormal
1466 control transfer. This new label will be marked LABEL_NONLOCAL; this
1467 mark will trigger proper behavior in the cfg, as well as cause the
1468 (hairy target-specific) non-local goto receiver code to be generated
1469 when we expand rtl. Enter this association into var_map so that we
1470 can insert the new label into the IL during a second pass. */
1472 slot
= htab_find_slot (i
->var_map
, &dummy
, INSERT
);
1473 elt
= (struct var_map_elt
*) *slot
;
1476 new_label
= create_artificial_label ();
1477 DECL_NONLOCAL (new_label
) = 1;
1479 elt
= GGC_NEW (struct var_map_elt
);
1481 elt
->new = new_label
;
1485 new_label
= elt
->new;
1487 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1488 field
= get_nl_goto_field (i
);
1489 x
= get_frame_field (info
, target_context
, field
, &wi
->tsi
);
1490 x
= build_addr (x
, target_context
);
1491 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1492 arg
= tree_cons (NULL
, x
, NULL
);
1493 x
= build_addr (new_label
, target_context
);
1494 arg
= tree_cons (NULL
, x
, arg
);
1495 x
= implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
];
1496 x
= build_function_call_expr (x
, arg
);
1498 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
1499 *tsi_stmt_ptr (wi
->tsi
) = x
;
1504 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1505 are referenced via nonlocal goto from a nested function. The rewrite
1506 will involve installing a newly generated DECL_NONLOCAL label, and
1507 (potentially) a branch around the rtl gunk that is assumed to be
1508 attached to such a label. */
1511 convert_nl_goto_receiver (tree
*tp
, int *walk_subtrees
, void *data
)
1513 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1514 struct nesting_info
*info
= wi
->info
;
1515 tree t
= *tp
, label
, new_label
, x
;
1516 struct var_map_elt
*elt
, dummy
;
1517 tree_stmt_iterator tmp_tsi
;
1520 if (TREE_CODE (t
) != LABEL_EXPR
)
1522 label
= LABEL_EXPR_LABEL (t
);
1525 elt
= (struct var_map_elt
*) htab_find (info
->var_map
, &dummy
);
1528 new_label
= elt
->new;
1530 /* If there's any possibility that the previous statement falls through,
1531 then we must branch around the new non-local label. */
1533 tsi_prev (&tmp_tsi
);
1534 if (tsi_end_p (tmp_tsi
) || block_may_fallthru (tsi_stmt (tmp_tsi
)))
1536 x
= build1 (GOTO_EXPR
, void_type_node
, label
);
1537 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1539 x
= build1 (LABEL_EXPR
, void_type_node
, new_label
);
1540 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1545 /* Called via walk_function+walk_tree, rewrite all references to addresses
1546 of nested functions that require the use of trampolines. The rewrite
1547 will involve a reference a trampoline generated for the occasion. */
1550 convert_tramp_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1552 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1553 struct nesting_info
*info
= wi
->info
, *i
;
1554 tree t
= *tp
, decl
, target_context
, x
, arg
;
1557 switch (TREE_CODE (t
))
1561 T.1 = &CHAIN->tramp;
1562 T.2 = __builtin_adjust_trampoline (T.1);
1563 T.3 = (func_type)T.2;
1566 decl
= TREE_OPERAND (t
, 0);
1567 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1570 /* Only need to process nested functions. */
1571 target_context
= decl_function_context (decl
);
1572 if (!target_context
)
1575 /* If the nested function doesn't use a static chain, then
1576 it doesn't need a trampoline. */
1577 if (DECL_NO_STATIC_CHAIN (decl
))
1580 /* Lookup the immediate parent of the callee, as that's where
1581 we need to insert the trampoline. */
1582 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1584 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1586 /* Compute the address of the field holding the trampoline. */
1587 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
1588 x
= build_addr (x
, target_context
);
1589 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1590 arg
= tree_cons (NULL
, x
, NULL
);
1592 /* Do machine-specific ugliness. Normally this will involve
1593 computing extra alignment, but it can really be anything. */
1594 x
= implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
];
1595 x
= build_function_call_expr (x
, arg
);
1596 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1598 /* Cast back to the proper function type. */
1599 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1600 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1606 /* Only walk call arguments, lest we generate trampolines for
1608 walk_tree (&TREE_OPERAND (t
, 1), convert_tramp_reference
, wi
, NULL
);
1612 if (!IS_TYPE_OR_DECL_P (t
))
1620 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1621 reference nested functions to make sure that the static chain is
1622 set up properly for the call. */
1625 convert_call_expr (tree
*tp
, int *walk_subtrees
, void *data
)
1627 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1628 struct nesting_info
*info
= wi
->info
;
1629 tree t
= *tp
, decl
, target_context
;
1630 char save_static_chain_added
;
1634 switch (TREE_CODE (t
))
1637 decl
= get_callee_fndecl (t
);
1640 target_context
= decl_function_context (decl
);
1641 if (target_context
&& !DECL_NO_STATIC_CHAIN (decl
))
1644 = get_static_chain (info
, target_context
, &wi
->tsi
);
1645 info
->static_chain_added
1646 |= (1 << (info
->context
!= target_context
));
1652 case WITH_SIZE_EXPR
:
1653 /* Only return modify and with_size_expr may contain calls. */
1658 save_static_chain_added
= info
->static_chain_added
;
1659 info
->static_chain_added
= 0;
1660 walk_body (convert_call_expr
, info
, &OMP_PARALLEL_BODY (t
));
1661 for (i
= 0; i
< 2; i
++)
1664 if ((info
->static_chain_added
& (1 << i
)) == 0)
1666 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
1667 /* Don't add CHAIN.* or FRAME.* twice. */
1668 for (c
= OMP_PARALLEL_CLAUSES (t
); c
; c
= OMP_CLAUSE_CHAIN (c
))
1669 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
1670 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
1671 && OMP_CLAUSE_DECL (c
) == decl
)
1675 c
= build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE
);
1676 OMP_CLAUSE_DECL (c
) = decl
;
1677 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1678 OMP_PARALLEL_CLAUSES (t
) = c
;
1681 info
->static_chain_added
|= save_static_chain_added
;
1691 walk_body (convert_call_expr
, info
, &OMP_BODY (t
));
1701 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1702 trampolines and call expressions. On the way back up, determine if
1703 a nested function actually uses its static chain; if not, remember that. */
1706 convert_all_function_calls (struct nesting_info
*root
)
1711 convert_all_function_calls (root
->inner
);
1713 walk_function (convert_tramp_reference
, root
);
1714 walk_function (convert_call_expr
, root
);
1716 /* If the function does not use a static chain, then remember that. */
1717 if (root
->outer
&& !root
->chain_decl
&& !root
->chain_field
)
1718 DECL_NO_STATIC_CHAIN (root
->context
) = 1;
1720 gcc_assert (!DECL_NO_STATIC_CHAIN (root
->context
));
1727 /* Do "everything else" to clean up or complete state collected by the
1728 various walking passes -- lay out the types and decls, generate code
1729 to initialize the frame decl, store critical expressions in the
1730 struct function for rtl to find. */
1733 finalize_nesting_tree_1 (struct nesting_info
*root
)
1735 tree stmt_list
= NULL
;
1736 tree context
= root
->context
;
1737 struct function
*sf
;
1739 /* If we created a non-local frame type or decl, we need to lay them
1740 out at this time. */
1741 if (root
->frame_type
)
1743 /* In some cases the frame type will trigger the -Wpadded warning.
1744 This is not helpful; suppress it. */
1745 int save_warn_padded
= warn_padded
;
1747 layout_type (root
->frame_type
);
1748 warn_padded
= save_warn_padded
;
1749 layout_decl (root
->frame_decl
, 0);
1752 /* If any parameters were referenced non-locally, then we need to
1753 insert a copy. Likewise, if any variables were referenced by
1754 pointer, we need to initialize the address. */
1755 if (root
->any_parm_remapped
)
1758 for (p
= DECL_ARGUMENTS (context
); p
; p
= TREE_CHAIN (p
))
1762 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
1766 if (use_pointer_in_frame (p
))
1767 x
= build_addr (p
, context
);
1771 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1772 root
->frame_decl
, field
, NULL_TREE
);
1773 x
= build2 (MODIFY_EXPR
, TREE_TYPE (field
), y
, x
);
1774 append_to_statement_list (x
, &stmt_list
);
1778 /* If a chain_field was created, then it needs to be initialized
1780 if (root
->chain_field
)
1782 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
1783 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
1784 x
= build2 (MODIFY_EXPR
, TREE_TYPE (x
), x
, get_chain_decl (root
));
1785 append_to_statement_list (x
, &stmt_list
);
1788 /* If trampolines were created, then we need to initialize them. */
1789 if (root
->any_tramp_created
)
1791 struct nesting_info
*i
;
1792 for (i
= root
->inner
; i
; i
= i
->next
)
1796 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
1800 if (DECL_NO_STATIC_CHAIN (i
->context
))
1801 x
= null_pointer_node
;
1803 x
= build_addr (root
->frame_decl
, context
);
1804 arg
= tree_cons (NULL
, x
, NULL
);
1806 x
= build_addr (i
->context
, context
);
1807 arg
= tree_cons (NULL
, x
, arg
);
1809 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1810 root
->frame_decl
, field
, NULL_TREE
);
1811 x
= build_addr (x
, context
);
1812 arg
= tree_cons (NULL
, x
, arg
);
1814 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
1815 x
= build_function_call_expr (x
, arg
);
1817 append_to_statement_list (x
, &stmt_list
);
1821 /* If we created initialization statements, insert them. */
1824 annotate_all_with_locus (&stmt_list
,
1825 DECL_SOURCE_LOCATION (context
));
1826 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context
)),
1828 BIND_EXPR_BODY (DECL_SAVED_TREE (context
)) = stmt_list
;
1831 /* If a chain_decl was created, then it needs to be registered with
1832 struct function so that it gets initialized from the static chain
1833 register at the beginning of the function. */
1834 sf
= DECL_STRUCT_FUNCTION (root
->context
);
1835 sf
->static_chain_decl
= root
->chain_decl
;
1837 /* Similarly for the non-local goto save area. */
1838 if (root
->nl_goto_field
)
1840 sf
->nonlocal_goto_save_area
1841 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
1842 sf
->has_nonlocal_label
= 1;
1845 /* Make sure all new local variables get inserted into the
1846 proper BIND_EXPR. */
1847 if (root
->new_local_var_chain
)
1848 declare_vars (root
->new_local_var_chain
, DECL_SAVED_TREE (root
->context
),
1850 if (root
->debug_var_chain
)
1851 declare_vars (root
->debug_var_chain
, DECL_SAVED_TREE (root
->context
),
1854 /* Dump the translated tree function. */
1855 dump_function (TDI_nested
, root
->context
);
1859 finalize_nesting_tree (struct nesting_info
*root
)
1864 finalize_nesting_tree (root
->inner
);
1865 finalize_nesting_tree_1 (root
);
1871 /* Unnest the nodes and pass them to cgraph. */
1874 unnest_nesting_tree_1 (struct nesting_info
*root
)
1876 struct cgraph_node
*node
= cgraph_node (root
->context
);
1878 /* For nested functions update the cgraph to reflect unnesting.
1879 We also delay finalizing of these functions up to this point. */
1882 cgraph_unnest_node (cgraph_node (root
->context
));
1883 cgraph_finalize_function (root
->context
, true);
1888 unnest_nesting_tree (struct nesting_info
*root
)
1893 unnest_nesting_tree (root
->inner
);
1894 unnest_nesting_tree_1 (root
);
1900 /* Free the data structures allocated during this pass. */
1903 free_nesting_tree (struct nesting_info
*root
)
1905 struct nesting_info
*next
;
1909 free_nesting_tree (root
->inner
);
1910 htab_delete (root
->var_map
);
1918 static GTY(()) struct nesting_info
*root
;
1920 /* Main entry point for this pass. Process FNDECL and all of its nested
1921 subroutines and turn them into something less tightly bound. */
1924 lower_nested_functions (tree fndecl
)
1926 struct cgraph_node
*cgn
;
1928 /* If there are no nested functions, there's nothing to do. */
1929 cgn
= cgraph_node (fndecl
);
1933 root
= create_nesting_tree (cgn
);
1934 walk_all_functions (convert_nonlocal_reference
, root
);
1935 walk_all_functions (convert_local_reference
, root
);
1936 walk_all_functions (convert_nl_goto_reference
, root
);
1937 walk_all_functions (convert_nl_goto_receiver
, root
);
1938 convert_all_function_calls (root
);
1939 finalize_nesting_tree (root
);
1940 unnest_nesting_tree (root
);
1941 free_nesting_tree (root
);
1945 #include "gt-tree-nested.h"