1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
35 #include "langhooks.h"
36 #include "pointer-set.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. */
81 struct nesting_info
*outer
;
82 struct nesting_info
*inner
;
83 struct nesting_info
*next
;
85 struct pointer_map_t
*field_map
;
86 struct pointer_map_t
*var_map
;
87 bitmap suppress_expansion
;
90 tree new_local_var_chain
;
98 bool any_parm_remapped
;
99 bool any_tramp_created
;
100 char static_chain_added
;
104 /* Iterate over the nesting tree, starting with ROOT, depth first. */
106 static inline struct nesting_info
*
107 iter_nestinfo_start (struct nesting_info
*root
)
114 static inline struct nesting_info
*
115 iter_nestinfo_next (struct nesting_info
*node
)
118 return iter_nestinfo_start (node
->next
);
122 #define FOR_EACH_NEST_INFO(I, ROOT) \
123 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
125 /* Obstack used for the bitmaps in the struct above. */
126 static struct bitmap_obstack nesting_info_bitmap_obstack
;
129 /* We're working in so many different function contexts simultaneously,
130 that create_tmp_var is dangerous. Prevent mishap. */
131 #define create_tmp_var cant_use_create_tmp_var_here_dummy
133 /* Like create_tmp_var, except record the variable for registration at
134 the given nesting level. */
137 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
141 /* If the type is of variable size or a type which must be created by the
142 frontend, something is wrong. Note that we explicitly allow
143 incomplete types here, since we create them ourselves here. */
144 gcc_assert (!TREE_ADDRESSABLE (type
));
145 gcc_assert (!TYPE_SIZE_UNIT (type
)
146 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
148 tmp_var
= create_tmp_var_raw (type
, prefix
);
149 DECL_CONTEXT (tmp_var
) = info
->context
;
150 TREE_CHAIN (tmp_var
) = info
->new_local_var_chain
;
151 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
152 if (TREE_CODE (type
) == COMPLEX_TYPE
153 || TREE_CODE (type
) == VECTOR_TYPE
)
154 DECL_GIMPLE_REG_P (tmp_var
) = 1;
156 info
->new_local_var_chain
= tmp_var
;
161 /* Take the address of EXP to be used within function CONTEXT.
162 Mark it for addressability as necessary. */
165 build_addr (tree exp
, tree context
)
171 while (handled_component_p (base
))
172 base
= TREE_OPERAND (base
, 0);
175 TREE_ADDRESSABLE (base
) = 1;
177 /* Building the ADDR_EXPR will compute a set of properties for
178 that ADDR_EXPR. Those properties are unfortunately context
179 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
181 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
182 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
183 way the properties are for the ADDR_EXPR are computed properly. */
184 save_context
= current_function_decl
;
185 current_function_decl
= context
;
186 retval
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
187 current_function_decl
= save_context
;
191 /* Insert FIELD into TYPE, sorted by alignment requirements. */
194 insert_field_into_struct (tree type
, tree field
)
198 DECL_CONTEXT (field
) = type
;
200 for (p
= &TYPE_FIELDS (type
); *p
; p
= &TREE_CHAIN (*p
))
201 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
204 TREE_CHAIN (field
) = *p
;
207 /* Set correct alignment for frame struct type. */
208 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
209 TYPE_ALIGN (type
) = DECL_ALIGN (field
);
212 /* Build or return the RECORD_TYPE that describes the frame state that is
213 shared between INFO->CONTEXT and its nested functions. This record will
214 not be complete until finalize_nesting_tree; up until that point we'll
215 be adding fields as necessary.
217 We also build the DECL that represents this frame in the function. */
220 get_frame_type (struct nesting_info
*info
)
222 tree type
= info
->frame_type
;
227 type
= make_node (RECORD_TYPE
);
229 name
= concat ("FRAME.",
230 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
232 TYPE_NAME (type
) = get_identifier (name
);
235 info
->frame_type
= type
;
236 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
238 /* ??? Always make it addressable for now, since it is meant to
239 be pointed to by the static chain pointer. This pessimizes
240 when it turns out that no static chains are needed because
241 the nested functions referencing non-local variables are not
242 reachable, but the true pessimization is to create the non-
243 local frame structure in the first place. */
244 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
249 /* Return true if DECL should be referenced by pointer in the non-local
253 use_pointer_in_frame (tree decl
)
255 if (TREE_CODE (decl
) == PARM_DECL
)
257 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
258 sized decls, and inefficient to copy large aggregates. Don't bother
259 moving anything but scalar variables. */
260 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
264 /* Variable sized types make things "interesting" in the frame. */
265 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
269 /* Given DECL, a non-locally accessed variable, find or create a field
270 in the non-local frame structure for the given nesting context. */
273 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
274 enum insert_option insert
)
278 if (insert
== NO_INSERT
)
280 slot
= pointer_map_contains (info
->field_map
, decl
);
281 return slot
? (tree
) *slot
: NULL_TREE
;
284 slot
= pointer_map_insert (info
->field_map
, decl
);
287 tree field
= make_node (FIELD_DECL
);
288 DECL_NAME (field
) = DECL_NAME (decl
);
290 if (use_pointer_in_frame (decl
))
292 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
293 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
294 DECL_NONADDRESSABLE_P (field
) = 1;
298 TREE_TYPE (field
) = TREE_TYPE (decl
);
299 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
300 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
301 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
302 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
303 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
304 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
307 insert_field_into_struct (get_frame_type (info
), field
);
310 if (TREE_CODE (decl
) == PARM_DECL
)
311 info
->any_parm_remapped
= true;
317 /* Build or return the variable that holds the static chain within
318 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
321 get_chain_decl (struct nesting_info
*info
)
323 tree decl
= info
->chain_decl
;
329 type
= get_frame_type (info
->outer
);
330 type
= build_pointer_type (type
);
332 /* Note that this variable is *not* entered into any BIND_EXPR;
333 the construction of this variable is handled specially in
334 expand_function_start and initialize_inlined_parameters.
335 Note also that it's represented as a parameter. This is more
336 close to the truth, since the initial value does come from
338 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
339 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
340 DECL_ARTIFICIAL (decl
) = 1;
341 DECL_IGNORED_P (decl
) = 1;
342 TREE_USED (decl
) = 1;
343 DECL_CONTEXT (decl
) = info
->context
;
344 DECL_ARG_TYPE (decl
) = type
;
346 /* Tell tree-inline.c that we never write to this variable, so
347 it can copy-prop the replacement value immediately. */
348 TREE_READONLY (decl
) = 1;
350 info
->chain_decl
= decl
;
353 && (dump_flags
& TDF_DETAILS
)
354 && !DECL_STATIC_CHAIN (info
->context
))
355 fprintf (dump_file
, "Setting static-chain for %s\n",
356 lang_hooks
.decl_printable_name (info
->context
, 2));
358 DECL_STATIC_CHAIN (info
->context
) = 1;
363 /* Build or return the field within the non-local frame state that holds
364 the static chain for INFO->CONTEXT. This is the way to walk back up
365 multiple nesting levels. */
368 get_chain_field (struct nesting_info
*info
)
370 tree field
= info
->chain_field
;
374 tree type
= build_pointer_type (get_frame_type (info
->outer
));
376 field
= make_node (FIELD_DECL
);
377 DECL_NAME (field
) = get_identifier ("__chain");
378 TREE_TYPE (field
) = type
;
379 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
380 DECL_NONADDRESSABLE_P (field
) = 1;
382 insert_field_into_struct (get_frame_type (info
), field
);
384 info
->chain_field
= field
;
387 && (dump_flags
& TDF_DETAILS
)
388 && !DECL_STATIC_CHAIN (info
->context
))
389 fprintf (dump_file
, "Setting static-chain for %s\n",
390 lang_hooks
.decl_printable_name (info
->context
, 2));
392 DECL_STATIC_CHAIN (info
->context
) = 1;
397 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
400 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
405 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
406 gimple_call_set_lhs (call
, t
);
407 if (! gsi_end_p (*gsi
))
408 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
409 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
415 /* Copy EXP into a temporary. Allocate the temporary in the context of
416 INFO and insert the initialization statement before GSI. */
419 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
424 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
425 stmt
= gimple_build_assign (t
, exp
);
426 if (! gsi_end_p (*gsi
))
427 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
428 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
434 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
437 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
438 gimple_stmt_iterator
*gsi
)
440 if (is_gimple_val (exp
))
443 return init_tmp_var (info
, exp
, gsi
);
446 /* Similarly, but copy from the temporary and insert the statement
447 after the iterator. */
450 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
455 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
456 stmt
= gimple_build_assign (exp
, t
);
457 if (! gsi_end_p (*gsi
))
458 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
459 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
464 /* Build or return the type used to represent a nested function trampoline. */
466 static GTY(()) tree trampoline_type
;
469 get_trampoline_type (struct nesting_info
*info
)
471 unsigned align
, size
;
475 return trampoline_type
;
477 align
= TRAMPOLINE_ALIGNMENT
;
478 size
= TRAMPOLINE_SIZE
;
480 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
481 then allocate extra space so that we can do dynamic alignment. */
482 if (align
> STACK_BOUNDARY
)
484 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
485 align
= STACK_BOUNDARY
;
488 t
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
489 t
= build_array_type (char_type_node
, t
);
490 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
491 FIELD_DECL
, get_identifier ("__data"), t
);
492 DECL_ALIGN (t
) = align
;
493 DECL_USER_ALIGN (t
) = 1;
495 trampoline_type
= make_node (RECORD_TYPE
);
496 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
497 TYPE_FIELDS (trampoline_type
) = t
;
498 layout_type (trampoline_type
);
499 DECL_CONTEXT (t
) = trampoline_type
;
501 return trampoline_type
;
504 /* Given DECL, a nested function, find or create a field in the non-local
505 frame structure for a trampoline for this function. */
508 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
509 enum insert_option insert
)
513 if (insert
== NO_INSERT
)
515 slot
= pointer_map_contains (info
->var_map
, decl
);
516 return slot
? (tree
) *slot
: NULL_TREE
;
519 slot
= pointer_map_insert (info
->var_map
, decl
);
522 tree field
= make_node (FIELD_DECL
);
523 DECL_NAME (field
) = DECL_NAME (decl
);
524 TREE_TYPE (field
) = get_trampoline_type (info
);
525 TREE_ADDRESSABLE (field
) = 1;
527 insert_field_into_struct (get_frame_type (info
), field
);
530 info
->any_tramp_created
= true;
536 /* Build or return the field within the non-local frame state that holds
537 the non-local goto "jmp_buf". The buffer itself is maintained by the
538 rtl middle-end as dynamic stack space is allocated. */
541 get_nl_goto_field (struct nesting_info
*info
)
543 tree field
= info
->nl_goto_field
;
549 /* For __builtin_nonlocal_goto, we need N words. The first is the
550 frame pointer, the rest is for the target's stack pointer save
551 area. The number of words is controlled by STACK_SAVEAREA_MODE;
552 not the best interface, but it'll do for now. */
553 if (Pmode
== ptr_mode
)
554 type
= ptr_type_node
;
556 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
558 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
559 size
= size
/ GET_MODE_SIZE (Pmode
);
562 type
= build_array_type
563 (type
, build_index_type (build_int_cst (NULL_TREE
, size
)));
565 field
= make_node (FIELD_DECL
);
566 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
567 TREE_TYPE (field
) = type
;
568 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
569 TREE_ADDRESSABLE (field
) = 1;
571 insert_field_into_struct (get_frame_type (info
), field
);
573 info
->nl_goto_field
= field
;
579 /* Invoke CALLBACK on all statements of GIMPLE sequence SEQ. */
582 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
583 struct nesting_info
*info
, gimple_seq seq
)
585 struct walk_stmt_info wi
;
587 memset (&wi
, 0, sizeof (wi
));
590 walk_gimple_seq (seq
, callback_stmt
, callback_op
, &wi
);
594 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
597 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
598 struct nesting_info
*info
)
600 walk_body (callback_stmt
, callback_op
, info
, gimple_body (info
->context
));
603 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
606 walk_gimple_omp_for (gimple for_stmt
,
607 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
608 struct nesting_info
*info
)
610 struct walk_stmt_info wi
;
615 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body (for_stmt
));
617 seq
= gimple_seq_alloc ();
618 memset (&wi
, 0, sizeof (wi
));
620 wi
.gsi
= gsi_last (seq
);
622 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
625 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
629 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
634 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
637 t
= gimple_omp_for_incr (for_stmt
, i
);
638 gcc_assert (BINARY_CLASS_P (t
));
640 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
643 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
646 if (gimple_seq_empty_p (seq
))
647 gimple_seq_free (seq
);
650 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
651 annotate_all_with_location (seq
, gimple_location (for_stmt
));
652 gimple_seq_add_seq (&pre_body
, seq
);
653 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
657 /* Similarly for ROOT and all functions nested underneath, depth first. */
660 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
661 struct nesting_info
*root
)
663 struct nesting_info
*n
;
664 FOR_EACH_NEST_INFO (n
, root
)
665 walk_function (callback_stmt
, callback_op
, n
);
669 /* We have to check for a fairly pathological case. The operands of function
670 nested function are to be interpreted in the context of the enclosing
671 function. So if any are variably-sized, they will get remapped when the
672 enclosing function is inlined. But that remapping would also have to be
673 done in the types of the PARM_DECLs of the nested function, meaning the
674 argument types of that function will disagree with the arguments in the
675 calls to that function. So we'd either have to make a copy of the nested
676 function corresponding to each time the enclosing function was inlined or
677 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
678 function. The former is not practical. The latter would still require
679 detecting this case to know when to add the conversions. So, for now at
680 least, we don't inline such an enclosing function.
682 We have to do that check recursively, so here return indicating whether
683 FNDECL has such a nested function. ORIG_FN is the function we were
684 trying to inline to use for checking whether any argument is variably
685 modified by anything in it.
687 It would be better to do this in tree-inline.c so that we could give
688 the appropriate warning for why a function can't be inlined, but that's
689 too late since the nesting structure has already been flattened and
690 adding a flag just to record this fact seems a waste of a flag. */
693 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
695 struct cgraph_node
*cgn
= cgraph_node (fndecl
);
698 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
700 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= TREE_CHAIN (arg
))
701 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
704 if (check_for_nested_with_variably_modified (cgn
->decl
, orig_fndecl
))
711 /* Construct our local datastructure describing the function nesting
712 tree rooted by CGN. */
714 static struct nesting_info
*
715 create_nesting_tree (struct cgraph_node
*cgn
)
717 struct nesting_info
*info
= XCNEW (struct nesting_info
);
718 info
->field_map
= pointer_map_create ();
719 info
->var_map
= pointer_map_create ();
720 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
721 info
->context
= cgn
->decl
;
723 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
725 struct nesting_info
*sub
= create_nesting_tree (cgn
);
727 sub
->next
= info
->inner
;
731 /* See discussion at check_for_nested_with_variably_modified for a
732 discussion of why this has to be here. */
733 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
734 DECL_UNINLINABLE (info
->context
) = true;
739 /* Return an expression computing the static chain for TARGET_CONTEXT
740 from INFO->CONTEXT. Insert any necessary computations before TSI. */
743 get_static_chain (struct nesting_info
*info
, tree target_context
,
744 gimple_stmt_iterator
*gsi
)
746 struct nesting_info
*i
;
749 if (info
->context
== target_context
)
751 x
= build_addr (info
->frame_decl
, target_context
);
755 x
= get_chain_decl (info
);
757 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
759 tree field
= get_chain_field (i
);
761 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
762 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
763 x
= init_tmp_var (info
, x
, gsi
);
771 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
772 frame as seen from INFO->CONTEXT. Insert any necessary computations
776 get_frame_field (struct nesting_info
*info
, tree target_context
,
777 tree field
, gimple_stmt_iterator
*gsi
)
779 struct nesting_info
*i
;
782 if (info
->context
== target_context
)
784 /* Make sure frame_decl gets created. */
785 (void) get_frame_type (info
);
786 x
= info
->frame_decl
;
790 x
= get_chain_decl (info
);
792 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
794 tree field
= get_chain_field (i
);
796 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
797 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
798 x
= init_tmp_var (info
, x
, gsi
);
801 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
804 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
808 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
810 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
811 in the nested function with DECL_VALUE_EXPR set to reference the true
812 variable in the parent function. This is used both for debug info
813 and in OpenMP lowering. */
816 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
819 struct nesting_info
*i
;
820 tree x
, field
, new_decl
;
823 slot
= pointer_map_insert (info
->var_map
, decl
);
828 target_context
= decl_function_context (decl
);
830 /* A copy of the code in get_frame_field, but without the temporaries. */
831 if (info
->context
== target_context
)
833 /* Make sure frame_decl gets created. */
834 (void) get_frame_type (info
);
835 x
= info
->frame_decl
;
840 x
= get_chain_decl (info
);
841 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
843 field
= get_chain_field (i
);
844 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
845 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
847 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
850 field
= lookup_field_for_decl (i
, decl
, INSERT
);
851 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
852 if (use_pointer_in_frame (decl
))
853 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
855 /* ??? We should be remapping types as well, surely. */
856 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
857 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
858 DECL_CONTEXT (new_decl
) = info
->context
;
859 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
860 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
861 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
862 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
863 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
864 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
865 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
866 if ((TREE_CODE (decl
) == PARM_DECL
867 || TREE_CODE (decl
) == RESULT_DECL
868 || TREE_CODE (decl
) == VAR_DECL
)
869 && DECL_BY_REFERENCE (decl
))
870 DECL_BY_REFERENCE (new_decl
) = 1;
872 SET_DECL_VALUE_EXPR (new_decl
, x
);
873 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
876 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
877 info
->debug_var_chain
= new_decl
;
880 && info
->context
!= target_context
881 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
882 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
888 /* Callback for walk_gimple_stmt, rewrite all references to VAR
889 and PARM_DECLs that belong to outer functions.
891 The rewrite will involve some number of structure accesses back up
892 the static chain. E.g. for a variable FOO up one nesting level it'll
893 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
894 indirections apply to decls for which use_pointer_in_frame is true. */
897 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
899 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
900 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
904 switch (TREE_CODE (t
))
907 /* Non-automatic variables are never processed. */
908 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
913 if (decl_function_context (t
) != info
->context
)
918 x
= get_nonlocal_debug_decl (info
, t
);
919 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
921 tree target_context
= decl_function_context (t
);
922 struct nesting_info
*i
;
923 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
925 x
= lookup_field_for_decl (i
, t
, INSERT
);
926 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
927 if (use_pointer_in_frame (t
))
929 x
= init_tmp_var (info
, x
, &wi
->gsi
);
930 x
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (x
)), x
);
937 x
= save_tmp_var (info
, x
, &wi
->gsi
);
939 x
= init_tmp_var (info
, x
, &wi
->gsi
);
947 /* We're taking the address of a label from a parent function, but
948 this is not itself a non-local goto. Mark the label such that it
949 will not be deleted, much as we would with a label address in
951 if (decl_function_context (t
) != info
->context
)
952 FORCED_LABEL (t
) = 1;
957 bool save_val_only
= wi
->val_only
;
959 wi
->val_only
= false;
962 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
969 /* If we changed anything, we might no longer be directly
970 referencing a decl. */
971 save_context
= current_function_decl
;
972 current_function_decl
= info
->context
;
973 recompute_tree_invariant_for_addr_expr (t
);
974 current_function_decl
= save_context
;
976 /* If the callback converted the address argument in a context
977 where we only accept variables (and min_invariant, presumably),
978 then compute the address into a temporary. */
980 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
990 case ARRAY_RANGE_REF
:
992 /* Go down this entire nest and just look at the final prefix and
993 anything that describes the references. Otherwise, we lose track
994 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
997 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
999 if (TREE_CODE (t
) == COMPONENT_REF
)
1000 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1002 else if (TREE_CODE (t
) == ARRAY_REF
1003 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1005 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1007 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1009 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1012 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1014 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1016 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1020 wi
->val_only
= false;
1021 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1024 case VIEW_CONVERT_EXPR
:
1025 /* Just request to look at the subtrees, leaving val_only and lhs
1026 untouched. This might actually be for !val_only + lhs, in which
1027 case we don't want to force a replacement by a temporary. */
1032 if (!IS_TYPE_OR_DECL_P (t
))
1035 wi
->val_only
= true;
1044 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1045 struct walk_stmt_info
*);
1047 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1048 and PARM_DECLs that belong to outer functions. */
1051 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1053 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1054 bool need_chain
= false, need_stmts
= false;
1057 bitmap new_suppress
;
1059 new_suppress
= BITMAP_GGC_ALLOC ();
1060 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1062 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1064 switch (OMP_CLAUSE_CODE (clause
))
1066 case OMP_CLAUSE_REDUCTION
:
1067 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1069 goto do_decl_clause
;
1071 case OMP_CLAUSE_LASTPRIVATE
:
1072 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1074 goto do_decl_clause
;
1076 case OMP_CLAUSE_PRIVATE
:
1077 case OMP_CLAUSE_FIRSTPRIVATE
:
1078 case OMP_CLAUSE_COPYPRIVATE
:
1079 case OMP_CLAUSE_SHARED
:
1081 decl
= OMP_CLAUSE_DECL (clause
);
1082 if (TREE_CODE (decl
) == VAR_DECL
1083 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1085 if (decl_function_context (decl
) != info
->context
)
1087 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1088 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1089 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1094 case OMP_CLAUSE_SCHEDULE
:
1095 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1099 case OMP_CLAUSE_NUM_THREADS
:
1100 wi
->val_only
= true;
1102 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1106 case OMP_CLAUSE_NOWAIT
:
1107 case OMP_CLAUSE_ORDERED
:
1108 case OMP_CLAUSE_DEFAULT
:
1109 case OMP_CLAUSE_COPYIN
:
1110 case OMP_CLAUSE_COLLAPSE
:
1111 case OMP_CLAUSE_UNTIED
:
1119 info
->suppress_expansion
= new_suppress
;
1122 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1123 switch (OMP_CLAUSE_CODE (clause
))
1125 case OMP_CLAUSE_REDUCTION
:
1126 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1129 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1130 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1132 walk_body (convert_nonlocal_reference_stmt
,
1133 convert_nonlocal_reference_op
, info
,
1134 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1135 walk_body (convert_nonlocal_reference_stmt
,
1136 convert_nonlocal_reference_op
, info
,
1137 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1138 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1143 case OMP_CLAUSE_LASTPRIVATE
:
1144 walk_body (convert_nonlocal_reference_stmt
,
1145 convert_nonlocal_reference_op
, info
,
1146 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1156 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1159 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1161 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1162 type
= TREE_TYPE (type
);
1164 if (TYPE_NAME (type
)
1165 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1166 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1167 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1169 while (POINTER_TYPE_P (type
)
1170 || TREE_CODE (type
) == VECTOR_TYPE
1171 || TREE_CODE (type
) == FUNCTION_TYPE
1172 || TREE_CODE (type
) == METHOD_TYPE
)
1173 type
= TREE_TYPE (type
);
1175 if (TREE_CODE (type
) == ARRAY_TYPE
)
1179 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1180 domain
= TYPE_DOMAIN (type
);
1183 t
= TYPE_MIN_VALUE (domain
);
1184 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1185 && decl_function_context (t
) != info
->context
)
1186 get_nonlocal_debug_decl (info
, t
);
1187 t
= TYPE_MAX_VALUE (domain
);
1188 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1189 && decl_function_context (t
) != info
->context
)
1190 get_nonlocal_debug_decl (info
, t
);
1195 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1199 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1203 for (var
= BLOCK_VARS (block
); var
; var
= TREE_CHAIN (var
))
1204 if (TREE_CODE (var
) == VAR_DECL
1205 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1206 && DECL_HAS_VALUE_EXPR_P (var
)
1207 && decl_function_context (var
) != info
->context
)
1208 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1211 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1212 PARM_DECLs that belong to outer functions. This handles statements
1213 that are not handled via the standard recursion done in
1214 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1215 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1216 operands of STMT have been handled by this function. */
1219 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1220 struct walk_stmt_info
*wi
)
1222 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1223 tree save_local_var_chain
;
1224 bitmap save_suppress
;
1225 gimple stmt
= gsi_stmt (*gsi
);
1227 switch (gimple_code (stmt
))
1230 /* Don't walk non-local gotos for now. */
1231 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1233 wi
->val_only
= true;
1235 *handled_ops_p
= true;
1240 case GIMPLE_OMP_PARALLEL
:
1241 case GIMPLE_OMP_TASK
:
1242 save_suppress
= info
->suppress_expansion
;
1243 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1247 decl
= get_chain_decl (info
);
1248 c
= build_omp_clause (gimple_location (stmt
),
1249 OMP_CLAUSE_FIRSTPRIVATE
);
1250 OMP_CLAUSE_DECL (c
) = decl
;
1251 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1252 gimple_omp_taskreg_set_clauses (stmt
, c
);
1255 save_local_var_chain
= info
->new_local_var_chain
;
1256 info
->new_local_var_chain
= NULL
;
1258 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1259 info
, gimple_omp_body (stmt
));
1261 if (info
->new_local_var_chain
)
1262 declare_vars (info
->new_local_var_chain
,
1263 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1265 info
->new_local_var_chain
= save_local_var_chain
;
1266 info
->suppress_expansion
= save_suppress
;
1269 case GIMPLE_OMP_FOR
:
1270 save_suppress
= info
->suppress_expansion
;
1271 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1272 walk_gimple_omp_for (stmt
, convert_nonlocal_reference_stmt
,
1273 convert_nonlocal_reference_op
, info
);
1274 walk_body (convert_nonlocal_reference_stmt
,
1275 convert_nonlocal_reference_op
, info
, gimple_omp_body (stmt
));
1276 info
->suppress_expansion
= save_suppress
;
1279 case GIMPLE_OMP_SECTIONS
:
1280 save_suppress
= info
->suppress_expansion
;
1281 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1282 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1283 info
, gimple_omp_body (stmt
));
1284 info
->suppress_expansion
= save_suppress
;
1287 case GIMPLE_OMP_SINGLE
:
1288 save_suppress
= info
->suppress_expansion
;
1289 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1290 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1291 info
, gimple_omp_body (stmt
));
1292 info
->suppress_expansion
= save_suppress
;
1295 case GIMPLE_OMP_SECTION
:
1296 case GIMPLE_OMP_MASTER
:
1297 case GIMPLE_OMP_ORDERED
:
1298 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1299 info
, gimple_omp_body (stmt
));
1303 if (!optimize
&& gimple_bind_block (stmt
))
1304 note_nonlocal_block_vlas (info
, gimple_bind_block (stmt
));
1306 *handled_ops_p
= false;
1310 wi
->val_only
= true;
1312 *handled_ops_p
= false;
1316 /* For every other statement that we are not interested in
1317 handling here, let the walker traverse the operands. */
1318 *handled_ops_p
= false;
1322 /* We have handled all of STMT operands, no need to traverse the operands. */
1323 *handled_ops_p
= true;
1328 /* A subroutine of convert_local_reference. Create a local variable
1329 in the parent function with DECL_VALUE_EXPR set to reference the
1330 field in FRAME. This is used both for debug info and in OpenMP
1334 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1339 slot
= pointer_map_insert (info
->var_map
, decl
);
1341 return (tree
) *slot
;
1343 /* Make sure frame_decl gets created. */
1344 (void) get_frame_type (info
);
1345 x
= info
->frame_decl
;
1346 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1348 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1349 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1350 DECL_CONTEXT (new_decl
) = info
->context
;
1351 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1352 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1353 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1354 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1355 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1356 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1357 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1358 if ((TREE_CODE (decl
) == PARM_DECL
1359 || TREE_CODE (decl
) == RESULT_DECL
1360 || TREE_CODE (decl
) == VAR_DECL
)
1361 && DECL_BY_REFERENCE (decl
))
1362 DECL_BY_REFERENCE (new_decl
) = 1;
1364 SET_DECL_VALUE_EXPR (new_decl
, x
);
1365 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1368 TREE_CHAIN (new_decl
) = info
->debug_var_chain
;
1369 info
->debug_var_chain
= new_decl
;
1371 /* Do not emit debug info twice. */
1372 DECL_IGNORED_P (decl
) = 1;
1378 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1379 and PARM_DECLs that were referenced by inner nested functions.
1380 The rewrite will be a structure reference to the local frame variable. */
1382 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1385 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1387 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1388 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1389 tree t
= *tp
, field
, x
;
1393 switch (TREE_CODE (t
))
1396 /* Non-automatic variables are never processed. */
1397 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1402 if (decl_function_context (t
) == info
->context
)
1404 /* If we copied a pointer to the frame, then the original decl
1405 is used unchanged in the parent function. */
1406 if (use_pointer_in_frame (t
))
1409 /* No need to transform anything if no child references the
1411 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1416 x
= get_local_debug_decl (info
, t
, field
);
1417 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1418 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1423 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1425 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1433 save_val_only
= wi
->val_only
;
1434 wi
->val_only
= false;
1436 wi
->changed
= false;
1437 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1438 wi
->val_only
= save_val_only
;
1440 /* If we converted anything ... */
1445 /* Then the frame decl is now addressable. */
1446 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1448 save_context
= current_function_decl
;
1449 current_function_decl
= info
->context
;
1450 recompute_tree_invariant_for_addr_expr (t
);
1451 current_function_decl
= save_context
;
1453 /* If we are in a context where we only accept values, then
1454 compute the address into a temporary. */
1456 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1465 case ARRAY_RANGE_REF
:
1467 /* Go down this entire nest and just look at the final prefix and
1468 anything that describes the references. Otherwise, we lose track
1469 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1470 save_val_only
= wi
->val_only
;
1471 wi
->val_only
= true;
1473 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1475 if (TREE_CODE (t
) == COMPONENT_REF
)
1476 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1478 else if (TREE_CODE (t
) == ARRAY_REF
1479 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1481 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1483 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1485 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1488 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1490 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1492 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1496 wi
->val_only
= false;
1497 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1498 wi
->val_only
= save_val_only
;
1501 case VIEW_CONVERT_EXPR
:
1502 /* Just request to look at the subtrees, leaving val_only and lhs
1503 untouched. This might actually be for !val_only + lhs, in which
1504 case we don't want to force a replacement by a temporary. */
1509 if (!IS_TYPE_OR_DECL_P (t
))
1512 wi
->val_only
= true;
1521 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1522 struct walk_stmt_info
*);
1524 /* Helper for convert_local_reference. Convert all the references in
1525 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1528 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1530 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1531 bool need_frame
= false, need_stmts
= false;
1534 bitmap new_suppress
;
1536 new_suppress
= BITMAP_GGC_ALLOC ();
1537 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1539 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1541 switch (OMP_CLAUSE_CODE (clause
))
1543 case OMP_CLAUSE_REDUCTION
:
1544 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1546 goto do_decl_clause
;
1548 case OMP_CLAUSE_LASTPRIVATE
:
1549 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1551 goto do_decl_clause
;
1553 case OMP_CLAUSE_PRIVATE
:
1554 case OMP_CLAUSE_FIRSTPRIVATE
:
1555 case OMP_CLAUSE_COPYPRIVATE
:
1556 case OMP_CLAUSE_SHARED
:
1558 decl
= OMP_CLAUSE_DECL (clause
);
1559 if (TREE_CODE (decl
) == VAR_DECL
1560 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1562 if (decl_function_context (decl
) == info
->context
1563 && !use_pointer_in_frame (decl
))
1565 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1568 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1569 OMP_CLAUSE_DECL (clause
)
1570 = get_local_debug_decl (info
, decl
, field
);
1576 case OMP_CLAUSE_SCHEDULE
:
1577 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1581 case OMP_CLAUSE_NUM_THREADS
:
1582 wi
->val_only
= true;
1584 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1588 case OMP_CLAUSE_NOWAIT
:
1589 case OMP_CLAUSE_ORDERED
:
1590 case OMP_CLAUSE_DEFAULT
:
1591 case OMP_CLAUSE_COPYIN
:
1592 case OMP_CLAUSE_COLLAPSE
:
1593 case OMP_CLAUSE_UNTIED
:
1601 info
->suppress_expansion
= new_suppress
;
1604 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1605 switch (OMP_CLAUSE_CODE (clause
))
1607 case OMP_CLAUSE_REDUCTION
:
1608 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1611 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1612 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1614 walk_body (convert_local_reference_stmt
,
1615 convert_local_reference_op
, info
,
1616 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1617 walk_body (convert_local_reference_stmt
,
1618 convert_local_reference_op
, info
,
1619 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1620 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1625 case OMP_CLAUSE_LASTPRIVATE
:
1626 walk_body (convert_local_reference_stmt
,
1627 convert_local_reference_op
, info
,
1628 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1639 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1640 and PARM_DECLs that were referenced by inner nested functions.
1641 The rewrite will be a structure reference to the local frame variable. */
1644 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1645 struct walk_stmt_info
*wi
)
1647 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1648 tree save_local_var_chain
;
1649 bitmap save_suppress
;
1650 gimple stmt
= gsi_stmt (*gsi
);
1652 switch (gimple_code (stmt
))
1654 case GIMPLE_OMP_PARALLEL
:
1655 case GIMPLE_OMP_TASK
:
1656 save_suppress
= info
->suppress_expansion
;
1657 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1661 (void) get_frame_type (info
);
1662 c
= build_omp_clause (gimple_location (stmt
),
1664 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1665 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1666 gimple_omp_taskreg_set_clauses (stmt
, c
);
1669 save_local_var_chain
= info
->new_local_var_chain
;
1670 info
->new_local_var_chain
= NULL
;
1672 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1673 gimple_omp_body (stmt
));
1675 if (info
->new_local_var_chain
)
1676 declare_vars (info
->new_local_var_chain
,
1677 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1678 info
->new_local_var_chain
= save_local_var_chain
;
1679 info
->suppress_expansion
= save_suppress
;
1682 case GIMPLE_OMP_FOR
:
1683 save_suppress
= info
->suppress_expansion
;
1684 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1685 walk_gimple_omp_for (stmt
, convert_local_reference_stmt
,
1686 convert_local_reference_op
, info
);
1687 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1688 info
, gimple_omp_body (stmt
));
1689 info
->suppress_expansion
= save_suppress
;
1692 case GIMPLE_OMP_SECTIONS
:
1693 save_suppress
= info
->suppress_expansion
;
1694 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1695 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1696 info
, gimple_omp_body (stmt
));
1697 info
->suppress_expansion
= save_suppress
;
1700 case GIMPLE_OMP_SINGLE
:
1701 save_suppress
= info
->suppress_expansion
;
1702 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1703 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1704 info
, gimple_omp_body (stmt
));
1705 info
->suppress_expansion
= save_suppress
;
1708 case GIMPLE_OMP_SECTION
:
1709 case GIMPLE_OMP_MASTER
:
1710 case GIMPLE_OMP_ORDERED
:
1711 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1712 info
, gimple_omp_body (stmt
));
1716 wi
->val_only
= true;
1718 *handled_ops_p
= false;
1722 /* For every other statement that we are not interested in
1723 handling here, let the walker traverse the operands. */
1724 *handled_ops_p
= false;
1728 /* Indicate that we have handled all the operands ourselves. */
1729 *handled_ops_p
= true;
1734 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1735 that reference labels from outer functions. The rewrite will be a
1736 call to __builtin_nonlocal_goto. */
1739 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1740 struct walk_stmt_info
*wi
)
1742 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
1743 tree label
, new_label
, target_context
, x
, field
;
1746 gimple stmt
= gsi_stmt (*gsi
);
1748 if (gimple_code (stmt
) != GIMPLE_GOTO
)
1750 *handled_ops_p
= false;
1754 label
= gimple_goto_dest (stmt
);
1755 if (TREE_CODE (label
) != LABEL_DECL
)
1757 *handled_ops_p
= false;
1761 target_context
= decl_function_context (label
);
1762 if (target_context
== info
->context
)
1764 *handled_ops_p
= false;
1768 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1771 /* The original user label may also be use for a normal goto, therefore
1772 we must create a new label that will actually receive the abnormal
1773 control transfer. This new label will be marked LABEL_NONLOCAL; this
1774 mark will trigger proper behavior in the cfg, as well as cause the
1775 (hairy target-specific) non-local goto receiver code to be generated
1776 when we expand rtl. Enter this association into var_map so that we
1777 can insert the new label into the IL during a second pass. */
1778 slot
= pointer_map_insert (i
->var_map
, label
);
1781 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
1782 DECL_NONLOCAL (new_label
) = 1;
1786 new_label
= (tree
) *slot
;
1788 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1789 field
= get_nl_goto_field (i
);
1790 x
= get_frame_field (info
, target_context
, field
, &wi
->gsi
);
1791 x
= build_addr (x
, target_context
);
1792 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
1793 call
= gimple_build_call (implicit_built_in_decls
[BUILT_IN_NONLOCAL_GOTO
], 2,
1794 build_addr (new_label
, target_context
), x
);
1795 gsi_replace (&wi
->gsi
, call
, false);
1797 /* We have handled all of STMT's operands, no need to keep going. */
1798 *handled_ops_p
= true;
1803 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1804 are referenced via nonlocal goto from a nested function. The rewrite
1805 will involve installing a newly generated DECL_NONLOCAL label, and
1806 (potentially) a branch around the rtl gunk that is assumed to be
1807 attached to such a label. */
1810 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1811 struct walk_stmt_info
*wi
)
1813 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1814 tree label
, new_label
;
1815 gimple_stmt_iterator tmp_gsi
;
1817 gimple stmt
= gsi_stmt (*gsi
);
1819 if (gimple_code (stmt
) != GIMPLE_LABEL
)
1821 *handled_ops_p
= false;
1825 label
= gimple_label_label (stmt
);
1827 slot
= pointer_map_contains (info
->var_map
, label
);
1830 *handled_ops_p
= false;
1834 /* If there's any possibility that the previous statement falls through,
1835 then we must branch around the new non-local label. */
1837 gsi_prev (&tmp_gsi
);
1838 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
1840 gimple stmt
= gimple_build_goto (label
);
1841 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1844 new_label
= (tree
) *slot
;
1845 stmt
= gimple_build_label (new_label
);
1846 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1848 *handled_ops_p
= true;
1853 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1854 of nested functions that require the use of trampolines. The rewrite
1855 will involve a reference a trampoline generated for the occasion. */
1858 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1860 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1861 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
1862 tree t
= *tp
, decl
, target_context
, x
, builtin
;
1866 switch (TREE_CODE (t
))
1870 T.1 = &CHAIN->tramp;
1871 T.2 = __builtin_adjust_trampoline (T.1);
1872 T.3 = (func_type)T.2;
1875 decl
= TREE_OPERAND (t
, 0);
1876 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1879 /* Only need to process nested functions. */
1880 target_context
= decl_function_context (decl
);
1881 if (!target_context
)
1884 /* If the nested function doesn't use a static chain, then
1885 it doesn't need a trampoline. */
1886 if (!DECL_STATIC_CHAIN (decl
))
1889 /* If we don't want a trampoline, then don't build one. */
1890 if (TREE_NO_TRAMPOLINE (t
))
1893 /* Lookup the immediate parent of the callee, as that's where
1894 we need to insert the trampoline. */
1895 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1897 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1899 /* Compute the address of the field holding the trampoline. */
1900 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1901 x
= build_addr (x
, target_context
);
1902 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
1904 /* Do machine-specific ugliness. Normally this will involve
1905 computing extra alignment, but it can really be anything. */
1906 builtin
= implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
];
1907 call
= gimple_build_call (builtin
, 1, x
);
1908 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
1910 /* Cast back to the proper function type. */
1911 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1912 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1918 if (!IS_TYPE_OR_DECL_P (t
))
1927 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1928 to addresses of nested functions that require the use of
1929 trampolines. The rewrite will involve a reference a trampoline
1930 generated for the occasion. */
1933 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1934 struct walk_stmt_info
*wi
)
1936 gimple stmt
= gsi_stmt (*gsi
);
1938 switch (gimple_code (stmt
))
1942 /* Only walk call arguments, lest we generate trampolines for
1944 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
1945 for (i
= 0; i
< nargs
; i
++)
1946 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
1949 *handled_ops_p
= true;
1957 *handled_ops_p
= false;
1963 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
1964 that reference nested functions to make sure that the static chain
1965 is set up properly for the call. */
1968 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1969 struct walk_stmt_info
*wi
)
1971 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1972 tree decl
, target_context
;
1973 char save_static_chain_added
;
1975 gimple stmt
= gsi_stmt (*gsi
);
1977 switch (gimple_code (stmt
))
1980 if (gimple_call_chain (stmt
))
1982 decl
= gimple_call_fndecl (stmt
);
1985 target_context
= decl_function_context (decl
);
1986 if (target_context
&& DECL_STATIC_CHAIN (decl
))
1988 gimple_call_set_chain (stmt
, get_static_chain (info
, target_context
,
1990 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
1994 case GIMPLE_OMP_PARALLEL
:
1995 case GIMPLE_OMP_TASK
:
1996 save_static_chain_added
= info
->static_chain_added
;
1997 info
->static_chain_added
= 0;
1998 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body (stmt
));
1999 for (i
= 0; i
< 2; i
++)
2002 if ((info
->static_chain_added
& (1 << i
)) == 0)
2004 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2005 /* Don't add CHAIN.* or FRAME.* twice. */
2006 for (c
= gimple_omp_taskreg_clauses (stmt
);
2008 c
= OMP_CLAUSE_CHAIN (c
))
2009 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2010 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2011 && OMP_CLAUSE_DECL (c
) == decl
)
2015 c
= build_omp_clause (gimple_location (stmt
),
2016 i
? OMP_CLAUSE_FIRSTPRIVATE
2017 : OMP_CLAUSE_SHARED
);
2018 OMP_CLAUSE_DECL (c
) = decl
;
2019 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2020 gimple_omp_taskreg_set_clauses (stmt
, c
);
2023 info
->static_chain_added
|= save_static_chain_added
;
2026 case GIMPLE_OMP_FOR
:
2027 walk_body (convert_gimple_call
, NULL
, info
,
2028 gimple_omp_for_pre_body (stmt
));
2030 case GIMPLE_OMP_SECTIONS
:
2031 case GIMPLE_OMP_SECTION
:
2032 case GIMPLE_OMP_SINGLE
:
2033 case GIMPLE_OMP_MASTER
:
2034 case GIMPLE_OMP_ORDERED
:
2035 case GIMPLE_OMP_CRITICAL
:
2036 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body (stmt
));
2040 /* Keep looking for other operands. */
2041 *handled_ops_p
= false;
2045 *handled_ops_p
= true;
2049 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2050 call expressions. At the same time, determine if a nested function
2051 actually uses its static chain; if not, remember that. */
2054 convert_all_function_calls (struct nesting_info
*root
)
2056 struct nesting_info
*n
;
2060 /* First, optimistically clear static_chain for all decls that haven't
2061 used the static chain already for variable access. */
2062 FOR_EACH_NEST_INFO (n
, root
)
2064 tree decl
= n
->context
;
2065 if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2067 DECL_STATIC_CHAIN (decl
) = 0;
2068 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2069 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2070 lang_hooks
.decl_printable_name (decl
, 2));
2073 DECL_STATIC_CHAIN (decl
) = 1;
2076 /* Walk the functions and perform transformations. Note that these
2077 transformations can induce new uses of the static chain, which in turn
2078 require re-examining all users of the decl. */
2079 /* ??? It would make sense to try to use the call graph to speed this up,
2080 but the call graph hasn't really been built yet. Even if it did, we
2081 would still need to iterate in this loop since address-of references
2082 wouldn't show up in the callgraph anyway. */
2086 any_changed
= false;
2089 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2090 fputc ('\n', dump_file
);
2092 FOR_EACH_NEST_INFO (n
, root
)
2094 tree decl
= n
->context
;
2095 bool old_static_chain
= DECL_STATIC_CHAIN (decl
);
2097 walk_function (convert_tramp_reference_stmt
,
2098 convert_tramp_reference_op
, n
);
2099 walk_function (convert_gimple_call
, NULL
, n
);
2101 /* If a call to another function created the use of a chain
2102 within this function, we'll have to continue iteration. */
2103 if (!old_static_chain
&& DECL_STATIC_CHAIN (decl
))
2107 while (any_changed
);
2109 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2110 fprintf (dump_file
, "convert_all_function_calls iterations: %d\n\n",
2114 struct nesting_copy_body_data
2117 struct nesting_info
*root
;
2120 /* A helper subroutine for debug_var_chain type remapping. */
2123 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2125 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2126 void **slot
= pointer_map_contains (nid
->root
->var_map
, decl
);
2129 return (tree
) *slot
;
2131 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2133 tree new_decl
= copy_decl_no_change (decl
, id
);
2134 DECL_ORIGINAL_TYPE (new_decl
)
2135 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2139 if (TREE_CODE (decl
) == VAR_DECL
2140 || TREE_CODE (decl
) == PARM_DECL
2141 || TREE_CODE (decl
) == RESULT_DECL
)
2144 return copy_decl_no_change (decl
, id
);
2147 /* A helper function for remap_vla_decls. See if *TP contains
2148 some remapped variables. */
2151 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2153 struct nesting_info
*root
= (struct nesting_info
*) data
;
2160 slot
= pointer_map_contains (root
->var_map
, t
);
2163 return (tree
) *slot
;
2168 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2172 remap_vla_decls (tree block
, struct nesting_info
*root
)
2174 tree var
, subblock
, val
, type
;
2175 struct nesting_copy_body_data id
;
2177 for (subblock
= BLOCK_SUBBLOCKS (block
);
2179 subblock
= BLOCK_CHAIN (subblock
))
2180 remap_vla_decls (subblock
, root
);
2182 for (var
= BLOCK_VARS (block
); var
; var
= TREE_CHAIN (var
))
2184 if (TREE_CODE (var
) == VAR_DECL
2185 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
2186 && DECL_HAS_VALUE_EXPR_P (var
))
2188 type
= TREE_TYPE (var
);
2189 val
= DECL_VALUE_EXPR (var
);
2190 if (walk_tree (&type
, contains_remapped_vars
, root
, NULL
) != NULL
2191 || walk_tree (&val
, contains_remapped_vars
, root
, NULL
) != NULL
)
2195 if (var
== NULL_TREE
)
2198 memset (&id
, 0, sizeof (id
));
2199 id
.cb
.copy_decl
= nesting_copy_decl
;
2200 id
.cb
.decl_map
= pointer_map_create ();
2203 for (; var
; var
= TREE_CHAIN (var
))
2204 if (TREE_CODE (var
) == VAR_DECL
2205 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
2206 && DECL_HAS_VALUE_EXPR_P (var
))
2208 struct nesting_info
*i
;
2209 tree newt
, t
, context
;
2211 t
= type
= TREE_TYPE (var
);
2212 val
= DECL_VALUE_EXPR (var
);
2213 if (walk_tree (&type
, contains_remapped_vars
, root
, NULL
) == NULL
2214 && walk_tree (&val
, contains_remapped_vars
, root
, NULL
) == NULL
)
2217 context
= decl_function_context (var
);
2218 for (i
= root
; i
; i
= i
->outer
)
2219 if (i
->context
== context
)
2225 id
.cb
.src_fn
= i
->context
;
2226 id
.cb
.dst_fn
= i
->context
;
2227 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2229 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2230 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2232 newt
= TREE_TYPE (newt
);
2235 if (TYPE_NAME (newt
)
2236 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2237 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2239 && TYPE_NAME (newt
) == TYPE_NAME (t
))
2240 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2242 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2243 if (val
!= DECL_VALUE_EXPR (var
))
2244 SET_DECL_VALUE_EXPR (var
, val
);
2247 pointer_map_destroy (id
.cb
.decl_map
);
2250 /* Do "everything else" to clean up or complete state collected by the
2251 various walking passes -- lay out the types and decls, generate code
2252 to initialize the frame decl, store critical expressions in the
2253 struct function for rtl to find. */
2256 finalize_nesting_tree_1 (struct nesting_info
*root
)
2258 gimple_seq stmt_list
;
2260 tree context
= root
->context
;
2261 struct function
*sf
;
2265 /* If we created a non-local frame type or decl, we need to lay them
2266 out at this time. */
2267 if (root
->frame_type
)
2269 /* In some cases the frame type will trigger the -Wpadded warning.
2270 This is not helpful; suppress it. */
2271 int save_warn_padded
= warn_padded
;
2275 layout_type (root
->frame_type
);
2276 warn_padded
= save_warn_padded
;
2277 layout_decl (root
->frame_decl
, 0);
2279 /* Remove root->frame_decl from root->new_local_var_chain, so
2280 that we can declare it also in the lexical blocks, which
2281 helps ensure virtual regs that end up appearing in its RTL
2282 expression get substituted in instantiate_virtual_regs(). */
2283 for (adjust
= &root
->new_local_var_chain
;
2284 *adjust
!= root
->frame_decl
;
2285 adjust
= &TREE_CHAIN (*adjust
))
2286 gcc_assert (TREE_CHAIN (*adjust
));
2287 *adjust
= TREE_CHAIN (*adjust
);
2289 TREE_CHAIN (root
->frame_decl
) = NULL_TREE
;
2290 declare_vars (root
->frame_decl
,
2291 gimple_seq_first_stmt (gimple_body (context
)), true);
2294 /* If any parameters were referenced non-locally, then we need to
2295 insert a copy. Likewise, if any variables were referenced by
2296 pointer, we need to initialize the address. */
2297 if (root
->any_parm_remapped
)
2300 for (p
= DECL_ARGUMENTS (context
); p
; p
= TREE_CHAIN (p
))
2304 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
2308 if (use_pointer_in_frame (p
))
2309 x
= build_addr (p
, context
);
2313 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2314 root
->frame_decl
, field
, NULL_TREE
);
2315 stmt
= gimple_build_assign (y
, x
);
2316 gimple_seq_add_stmt (&stmt_list
, stmt
);
2317 /* If the assignment is from a non-register the stmt is
2318 not valid gimple. Make it so by using a temporary instead. */
2319 if (!is_gimple_reg (x
)
2320 && is_gimple_reg_type (TREE_TYPE (x
)))
2322 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
2323 x
= init_tmp_var (root
, x
, &gsi
);
2324 gimple_assign_set_rhs1 (stmt
, x
);
2329 /* If a chain_field was created, then it needs to be initialized
2331 if (root
->chain_field
)
2333 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
2334 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
2335 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
2336 gimple_seq_add_stmt (&stmt_list
, stmt
);
2339 /* If trampolines were created, then we need to initialize them. */
2340 if (root
->any_tramp_created
)
2342 struct nesting_info
*i
;
2343 for (i
= root
->inner
; i
; i
= i
->next
)
2345 tree arg1
, arg2
, arg3
, x
, field
;
2347 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
2351 gcc_assert (DECL_STATIC_CHAIN (i
->context
));
2352 arg3
= build_addr (root
->frame_decl
, context
);
2354 arg2
= build_addr (i
->context
, context
);
2356 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2357 root
->frame_decl
, field
, NULL_TREE
);
2358 arg1
= build_addr (x
, context
);
2360 x
= implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
];
2361 stmt
= gimple_build_call (x
, 3, arg1
, arg2
, arg3
);
2362 gimple_seq_add_stmt (&stmt_list
, stmt
);
2366 /* If we created initialization statements, insert them. */
2370 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
2371 bind
= gimple_seq_first_stmt (gimple_body (context
));
2372 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
2373 gimple_bind_set_body (bind
, stmt_list
);
2376 /* If a chain_decl was created, then it needs to be registered with
2377 struct function so that it gets initialized from the static chain
2378 register at the beginning of the function. */
2379 sf
= DECL_STRUCT_FUNCTION (root
->context
);
2380 sf
->static_chain_decl
= root
->chain_decl
;
2382 /* Similarly for the non-local goto save area. */
2383 if (root
->nl_goto_field
)
2385 sf
->nonlocal_goto_save_area
2386 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
2387 sf
->has_nonlocal_label
= 1;
2390 /* Make sure all new local variables get inserted into the
2391 proper BIND_EXPR. */
2392 if (root
->new_local_var_chain
)
2393 declare_vars (root
->new_local_var_chain
,
2394 gimple_seq_first_stmt (gimple_body (root
->context
)),
2397 if (root
->debug_var_chain
)
2402 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
2404 for (debug_var
= root
->debug_var_chain
; debug_var
;
2405 debug_var
= TREE_CHAIN (debug_var
))
2406 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2409 /* If there are any debug decls with variable length types,
2410 remap those types using other debug_var_chain variables. */
2413 struct nesting_copy_body_data id
;
2415 memset (&id
, 0, sizeof (id
));
2416 id
.cb
.copy_decl
= nesting_copy_decl
;
2417 id
.cb
.decl_map
= pointer_map_create ();
2420 for (; debug_var
; debug_var
= TREE_CHAIN (debug_var
))
2421 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2423 tree type
= TREE_TYPE (debug_var
);
2424 tree newt
, t
= type
;
2425 struct nesting_info
*i
;
2427 for (i
= root
; i
; i
= i
->outer
)
2428 if (variably_modified_type_p (type
, i
->context
))
2434 id
.cb
.src_fn
= i
->context
;
2435 id
.cb
.dst_fn
= i
->context
;
2436 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2438 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
2439 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2441 newt
= TREE_TYPE (newt
);
2444 if (TYPE_NAME (newt
)
2445 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2446 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2448 && TYPE_NAME (newt
) == TYPE_NAME (t
))
2449 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2452 pointer_map_destroy (id
.cb
.decl_map
);
2455 scope
= gimple_seq_first_stmt (gimple_body (root
->context
));
2456 if (gimple_bind_block (scope
))
2457 declare_vars (root
->debug_var_chain
, scope
, true);
2459 BLOCK_VARS (DECL_INITIAL (root
->context
))
2460 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
2461 root
->debug_var_chain
);
2464 /* Dump the translated tree function. */
2467 fputs ("\n\n", dump_file
);
2468 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
2473 finalize_nesting_tree (struct nesting_info
*root
)
2475 struct nesting_info
*n
;
2476 FOR_EACH_NEST_INFO (n
, root
)
2477 finalize_nesting_tree_1 (n
);
2480 /* Unnest the nodes and pass them to cgraph. */
2483 unnest_nesting_tree_1 (struct nesting_info
*root
)
2485 struct cgraph_node
*node
= cgraph_node (root
->context
);
2487 /* For nested functions update the cgraph to reflect unnesting.
2488 We also delay finalizing of these functions up to this point. */
2491 cgraph_unnest_node (cgraph_node (root
->context
));
2492 cgraph_finalize_function (root
->context
, true);
2497 unnest_nesting_tree (struct nesting_info
*root
)
2499 struct nesting_info
*n
;
2500 FOR_EACH_NEST_INFO (n
, root
)
2501 unnest_nesting_tree_1 (n
);
2504 /* Free the data structures allocated during this pass. */
2507 free_nesting_tree (struct nesting_info
*root
)
2509 struct nesting_info
*node
, *next
;
2511 node
= iter_nestinfo_start (root
);
2514 next
= iter_nestinfo_next (node
);
2515 pointer_map_destroy (node
->var_map
);
2516 pointer_map_destroy (node
->field_map
);
2523 /* Gimplify a function and all its nested functions. */
2525 gimplify_all_functions (struct cgraph_node
*root
)
2527 struct cgraph_node
*iter
;
2528 if (!gimple_body (root
->decl
))
2529 gimplify_function_tree (root
->decl
);
2530 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
2531 gimplify_all_functions (iter
);
2534 /* Main entry point for this pass. Process FNDECL and all of its nested
2535 subroutines and turn them into something less tightly bound. */
2538 lower_nested_functions (tree fndecl
)
2540 struct cgraph_node
*cgn
;
2541 struct nesting_info
*root
;
2543 /* If there are no nested functions, there's nothing to do. */
2544 cgn
= cgraph_node (fndecl
);
2548 gimplify_all_functions (cgn
);
2550 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
2552 fprintf (dump_file
, "\n;; Function %s\n\n",
2553 lang_hooks
.decl_printable_name (fndecl
, 2));
2555 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
2556 root
= create_nesting_tree (cgn
);
2558 walk_all_functions (convert_nonlocal_reference_stmt
,
2559 convert_nonlocal_reference_op
,
2561 walk_all_functions (convert_local_reference_stmt
,
2562 convert_local_reference_op
,
2564 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
2565 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
2567 convert_all_function_calls (root
);
2568 finalize_nesting_tree (root
);
2569 unnest_nesting_tree (root
);
2571 free_nesting_tree (root
);
2572 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
2576 dump_end (TDI_nested
, dump_file
);
2581 #include "gt-tree-nested.h"