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"
39 /* The object of this pass is to lower the representation of a set of nested
40 functions in order to expose all of the gory details of the various
41 nonlocal references. We want to do this sooner rather than later, in
42 order to give us more freedom in emitting all of the functions in question.
44 Back in olden times, when gcc was young, we developed an insanely
45 complicated scheme whereby variables which were referenced nonlocally
46 were forced to live in the stack of the declaring function, and then
47 the nested functions magically discovered where these variables were
48 placed. In order for this scheme to function properly, it required
49 that the outer function be partially expanded, then we switch to
50 compiling the inner function, and once done with those we switch back
51 to compiling the outer function. Such delicate ordering requirements
52 makes it difficult to do whole translation unit optimizations
53 involving such functions.
55 The implementation here is much more direct. Everything that can be
56 referenced by an inner function is a member of an explicitly created
57 structure herein called the "nonlocal frame struct". The incoming
58 static chain for a nested function is a pointer to this struct in
59 the parent. In this way, we settle on known offsets from a known
60 base, and so are decoupled from the logic that places objects in the
61 function's stack frame. More importantly, we don't have to wait for
62 that to happen -- since the compilation of the inner function is no
63 longer tied to a real stack frame, the nonlocal frame struct can be
64 allocated anywhere. Which means that the outer function is now
67 Theory of operation here is very simple. Iterate over all the
68 statements in all the functions (depth first) several times,
69 allocating structures and fields on demand. In general we want to
70 examine inner functions first, so that we can avoid making changes
71 to outer functions which are unnecessary.
73 The order of the passes matters a bit, in that later passes will be
74 skipped if it is discovered that the functions don't actually interact
75 at all. That is, they're nested in the lexical sense but could have
76 been written as independent functions without change. */
79 struct var_map_elt
GTY(())
85 struct nesting_info
GTY ((chain_next ("%h.next")))
87 struct nesting_info
*outer
;
88 struct nesting_info
*inner
;
89 struct nesting_info
*next
;
91 htab_t
GTY ((param_is (struct var_map_elt
))) field_map
;
92 htab_t
GTY ((param_is (struct var_map_elt
))) var_map
;
93 bitmap suppress_expansion
;
96 tree new_local_var_chain
;
104 bool any_parm_remapped
;
105 bool any_tramp_created
;
106 char static_chain_added
;
110 /* Hashing and equality functions for nesting_info->var_map. */
113 var_map_hash (const void *x
)
115 const struct var_map_elt
*a
= (const struct var_map_elt
*) x
;
116 return htab_hash_pointer (a
->old
);
120 var_map_eq (const void *x
, const void *y
)
122 const struct var_map_elt
*a
= (const struct var_map_elt
*) x
;
123 const struct var_map_elt
*b
= (const struct var_map_elt
*) y
;
124 return a
->old
== b
->old
;
127 /* We're working in so many different function contexts simultaneously,
128 that create_tmp_var is dangerous. Prevent mishap. */
129 #define create_tmp_var cant_use_create_tmp_var_here_dummy
131 /* Like create_tmp_var, except record the variable for registration at
132 the given nesting level. */
135 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
139 /* If the type is of variable size or a type which must be created by the
140 frontend, something is wrong. Note that we explicitly allow
141 incomplete types here, since we create them ourselves here. */
142 gcc_assert (!TREE_ADDRESSABLE (type
));
143 gcc_assert (!TYPE_SIZE_UNIT (type
)
144 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
146 tmp_var
= create_tmp_var_raw (type
, prefix
);
147 DECL_CONTEXT (tmp_var
) = info
->context
;
148 TREE_CHAIN (tmp_var
) = info
->new_local_var_chain
;
149 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
150 if (TREE_CODE (type
) == COMPLEX_TYPE
)
151 DECL_COMPLEX_GIMPLE_REG_P (tmp_var
) = 1;
153 info
->new_local_var_chain
= tmp_var
;
158 /* Take the address of EXP to be used within function CONTEXT.
159 Mark it for addressability as necessary. */
162 build_addr (tree exp
, tree context
)
168 while (handled_component_p (base
))
169 base
= TREE_OPERAND (base
, 0);
172 TREE_ADDRESSABLE (base
) = 1;
174 /* Building the ADDR_EXPR will compute a set of properties for
175 that ADDR_EXPR. Those properties are unfortunately context
176 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
178 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
179 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
180 way the properties are for the ADDR_EXPR are computed properly. */
181 save_context
= current_function_decl
;
182 current_function_decl
= context
;
183 retval
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
184 current_function_decl
= save_context
;;
188 /* Insert FIELD into TYPE, sorted by alignment requirements. */
191 insert_field_into_struct (tree type
, tree field
)
195 DECL_CONTEXT (field
) = type
;
197 for (p
= &TYPE_FIELDS (type
); *p
; p
= &TREE_CHAIN (*p
))
198 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
201 TREE_CHAIN (field
) = *p
;
205 /* Build or return the RECORD_TYPE that describes the frame state that is
206 shared between INFO->CONTEXT and its nested functions. This record will
207 not be complete until finalize_nesting_tree; up until that point we'll
208 be adding fields as necessary.
210 We also build the DECL that represents this frame in the function. */
213 get_frame_type (struct nesting_info
*info
)
215 tree type
= info
->frame_type
;
220 type
= make_node (RECORD_TYPE
);
222 name
= concat ("FRAME.",
223 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
225 TYPE_NAME (type
) = get_identifier (name
);
228 info
->frame_type
= type
;
229 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
231 /* ??? Always make it addressable for now, since it is meant to
232 be pointed to by the static chain pointer. This pessimizes
233 when it turns out that no static chains are needed because
234 the nested functions referencing non-local variables are not
235 reachable, but the true pessimization is to create the non-
236 local frame structure in the first place. */
237 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
242 /* Return true if DECL should be referenced by pointer in the non-local
246 use_pointer_in_frame (tree decl
)
248 if (TREE_CODE (decl
) == PARM_DECL
)
250 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
251 sized decls, and inefficient to copy large aggregates. Don't bother
252 moving anything but scalar variables. */
253 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
257 /* Variable sized types make things "interesting" in the frame. */
258 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
262 /* Given DECL, a non-locally accessed variable, find or create a field
263 in the non-local frame structure for the given nesting context. */
266 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
267 enum insert_option insert
)
269 struct var_map_elt
*elt
, dummy
;
274 slot
= htab_find_slot (info
->field_map
, &dummy
, insert
);
277 gcc_assert (insert
!= INSERT
);
280 elt
= (struct var_map_elt
*) *slot
;
282 if (!elt
&& insert
== INSERT
)
284 field
= make_node (FIELD_DECL
);
285 DECL_NAME (field
) = DECL_NAME (decl
);
287 if (use_pointer_in_frame (decl
))
289 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
290 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
291 DECL_NONADDRESSABLE_P (field
) = 1;
295 TREE_TYPE (field
) = TREE_TYPE (decl
);
296 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
297 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
298 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
299 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
300 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
301 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
304 insert_field_into_struct (get_frame_type (info
), field
);
306 elt
= GGC_NEW (struct var_map_elt
);
311 if (TREE_CODE (decl
) == PARM_DECL
)
312 info
->any_parm_remapped
= true;
315 field
= elt
? elt
->new : NULL
;
320 /* Build or return the variable that holds the static chain within
321 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
324 get_chain_decl (struct nesting_info
*info
)
326 tree decl
= info
->chain_decl
;
331 type
= get_frame_type (info
->outer
);
332 type
= build_pointer_type (type
);
334 /* Note that this variable is *not* entered into any BIND_EXPR;
335 the construction of this variable is handled specially in
336 expand_function_start and initialize_inlined_parameters.
337 Note also that it's represented as a parameter. This is more
338 close to the truth, since the initial value does come from
340 decl
= build_decl (PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
341 DECL_ARTIFICIAL (decl
) = 1;
342 DECL_IGNORED_P (decl
) = 1;
343 TREE_USED (decl
) = 1;
344 DECL_CONTEXT (decl
) = info
->context
;
345 DECL_ARG_TYPE (decl
) = type
;
347 /* Tell tree-inline.c that we never write to this variable, so
348 it can copy-prop the replacement value immediately. */
349 TREE_READONLY (decl
) = 1;
351 info
->chain_decl
= decl
;
356 /* Build or return the field within the non-local frame state that holds
357 the static chain for INFO->CONTEXT. This is the way to walk back up
358 multiple nesting levels. */
361 get_chain_field (struct nesting_info
*info
)
363 tree field
= info
->chain_field
;
366 tree type
= build_pointer_type (get_frame_type (info
->outer
));
368 field
= make_node (FIELD_DECL
);
369 DECL_NAME (field
) = get_identifier ("__chain");
370 TREE_TYPE (field
) = type
;
371 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
372 DECL_NONADDRESSABLE_P (field
) = 1;
374 insert_field_into_struct (get_frame_type (info
), field
);
376 info
->chain_field
= field
;
381 /* Copy EXP into a temporary. Allocate the temporary in the context of
382 INFO and insert the initialization statement before TSI. */
385 init_tmp_var (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
389 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
390 stmt
= build2 (MODIFY_EXPR
, TREE_TYPE (t
), t
, exp
);
391 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
392 tsi_link_before (tsi
, stmt
, TSI_SAME_STMT
);
397 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
400 tsi_gimplify_val (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
402 if (is_gimple_val (exp
))
405 return init_tmp_var (info
, exp
, tsi
);
408 /* Similarly, but copy from the temporary and insert the statement
409 after the iterator. */
412 save_tmp_var (struct nesting_info
*info
, tree exp
,
413 tree_stmt_iterator
*tsi
)
417 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
418 stmt
= build2 (MODIFY_EXPR
, TREE_TYPE (t
), exp
, t
);
419 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
420 tsi_link_after (tsi
, stmt
, TSI_SAME_STMT
);
425 /* Build or return the type used to represent a nested function trampoline. */
427 static GTY(()) tree trampoline_type
;
430 get_trampoline_type (void)
433 unsigned align
, size
;
436 return trampoline_type
;
438 align
= TRAMPOLINE_ALIGNMENT
;
439 size
= TRAMPOLINE_SIZE
;
441 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
442 then allocate extra space so that we can do dynamic alignment. */
443 if (align
> STACK_BOUNDARY
)
445 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
446 align
= STACK_BOUNDARY
;
449 t
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
450 t
= build_array_type (char_type_node
, t
);
451 t
= build_decl (FIELD_DECL
, get_identifier ("__data"), t
);
452 DECL_ALIGN (t
) = align
;
453 DECL_USER_ALIGN (t
) = 1;
455 record
= make_node (RECORD_TYPE
);
456 TYPE_NAME (record
) = get_identifier ("__builtin_trampoline");
457 TYPE_FIELDS (record
) = t
;
458 layout_type (record
);
463 /* Given DECL, a nested function, find or create a field in the non-local
464 frame structure for a trampoline for this function. */
467 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
468 enum insert_option insert
)
470 struct var_map_elt
*elt
, dummy
;
475 slot
= htab_find_slot (info
->var_map
, &dummy
, insert
);
478 gcc_assert (insert
!= INSERT
);
481 elt
= (struct var_map_elt
*) *slot
;
483 if (!elt
&& insert
== INSERT
)
485 field
= make_node (FIELD_DECL
);
486 DECL_NAME (field
) = DECL_NAME (decl
);
487 TREE_TYPE (field
) = get_trampoline_type ();
488 TREE_ADDRESSABLE (field
) = 1;
490 insert_field_into_struct (get_frame_type (info
), field
);
492 elt
= GGC_NEW (struct var_map_elt
);
497 info
->any_tramp_created
= true;
500 field
= elt
? elt
->new : NULL
;
505 /* Build or return the field within the non-local frame state that holds
506 the non-local goto "jmp_buf". The buffer itself is maintained by the
507 rtl middle-end as dynamic stack space is allocated. */
510 get_nl_goto_field (struct nesting_info
*info
)
512 tree field
= info
->nl_goto_field
;
518 /* For __builtin_nonlocal_goto, we need N words. The first is the
519 frame pointer, the rest is for the target's stack pointer save
520 area. The number of words is controlled by STACK_SAVEAREA_MODE;
521 not the best interface, but it'll do for now. */
522 if (Pmode
== ptr_mode
)
523 type
= ptr_type_node
;
525 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
527 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
528 size
= size
/ GET_MODE_SIZE (Pmode
);
531 type
= build_array_type
532 (type
, build_index_type (build_int_cst (NULL_TREE
, size
)));
534 field
= make_node (FIELD_DECL
);
535 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
536 TREE_TYPE (field
) = type
;
537 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
538 TREE_ADDRESSABLE (field
) = 1;
540 insert_field_into_struct (get_frame_type (info
), field
);
542 info
->nl_goto_field
= field
;
548 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
551 walk_asm_expr (struct walk_stmt_info
*wi
, tree stmt
)
553 int noutputs
= list_length (ASM_OUTPUTS (stmt
));
554 const char **oconstraints
555 = (const char **) alloca ((noutputs
) * sizeof (const char *));
558 const char *constraint
;
559 bool allows_mem
, allows_reg
, is_inout
;
562 for (i
=0, link
= ASM_OUTPUTS (stmt
); link
; ++i
, link
= TREE_CHAIN (link
))
564 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
565 oconstraints
[i
] = constraint
;
566 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
567 &allows_reg
, &is_inout
);
569 wi
->val_only
= (allows_reg
|| !allows_mem
);
570 walk_tree (&TREE_VALUE (link
), wi
->callback
, wi
, NULL
);
573 for (link
= ASM_INPUTS (stmt
); link
; link
= TREE_CHAIN (link
))
575 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
576 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
577 oconstraints
, &allows_mem
, &allows_reg
);
579 wi
->val_only
= (allows_reg
|| !allows_mem
);
580 /* Although input "m" is not really a LHS, we need a lvalue. */
581 wi
->is_lhs
= !wi
->val_only
;
582 walk_tree (&TREE_VALUE (link
), wi
->callback
, wi
, NULL
);
589 /* Iterate over all sub-statements of *TP calling walk_tree with
590 WI->CALLBACK for every sub-expression in each statement found. */
593 walk_stmts (struct walk_stmt_info
*wi
, tree
*tp
)
601 if (wi
->want_locations
&& EXPR_HAS_LOCATION (t
))
602 input_location
= EXPR_LOCATION (t
);
604 switch (TREE_CODE (t
))
608 tree_stmt_iterator i
;
609 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
612 walk_stmts (wi
, tsi_stmt_ptr (i
));
618 walk_tree (&COND_EXPR_COND (t
), wi
->callback
, wi
, NULL
);
619 walk_stmts (wi
, &COND_EXPR_THEN (t
));
620 walk_stmts (wi
, &COND_EXPR_ELSE (t
));
623 walk_stmts (wi
, &CATCH_BODY (t
));
626 walk_stmts (wi
, &EH_FILTER_FAILURE (t
));
629 case TRY_FINALLY_EXPR
:
630 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
631 walk_stmts (wi
, &TREE_OPERAND (t
, 1));
635 if (wi
->want_bind_expr
)
638 wi
->callback (tp
, &walk_subtrees
, wi
);
642 walk_stmts (wi
, &BIND_EXPR_BODY (t
));
646 if (wi
->want_return_expr
)
649 wi
->callback (tp
, &walk_subtrees
, wi
);
653 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
657 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
658 wi
->val_only
= !is_gimple_formal_tmp_var (TREE_OPERAND (t
, 0));
659 walk_tree (&TREE_OPERAND (t
, 1), wi
->callback
, wi
, NULL
);
661 /* If the rhs is appropriate for a memory, we may use a
662 COMPONENT_REF on the lhs. */
663 wi
->val_only
= !is_gimple_mem_rhs (TREE_OPERAND (t
, 1));
665 walk_tree (&TREE_OPERAND (t
, 0), wi
->callback
, wi
, NULL
);
672 walk_asm_expr (wi
, *tp
);
677 walk_tree (tp
, wi
->callback
, wi
, NULL
);
682 /* Invoke CALLBACK on all statements of *STMT_P. */
685 walk_body (walk_tree_fn callback
, struct nesting_info
*info
, tree
*stmt_p
)
687 struct walk_stmt_info wi
;
689 memset (&wi
, 0, sizeof (wi
));
690 wi
.callback
= callback
;
694 walk_stmts (&wi
, stmt_p
);
697 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
700 walk_function (walk_tree_fn callback
, struct nesting_info
*info
)
702 walk_body (callback
, info
, &DECL_SAVED_TREE (info
->context
));
705 /* Similarly for ROOT and all functions nested underneath, depth first. */
708 walk_all_functions (walk_tree_fn callback
, struct nesting_info
*root
)
713 walk_all_functions (callback
, root
->inner
);
714 walk_function (callback
, root
);
720 /* We have to check for a fairly pathological case. The operands of function
721 nested function are to be interpreted in the context of the enclosing
722 function. So if any are variably-sized, they will get remapped when the
723 enclosing function is inlined. But that remapping would also have to be
724 done in the types of the PARM_DECLs of the nested function, meaning the
725 argument types of that function will disagree with the arguments in the
726 calls to that function. So we'd either have to make a copy of the nested
727 function corresponding to each time the enclosing function was inlined or
728 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
729 function. The former is not practical. The latter would still require
730 detecting this case to know when to add the conversions. So, for now at
731 least, we don't inline such an enclosing function.
733 We have to do that check recursively, so here return indicating whether
734 FNDECL has such a nested function. ORIG_FN is the function we were
735 trying to inline to use for checking whether any argument is variably
736 modified by anything in it.
738 It would be better to do this in tree-inline.c so that we could give
739 the appropriate warning for why a function can't be inlined, but that's
740 too late since the nesting structure has already been flattened and
741 adding a flag just to record this fact seems a waste of a flag. */
744 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
746 struct cgraph_node
*cgn
= cgraph_node (fndecl
);
749 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
751 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= TREE_CHAIN (arg
))
752 if (variably_modified_type_p (TREE_TYPE (arg
), 0), orig_fndecl
)
755 if (check_for_nested_with_variably_modified (cgn
->decl
, orig_fndecl
))
762 /* Construct our local datastructure describing the function nesting
763 tree rooted by CGN. */
765 static struct nesting_info
*
766 create_nesting_tree (struct cgraph_node
*cgn
)
768 struct nesting_info
*info
= GGC_CNEW (struct nesting_info
);
769 info
->field_map
= htab_create_ggc (7, var_map_hash
, var_map_eq
, ggc_free
);
770 info
->var_map
= htab_create_ggc (7, var_map_hash
, var_map_eq
, ggc_free
);
771 info
->suppress_expansion
= BITMAP_GGC_ALLOC ();
772 info
->context
= cgn
->decl
;
774 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
776 struct nesting_info
*sub
= create_nesting_tree (cgn
);
778 sub
->next
= info
->inner
;
782 /* See discussion at check_for_nested_with_variably_modified for a
783 discussion of why this has to be here. */
784 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
785 DECL_UNINLINABLE (info
->context
) = true;
790 /* Return an expression computing the static chain for TARGET_CONTEXT
791 from INFO->CONTEXT. Insert any necessary computations before TSI. */
794 get_static_chain (struct nesting_info
*info
, tree target_context
,
795 tree_stmt_iterator
*tsi
)
797 struct nesting_info
*i
;
800 if (info
->context
== target_context
)
802 x
= build_addr (info
->frame_decl
, target_context
);
806 x
= get_chain_decl (info
);
808 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
810 tree field
= get_chain_field (i
);
812 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
813 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
814 x
= init_tmp_var (info
, x
, tsi
);
821 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
822 frame as seen from INFO->CONTEXT. Insert any necessary computations
826 get_frame_field (struct nesting_info
*info
, tree target_context
,
827 tree field
, tree_stmt_iterator
*tsi
)
829 struct nesting_info
*i
;
832 if (info
->context
== target_context
)
834 /* Make sure frame_decl gets created. */
835 (void) get_frame_type (info
);
836 x
= info
->frame_decl
;
840 x
= get_chain_decl (info
);
842 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
844 tree field
= get_chain_field (i
);
846 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
847 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
848 x
= init_tmp_var (info
, x
, tsi
);
851 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
854 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
858 /* A subroutine of convert_nonlocal_reference. Create a local variable
859 in the nested function with DECL_VALUE_EXPR set to reference the true
860 variable in the parent function. This is used both for debug info
861 and in OpenMP lowering. */
864 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
866 struct var_map_elt
*elt
, dummy
;
868 struct nesting_info
*i
;
869 tree x
, field
, new_decl
;
873 slot
= htab_find_slot (info
->var_map
, &dummy
, INSERT
);
879 target_context
= decl_function_context (decl
);
881 /* A copy of the code in get_frame_field, but without the temporaries. */
882 if (info
->context
== target_context
)
884 /* Make sure frame_decl gets created. */
885 (void) get_frame_type (info
);
886 x
= info
->frame_decl
;
891 x
= get_chain_decl (info
);
892 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
894 field
= get_chain_field (i
);
895 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
896 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
898 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
901 field
= lookup_field_for_decl (i
, decl
, INSERT
);
902 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
903 if (use_pointer_in_frame (decl
))
904 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
906 /* ??? We should be remapping types as well, surely. */
907 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
908 DECL_CONTEXT (new_decl
) = info
->context
;
909 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
910 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
911 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
912 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
913 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
914 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
915 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
916 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
918 SET_DECL_VALUE_EXPR (new_decl
, x
);
919 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
921 elt
= ggc_alloc (sizeof (*elt
));
926 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
927 info
->debug_var_chain
= new_decl
;
932 /* Called via walk_function+walk_tree, rewrite all references to VAR
933 and PARM_DECLs that belong to outer functions.
935 The rewrite will involve some number of structure accesses back up
936 the static chain. E.g. for a variable FOO up one nesting level it'll
937 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
938 indirections apply to decls for which use_pointer_in_frame is true. */
940 static bool convert_nonlocal_omp_clauses (tree
*, struct walk_stmt_info
*);
943 convert_nonlocal_reference (tree
*tp
, int *walk_subtrees
, void *data
)
945 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
946 struct nesting_info
*info
= wi
->info
;
948 tree save_local_var_chain
;
949 bitmap save_suppress
;
952 switch (TREE_CODE (t
))
955 /* Non-automatic variables are never processed. */
956 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
961 if (decl_function_context (t
) != info
->context
)
966 x
= get_nonlocal_debug_decl (info
, t
);
967 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
969 tree target_context
= decl_function_context (t
);
970 struct nesting_info
*i
;
971 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
973 x
= lookup_field_for_decl (i
, t
, INSERT
);
974 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
975 if (use_pointer_in_frame (t
))
977 x
= init_tmp_var (info
, x
, &wi
->tsi
);
978 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
985 x
= save_tmp_var (info
, x
, &wi
->tsi
);
987 x
= init_tmp_var (info
, x
, &wi
->tsi
);
995 /* Don't walk non-local gotos for now. */
996 if (TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
)
1005 /* We're taking the address of a label from a parent function, but
1006 this is not itself a non-local goto. Mark the label such that it
1007 will not be deleted, much as we would with a label address in
1009 if (decl_function_context (t
) != info
->context
)
1010 FORCED_LABEL (t
) = 1;
1015 bool save_val_only
= wi
->val_only
;
1017 wi
->val_only
= false;
1019 wi
->changed
= false;
1020 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference
, wi
, NULL
);
1021 wi
->val_only
= true;
1027 /* If we changed anything, then TREE_INVARIANT is be wrong,
1028 since we're no longer directly referencing a decl. */
1029 save_context
= current_function_decl
;
1030 current_function_decl
= info
->context
;
1031 recompute_tree_invariant_for_addr_expr (t
);
1032 current_function_decl
= save_context
;
1034 /* If the callback converted the address argument in a context
1035 where we only accept variables (and min_invariant, presumably),
1036 then compute the address into a temporary. */
1038 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1047 case ARRAY_RANGE_REF
:
1049 /* Go down this entire nest and just look at the final prefix and
1050 anything that describes the references. Otherwise, we lose track
1051 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1052 wi
->val_only
= true;
1054 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1056 if (TREE_CODE (t
) == COMPONENT_REF
)
1057 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1059 else if (TREE_CODE (t
) == ARRAY_REF
1060 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1062 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
1064 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1066 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference
, wi
,
1069 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1071 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
1073 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1077 wi
->val_only
= false;
1078 walk_tree (tp
, convert_nonlocal_reference
, wi
, NULL
);
1082 save_suppress
= info
->suppress_expansion
;
1083 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1086 decl
= get_chain_decl (info
);
1087 c
= build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE
);
1088 OMP_CLAUSE_DECL (c
) = decl
;
1089 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1090 OMP_PARALLEL_CLAUSES (t
) = c
;
1093 save_local_var_chain
= info
->new_local_var_chain
;
1094 info
->new_local_var_chain
= NULL
;
1096 walk_body (convert_nonlocal_reference
, info
, &OMP_PARALLEL_BODY (t
));
1098 if (info
->new_local_var_chain
)
1099 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1100 info
->new_local_var_chain
= save_local_var_chain
;
1101 info
->suppress_expansion
= save_suppress
;
1107 save_suppress
= info
->suppress_expansion
;
1108 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t
), wi
);
1109 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1110 info
->suppress_expansion
= save_suppress
;
1116 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1120 if (!IS_TYPE_OR_DECL_P (t
))
1123 wi
->val_only
= true;
1133 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1135 struct nesting_info
*info
= wi
->info
;
1136 bool need_chain
= false;
1139 bitmap new_suppress
;
1141 new_suppress
= BITMAP_GGC_ALLOC ();
1142 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1144 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1146 switch (OMP_CLAUSE_CODE (clause
))
1148 case OMP_CLAUSE_PRIVATE
:
1149 case OMP_CLAUSE_FIRSTPRIVATE
:
1150 case OMP_CLAUSE_LASTPRIVATE
:
1151 case OMP_CLAUSE_REDUCTION
:
1152 case OMP_CLAUSE_COPYPRIVATE
:
1153 case OMP_CLAUSE_SHARED
:
1154 decl
= OMP_CLAUSE_DECL (clause
);
1155 if (decl_function_context (decl
) != info
->context
)
1157 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1158 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1163 case OMP_CLAUSE_SCHEDULE
:
1164 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1168 case OMP_CLAUSE_NUM_THREADS
:
1169 wi
->val_only
= true;
1171 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1175 case OMP_CLAUSE_NOWAIT
:
1176 case OMP_CLAUSE_ORDERED
:
1177 case OMP_CLAUSE_DEFAULT
:
1178 case OMP_CLAUSE_COPYIN
:
1186 info
->suppress_expansion
= new_suppress
;
1191 /* A subroutine of convert_local_reference. Create a local variable
1192 in the parent function with DECL_VALUE_EXPR set to reference the
1193 field in FRAME. This is used both for debug info and in OpenMP
1197 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1199 struct var_map_elt
*elt
, dummy
;
1204 slot
= htab_find_slot (info
->var_map
, &dummy
, INSERT
);
1210 /* Make sure frame_decl gets created. */
1211 (void) get_frame_type (info
);
1212 x
= info
->frame_decl
;
1213 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1215 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1216 DECL_CONTEXT (new_decl
) = info
->context
;
1217 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
1218 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1219 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1220 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1221 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1222 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1223 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1224 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1226 SET_DECL_VALUE_EXPR (new_decl
, x
);
1227 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1229 elt
= ggc_alloc (sizeof (*elt
));
1231 elt
->new = new_decl
;
1234 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
1235 info
->debug_var_chain
= new_decl
;
1237 /* Do not emit debug info twice. */
1238 DECL_IGNORED_P (decl
) = 1;
1243 /* Called via walk_function+walk_tree, rewrite all references to VAR
1244 and PARM_DECLs that were referenced by inner nested functions.
1245 The rewrite will be a structure reference to the local frame variable. */
1247 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1250 convert_local_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1252 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1253 struct nesting_info
*info
= wi
->info
;
1254 tree t
= *tp
, field
, x
;
1256 tree save_local_var_chain
;
1257 bitmap save_suppress
;
1260 switch (TREE_CODE (t
))
1263 /* Non-automatic variables are never processed. */
1264 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1269 if (decl_function_context (t
) == info
->context
)
1271 /* If we copied a pointer to the frame, then the original decl
1272 is used unchanged in the parent function. */
1273 if (use_pointer_in_frame (t
))
1276 /* No need to transform anything if no child references the
1278 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1283 x
= get_local_debug_decl (info
, t
, field
);
1284 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1285 x
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
1290 x
= save_tmp_var (info
, x
, &wi
->tsi
);
1292 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1300 save_val_only
= wi
->val_only
;
1301 wi
->val_only
= false;
1303 wi
->changed
= false;
1304 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference
, wi
, NULL
);
1305 wi
->val_only
= save_val_only
;
1307 /* If we converted anything ... */
1312 /* Then the frame decl is now addressable. */
1313 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1315 save_context
= current_function_decl
;
1316 current_function_decl
= info
->context
;
1317 recompute_tree_invariant_for_addr_expr (t
);
1318 current_function_decl
= save_context
;
1320 /* If we are in a context where we only accept values, then
1321 compute the address into a temporary. */
1323 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1331 case ARRAY_RANGE_REF
:
1333 /* Go down this entire nest and just look at the final prefix and
1334 anything that describes the references. Otherwise, we lose track
1335 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1336 save_val_only
= wi
->val_only
;
1337 wi
->val_only
= true;
1339 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1341 if (TREE_CODE (t
) == COMPONENT_REF
)
1342 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1344 else if (TREE_CODE (t
) == ARRAY_REF
1345 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1347 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1349 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1351 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference
, wi
,
1354 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1356 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1358 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1362 wi
->val_only
= false;
1363 walk_tree (tp
, convert_local_reference
, wi
, NULL
);
1364 wi
->val_only
= save_val_only
;
1368 save_suppress
= info
->suppress_expansion
;
1369 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1372 (void) get_frame_type (info
);
1373 c
= build_omp_clause (OMP_CLAUSE_SHARED
);
1374 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1375 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1376 OMP_PARALLEL_CLAUSES (t
) = c
;
1379 save_local_var_chain
= info
->new_local_var_chain
;
1380 info
->new_local_var_chain
= NULL
;
1382 walk_body (convert_local_reference
, info
, &OMP_PARALLEL_BODY (t
));
1384 if (info
->new_local_var_chain
)
1385 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1386 info
->new_local_var_chain
= save_local_var_chain
;
1387 info
->suppress_expansion
= save_suppress
;
1393 save_suppress
= info
->suppress_expansion
;
1394 convert_local_omp_clauses (&OMP_CLAUSES (t
), wi
);
1395 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1396 info
->suppress_expansion
= save_suppress
;
1402 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1406 if (!IS_TYPE_OR_DECL_P (t
))
1409 wi
->val_only
= true;
1419 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1421 struct nesting_info
*info
= wi
->info
;
1422 bool need_frame
= false;
1425 bitmap new_suppress
;
1427 new_suppress
= BITMAP_GGC_ALLOC ();
1428 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1430 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1432 switch (OMP_CLAUSE_CODE (clause
))
1434 case OMP_CLAUSE_PRIVATE
:
1435 case OMP_CLAUSE_FIRSTPRIVATE
:
1436 case OMP_CLAUSE_LASTPRIVATE
:
1437 case OMP_CLAUSE_REDUCTION
:
1438 case OMP_CLAUSE_COPYPRIVATE
:
1439 case OMP_CLAUSE_SHARED
:
1440 decl
= OMP_CLAUSE_DECL (clause
);
1441 if (decl_function_context (decl
) == info
->context
1442 && !use_pointer_in_frame (decl
))
1444 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1447 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1448 OMP_CLAUSE_DECL (clause
)
1449 = get_local_debug_decl (info
, decl
, field
);
1455 case OMP_CLAUSE_SCHEDULE
:
1456 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1460 case OMP_CLAUSE_NUM_THREADS
:
1461 wi
->val_only
= true;
1463 convert_local_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
, wi
);
1466 case OMP_CLAUSE_NOWAIT
:
1467 case OMP_CLAUSE_ORDERED
:
1468 case OMP_CLAUSE_DEFAULT
:
1469 case OMP_CLAUSE_COPYIN
:
1477 info
->suppress_expansion
= new_suppress
;
1482 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1483 reference labels from outer functions. The rewrite will be a
1484 call to __builtin_nonlocal_goto. */
1487 convert_nl_goto_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1489 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1490 struct nesting_info
*info
= wi
->info
, *i
;
1491 tree t
= *tp
, label
, new_label
, target_context
, x
, arg
, field
;
1492 struct var_map_elt
*elt
, dummy
;
1496 if (TREE_CODE (t
) != GOTO_EXPR
)
1498 label
= GOTO_DESTINATION (t
);
1499 if (TREE_CODE (label
) != LABEL_DECL
)
1501 target_context
= decl_function_context (label
);
1502 if (target_context
== info
->context
)
1505 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1508 /* The original user label may also be use for a normal goto, therefore
1509 we must create a new label that will actually receive the abnormal
1510 control transfer. This new label will be marked LABEL_NONLOCAL; this
1511 mark will trigger proper behavior in the cfg, as well as cause the
1512 (hairy target-specific) non-local goto receiver code to be generated
1513 when we expand rtl. Enter this association into var_map so that we
1514 can insert the new label into the IL during a second pass. */
1516 slot
= htab_find_slot (i
->var_map
, &dummy
, INSERT
);
1517 elt
= (struct var_map_elt
*) *slot
;
1520 new_label
= create_artificial_label ();
1521 DECL_NONLOCAL (new_label
) = 1;
1523 elt
= GGC_NEW (struct var_map_elt
);
1525 elt
->new = new_label
;
1529 new_label
= elt
->new;
1531 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1532 field
= get_nl_goto_field (i
);
1533 x
= get_frame_field (info
, target_context
, field
, &wi
->tsi
);
1534 x
= build_addr (x
, target_context
);
1535 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1536 arg
= tree_cons (NULL
, x
, NULL
);
1537 x
= build_addr (new_label
, target_context
);
1538 arg
= tree_cons (NULL
, x
, arg
);
1539 x
= implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
];
1540 x
= build_function_call_expr (x
, arg
);
1542 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
1543 *tsi_stmt_ptr (wi
->tsi
) = x
;
1548 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1549 are referenced via nonlocal goto from a nested function. The rewrite
1550 will involve installing a newly generated DECL_NONLOCAL label, and
1551 (potentially) a branch around the rtl gunk that is assumed to be
1552 attached to such a label. */
1555 convert_nl_goto_receiver (tree
*tp
, int *walk_subtrees
, void *data
)
1557 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1558 struct nesting_info
*info
= wi
->info
;
1559 tree t
= *tp
, label
, new_label
, x
;
1560 struct var_map_elt
*elt
, dummy
;
1561 tree_stmt_iterator tmp_tsi
;
1564 if (TREE_CODE (t
) != LABEL_EXPR
)
1566 label
= LABEL_EXPR_LABEL (t
);
1569 elt
= (struct var_map_elt
*) htab_find (info
->var_map
, &dummy
);
1572 new_label
= elt
->new;
1574 /* If there's any possibility that the previous statement falls through,
1575 then we must branch around the new non-local label. */
1577 tsi_prev (&tmp_tsi
);
1578 if (tsi_end_p (tmp_tsi
) || block_may_fallthru (tsi_stmt (tmp_tsi
)))
1580 x
= build1 (GOTO_EXPR
, void_type_node
, label
);
1581 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1583 x
= build1 (LABEL_EXPR
, void_type_node
, new_label
);
1584 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1589 /* Called via walk_function+walk_tree, rewrite all references to addresses
1590 of nested functions that require the use of trampolines. The rewrite
1591 will involve a reference a trampoline generated for the occasion. */
1594 convert_tramp_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1596 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1597 struct nesting_info
*info
= wi
->info
, *i
;
1598 tree t
= *tp
, decl
, target_context
, x
, arg
;
1601 switch (TREE_CODE (t
))
1605 T.1 = &CHAIN->tramp;
1606 T.2 = __builtin_adjust_trampoline (T.1);
1607 T.3 = (func_type)T.2;
1610 decl
= TREE_OPERAND (t
, 0);
1611 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1614 /* Only need to process nested functions. */
1615 target_context
= decl_function_context (decl
);
1616 if (!target_context
)
1619 /* If the nested function doesn't use a static chain, then
1620 it doesn't need a trampoline. */
1621 if (DECL_NO_STATIC_CHAIN (decl
))
1624 /* Lookup the immediate parent of the callee, as that's where
1625 we need to insert the trampoline. */
1626 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1628 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1630 /* Compute the address of the field holding the trampoline. */
1631 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
1632 x
= build_addr (x
, target_context
);
1633 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1634 arg
= tree_cons (NULL
, x
, NULL
);
1636 /* Do machine-specific ugliness. Normally this will involve
1637 computing extra alignment, but it can really be anything. */
1638 x
= implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
];
1639 x
= build_function_call_expr (x
, arg
);
1640 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1642 /* Cast back to the proper function type. */
1643 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1644 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1650 /* Only walk call arguments, lest we generate trampolines for
1652 walk_tree (&TREE_OPERAND (t
, 1), convert_tramp_reference
, wi
, NULL
);
1656 if (!IS_TYPE_OR_DECL_P (t
))
1664 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1665 reference nested functions to make sure that the static chain is
1666 set up properly for the call. */
1669 convert_call_expr (tree
*tp
, int *walk_subtrees
, void *data
)
1671 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1672 struct nesting_info
*info
= wi
->info
;
1673 tree t
= *tp
, decl
, target_context
;
1674 char save_static_chain_added
;
1678 switch (TREE_CODE (t
))
1681 decl
= get_callee_fndecl (t
);
1684 target_context
= decl_function_context (decl
);
1685 if (target_context
&& !DECL_NO_STATIC_CHAIN (decl
))
1688 = get_static_chain (info
, target_context
, &wi
->tsi
);
1689 info
->static_chain_added
1690 |= (1 << (info
->context
!= target_context
));
1696 case WITH_SIZE_EXPR
:
1697 /* Only return modify and with_size_expr may contain calls. */
1702 save_static_chain_added
= info
->static_chain_added
;
1703 info
->static_chain_added
= 0;
1704 walk_body (convert_call_expr
, info
, &OMP_PARALLEL_BODY (t
));
1705 for (i
= 0; i
< 2; i
++)
1708 if ((info
->static_chain_added
& (1 << i
)) == 0)
1710 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
1711 /* Don't add CHAIN.* or FRAME.* twice. */
1712 for (c
= OMP_PARALLEL_CLAUSES (t
); c
; c
= OMP_CLAUSE_CHAIN (c
))
1713 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
1714 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
1715 && OMP_CLAUSE_DECL (c
) == decl
)
1719 c
= build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE
);
1720 OMP_CLAUSE_DECL (c
) = decl
;
1721 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1722 OMP_PARALLEL_CLAUSES (t
) = c
;
1725 info
->static_chain_added
|= save_static_chain_added
;
1735 walk_body (convert_call_expr
, info
, &OMP_BODY (t
));
1745 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1746 trampolines and call expressions. On the way back up, determine if
1747 a nested function actually uses its static chain; if not, remember that. */
1750 convert_all_function_calls (struct nesting_info
*root
)
1755 convert_all_function_calls (root
->inner
);
1757 walk_function (convert_tramp_reference
, root
);
1758 walk_function (convert_call_expr
, root
);
1760 /* If the function does not use a static chain, then remember that. */
1761 if (root
->outer
&& !root
->chain_decl
&& !root
->chain_field
)
1762 DECL_NO_STATIC_CHAIN (root
->context
) = 1;
1764 gcc_assert (!DECL_NO_STATIC_CHAIN (root
->context
));
1771 /* Do "everything else" to clean up or complete state collected by the
1772 various walking passes -- lay out the types and decls, generate code
1773 to initialize the frame decl, store critical expressions in the
1774 struct function for rtl to find. */
1777 finalize_nesting_tree_1 (struct nesting_info
*root
)
1779 tree stmt_list
= NULL
;
1780 tree context
= root
->context
;
1781 struct function
*sf
;
1783 /* If we created a non-local frame type or decl, we need to lay them
1784 out at this time. */
1785 if (root
->frame_type
)
1787 /* In some cases the frame type will trigger the -Wpadded warning.
1788 This is not helpful; suppress it. */
1789 int save_warn_padded
= warn_padded
;
1791 layout_type (root
->frame_type
);
1792 warn_padded
= save_warn_padded
;
1793 layout_decl (root
->frame_decl
, 0);
1796 /* If any parameters were referenced non-locally, then we need to
1797 insert a copy. Likewise, if any variables were referenced by
1798 pointer, we need to initialize the address. */
1799 if (root
->any_parm_remapped
)
1802 for (p
= DECL_ARGUMENTS (context
); p
; p
= TREE_CHAIN (p
))
1806 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
1810 if (use_pointer_in_frame (p
))
1811 x
= build_addr (p
, context
);
1815 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1816 root
->frame_decl
, field
, NULL_TREE
);
1817 x
= build2 (MODIFY_EXPR
, TREE_TYPE (field
), y
, x
);
1818 append_to_statement_list (x
, &stmt_list
);
1822 /* If a chain_field was created, then it needs to be initialized
1824 if (root
->chain_field
)
1826 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
1827 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
1828 x
= build2 (MODIFY_EXPR
, TREE_TYPE (x
), x
, get_chain_decl (root
));
1829 append_to_statement_list (x
, &stmt_list
);
1832 /* If trampolines were created, then we need to initialize them. */
1833 if (root
->any_tramp_created
)
1835 struct nesting_info
*i
;
1836 for (i
= root
->inner
; i
; i
= i
->next
)
1840 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
1844 if (DECL_NO_STATIC_CHAIN (i
->context
))
1845 x
= null_pointer_node
;
1847 x
= build_addr (root
->frame_decl
, context
);
1848 arg
= tree_cons (NULL
, x
, NULL
);
1850 x
= build_addr (i
->context
, context
);
1851 arg
= tree_cons (NULL
, x
, arg
);
1853 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1854 root
->frame_decl
, field
, NULL_TREE
);
1855 x
= build_addr (x
, context
);
1856 arg
= tree_cons (NULL
, x
, arg
);
1858 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
1859 x
= build_function_call_expr (x
, arg
);
1861 append_to_statement_list (x
, &stmt_list
);
1865 /* If we created initialization statements, insert them. */
1868 annotate_all_with_locus (&stmt_list
,
1869 DECL_SOURCE_LOCATION (context
));
1870 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context
)),
1872 BIND_EXPR_BODY (DECL_SAVED_TREE (context
)) = stmt_list
;
1875 /* If a chain_decl was created, then it needs to be registered with
1876 struct function so that it gets initialized from the static chain
1877 register at the beginning of the function. */
1878 sf
= DECL_STRUCT_FUNCTION (root
->context
);
1879 sf
->static_chain_decl
= root
->chain_decl
;
1881 /* Similarly for the non-local goto save area. */
1882 if (root
->nl_goto_field
)
1884 sf
->nonlocal_goto_save_area
1885 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
1886 sf
->has_nonlocal_label
= 1;
1889 /* Make sure all new local variables get inserted into the
1890 proper BIND_EXPR. */
1891 if (root
->new_local_var_chain
)
1892 declare_vars (root
->new_local_var_chain
, DECL_SAVED_TREE (root
->context
),
1894 if (root
->debug_var_chain
)
1895 declare_vars (root
->debug_var_chain
, DECL_SAVED_TREE (root
->context
),
1898 /* Dump the translated tree function. */
1899 dump_function (TDI_nested
, root
->context
);
1903 finalize_nesting_tree (struct nesting_info
*root
)
1908 finalize_nesting_tree (root
->inner
);
1909 finalize_nesting_tree_1 (root
);
1915 /* Unnest the nodes and pass them to cgraph. */
1918 unnest_nesting_tree_1 (struct nesting_info
*root
)
1920 struct cgraph_node
*node
= cgraph_node (root
->context
);
1922 /* For nested functions update the cgraph to reflect unnesting.
1923 We also delay finalizing of these functions up to this point. */
1926 cgraph_unnest_node (cgraph_node (root
->context
));
1927 cgraph_finalize_function (root
->context
, true);
1932 unnest_nesting_tree (struct nesting_info
*root
)
1937 unnest_nesting_tree (root
->inner
);
1938 unnest_nesting_tree_1 (root
);
1944 /* Free the data structures allocated during this pass. */
1947 free_nesting_tree (struct nesting_info
*root
)
1949 struct nesting_info
*next
;
1953 free_nesting_tree (root
->inner
);
1954 htab_delete (root
->var_map
);
1962 static GTY(()) struct nesting_info
*root
;
1964 /* Main entry point for this pass. Process FNDECL and all of its nested
1965 subroutines and turn them into something less tightly bound. */
1968 lower_nested_functions (tree fndecl
)
1970 struct cgraph_node
*cgn
;
1972 /* If there are no nested functions, there's nothing to do. */
1973 cgn
= cgraph_node (fndecl
);
1977 root
= create_nesting_tree (cgn
);
1978 walk_all_functions (convert_nonlocal_reference
, root
);
1979 walk_all_functions (convert_local_reference
, root
);
1980 walk_all_functions (convert_nl_goto_reference
, root
);
1981 walk_all_functions (convert_nl_goto_receiver
, root
);
1982 convert_all_function_calls (root
);
1983 finalize_nesting_tree (root
);
1984 unnest_nesting_tree (root
);
1985 free_nesting_tree (root
);
1989 #include "gt-tree-nested.h"