1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 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
;
187 /* Set correct alignment for frame struct type. */
188 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
189 TYPE_ALIGN (type
) = DECL_ALIGN (field
);
192 /* Build or return the RECORD_TYPE that describes the frame state that is
193 shared between INFO->CONTEXT and its nested functions. This record will
194 not be complete until finalize_nesting_tree; up until that point we'll
195 be adding fields as necessary.
197 We also build the DECL that represents this frame in the function. */
200 get_frame_type (struct nesting_info
*info
)
202 tree type
= info
->frame_type
;
207 type
= make_node (RECORD_TYPE
);
209 name
= concat ("FRAME.",
210 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
212 TYPE_NAME (type
) = get_identifier (name
);
215 info
->frame_type
= type
;
216 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
218 /* ??? Always make it addressable for now, since it is meant to
219 be pointed to by the static chain pointer. This pessimizes
220 when it turns out that no static chains are needed because
221 the nested functions referencing non-local variables are not
222 reachable, but the true pessimization is to create the non-
223 local frame structure in the first place. */
224 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
229 /* Return true if DECL should be referenced by pointer in the non-local
233 use_pointer_in_frame (tree decl
)
235 if (TREE_CODE (decl
) == PARM_DECL
)
237 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
238 sized decls, and inefficient to copy large aggregates. Don't bother
239 moving anything but scalar variables. */
240 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
244 /* Variable sized types make things "interesting" in the frame. */
245 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
249 /* Given DECL, a non-locally accessed variable, find or create a field
250 in the non-local frame structure for the given nesting context. */
253 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
254 enum insert_option insert
)
258 if (insert
== NO_INSERT
)
260 slot
= pointer_map_contains (info
->field_map
, decl
);
261 return slot
? *slot
: NULL
;
264 slot
= pointer_map_insert (info
->field_map
, decl
);
267 tree field
= make_node (FIELD_DECL
);
268 DECL_NAME (field
) = DECL_NAME (decl
);
270 if (use_pointer_in_frame (decl
))
272 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
273 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
274 DECL_NONADDRESSABLE_P (field
) = 1;
278 TREE_TYPE (field
) = TREE_TYPE (decl
);
279 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
280 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
281 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
282 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
283 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
284 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
287 insert_field_into_struct (get_frame_type (info
), field
);
290 if (TREE_CODE (decl
) == PARM_DECL
)
291 info
->any_parm_remapped
= true;
297 /* Build or return the variable that holds the static chain within
298 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
301 get_chain_decl (struct nesting_info
*info
)
303 tree decl
= info
->chain_decl
;
308 type
= get_frame_type (info
->outer
);
309 type
= build_pointer_type (type
);
311 /* Note that this variable is *not* entered into any BIND_EXPR;
312 the construction of this variable is handled specially in
313 expand_function_start and initialize_inlined_parameters.
314 Note also that it's represented as a parameter. This is more
315 close to the truth, since the initial value does come from
317 decl
= build_decl (PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
318 DECL_ARTIFICIAL (decl
) = 1;
319 DECL_IGNORED_P (decl
) = 1;
320 TREE_USED (decl
) = 1;
321 DECL_CONTEXT (decl
) = info
->context
;
322 DECL_ARG_TYPE (decl
) = type
;
324 /* Tell tree-inline.c that we never write to this variable, so
325 it can copy-prop the replacement value immediately. */
326 TREE_READONLY (decl
) = 1;
328 info
->chain_decl
= decl
;
333 /* Build or return the field within the non-local frame state that holds
334 the static chain for INFO->CONTEXT. This is the way to walk back up
335 multiple nesting levels. */
338 get_chain_field (struct nesting_info
*info
)
340 tree field
= info
->chain_field
;
343 tree type
= build_pointer_type (get_frame_type (info
->outer
));
345 field
= make_node (FIELD_DECL
);
346 DECL_NAME (field
) = get_identifier ("__chain");
347 TREE_TYPE (field
) = type
;
348 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
349 DECL_NONADDRESSABLE_P (field
) = 1;
351 insert_field_into_struct (get_frame_type (info
), field
);
353 info
->chain_field
= field
;
358 /* Copy EXP into a temporary. Allocate the temporary in the context of
359 INFO and insert the initialization statement before TSI. */
362 init_tmp_var (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
366 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
367 stmt
= build_gimple_modify_stmt (t
, exp
);
368 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
369 tsi_link_before (tsi
, stmt
, TSI_SAME_STMT
);
374 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
377 tsi_gimplify_val (struct nesting_info
*info
, tree exp
, tree_stmt_iterator
*tsi
)
379 if (is_gimple_val (exp
))
382 return init_tmp_var (info
, exp
, tsi
);
385 /* Similarly, but copy from the temporary and insert the statement
386 after the iterator. */
389 save_tmp_var (struct nesting_info
*info
, tree exp
,
390 tree_stmt_iterator
*tsi
)
394 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
395 stmt
= build_gimple_modify_stmt (exp
, t
);
396 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (tsi_stmt (*tsi
)));
397 tsi_link_after (tsi
, stmt
, TSI_SAME_STMT
);
402 /* Build or return the type used to represent a nested function trampoline. */
404 static GTY(()) tree trampoline_type
;
407 get_trampoline_type (void)
409 unsigned align
, size
;
413 return trampoline_type
;
415 align
= TRAMPOLINE_ALIGNMENT
;
416 size
= TRAMPOLINE_SIZE
;
418 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
419 then allocate extra space so that we can do dynamic alignment. */
420 if (align
> STACK_BOUNDARY
)
422 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
423 align
= STACK_BOUNDARY
;
426 t
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
427 t
= build_array_type (char_type_node
, t
);
428 t
= build_decl (FIELD_DECL
, get_identifier ("__data"), t
);
429 DECL_ALIGN (t
) = align
;
430 DECL_USER_ALIGN (t
) = 1;
432 trampoline_type
= make_node (RECORD_TYPE
);
433 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
434 TYPE_FIELDS (trampoline_type
) = t
;
435 layout_type (trampoline_type
);
436 DECL_CONTEXT (t
) = trampoline_type
;
438 return trampoline_type
;
441 /* Given DECL, a nested function, find or create a field in the non-local
442 frame structure for a trampoline for this function. */
445 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
446 enum insert_option insert
)
450 if (insert
== NO_INSERT
)
452 slot
= pointer_map_contains (info
->var_map
, decl
);
453 return slot
? *slot
: NULL
;
456 slot
= pointer_map_insert (info
->var_map
, decl
);
459 tree field
= make_node (FIELD_DECL
);
460 DECL_NAME (field
) = DECL_NAME (decl
);
461 TREE_TYPE (field
) = get_trampoline_type ();
462 TREE_ADDRESSABLE (field
) = 1;
464 insert_field_into_struct (get_frame_type (info
), field
);
467 info
->any_tramp_created
= true;
473 /* Build or return the field within the non-local frame state that holds
474 the non-local goto "jmp_buf". The buffer itself is maintained by the
475 rtl middle-end as dynamic stack space is allocated. */
478 get_nl_goto_field (struct nesting_info
*info
)
480 tree field
= info
->nl_goto_field
;
486 /* For __builtin_nonlocal_goto, we need N words. The first is the
487 frame pointer, the rest is for the target's stack pointer save
488 area. The number of words is controlled by STACK_SAVEAREA_MODE;
489 not the best interface, but it'll do for now. */
490 if (Pmode
== ptr_mode
)
491 type
= ptr_type_node
;
493 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
495 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
496 size
= size
/ GET_MODE_SIZE (Pmode
);
499 type
= build_array_type
500 (type
, build_index_type (build_int_cst (NULL_TREE
, size
)));
502 field
= make_node (FIELD_DECL
);
503 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
504 TREE_TYPE (field
) = type
;
505 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
506 TREE_ADDRESSABLE (field
) = 1;
508 insert_field_into_struct (get_frame_type (info
), field
);
510 info
->nl_goto_field
= field
;
516 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
519 walk_asm_expr (struct walk_stmt_info
*wi
, tree stmt
)
521 int noutputs
= list_length (ASM_OUTPUTS (stmt
));
522 const char **oconstraints
523 = (const char **) alloca ((noutputs
) * sizeof (const char *));
526 const char *constraint
;
527 bool allows_mem
, allows_reg
, is_inout
;
530 for (i
=0, link
= ASM_OUTPUTS (stmt
); link
; ++i
, link
= TREE_CHAIN (link
))
532 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
533 oconstraints
[i
] = constraint
;
534 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
535 &allows_reg
, &is_inout
);
537 wi
->val_only
= (allows_reg
|| !allows_mem
);
538 walk_tree (&TREE_VALUE (link
), wi
->callback
, wi
, NULL
);
541 for (link
= ASM_INPUTS (stmt
); link
; link
= TREE_CHAIN (link
))
543 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
544 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
545 oconstraints
, &allows_mem
, &allows_reg
);
547 wi
->val_only
= (allows_reg
|| !allows_mem
);
548 /* Although input "m" is not really a LHS, we need a lvalue. */
549 wi
->is_lhs
= !wi
->val_only
;
550 walk_tree (&TREE_VALUE (link
), wi
->callback
, wi
, NULL
);
557 /* Iterate over all sub-statements of *TP calling walk_tree with
558 WI->CALLBACK for every sub-expression in each statement found. */
561 walk_stmts (struct walk_stmt_info
*wi
, tree
*tp
)
569 if (wi
->want_locations
&& EXPR_HAS_LOCATION (t
))
570 input_location
= EXPR_LOCATION (t
);
572 switch (TREE_CODE (t
))
576 tree_stmt_iterator i
;
577 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
580 walk_stmts (wi
, tsi_stmt_ptr (i
));
586 walk_tree (&COND_EXPR_COND (t
), wi
->callback
, wi
, NULL
);
587 walk_stmts (wi
, &COND_EXPR_THEN (t
));
588 walk_stmts (wi
, &COND_EXPR_ELSE (t
));
591 walk_stmts (wi
, &CATCH_BODY (t
));
594 walk_stmts (wi
, &EH_FILTER_FAILURE (t
));
597 case TRY_FINALLY_EXPR
:
598 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
599 walk_stmts (wi
, &TREE_OPERAND (t
, 1));
603 if (wi
->want_bind_expr
)
606 wi
->callback (tp
, &walk_subtrees
, wi
);
610 walk_stmts (wi
, &BIND_EXPR_BODY (t
));
614 if (wi
->want_return_expr
)
617 wi
->callback (tp
, &walk_subtrees
, wi
);
621 walk_stmts (wi
, &TREE_OPERAND (t
, 0));
624 case GIMPLE_MODIFY_STMT
:
625 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
626 wi
->val_only
= !is_gimple_formal_tmp_var (GIMPLE_STMT_OPERAND (t
, 0));
627 walk_tree (&GIMPLE_STMT_OPERAND (t
, 1), wi
->callback
, wi
, NULL
);
629 /* If the rhs is appropriate for a memory, we may use a
630 COMPONENT_REF on the lhs. */
631 wi
->val_only
= !is_gimple_mem_rhs (GIMPLE_STMT_OPERAND (t
, 1));
633 walk_tree (&GIMPLE_STMT_OPERAND (t
, 0), wi
->callback
, wi
, NULL
);
640 walk_asm_expr (wi
, *tp
);
645 walk_tree (tp
, wi
->callback
, wi
, NULL
);
650 /* Invoke CALLBACK on all statements of *STMT_P. */
653 walk_body (walk_tree_fn callback
, struct nesting_info
*info
, tree
*stmt_p
)
655 struct walk_stmt_info wi
;
657 memset (&wi
, 0, sizeof (wi
));
658 wi
.callback
= callback
;
662 walk_stmts (&wi
, stmt_p
);
665 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
668 walk_function (walk_tree_fn callback
, struct nesting_info
*info
)
670 walk_body (callback
, info
, &DECL_SAVED_TREE (info
->context
));
673 /* Invoke CALLBACK on OMP_FOR init, cond, incr and pre-body. */
676 walk_omp_for (walk_tree_fn callback
, struct nesting_info
*info
, tree for_stmt
)
678 struct walk_stmt_info wi
;
679 tree t
, list
= NULL
, empty
;
681 walk_body (callback
, info
, &OMP_FOR_PRE_BODY (for_stmt
));
683 empty
= build_empty_stmt ();
684 append_to_statement_list_force (empty
, &list
);
685 memset (&wi
, 0, sizeof (wi
));
686 wi
.callback
= callback
;
688 wi
.tsi
= tsi_last (list
);
690 t
= OMP_FOR_INIT (for_stmt
);
691 gcc_assert (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
);
692 SET_EXPR_LOCUS (empty
, EXPR_LOCUS (t
));
694 walk_tree (&GIMPLE_STMT_OPERAND (t
, 0), callback
, &wi
, NULL
);
697 walk_tree (&GIMPLE_STMT_OPERAND (t
, 1), callback
, &wi
, NULL
);
699 t
= OMP_FOR_COND (for_stmt
);
700 gcc_assert (COMPARISON_CLASS_P (t
));
701 SET_EXPR_LOCUS (empty
, EXPR_LOCUS (t
));
703 walk_tree (&TREE_OPERAND (t
, 0), callback
, &wi
, NULL
);
706 walk_tree (&TREE_OPERAND (t
, 1), callback
, &wi
, NULL
);
708 t
= OMP_FOR_INCR (for_stmt
);
709 gcc_assert (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
);
710 SET_EXPR_LOCUS (empty
, EXPR_LOCUS (t
));
712 walk_tree (&GIMPLE_STMT_OPERAND (t
, 0), callback
, &wi
, NULL
);
713 t
= GIMPLE_STMT_OPERAND (t
, 1);
714 gcc_assert (BINARY_CLASS_P (t
));
716 walk_tree (&TREE_OPERAND (t
, 0), callback
, &wi
, NULL
);
719 walk_tree (&TREE_OPERAND (t
, 1), callback
, &wi
, NULL
);
721 /* Remove empty statement added above from the end of statement list. */
722 tsi_delink (&wi
.tsi
);
723 append_to_statement_list (list
, &OMP_FOR_PRE_BODY (for_stmt
));
726 /* Similarly for ROOT and all functions nested underneath, depth first. */
729 walk_all_functions (walk_tree_fn callback
, struct nesting_info
*root
)
734 walk_all_functions (callback
, root
->inner
);
735 walk_function (callback
, root
);
741 /* We have to check for a fairly pathological case. The operands of function
742 nested function are to be interpreted in the context of the enclosing
743 function. So if any are variably-sized, they will get remapped when the
744 enclosing function is inlined. But that remapping would also have to be
745 done in the types of the PARM_DECLs of the nested function, meaning the
746 argument types of that function will disagree with the arguments in the
747 calls to that function. So we'd either have to make a copy of the nested
748 function corresponding to each time the enclosing function was inlined or
749 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
750 function. The former is not practical. The latter would still require
751 detecting this case to know when to add the conversions. So, for now at
752 least, we don't inline such an enclosing function.
754 We have to do that check recursively, so here return indicating whether
755 FNDECL has such a nested function. ORIG_FN is the function we were
756 trying to inline to use for checking whether any argument is variably
757 modified by anything in it.
759 It would be better to do this in tree-inline.c so that we could give
760 the appropriate warning for why a function can't be inlined, but that's
761 too late since the nesting structure has already been flattened and
762 adding a flag just to record this fact seems a waste of a flag. */
765 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
767 struct cgraph_node
*cgn
= cgraph_node (fndecl
);
770 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
772 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= TREE_CHAIN (arg
))
773 if (variably_modified_type_p (TREE_TYPE (arg
), 0), orig_fndecl
)
776 if (check_for_nested_with_variably_modified (cgn
->decl
, orig_fndecl
))
783 /* Construct our local datastructure describing the function nesting
784 tree rooted by CGN. */
786 static struct nesting_info
*
787 create_nesting_tree (struct cgraph_node
*cgn
)
789 struct nesting_info
*info
= XCNEW (struct nesting_info
);
790 info
->field_map
= pointer_map_create ();
791 info
->var_map
= pointer_map_create ();
792 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
793 info
->context
= cgn
->decl
;
795 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
797 struct nesting_info
*sub
= create_nesting_tree (cgn
);
799 sub
->next
= info
->inner
;
803 /* See discussion at check_for_nested_with_variably_modified for a
804 discussion of why this has to be here. */
805 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
806 DECL_UNINLINABLE (info
->context
) = true;
811 /* Return an expression computing the static chain for TARGET_CONTEXT
812 from INFO->CONTEXT. Insert any necessary computations before TSI. */
815 get_static_chain (struct nesting_info
*info
, tree target_context
,
816 tree_stmt_iterator
*tsi
)
818 struct nesting_info
*i
;
821 if (info
->context
== target_context
)
823 x
= build_addr (info
->frame_decl
, target_context
);
827 x
= get_chain_decl (info
);
829 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
831 tree field
= get_chain_field (i
);
833 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
834 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
835 x
= init_tmp_var (info
, x
, tsi
);
842 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
843 frame as seen from INFO->CONTEXT. Insert any necessary computations
847 get_frame_field (struct nesting_info
*info
, tree target_context
,
848 tree field
, tree_stmt_iterator
*tsi
)
850 struct nesting_info
*i
;
853 if (info
->context
== target_context
)
855 /* Make sure frame_decl gets created. */
856 (void) get_frame_type (info
);
857 x
= info
->frame_decl
;
861 x
= get_chain_decl (info
);
863 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
865 tree field
= get_chain_field (i
);
867 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
868 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
869 x
= init_tmp_var (info
, x
, tsi
);
872 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
875 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
879 /* A subroutine of convert_nonlocal_reference. Create a local variable
880 in the nested function with DECL_VALUE_EXPR set to reference the true
881 variable in the parent function. This is used both for debug info
882 and in OpenMP lowering. */
885 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
888 struct nesting_info
*i
;
889 tree x
, field
, new_decl
;
892 slot
= pointer_map_insert (info
->var_map
, decl
);
897 target_context
= decl_function_context (decl
);
899 /* A copy of the code in get_frame_field, but without the temporaries. */
900 if (info
->context
== target_context
)
902 /* Make sure frame_decl gets created. */
903 (void) get_frame_type (info
);
904 x
= info
->frame_decl
;
909 x
= get_chain_decl (info
);
910 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
912 field
= get_chain_field (i
);
913 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
914 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
916 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
919 field
= lookup_field_for_decl (i
, decl
, INSERT
);
920 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
921 if (use_pointer_in_frame (decl
))
922 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
924 /* ??? We should be remapping types as well, surely. */
925 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
926 DECL_CONTEXT (new_decl
) = info
->context
;
927 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
928 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
929 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
930 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
931 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
932 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
933 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
934 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
936 SET_DECL_VALUE_EXPR (new_decl
, x
);
937 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
940 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
941 info
->debug_var_chain
= new_decl
;
946 /* Called via walk_function+walk_tree, rewrite all references to VAR
947 and PARM_DECLs that belong to outer functions.
949 The rewrite will involve some number of structure accesses back up
950 the static chain. E.g. for a variable FOO up one nesting level it'll
951 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
952 indirections apply to decls for which use_pointer_in_frame is true. */
954 static bool convert_nonlocal_omp_clauses (tree
*, struct walk_stmt_info
*);
957 convert_nonlocal_reference (tree
*tp
, int *walk_subtrees
, void *data
)
959 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
960 struct nesting_info
*info
= wi
->info
;
962 tree save_local_var_chain
;
963 bitmap save_suppress
;
966 switch (TREE_CODE (t
))
969 /* Non-automatic variables are never processed. */
970 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
975 if (decl_function_context (t
) != info
->context
)
980 x
= get_nonlocal_debug_decl (info
, t
);
981 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
983 tree target_context
= decl_function_context (t
);
984 struct nesting_info
*i
;
985 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
987 x
= lookup_field_for_decl (i
, t
, INSERT
);
988 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
989 if (use_pointer_in_frame (t
))
991 x
= init_tmp_var (info
, x
, &wi
->tsi
);
992 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
999 x
= save_tmp_var (info
, x
, &wi
->tsi
);
1001 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1009 /* Don't walk non-local gotos for now. */
1010 if (TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
)
1013 wi
->val_only
= true;
1019 /* We're taking the address of a label from a parent function, but
1020 this is not itself a non-local goto. Mark the label such that it
1021 will not be deleted, much as we would with a label address in
1023 if (decl_function_context (t
) != info
->context
)
1024 FORCED_LABEL (t
) = 1;
1029 bool save_val_only
= wi
->val_only
;
1031 wi
->val_only
= false;
1033 wi
->changed
= false;
1034 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference
, wi
, NULL
);
1035 wi
->val_only
= true;
1041 /* If we changed anything, we might no longer be directly
1042 referencing a decl. */
1043 save_context
= current_function_decl
;
1044 current_function_decl
= info
->context
;
1045 recompute_tree_invariant_for_addr_expr (t
);
1046 current_function_decl
= save_context
;
1048 /* If the callback converted the address argument in a context
1049 where we only accept variables (and min_invariant, presumably),
1050 then compute the address into a temporary. */
1052 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1061 case ARRAY_RANGE_REF
:
1063 /* Go down this entire nest and just look at the final prefix and
1064 anything that describes the references. Otherwise, we lose track
1065 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1066 wi
->val_only
= true;
1068 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1070 if (TREE_CODE (t
) == COMPONENT_REF
)
1071 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1073 else if (TREE_CODE (t
) == ARRAY_REF
1074 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1076 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
1078 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1080 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference
, wi
,
1083 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1085 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference
, wi
,
1087 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference
, wi
,
1091 wi
->val_only
= false;
1092 walk_tree (tp
, convert_nonlocal_reference
, wi
, NULL
);
1095 case VIEW_CONVERT_EXPR
:
1096 /* Just request to look at the subtrees, leaving val_only and lhs
1097 untouched. This might actually be for !val_only + lhs, in which
1098 case we don't want to force a replacement by a temporary. */
1103 save_suppress
= info
->suppress_expansion
;
1104 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1107 decl
= get_chain_decl (info
);
1108 c
= build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE
);
1109 OMP_CLAUSE_DECL (c
) = decl
;
1110 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1111 OMP_PARALLEL_CLAUSES (t
) = c
;
1114 save_local_var_chain
= info
->new_local_var_chain
;
1115 info
->new_local_var_chain
= NULL
;
1117 walk_body (convert_nonlocal_reference
, info
, &OMP_PARALLEL_BODY (t
));
1119 if (info
->new_local_var_chain
)
1120 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1121 info
->new_local_var_chain
= save_local_var_chain
;
1122 info
->suppress_expansion
= save_suppress
;
1126 save_suppress
= info
->suppress_expansion
;
1127 convert_nonlocal_omp_clauses (&OMP_FOR_CLAUSES (t
), wi
);
1128 walk_omp_for (convert_nonlocal_reference
, info
, t
);
1129 walk_body (convert_nonlocal_reference
, info
, &OMP_FOR_BODY (t
));
1130 info
->suppress_expansion
= save_suppress
;
1135 save_suppress
= info
->suppress_expansion
;
1136 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t
), wi
);
1137 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1138 info
->suppress_expansion
= save_suppress
;
1144 walk_body (convert_nonlocal_reference
, info
, &OMP_BODY (t
));
1148 if (!IS_TYPE_OR_DECL_P (t
))
1151 wi
->val_only
= true;
1161 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1163 struct nesting_info
*info
= wi
->info
;
1164 bool need_chain
= false;
1167 bitmap new_suppress
;
1169 new_suppress
= BITMAP_GGC_ALLOC ();
1170 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1172 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1174 switch (OMP_CLAUSE_CODE (clause
))
1176 case OMP_CLAUSE_PRIVATE
:
1177 case OMP_CLAUSE_FIRSTPRIVATE
:
1178 case OMP_CLAUSE_LASTPRIVATE
:
1179 case OMP_CLAUSE_REDUCTION
:
1180 case OMP_CLAUSE_COPYPRIVATE
:
1181 case OMP_CLAUSE_SHARED
:
1182 decl
= OMP_CLAUSE_DECL (clause
);
1183 if (decl_function_context (decl
) != info
->context
)
1185 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1186 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1191 case OMP_CLAUSE_SCHEDULE
:
1192 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1196 case OMP_CLAUSE_NUM_THREADS
:
1197 wi
->val_only
= true;
1199 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1203 case OMP_CLAUSE_NOWAIT
:
1204 case OMP_CLAUSE_ORDERED
:
1205 case OMP_CLAUSE_DEFAULT
:
1206 case OMP_CLAUSE_COPYIN
:
1214 info
->suppress_expansion
= new_suppress
;
1219 /* A subroutine of convert_local_reference. Create a local variable
1220 in the parent function with DECL_VALUE_EXPR set to reference the
1221 field in FRAME. This is used both for debug info and in OpenMP
1225 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1230 slot
= pointer_map_insert (info
->var_map
, decl
);
1234 /* Make sure frame_decl gets created. */
1235 (void) get_frame_type (info
);
1236 x
= info
->frame_decl
;
1237 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1239 new_decl
= build_decl (VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1240 DECL_CONTEXT (new_decl
) = info
->context
;
1241 DECL_SOURCE_LOCATION (new_decl
) = DECL_SOURCE_LOCATION (decl
);
1242 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1243 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1244 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1245 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1246 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1247 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1248 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1250 SET_DECL_VALUE_EXPR (new_decl
, x
);
1251 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1254 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
1255 info
->debug_var_chain
= new_decl
;
1257 /* Do not emit debug info twice. */
1258 DECL_IGNORED_P (decl
) = 1;
1263 /* Called via walk_function+walk_tree, rewrite all references to VAR
1264 and PARM_DECLs that were referenced by inner nested functions.
1265 The rewrite will be a structure reference to the local frame variable. */
1267 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1270 convert_local_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1272 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1273 struct nesting_info
*info
= wi
->info
;
1274 tree t
= *tp
, field
, x
;
1276 tree save_local_var_chain
;
1277 bitmap save_suppress
;
1280 switch (TREE_CODE (t
))
1283 /* Non-automatic variables are never processed. */
1284 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1289 if (decl_function_context (t
) == info
->context
)
1291 /* If we copied a pointer to the frame, then the original decl
1292 is used unchanged in the parent function. */
1293 if (use_pointer_in_frame (t
))
1296 /* No need to transform anything if no child references the
1298 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1303 x
= get_local_debug_decl (info
, t
, field
);
1304 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1305 x
= get_frame_field (info
, info
->context
, field
, &wi
->tsi
);
1310 x
= save_tmp_var (info
, x
, &wi
->tsi
);
1312 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1320 save_val_only
= wi
->val_only
;
1321 wi
->val_only
= false;
1323 wi
->changed
= false;
1324 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference
, wi
, NULL
);
1325 wi
->val_only
= save_val_only
;
1327 /* If we converted anything ... */
1332 /* Then the frame decl is now addressable. */
1333 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1335 save_context
= current_function_decl
;
1336 current_function_decl
= info
->context
;
1337 recompute_tree_invariant_for_addr_expr (t
);
1338 current_function_decl
= save_context
;
1340 /* If we are in a context where we only accept values, then
1341 compute the address into a temporary. */
1343 *tp
= tsi_gimplify_val (wi
->info
, t
, &wi
->tsi
);
1351 case ARRAY_RANGE_REF
:
1353 /* Go down this entire nest and just look at the final prefix and
1354 anything that describes the references. Otherwise, we lose track
1355 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1356 save_val_only
= wi
->val_only
;
1357 wi
->val_only
= true;
1359 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1361 if (TREE_CODE (t
) == COMPONENT_REF
)
1362 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1364 else if (TREE_CODE (t
) == ARRAY_REF
1365 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1367 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1369 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1371 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference
, wi
,
1374 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1376 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference
, wi
,
1378 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference
, wi
,
1382 wi
->val_only
= false;
1383 walk_tree (tp
, convert_local_reference
, wi
, NULL
);
1384 wi
->val_only
= save_val_only
;
1387 case VIEW_CONVERT_EXPR
:
1388 /* Just request to look at the subtrees, leaving val_only and lhs
1389 untouched. This might actually be for !val_only + lhs, in which
1390 case we don't want to force a replacement by a temporary. */
1395 save_suppress
= info
->suppress_expansion
;
1396 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t
), wi
))
1399 (void) get_frame_type (info
);
1400 c
= build_omp_clause (OMP_CLAUSE_SHARED
);
1401 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1402 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1403 OMP_PARALLEL_CLAUSES (t
) = c
;
1406 save_local_var_chain
= info
->new_local_var_chain
;
1407 info
->new_local_var_chain
= NULL
;
1409 walk_body (convert_local_reference
, info
, &OMP_PARALLEL_BODY (t
));
1411 if (info
->new_local_var_chain
)
1412 declare_vars (info
->new_local_var_chain
, OMP_PARALLEL_BODY (t
), false);
1413 info
->new_local_var_chain
= save_local_var_chain
;
1414 info
->suppress_expansion
= save_suppress
;
1418 save_suppress
= info
->suppress_expansion
;
1419 convert_local_omp_clauses (&OMP_FOR_CLAUSES (t
), wi
);
1420 walk_omp_for (convert_local_reference
, info
, t
);
1421 walk_body (convert_local_reference
, info
, &OMP_FOR_BODY (t
));
1422 info
->suppress_expansion
= save_suppress
;
1427 save_suppress
= info
->suppress_expansion
;
1428 convert_local_omp_clauses (&OMP_CLAUSES (t
), wi
);
1429 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1430 info
->suppress_expansion
= save_suppress
;
1436 walk_body (convert_local_reference
, info
, &OMP_BODY (t
));
1440 if (!IS_TYPE_OR_DECL_P (t
))
1443 wi
->val_only
= true;
1453 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1455 struct nesting_info
*info
= wi
->info
;
1456 bool need_frame
= false;
1459 bitmap new_suppress
;
1461 new_suppress
= BITMAP_GGC_ALLOC ();
1462 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1464 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1466 switch (OMP_CLAUSE_CODE (clause
))
1468 case OMP_CLAUSE_PRIVATE
:
1469 case OMP_CLAUSE_FIRSTPRIVATE
:
1470 case OMP_CLAUSE_LASTPRIVATE
:
1471 case OMP_CLAUSE_REDUCTION
:
1472 case OMP_CLAUSE_COPYPRIVATE
:
1473 case OMP_CLAUSE_SHARED
:
1474 decl
= OMP_CLAUSE_DECL (clause
);
1475 if (decl_function_context (decl
) == info
->context
1476 && !use_pointer_in_frame (decl
))
1478 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1481 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1482 OMP_CLAUSE_DECL (clause
)
1483 = get_local_debug_decl (info
, decl
, field
);
1489 case OMP_CLAUSE_SCHEDULE
:
1490 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1494 case OMP_CLAUSE_NUM_THREADS
:
1495 wi
->val_only
= true;
1497 convert_local_reference (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
, wi
);
1500 case OMP_CLAUSE_NOWAIT
:
1501 case OMP_CLAUSE_ORDERED
:
1502 case OMP_CLAUSE_DEFAULT
:
1503 case OMP_CLAUSE_COPYIN
:
1511 info
->suppress_expansion
= new_suppress
;
1516 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1517 reference labels from outer functions. The rewrite will be a
1518 call to __builtin_nonlocal_goto. */
1521 convert_nl_goto_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1523 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1524 struct nesting_info
*info
= wi
->info
, *i
;
1525 tree t
= *tp
, label
, new_label
, target_context
, x
, field
;
1529 if (TREE_CODE (t
) != GOTO_EXPR
)
1531 label
= GOTO_DESTINATION (t
);
1532 if (TREE_CODE (label
) != LABEL_DECL
)
1534 target_context
= decl_function_context (label
);
1535 if (target_context
== info
->context
)
1538 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1541 /* The original user label may also be use for a normal goto, therefore
1542 we must create a new label that will actually receive the abnormal
1543 control transfer. This new label will be marked LABEL_NONLOCAL; this
1544 mark will trigger proper behavior in the cfg, as well as cause the
1545 (hairy target-specific) non-local goto receiver code to be generated
1546 when we expand rtl. Enter this association into var_map so that we
1547 can insert the new label into the IL during a second pass. */
1548 slot
= pointer_map_insert (i
->var_map
, label
);
1551 new_label
= create_artificial_label ();
1552 DECL_NONLOCAL (new_label
) = 1;
1558 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1559 field
= get_nl_goto_field (i
);
1560 x
= get_frame_field (info
, target_context
, field
, &wi
->tsi
);
1561 x
= build_addr (x
, target_context
);
1562 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1563 x
= build_call_expr (implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
], 2,
1564 build_addr (new_label
, target_context
), x
);
1566 SET_EXPR_LOCUS (x
, EXPR_LOCUS (tsi_stmt (wi
->tsi
)));
1567 *tsi_stmt_ptr (wi
->tsi
) = x
;
1572 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1573 are referenced via nonlocal goto from a nested function. The rewrite
1574 will involve installing a newly generated DECL_NONLOCAL label, and
1575 (potentially) a branch around the rtl gunk that is assumed to be
1576 attached to such a label. */
1579 convert_nl_goto_receiver (tree
*tp
, int *walk_subtrees
, void *data
)
1581 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1582 struct nesting_info
*info
= wi
->info
;
1583 tree t
= *tp
, label
, new_label
, x
;
1584 tree_stmt_iterator tmp_tsi
;
1588 if (TREE_CODE (t
) != LABEL_EXPR
)
1590 label
= LABEL_EXPR_LABEL (t
);
1592 slot
= pointer_map_contains (info
->var_map
, label
);
1596 /* If there's any possibility that the previous statement falls through,
1597 then we must branch around the new non-local label. */
1599 tsi_prev (&tmp_tsi
);
1600 if (tsi_end_p (tmp_tsi
) || block_may_fallthru (tsi_stmt (tmp_tsi
)))
1602 x
= build1 (GOTO_EXPR
, void_type_node
, label
);
1603 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1606 new_label
= (tree
) *slot
;
1607 x
= build1 (LABEL_EXPR
, void_type_node
, new_label
);
1608 tsi_link_before (&wi
->tsi
, x
, TSI_SAME_STMT
);
1613 /* Called via walk_function+walk_tree, rewrite all references to addresses
1614 of nested functions that require the use of trampolines. The rewrite
1615 will involve a reference a trampoline generated for the occasion. */
1618 convert_tramp_reference (tree
*tp
, int *walk_subtrees
, void *data
)
1620 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1621 struct nesting_info
*info
= wi
->info
, *i
;
1622 tree t
= *tp
, decl
, target_context
, x
;
1625 switch (TREE_CODE (t
))
1629 T.1 = &CHAIN->tramp;
1630 T.2 = __builtin_adjust_trampoline (T.1);
1631 T.3 = (func_type)T.2;
1634 decl
= TREE_OPERAND (t
, 0);
1635 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1638 /* Only need to process nested functions. */
1639 target_context
= decl_function_context (decl
);
1640 if (!target_context
)
1643 /* If the nested function doesn't use a static chain, then
1644 it doesn't need a trampoline. */
1645 if (DECL_NO_STATIC_CHAIN (decl
))
1648 /* Lookup the immediate parent of the callee, as that's where
1649 we need to insert the trampoline. */
1650 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1652 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1654 /* Compute the address of the field holding the trampoline. */
1655 x
= get_frame_field (info
, target_context
, x
, &wi
->tsi
);
1656 x
= build_addr (x
, target_context
);
1657 x
= tsi_gimplify_val (info
, x
, &wi
->tsi
);
1659 /* Do machine-specific ugliness. Normally this will involve
1660 computing extra alignment, but it can really be anything. */
1661 x
= build_call_expr (implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
],
1663 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1665 /* Cast back to the proper function type. */
1666 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1667 x
= init_tmp_var (info
, x
, &wi
->tsi
);
1673 /* Only walk call arguments, lest we generate trampolines for
1676 int nargs
= call_expr_nargs (t
);
1678 for (i
= 0; i
< nargs
; i
++)
1679 walk_tree (&CALL_EXPR_ARG (t
, i
), convert_tramp_reference
, wi
, NULL
);
1684 if (!IS_TYPE_OR_DECL_P (t
))
1692 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1693 reference nested functions to make sure that the static chain is
1694 set up properly for the call. */
1697 convert_call_expr (tree
*tp
, int *walk_subtrees
, void *data
)
1699 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1700 struct nesting_info
*info
= wi
->info
;
1701 tree t
= *tp
, decl
, target_context
;
1702 char save_static_chain_added
;
1706 switch (TREE_CODE (t
))
1709 decl
= get_callee_fndecl (t
);
1712 target_context
= decl_function_context (decl
);
1713 if (target_context
&& !DECL_NO_STATIC_CHAIN (decl
))
1715 CALL_EXPR_STATIC_CHAIN (t
)
1716 = get_static_chain (info
, target_context
, &wi
->tsi
);
1717 info
->static_chain_added
1718 |= (1 << (info
->context
!= target_context
));
1723 case GIMPLE_MODIFY_STMT
:
1724 case WITH_SIZE_EXPR
:
1725 /* Only return modify and with_size_expr may contain calls. */
1730 save_static_chain_added
= info
->static_chain_added
;
1731 info
->static_chain_added
= 0;
1732 walk_body (convert_call_expr
, info
, &OMP_PARALLEL_BODY (t
));
1733 for (i
= 0; i
< 2; i
++)
1736 if ((info
->static_chain_added
& (1 << i
)) == 0)
1738 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
1739 /* Don't add CHAIN.* or FRAME.* twice. */
1740 for (c
= OMP_PARALLEL_CLAUSES (t
); c
; c
= OMP_CLAUSE_CHAIN (c
))
1741 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
1742 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
1743 && OMP_CLAUSE_DECL (c
) == decl
)
1747 c
= build_omp_clause (i
? OMP_CLAUSE_FIRSTPRIVATE
1748 : OMP_CLAUSE_SHARED
);
1749 OMP_CLAUSE_DECL (c
) = decl
;
1750 OMP_CLAUSE_CHAIN (c
) = OMP_PARALLEL_CLAUSES (t
);
1751 OMP_PARALLEL_CLAUSES (t
) = c
;
1754 info
->static_chain_added
|= save_static_chain_added
;
1758 walk_body (convert_call_expr
, info
, &OMP_FOR_PRE_BODY (t
));
1766 walk_body (convert_call_expr
, info
, &OMP_BODY (t
));
1776 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1777 trampolines and call expressions. On the way back up, determine if
1778 a nested function actually uses its static chain; if not, remember that. */
1781 convert_all_function_calls (struct nesting_info
*root
)
1786 convert_all_function_calls (root
->inner
);
1788 walk_function (convert_tramp_reference
, root
);
1789 walk_function (convert_call_expr
, root
);
1791 /* If the function does not use a static chain, then remember that. */
1792 if (root
->outer
&& !root
->chain_decl
&& !root
->chain_field
)
1793 DECL_NO_STATIC_CHAIN (root
->context
) = 1;
1795 gcc_assert (!DECL_NO_STATIC_CHAIN (root
->context
));
1802 /* Do "everything else" to clean up or complete state collected by the
1803 various walking passes -- lay out the types and decls, generate code
1804 to initialize the frame decl, store critical expressions in the
1805 struct function for rtl to find. */
1808 finalize_nesting_tree_1 (struct nesting_info
*root
)
1810 tree stmt_list
= NULL
;
1811 tree context
= root
->context
;
1812 struct function
*sf
;
1814 /* If we created a non-local frame type or decl, we need to lay them
1815 out at this time. */
1816 if (root
->frame_type
)
1818 /* In some cases the frame type will trigger the -Wpadded warning.
1819 This is not helpful; suppress it. */
1820 int save_warn_padded
= warn_padded
;
1822 layout_type (root
->frame_type
);
1823 warn_padded
= save_warn_padded
;
1824 layout_decl (root
->frame_decl
, 0);
1827 /* If any parameters were referenced non-locally, then we need to
1828 insert a copy. Likewise, if any variables were referenced by
1829 pointer, we need to initialize the address. */
1830 if (root
->any_parm_remapped
)
1833 for (p
= DECL_ARGUMENTS (context
); p
; p
= TREE_CHAIN (p
))
1837 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
1841 if (use_pointer_in_frame (p
))
1842 x
= build_addr (p
, context
);
1846 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1847 root
->frame_decl
, field
, NULL_TREE
);
1848 x
= build_gimple_modify_stmt (y
, x
);
1849 append_to_statement_list (x
, &stmt_list
);
1853 /* If a chain_field was created, then it needs to be initialized
1855 if (root
->chain_field
)
1857 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
1858 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
1859 x
= build_gimple_modify_stmt (x
, get_chain_decl (root
));
1860 append_to_statement_list (x
, &stmt_list
);
1863 /* If trampolines were created, then we need to initialize them. */
1864 if (root
->any_tramp_created
)
1866 struct nesting_info
*i
;
1867 for (i
= root
->inner
; i
; i
= i
->next
)
1869 tree arg1
, arg2
, arg3
, x
, field
;
1871 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
1875 if (DECL_NO_STATIC_CHAIN (i
->context
))
1876 arg3
= null_pointer_node
;
1878 arg3
= build_addr (root
->frame_decl
, context
);
1880 arg2
= build_addr (i
->context
, context
);
1882 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
1883 root
->frame_decl
, field
, NULL_TREE
);
1884 arg1
= build_addr (x
, context
);
1886 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
1887 x
= build_call_expr (x
, 3, arg1
, arg2
, arg3
);
1888 append_to_statement_list (x
, &stmt_list
);
1892 /* If we created initialization statements, insert them. */
1895 annotate_all_with_locus (&stmt_list
,
1896 DECL_SOURCE_LOCATION (context
));
1897 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context
)),
1899 BIND_EXPR_BODY (DECL_SAVED_TREE (context
)) = stmt_list
;
1902 /* If a chain_decl was created, then it needs to be registered with
1903 struct function so that it gets initialized from the static chain
1904 register at the beginning of the function. */
1905 sf
= DECL_STRUCT_FUNCTION (root
->context
);
1906 sf
->static_chain_decl
= root
->chain_decl
;
1908 /* Similarly for the non-local goto save area. */
1909 if (root
->nl_goto_field
)
1911 sf
->nonlocal_goto_save_area
1912 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
1913 sf
->has_nonlocal_label
= 1;
1916 /* Make sure all new local variables get inserted into the
1917 proper BIND_EXPR. */
1918 if (root
->new_local_var_chain
)
1919 declare_vars (root
->new_local_var_chain
, DECL_SAVED_TREE (root
->context
),
1921 if (root
->debug_var_chain
)
1922 declare_vars (root
->debug_var_chain
, DECL_SAVED_TREE (root
->context
),
1925 /* Dump the translated tree function. */
1926 dump_function (TDI_nested
, root
->context
);
1930 finalize_nesting_tree (struct nesting_info
*root
)
1935 finalize_nesting_tree (root
->inner
);
1936 finalize_nesting_tree_1 (root
);
1942 /* Unnest the nodes and pass them to cgraph. */
1945 unnest_nesting_tree_1 (struct nesting_info
*root
)
1947 struct cgraph_node
*node
= cgraph_node (root
->context
);
1949 /* For nested functions update the cgraph to reflect unnesting.
1950 We also delay finalizing of these functions up to this point. */
1953 cgraph_unnest_node (cgraph_node (root
->context
));
1954 cgraph_finalize_function (root
->context
, true);
1959 unnest_nesting_tree (struct nesting_info
*root
)
1964 unnest_nesting_tree (root
->inner
);
1965 unnest_nesting_tree_1 (root
);
1971 /* Free the data structures allocated during this pass. */
1974 free_nesting_tree (struct nesting_info
*root
)
1976 struct nesting_info
*next
;
1980 free_nesting_tree (root
->inner
);
1981 pointer_map_destroy (root
->var_map
);
1982 pointer_map_destroy (root
->field_map
);
1990 /* Main entry point for this pass. Process FNDECL and all of its nested
1991 subroutines and turn them into something less tightly bound. */
1994 lower_nested_functions (tree fndecl
)
1996 struct cgraph_node
*cgn
;
1997 struct nesting_info
*root
;
1999 /* If there are no nested functions, there's nothing to do. */
2000 cgn
= cgraph_node (fndecl
);
2004 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
2005 root
= create_nesting_tree (cgn
);
2006 walk_all_functions (convert_nonlocal_reference
, root
);
2007 walk_all_functions (convert_local_reference
, root
);
2008 walk_all_functions (convert_nl_goto_reference
, root
);
2009 walk_all_functions (convert_nl_goto_receiver
, root
);
2010 convert_all_function_calls (root
);
2011 finalize_nesting_tree (root
);
2012 unnest_nesting_tree (root
);
2013 free_nesting_tree (root
);
2014 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
2017 #include "gt-tree-nested.h"