1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005, 2006, 2007 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
35 #include "langhooks.h"
36 #include "pointer-set.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. */
82 struct nesting_info
*outer
;
83 struct nesting_info
*inner
;
84 struct nesting_info
*next
;
86 struct pointer_map_t
*field_map
;
87 struct pointer_map_t
*var_map
;
88 bitmap suppress_expansion
;
91 tree new_local_var_chain
;
99 bool any_parm_remapped
;
100 bool any_tramp_created
;
101 char static_chain_added
;
105 /* Obstack used for the bitmaps in the struct above. */
106 static struct bitmap_obstack nesting_info_bitmap_obstack
;
109 /* We're working in so many different function contexts simultaneously,
110 that create_tmp_var is dangerous. Prevent mishap. */
111 #define create_tmp_var cant_use_create_tmp_var_here_dummy
113 /* Like create_tmp_var, except record the variable for registration at
114 the given nesting level. */
117 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
121 /* If the type is of variable size or a type which must be created by the
122 frontend, something is wrong. Note that we explicitly allow
123 incomplete types here, since we create them ourselves here. */
124 gcc_assert (!TREE_ADDRESSABLE (type
));
125 gcc_assert (!TYPE_SIZE_UNIT (type
)
126 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
128 tmp_var
= create_tmp_var_raw (type
, prefix
);
129 DECL_CONTEXT (tmp_var
) = info
->context
;
130 TREE_CHAIN (tmp_var
) = info
->new_local_var_chain
;
131 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
132 if (TREE_CODE (type
) == COMPLEX_TYPE
133 || TREE_CODE (type
) == VECTOR_TYPE
)
134 DECL_GIMPLE_REG_P (tmp_var
) = 1;
136 info
->new_local_var_chain
= tmp_var
;
141 /* Take the address of EXP to be used within function CONTEXT.
142 Mark it for addressability as necessary. */
145 build_addr (tree exp
, tree context
)
151 while (handled_component_p (base
))
152 base
= TREE_OPERAND (base
, 0);
155 TREE_ADDRESSABLE (base
) = 1;
157 /* Building the ADDR_EXPR will compute a set of properties for
158 that ADDR_EXPR. Those properties are unfortunately context
159 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
161 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
162 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
163 way the properties are for the ADDR_EXPR are computed properly. */
164 save_context
= current_function_decl
;
165 current_function_decl
= context
;
166 retval
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
167 current_function_decl
= save_context
;
171 /* Insert FIELD into TYPE, sorted by alignment requirements. */
174 insert_field_into_struct (tree type
, tree field
)
178 DECL_CONTEXT (field
) = type
;
180 for (p
= &TYPE_FIELDS (type
); *p
; p
= &TREE_CHAIN (*p
))
181 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
184 TREE_CHAIN (field
) = *p
;
188 /* Build or return the RECORD_TYPE that describes the frame state that is
189 shared between INFO->CONTEXT and its nested functions. This record will
190 not be complete until finalize_nesting_tree; up until that point we'll
191 be adding fields as necessary.
193 We also build the DECL that represents this frame in the function. */
196 get_frame_type (struct nesting_info
*info
)
198 tree type
= info
->frame_type
;
203 type
= make_node (RECORD_TYPE
);
205 name
= concat ("FRAME.",
206 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
208 TYPE_NAME (type
) = get_identifier (name
);
211 info
->frame_type
= type
;
212 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
214 /* ??? Always make it addressable for now, since it is meant to
215 be pointed to by the static chain pointer. This pessimizes
216 when it turns out that no static chains are needed because
217 the nested functions referencing non-local variables are not
218 reachable, but the true pessimization is to create the non-
219 local frame structure in the first place. */
220 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
225 /* Return true if DECL should be referenced by pointer in the non-local
229 use_pointer_in_frame (tree decl
)
231 if (TREE_CODE (decl
) == PARM_DECL
)
233 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
234 sized decls, and inefficient to copy large aggregates. Don't bother
235 moving anything but scalar variables. */
236 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
240 /* Variable sized types make things "interesting" in the frame. */
241 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
245 /* Given DECL, a non-locally accessed variable, find or create a field
246 in the non-local frame structure for the given nesting context. */
249 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
250 enum insert_option insert
)
254 if (insert
== NO_INSERT
)
256 slot
= pointer_map_contains (info
->field_map
, decl
);
257 return slot
? *slot
: NULL
;
260 slot
= pointer_map_insert (info
->field_map
, decl
);
263 tree field
= make_node (FIELD_DECL
);
264 DECL_NAME (field
) = DECL_NAME (decl
);
266 if (use_pointer_in_frame (decl
))
268 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
269 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
270 DECL_NONADDRESSABLE_P (field
) = 1;
274 TREE_TYPE (field
) = TREE_TYPE (decl
);
275 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
276 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
277 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
278 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
279 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
280 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
283 insert_field_into_struct (get_frame_type (info
), field
);
286 if (TREE_CODE (decl
) == PARM_DECL
)
287 info
->any_parm_remapped
= true;
293 /* Build or return the variable that holds the static chain within
294 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
297 get_chain_decl (struct nesting_info
*info
)
299 tree decl
= info
->chain_decl
;
304 type
= get_frame_type (info
->outer
);
305 type
= build_pointer_type (type
);
307 /* Note that this variable is *not* entered into any BIND_EXPR;
308 the construction of this variable is handled specially in
309 expand_function_start and initialize_inlined_parameters.
310 Note also that it's represented as a parameter. This is more
311 close to the truth, since the initial value does come from
313 decl
= build_decl (PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
314 DECL_ARTIFICIAL (decl
) = 1;
315 DECL_IGNORED_P (decl
) = 1;
316 TREE_USED (decl
) = 1;
317 DECL_CONTEXT (decl
) = info
->context
;
318 DECL_ARG_TYPE (decl
) = type
;
320 /* Tell tree-inline.c that we never write to this variable, so
321 it can copy-prop the replacement value immediately. */
322 TREE_READONLY (decl
) = 1;
324 info
->chain_decl
= decl
;
329 /* Build or return the field within the non-local frame state that holds
330 the static chain for INFO->CONTEXT. This is the way to walk back up
331 multiple nesting levels. */
334 get_chain_field (struct nesting_info
*info
)
336 tree field
= info
->chain_field
;
339 tree type
= build_pointer_type (get_frame_type (info
->outer
));
341 field
= make_node (FIELD_DECL
);
342 DECL_NAME (field
) = get_identifier ("__chain");
343 TREE_TYPE (field
) = type
;
344 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
345 DECL_NONADDRESSABLE_P (field
) = 1;
347 insert_field_into_struct (get_frame_type (info
), field
);
349 info
->chain_field
= field
;
354 /* Copy EXP into a temporary. Allocate the temporary in the context of
355 INFO and insert the initialization statement before TSI. */
358 init_tmp_var (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
362 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
363 stmt
= build_gimple_modify_stmt (t
, exp
);
364 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
365 tsi_link_before (tsi
, stmt
, TSI_SAME_STMT
);
370 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
373 tsi_gimplify_val (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
375 if (is_gimple_val (exp
))
378 return init_tmp_var (info
, exp
, tsi
);
381 /* Similarly, but copy from the temporary and insert the statement
382 after the iterator. */
385 save_tmp_var (struct nesting_info
*info
, tree exp
,
386 tree_stmt_iterator
*tsi
)
390 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
391 stmt
= build_gimple_modify_stmt (exp
, t
);
392 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
393 tsi_link_after (tsi
, stmt
, TSI_SAME_STMT
);
398 /* Build or return the type used to represent a nested function trampoline. */
400 static GTY(()) tree trampoline_type
;
403 get_trampoline_type (void)
406 unsigned align
, size
;
409 return trampoline_type
;
411 align
= TRAMPOLINE_ALIGNMENT
;
412 size
= TRAMPOLINE_SIZE
;
414 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
415 then allocate extra space so that we can do dynamic alignment. */
416 if (align
> STACK_BOUNDARY
)
418 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
419 align
= STACK_BOUNDARY
;
422 t
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
423 t
= build_array_type (char_type_node
, t
);
424 t
= build_decl (FIELD_DECL
, get_identifier ("__data"), t
);
425 DECL_ALIGN (t
) = align
;
426 DECL_USER_ALIGN (t
) = 1;
428 record
= make_node (RECORD_TYPE
);
429 TYPE_NAME (record
) = get_identifier ("__builtin_trampoline");
430 TYPE_FIELDS (record
) = t
;
431 layout_type (record
);
436 /* Given DECL, a nested function, find or create a field in the non-local
437 frame structure for a trampoline for this function. */
440 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
441 enum insert_option insert
)
445 if (insert
== NO_INSERT
)
447 slot
= pointer_map_contains (info
->var_map
, decl
);
448 return slot
? *slot
: NULL
;
451 slot
= pointer_map_insert (info
->var_map
, decl
);
454 tree field
= make_node (FIELD_DECL
);
455 DECL_NAME (field
) = DECL_NAME (decl
);
456 TREE_TYPE (field
) = get_trampoline_type ();
457 TREE_ADDRESSABLE (field
) = 1;
459 insert_field_into_struct (get_frame_type (info
), field
);
462 info
->any_tramp_created
= true;
468 /* Build or return the field within the non-local frame state that holds
469 the non-local goto "jmp_buf". The buffer itself is maintained by the
470 rtl middle-end as dynamic stack space is allocated. */
473 get_nl_goto_field (struct nesting_info
*info
)
475 tree field
= info
->nl_goto_field
;
481 /* For __builtin_nonlocal_goto, we need N words. The first is the
482 frame pointer, the rest is for the target's stack pointer save
483 area. The number of words is controlled by STACK_SAVEAREA_MODE;
484 not the best interface, but it'll do for now. */
485 if (Pmode
== ptr_mode
)
486 type
= ptr_type_node
;
488 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
490 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
491 size
= size
/ GET_MODE_SIZE (Pmode
);
494 type
= build_array_type
495 (type
, build_index_type (build_int_cst (NULL_TREE
, size
)));
497 field
= make_node (FIELD_DECL
);
498 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
499 TREE_TYPE (field
) = type
;
500 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
501 TREE_ADDRESSABLE (field
) = 1;
503 insert_field_into_struct (get_frame_type (info
), field
);
505 info
->nl_goto_field
= field
;
511 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
514 walk_asm_expr (struct walk_stmt_info
*wi
, tree stmt
)
516 int noutputs
= list_length (ASM_OUTPUTS (stmt
));
517 const char **oconstraints
518 = (const char **) alloca ((noutputs
) * sizeof (const char *));
521 const char *constraint
;
522 bool allows_mem
, allows_reg
, is_inout
;
525 for (i
=0, link
= ASM_OUTPUTS (stmt
); link
; ++i
, link
= TREE_CHAIN (link
))
527 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
528 oconstraints
[i
] = constraint
;
529 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
530 &allows_reg
, &is_inout
);
532 wi
->val_only
= (allows_reg
|| !allows_mem
);
533 walk_tree (&TREE_VALUE (link
), wi
->callback
, wi
, NULL
);
536 for (link
= ASM_INPUTS (stmt
); link
; link
= TREE_CHAIN (link
))
538 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
539 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
540 oconstraints
, &allows_mem
, &allows_reg
);
542 wi
->val_only
= (allows_reg
|| !allows_mem
);
543 /* Although input "m" is not really a LHS, we need a lvalue. */
544 wi
->is_lhs
= !wi
->val_only
;
545 walk_tree (&TREE_VALUE (link
), wi
->callback
, wi
, NULL
);
552 /* Iterate over all sub-statements of *TP calling walk_tree with
553 WI->CALLBACK for every sub-expression in each statement found. */
556 walk_stmts (struct walk_stmt_info
*wi
, tree
*tp
)
564 if (wi
->want_locations
&& EXPR_HAS_LOCATION (t
))
565 input_location
= EXPR_LOCATION (t
);
567 switch (TREE_CODE (t
))
571 tree_stmt_iterator i
;
572 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
575 walk_stmts (wi
, tsi_stmt_ptr (i
));
581 walk_tree (&COND_EXPR_COND (t
), wi
->callback
, wi
, NULL
);
582 walk_stmts (wi
, &COND_EXPR_THEN (t
));
583 walk_stmts (wi
, &COND_EXPR_ELSE (t
));
586 walk_stmts (wi
, &CATCH_BODY (t
));
589 walk_stmts (wi
, &EH_FILTER_FAILURE (t
));
592 case TRY_FINALLY_EXPR
:
593 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
594 walk_stmts (wi
, &TREE_OPERAND (t
, 1));
598 if (wi
->want_bind_expr
)
601 wi
->callback (tp
, &walk_subtrees
, wi
);
605 walk_stmts (wi
, &BIND_EXPR_BODY (t
));
609 if (wi
->want_return_expr
)
612 wi
->callback (tp
, &walk_subtrees
, wi
);
616 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
619 case GIMPLE_MODIFY_STMT
:
620 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
621 wi
->val_only
= !is_gimple_formal_tmp_var (GIMPLE_STMT_OPERAND (t
, 0));
622 walk_tree (&GIMPLE_STMT_OPERAND (t
, 1), wi
->callback
, wi
, NULL
);
624 /* If the rhs is appropriate for a memory, we may use a
625 COMPONENT_REF on the lhs. */
626 wi
->val_only
= !is_gimple_mem_rhs (GIMPLE_STMT_OPERAND (t
, 1));
628 walk_tree (&GIMPLE_STMT_OPERAND (t
, 0), wi
->callback
, wi
, NULL
);
635 walk_asm_expr (wi
, *tp
);
640 walk_tree (tp
, wi
->callback
, wi
, NULL
);
645 /* Invoke CALLBACK on all statements of *STMT_P. */
648 walk_body (walk_tree_fn callback
, struct nesting_info
*info
, tree
*stmt_p
)
650 struct walk_stmt_info wi
;
652 memset (&wi
, 0, sizeof (wi
));
653 wi
.callback
= callback
;
657 walk_stmts (&wi
, stmt_p
);
660 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
663 walk_function (walk_tree_fn callback
, struct nesting_info
*info
)
665 walk_body (callback
, info
, &DECL_SAVED_TREE (info
->context
));
668 /* Similarly for ROOT and all functions nested underneath, depth first. */
671 walk_all_functions (walk_tree_fn callback
, struct nesting_info
*root
)
676 walk_all_functions (callback
, root
->inner
);
677 walk_function (callback
, root
);
683 /* We have to check for a fairly pathological case. The operands of function
684 nested function are to be interpreted in the context of the enclosing
685 function. So if any are variably-sized, they will get remapped when the
686 enclosing function is inlined. But that remapping would also have to be
687 done in the types of the PARM_DECLs of the nested function, meaning the
688 argument types of that function will disagree with the arguments in the
689 calls to that function. So we'd either have to make a copy of the nested
690 function corresponding to each time the enclosing function was inlined or
691 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
692 function. The former is not practical. The latter would still require
693 detecting this case to know when to add the conversions. So, for now at
694 least, we don't inline such an enclosing function.
696 We have to do that check recursively, so here return indicating whether
697 FNDECL has such a nested function. ORIG_FN is the function we were
698 trying to inline to use for checking whether any argument is variably
699 modified by anything in it.
701 It would be better to do this in tree-inline.c so that we could give
702 the appropriate warning for why a function can't be inlined, but that's
703 too late since the nesting structure has already been flattened and
704 adding a flag just to record this fact seems a waste of a flag. */
707 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
709 struct cgraph_node
*cgn
= cgraph_node (fndecl
);
712 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
714 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= TREE_CHAIN (arg
))
715 if (variably_modified_type_p (TREE_TYPE (arg
), 0), orig_fndecl
)
718 if (check_for_nested_with_variably_modified (cgn
->decl
, orig_fndecl
))
725 /* Construct our local datastructure describing the function nesting
726 tree rooted by CGN. */
728 static struct nesting_info
*
729 create_nesting_tree (struct cgraph_node
*cgn
)
731 struct nesting_info
*info
= XCNEW (struct nesting_info
);
732 info
->field_map
= pointer_map_create ();
733 info
->var_map
= pointer_map_create ();
734 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
735 info
->context
= cgn
->decl
;
737 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
739 struct nesting_info
*sub
= create_nesting_tree (cgn
);
741 sub
->next
= info
->inner
;
745 /* See discussion at check_for_nested_with_variably_modified for a
746 discussion of why this has to be here. */
747 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
748 DECL_UNINLINABLE (info
->context
) = true;
753 /* Return an expression computing the static chain for TARGET_CONTEXT
754 from INFO->CONTEXT. Insert any necessary computations before TSI. */
757 get_static_chain (struct nesting_info
*info
, tree target_context
,
758 tree_stmt_iterator
*tsi
)
760 struct nesting_info
*i
;
763 if (info
->context
== target_context
)
765 x
= build_addr (info
->frame_decl
, target_context
);
769 x
= get_chain_decl (info
);
771 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
773 tree field
= get_chain_field (i
);
775 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
776 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
777 x
= init_tmp_var (info
, x
, tsi
);
784 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
785 frame as seen from INFO->CONTEXT. Insert any necessary computations
789 get_frame_field (struct nesting_info
*info
, tree target_context
,
790 tree field
, tree_stmt_iterator
*tsi
)
792 struct nesting_info
*i
;
795 if (info
->context
== target_context
)
797 /* Make sure frame_decl gets created. */
798 (void) get_frame_type (info
);
799 x
= info
->frame_decl
;
803 x
= get_chain_decl (info
);
805 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
807 tree field
= get_chain_field (i
);
809 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
810 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
811 x
= init_tmp_var (info
, x
, tsi
);
814 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
817 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
821 /* A subroutine of convert_nonlocal_reference. Create a local variable
822 in the nested function with DECL_VALUE_EXPR set to reference the true
823 variable in the parent function. This is used both for debug info
824 and in OpenMP lowering. */
827 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
830 struct nesting_info
*i
;
831 tree x
, field
, new_decl
;
834 slot
= pointer_map_insert (info
->var_map
, decl
);
839 target_context
= decl_function_context (decl
);
841 /* A copy of the code in get_frame_field, but without the temporaries. */
842 if (info
->context
== target_context
)
844 /* Make sure frame_decl gets created. */
845 (void) get_frame_type (info
);
846 x
= info
->frame_decl
;
851 x
= get_chain_decl (info
);
852 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
854 field
= get_chain_field (i
);
855 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
856 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
858 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
861 field
= lookup_field_for_decl (i
, decl
, INSERT
);
862 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
863 if (use_pointer_in_frame (decl
))
864 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
866 /* ??? We should be remapping types as well, surely. */
867 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
868 DECL_CONTEXT (new_decl
) = info
->context
;
869 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
870 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
871 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
872 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
873 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
874 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
875 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
876 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
878 SET_DECL_VALUE_EXPR (new_decl
, x
);
879 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
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
);
1037 case VIEW_CONVERT_EXPR
:
1038 /* Just request to look at the subtrees, leaving val_only and lhs
1039 untouched. This might actually be for !val_only + lhs, in which
1040 case we don't want to force a replacement by a temporary. */
1045 save_suppress
= info
->suppress_expansion
;
1046 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1049 decl
= get_chain_decl (info
);
1050 c
= build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE
);
1051 OMP_CLAUSE_DECL (c
) = decl
;
1052 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1053 OMP_PARALLEL_CLAUSES (t
) = c
;
1056 save_local_var_chain
= info
->new_local_var_chain
;
1057 info
->new_local_var_chain
= NULL
;
1059 walk_body (convert_nonlocal_reference
, info
, &OMP_PARALLEL_BODY (t
));
1061 if (info
->new_local_var_chain
)
1062 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1063 info
->new_local_var_chain
= save_local_var_chain
;
1064 info
->suppress_expansion
= save_suppress
;
1070 save_suppress
= info
->suppress_expansion
;
1071 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t
), wi
);
1072 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1073 info
->suppress_expansion
= save_suppress
;
1079 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1083 if (!IS_TYPE_OR_DECL_P (t
))
1086 wi
->val_only
= true;
1096 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1098 struct nesting_info
*info
= wi
->info
;
1099 bool need_chain
= false;
1102 bitmap new_suppress
;
1104 new_suppress
= BITMAP_GGC_ALLOC ();
1105 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1107 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1109 switch (OMP_CLAUSE_CODE (clause
))
1111 case OMP_CLAUSE_PRIVATE
:
1112 case OMP_CLAUSE_FIRSTPRIVATE
:
1113 case OMP_CLAUSE_LASTPRIVATE
:
1114 case OMP_CLAUSE_REDUCTION
:
1115 case OMP_CLAUSE_COPYPRIVATE
:
1116 case OMP_CLAUSE_SHARED
:
1117 decl
= OMP_CLAUSE_DECL (clause
);
1118 if (decl_function_context (decl
) != info
->context
)
1120 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1121 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1126 case OMP_CLAUSE_SCHEDULE
:
1127 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1131 case OMP_CLAUSE_NUM_THREADS
:
1132 wi
->val_only
= true;
1134 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1138 case OMP_CLAUSE_NOWAIT
:
1139 case OMP_CLAUSE_ORDERED
:
1140 case OMP_CLAUSE_DEFAULT
:
1141 case OMP_CLAUSE_COPYIN
:
1149 info
->suppress_expansion
= new_suppress
;
1154 /* A subroutine of convert_local_reference. Create a local variable
1155 in the parent function with DECL_VALUE_EXPR set to reference the
1156 field in FRAME. This is used both for debug info and in OpenMP
1160 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1165 slot
= pointer_map_insert (info
->var_map
, decl
);
1169 /* Make sure frame_decl gets created. */
1170 (void) get_frame_type (info
);
1171 x
= info
->frame_decl
;
1172 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1174 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1175 DECL_CONTEXT (new_decl
) = info
->context
;
1176 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
1177 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1178 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1179 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1180 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1181 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1182 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1183 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1185 SET_DECL_VALUE_EXPR (new_decl
, x
);
1186 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1189 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
1190 info
->debug_var_chain
= new_decl
;
1192 /* Do not emit debug info twice. */
1193 DECL_IGNORED_P (decl
) = 1;
1198 /* Called via walk_function+walk_tree, rewrite all references to VAR
1199 and PARM_DECLs that were referenced by inner nested functions.
1200 The rewrite will be a structure reference to the local frame variable. */
1202 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1205 convert_local_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1207 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1208 struct nesting_info
*info
= wi
->info
;
1209 tree t
= *tp
, field
, x
;
1211 tree save_local_var_chain
;
1212 bitmap save_suppress
;
1215 switch (TREE_CODE (t
))
1218 /* Non-automatic variables are never processed. */
1219 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1224 if (decl_function_context (t
) == info
->context
)
1226 /* If we copied a pointer to the frame, then the original decl
1227 is used unchanged in the parent function. */
1228 if (use_pointer_in_frame (t
))
1231 /* No need to transform anything if no child references the
1233 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1238 x
= get_local_debug_decl (info
, t
, field
);
1239 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1240 x
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
1245 x
= save_tmp_var (info
, x
, &wi
->tsi
);
1247 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1255 save_val_only
= wi
->val_only
;
1256 wi
->val_only
= false;
1258 wi
->changed
= false;
1259 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference
, wi
, NULL
);
1260 wi
->val_only
= save_val_only
;
1262 /* If we converted anything ... */
1267 /* Then the frame decl is now addressable. */
1268 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1270 save_context
= current_function_decl
;
1271 current_function_decl
= info
->context
;
1272 recompute_tree_invariant_for_addr_expr (t
);
1273 current_function_decl
= save_context
;
1275 /* If we are in a context where we only accept values, then
1276 compute the address into a temporary. */
1278 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1286 case ARRAY_RANGE_REF
:
1288 /* Go down this entire nest and just look at the final prefix and
1289 anything that describes the references. Otherwise, we lose track
1290 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1291 save_val_only
= wi
->val_only
;
1292 wi
->val_only
= true;
1294 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1296 if (TREE_CODE (t
) == COMPONENT_REF
)
1297 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1299 else if (TREE_CODE (t
) == ARRAY_REF
1300 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1302 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1304 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1306 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference
, wi
,
1309 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1311 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1313 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1317 wi
->val_only
= false;
1318 walk_tree (tp
, convert_local_reference
, wi
, NULL
);
1319 wi
->val_only
= save_val_only
;
1322 case VIEW_CONVERT_EXPR
:
1323 /* Just request to look at the subtrees, leaving val_only and lhs
1324 untouched. This might actually be for !val_only + lhs, in which
1325 case we don't want to force a replacement by a temporary. */
1330 save_suppress
= info
->suppress_expansion
;
1331 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1334 (void) get_frame_type (info
);
1335 c
= build_omp_clause (OMP_CLAUSE_SHARED
);
1336 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1337 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1338 OMP_PARALLEL_CLAUSES (t
) = c
;
1341 save_local_var_chain
= info
->new_local_var_chain
;
1342 info
->new_local_var_chain
= NULL
;
1344 walk_body (convert_local_reference
, info
, &OMP_PARALLEL_BODY (t
));
1346 if (info
->new_local_var_chain
)
1347 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1348 info
->new_local_var_chain
= save_local_var_chain
;
1349 info
->suppress_expansion
= save_suppress
;
1355 save_suppress
= info
->suppress_expansion
;
1356 convert_local_omp_clauses (&OMP_CLAUSES (t
), wi
);
1357 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1358 info
->suppress_expansion
= save_suppress
;
1364 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1368 if (!IS_TYPE_OR_DECL_P (t
))
1371 wi
->val_only
= true;
1381 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1383 struct nesting_info
*info
= wi
->info
;
1384 bool need_frame
= false;
1387 bitmap new_suppress
;
1389 new_suppress
= BITMAP_GGC_ALLOC ();
1390 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1392 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1394 switch (OMP_CLAUSE_CODE (clause
))
1396 case OMP_CLAUSE_PRIVATE
:
1397 case OMP_CLAUSE_FIRSTPRIVATE
:
1398 case OMP_CLAUSE_LASTPRIVATE
:
1399 case OMP_CLAUSE_REDUCTION
:
1400 case OMP_CLAUSE_COPYPRIVATE
:
1401 case OMP_CLAUSE_SHARED
:
1402 decl
= OMP_CLAUSE_DECL (clause
);
1403 if (decl_function_context (decl
) == info
->context
1404 && !use_pointer_in_frame (decl
))
1406 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1409 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1410 OMP_CLAUSE_DECL (clause
)
1411 = get_local_debug_decl (info
, decl
, field
);
1417 case OMP_CLAUSE_SCHEDULE
:
1418 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1422 case OMP_CLAUSE_NUM_THREADS
:
1423 wi
->val_only
= true;
1425 convert_local_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
, wi
);
1428 case OMP_CLAUSE_NOWAIT
:
1429 case OMP_CLAUSE_ORDERED
:
1430 case OMP_CLAUSE_DEFAULT
:
1431 case OMP_CLAUSE_COPYIN
:
1439 info
->suppress_expansion
= new_suppress
;
1444 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1445 reference labels from outer functions. The rewrite will be a
1446 call to __builtin_nonlocal_goto. */
1449 convert_nl_goto_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1451 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1452 struct nesting_info
*info
= wi
->info
, *i
;
1453 tree t
= *tp
, label
, new_label
, target_context
, x
, field
;
1457 if (TREE_CODE (t
) != GOTO_EXPR
)
1459 label
= GOTO_DESTINATION (t
);
1460 if (TREE_CODE (label
) != LABEL_DECL
)
1462 target_context
= decl_function_context (label
);
1463 if (target_context
== info
->context
)
1466 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1469 /* The original user label may also be use for a normal goto, therefore
1470 we must create a new label that will actually receive the abnormal
1471 control transfer. This new label will be marked LABEL_NONLOCAL; this
1472 mark will trigger proper behavior in the cfg, as well as cause the
1473 (hairy target-specific) non-local goto receiver code to be generated
1474 when we expand rtl. Enter this association into var_map so that we
1475 can insert the new label into the IL during a second pass. */
1476 slot
= pointer_map_insert (i
->var_map
, label
);
1479 new_label
= create_artificial_label ();
1480 DECL_NONLOCAL (new_label
) = 1;
1486 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1487 field
= get_nl_goto_field (i
);
1488 x
= get_frame_field (info
, target_context
, field
, &wi
->tsi
);
1489 x
= build_addr (x
, target_context
);
1490 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1491 x
= build_call_expr (implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
], 2,
1492 build_addr (new_label
, target_context
), x
);
1494 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
1495 *tsi_stmt_ptr (wi
->tsi
) = x
;
1500 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1501 are referenced via nonlocal goto from a nested function. The rewrite
1502 will involve installing a newly generated DECL_NONLOCAL label, and
1503 (potentially) a branch around the rtl gunk that is assumed to be
1504 attached to such a label. */
1507 convert_nl_goto_receiver (tree
*tp
, int *walk_subtrees
, void *data
)
1509 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1510 struct nesting_info
*info
= wi
->info
;
1511 tree t
= *tp
, label
, new_label
, x
;
1512 tree_stmt_iterator tmp_tsi
;
1516 if (TREE_CODE (t
) != LABEL_EXPR
)
1518 label
= LABEL_EXPR_LABEL (t
);
1520 slot
= pointer_map_contains (info
->var_map
, label
);
1524 /* If there's any possibility that the previous statement falls through,
1525 then we must branch around the new non-local label. */
1527 tsi_prev (&tmp_tsi
);
1528 if (tsi_end_p (tmp_tsi
) || block_may_fallthru (tsi_stmt (tmp_tsi
)))
1530 x
= build1 (GOTO_EXPR
, void_type_node
, label
);
1531 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1534 new_label
= (tree
) *slot
;
1535 x
= build1 (LABEL_EXPR
, void_type_node
, new_label
);
1536 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1541 /* Called via walk_function+walk_tree, rewrite all references to addresses
1542 of nested functions that require the use of trampolines. The rewrite
1543 will involve a reference a trampoline generated for the occasion. */
1546 convert_tramp_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1548 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1549 struct nesting_info
*info
= wi
->info
, *i
;
1550 tree t
= *tp
, decl
, target_context
, x
;
1553 switch (TREE_CODE (t
))
1557 T.1 = &CHAIN->tramp;
1558 T.2 = __builtin_adjust_trampoline (T.1);
1559 T.3 = (func_type)T.2;
1562 decl
= TREE_OPERAND (t
, 0);
1563 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1566 /* Only need to process nested functions. */
1567 target_context
= decl_function_context (decl
);
1568 if (!target_context
)
1571 /* If the nested function doesn't use a static chain, then
1572 it doesn't need a trampoline. */
1573 if (DECL_NO_STATIC_CHAIN (decl
))
1576 /* Lookup the immediate parent of the callee, as that's where
1577 we need to insert the trampoline. */
1578 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1580 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1582 /* Compute the address of the field holding the trampoline. */
1583 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
1584 x
= build_addr (x
, target_context
);
1585 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1587 /* Do machine-specific ugliness. Normally this will involve
1588 computing extra alignment, but it can really be anything. */
1589 x
= build_call_expr (implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
],
1591 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1593 /* Cast back to the proper function type. */
1594 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1595 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1601 /* Only walk call arguments, lest we generate trampolines for
1604 int nargs
= call_expr_nargs (t
);
1606 for (i
= 0; i
< nargs
; i
++)
1607 walk_tree (&CALL_EXPR_ARG (t
, i
), 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
))
1643 CALL_EXPR_STATIC_CHAIN (t
)
1644 = get_static_chain (info
, target_context
, &wi
->tsi
);
1645 info
->static_chain_added
1646 |= (1 << (info
->context
!= target_context
));
1651 case GIMPLE_MODIFY_STMT
:
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
= build_gimple_modify_stmt (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
= build_gimple_modify_stmt (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
)
1794 tree arg1
, arg2
, arg3
, x
, field
;
1796 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
1800 if (DECL_NO_STATIC_CHAIN (i
->context
))
1801 arg3
= null_pointer_node
;
1803 arg3
= build_addr (root
->frame_decl
, context
);
1805 arg2
= build_addr (i
->context
, context
);
1807 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1808 root
->frame_decl
, field
, NULL_TREE
);
1809 arg1
= build_addr (x
, context
);
1811 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
1812 x
= build_call_expr (x
, 3, arg1
, arg2
, arg3
);
1813 append_to_statement_list (x
, &stmt_list
);
1817 /* If we created initialization statements, insert them. */
1820 annotate_all_with_locus (&stmt_list
,
1821 DECL_SOURCE_LOCATION (context
));
1822 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context
)),
1824 BIND_EXPR_BODY (DECL_SAVED_TREE (context
)) = stmt_list
;
1827 /* If a chain_decl was created, then it needs to be registered with
1828 struct function so that it gets initialized from the static chain
1829 register at the beginning of the function. */
1830 sf
= DECL_STRUCT_FUNCTION (root
->context
);
1831 sf
->static_chain_decl
= root
->chain_decl
;
1833 /* Similarly for the non-local goto save area. */
1834 if (root
->nl_goto_field
)
1836 sf
->nonlocal_goto_save_area
1837 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
1838 sf
->has_nonlocal_label
= 1;
1841 /* Make sure all new local variables get inserted into the
1842 proper BIND_EXPR. */
1843 if (root
->new_local_var_chain
)
1844 declare_vars (root
->new_local_var_chain
, DECL_SAVED_TREE (root
->context
),
1846 if (root
->debug_var_chain
)
1847 declare_vars (root
->debug_var_chain
, DECL_SAVED_TREE (root
->context
),
1850 /* Dump the translated tree function. */
1851 dump_function (TDI_nested
, root
->context
);
1855 finalize_nesting_tree (struct nesting_info
*root
)
1860 finalize_nesting_tree (root
->inner
);
1861 finalize_nesting_tree_1 (root
);
1867 /* Unnest the nodes and pass them to cgraph. */
1870 unnest_nesting_tree_1 (struct nesting_info
*root
)
1872 struct cgraph_node
*node
= cgraph_node (root
->context
);
1874 /* For nested functions update the cgraph to reflect unnesting.
1875 We also delay finalizing of these functions up to this point. */
1878 cgraph_unnest_node (cgraph_node (root
->context
));
1879 cgraph_finalize_function (root
->context
, true);
1884 unnest_nesting_tree (struct nesting_info
*root
)
1889 unnest_nesting_tree (root
->inner
);
1890 unnest_nesting_tree_1 (root
);
1896 /* Free the data structures allocated during this pass. */
1899 free_nesting_tree (struct nesting_info
*root
)
1901 struct nesting_info
*next
;
1905 free_nesting_tree (root
->inner
);
1906 pointer_map_destroy (root
->var_map
);
1907 pointer_map_destroy (root
->field_map
);
1915 /* Main entry point for this pass. Process FNDECL and all of its nested
1916 subroutines and turn them into something less tightly bound. */
1919 lower_nested_functions (tree fndecl
)
1921 struct cgraph_node
*cgn
;
1922 struct nesting_info
*root
;
1924 /* If there are no nested functions, there's nothing to do. */
1925 cgn
= cgraph_node (fndecl
);
1929 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
1930 root
= create_nesting_tree (cgn
);
1931 walk_all_functions (convert_nonlocal_reference
, root
);
1932 walk_all_functions (convert_local_reference
, root
);
1933 walk_all_functions (convert_nl_goto_reference
, root
);
1934 walk_all_functions (convert_nl_goto_receiver
, root
);
1935 convert_all_function_calls (root
);
1936 finalize_nesting_tree (root
);
1937 unnest_nesting_tree (root
);
1938 free_nesting_tree (root
);
1939 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
1942 #include "gt-tree-nested.h"