1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2013 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"
25 #include "stringpool.h"
26 #include "stor-layout.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
33 #include "gimple-iterator.h"
34 #include "gimple-walk.h"
35 #include "tree-iterator.h"
39 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
40 #include "langhooks.h"
41 #include "pointer-set.h"
42 #include "gimple-low.h"
45 /* The object of this pass is to lower the representation of a set of nested
46 functions in order to expose all of the gory details of the various
47 nonlocal references. We want to do this sooner rather than later, in
48 order to give us more freedom in emitting all of the functions in question.
50 Back in olden times, when gcc was young, we developed an insanely
51 complicated scheme whereby variables which were referenced nonlocally
52 were forced to live in the stack of the declaring function, and then
53 the nested functions magically discovered where these variables were
54 placed. In order for this scheme to function properly, it required
55 that the outer function be partially expanded, then we switch to
56 compiling the inner function, and once done with those we switch back
57 to compiling the outer function. Such delicate ordering requirements
58 makes it difficult to do whole translation unit optimizations
59 involving such functions.
61 The implementation here is much more direct. Everything that can be
62 referenced by an inner function is a member of an explicitly created
63 structure herein called the "nonlocal frame struct". The incoming
64 static chain for a nested function is a pointer to this struct in
65 the parent. In this way, we settle on known offsets from a known
66 base, and so are decoupled from the logic that places objects in the
67 function's stack frame. More importantly, we don't have to wait for
68 that to happen -- since the compilation of the inner function is no
69 longer tied to a real stack frame, the nonlocal frame struct can be
70 allocated anywhere. Which means that the outer function is now
73 Theory of operation here is very simple. Iterate over all the
74 statements in all the functions (depth first) several times,
75 allocating structures and fields on demand. In general we want to
76 examine inner functions first, so that we can avoid making changes
77 to outer functions which are unnecessary.
79 The order of the passes matters a bit, in that later passes will be
80 skipped if it is discovered that the functions don't actually interact
81 at all. That is, they're nested in the lexical sense but could have
82 been written as independent functions without change. */
87 struct nesting_info
*outer
;
88 struct nesting_info
*inner
;
89 struct nesting_info
*next
;
91 struct pointer_map_t
*field_map
;
92 struct pointer_map_t
*var_map
;
93 struct pointer_set_t
*mem_refs
;
94 bitmap suppress_expansion
;
97 tree new_local_var_chain
;
105 bool any_parm_remapped
;
106 bool any_tramp_created
;
107 char static_chain_added
;
111 /* Iterate over the nesting tree, starting with ROOT, depth first. */
113 static inline struct nesting_info
*
114 iter_nestinfo_start (struct nesting_info
*root
)
121 static inline struct nesting_info
*
122 iter_nestinfo_next (struct nesting_info
*node
)
125 return iter_nestinfo_start (node
->next
);
129 #define FOR_EACH_NEST_INFO(I, ROOT) \
130 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
132 /* Obstack used for the bitmaps in the struct above. */
133 static struct bitmap_obstack nesting_info_bitmap_obstack
;
136 /* We're working in so many different function contexts simultaneously,
137 that create_tmp_var is dangerous. Prevent mishap. */
138 #define create_tmp_var cant_use_create_tmp_var_here_dummy
140 /* Like create_tmp_var, except record the variable for registration at
141 the given nesting level. */
144 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
148 /* If the type is of variable size or a type which must be created by the
149 frontend, something is wrong. Note that we explicitly allow
150 incomplete types here, since we create them ourselves here. */
151 gcc_assert (!TREE_ADDRESSABLE (type
));
152 gcc_assert (!TYPE_SIZE_UNIT (type
)
153 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
155 tmp_var
= create_tmp_var_raw (type
, prefix
);
156 DECL_CONTEXT (tmp_var
) = info
->context
;
157 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
158 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
159 if (TREE_CODE (type
) == COMPLEX_TYPE
160 || TREE_CODE (type
) == VECTOR_TYPE
)
161 DECL_GIMPLE_REG_P (tmp_var
) = 1;
163 info
->new_local_var_chain
= tmp_var
;
168 /* Take the address of EXP to be used within function CONTEXT.
169 Mark it for addressability as necessary. */
172 build_addr (tree exp
, tree context
)
178 while (handled_component_p (base
))
179 base
= TREE_OPERAND (base
, 0);
182 TREE_ADDRESSABLE (base
) = 1;
184 /* Building the ADDR_EXPR will compute a set of properties for
185 that ADDR_EXPR. Those properties are unfortunately context
186 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
188 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
189 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
190 way the properties are for the ADDR_EXPR are computed properly. */
191 save_context
= current_function_decl
;
192 current_function_decl
= context
;
193 retval
= build_fold_addr_expr (exp
);
194 current_function_decl
= save_context
;
198 /* Insert FIELD into TYPE, sorted by alignment requirements. */
201 insert_field_into_struct (tree type
, tree field
)
205 DECL_CONTEXT (field
) = type
;
207 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
208 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
211 DECL_CHAIN (field
) = *p
;
214 /* Set correct alignment for frame struct type. */
215 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
216 TYPE_ALIGN (type
) = DECL_ALIGN (field
);
219 /* Build or return the RECORD_TYPE that describes the frame state that is
220 shared between INFO->CONTEXT and its nested functions. This record will
221 not be complete until finalize_nesting_tree; up until that point we'll
222 be adding fields as necessary.
224 We also build the DECL that represents this frame in the function. */
227 get_frame_type (struct nesting_info
*info
)
229 tree type
= info
->frame_type
;
234 type
= make_node (RECORD_TYPE
);
236 name
= concat ("FRAME.",
237 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
239 TYPE_NAME (type
) = get_identifier (name
);
242 info
->frame_type
= type
;
243 info
->frame_decl
= create_tmp_var_for (info
, type
, "FRAME");
244 DECL_NONLOCAL_FRAME (info
->frame_decl
) = 1;
246 /* ??? Always make it addressable for now, since it is meant to
247 be pointed to by the static chain pointer. This pessimizes
248 when it turns out that no static chains are needed because
249 the nested functions referencing non-local variables are not
250 reachable, but the true pessimization is to create the non-
251 local frame structure in the first place. */
252 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
257 /* Return true if DECL should be referenced by pointer in the non-local
261 use_pointer_in_frame (tree decl
)
263 if (TREE_CODE (decl
) == PARM_DECL
)
265 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
266 sized decls, and inefficient to copy large aggregates. Don't bother
267 moving anything but scalar variables. */
268 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
272 /* Variable sized types make things "interesting" in the frame. */
273 return DECL_SIZE (decl
) == NULL
|| !TREE_CONSTANT (DECL_SIZE (decl
));
277 /* Given DECL, a non-locally accessed variable, find or create a field
278 in the non-local frame structure for the given nesting context. */
281 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
282 enum insert_option insert
)
286 if (insert
== NO_INSERT
)
288 slot
= pointer_map_contains (info
->field_map
, decl
);
289 return slot
? (tree
) *slot
: NULL_TREE
;
292 slot
= pointer_map_insert (info
->field_map
, decl
);
295 tree field
= make_node (FIELD_DECL
);
296 DECL_NAME (field
) = DECL_NAME (decl
);
298 if (use_pointer_in_frame (decl
))
300 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
301 DECL_ALIGN (field
) = TYPE_ALIGN (TREE_TYPE (field
));
302 DECL_NONADDRESSABLE_P (field
) = 1;
306 TREE_TYPE (field
) = TREE_TYPE (decl
);
307 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
308 DECL_ALIGN (field
) = DECL_ALIGN (decl
);
309 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
310 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
311 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
312 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
315 insert_field_into_struct (get_frame_type (info
), field
);
318 if (TREE_CODE (decl
) == PARM_DECL
)
319 info
->any_parm_remapped
= true;
325 /* Build or return the variable that holds the static chain within
326 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
329 get_chain_decl (struct nesting_info
*info
)
331 tree decl
= info
->chain_decl
;
337 type
= get_frame_type (info
->outer
);
338 type
= build_pointer_type (type
);
340 /* Note that this variable is *not* entered into any BIND_EXPR;
341 the construction of this variable is handled specially in
342 expand_function_start and initialize_inlined_parameters.
343 Note also that it's represented as a parameter. This is more
344 close to the truth, since the initial value does come from
346 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
347 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
348 DECL_ARTIFICIAL (decl
) = 1;
349 DECL_IGNORED_P (decl
) = 1;
350 TREE_USED (decl
) = 1;
351 DECL_CONTEXT (decl
) = info
->context
;
352 DECL_ARG_TYPE (decl
) = type
;
354 /* Tell tree-inline.c that we never write to this variable, so
355 it can copy-prop the replacement value immediately. */
356 TREE_READONLY (decl
) = 1;
358 info
->chain_decl
= decl
;
361 && (dump_flags
& TDF_DETAILS
)
362 && !DECL_STATIC_CHAIN (info
->context
))
363 fprintf (dump_file
, "Setting static-chain for %s\n",
364 lang_hooks
.decl_printable_name (info
->context
, 2));
366 DECL_STATIC_CHAIN (info
->context
) = 1;
371 /* Build or return the field within the non-local frame state that holds
372 the static chain for INFO->CONTEXT. This is the way to walk back up
373 multiple nesting levels. */
376 get_chain_field (struct nesting_info
*info
)
378 tree field
= info
->chain_field
;
382 tree type
= build_pointer_type (get_frame_type (info
->outer
));
384 field
= make_node (FIELD_DECL
);
385 DECL_NAME (field
) = get_identifier ("__chain");
386 TREE_TYPE (field
) = type
;
387 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
388 DECL_NONADDRESSABLE_P (field
) = 1;
390 insert_field_into_struct (get_frame_type (info
), field
);
392 info
->chain_field
= field
;
395 && (dump_flags
& TDF_DETAILS
)
396 && !DECL_STATIC_CHAIN (info
->context
))
397 fprintf (dump_file
, "Setting static-chain for %s\n",
398 lang_hooks
.decl_printable_name (info
->context
, 2));
400 DECL_STATIC_CHAIN (info
->context
) = 1;
405 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
408 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
413 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
414 gimple_call_set_lhs (call
, t
);
415 if (! gsi_end_p (*gsi
))
416 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
417 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
423 /* Copy EXP into a temporary. Allocate the temporary in the context of
424 INFO and insert the initialization statement before GSI. */
427 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
432 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
433 stmt
= gimple_build_assign (t
, exp
);
434 if (! gsi_end_p (*gsi
))
435 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
436 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
442 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
445 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
446 gimple_stmt_iterator
*gsi
)
448 if (is_gimple_val (exp
))
451 return init_tmp_var (info
, exp
, gsi
);
454 /* Similarly, but copy from the temporary and insert the statement
455 after the iterator. */
458 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
463 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
464 stmt
= gimple_build_assign (exp
, t
);
465 if (! gsi_end_p (*gsi
))
466 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
467 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
472 /* Build or return the type used to represent a nested function trampoline. */
474 static GTY(()) tree trampoline_type
;
477 get_trampoline_type (struct nesting_info
*info
)
479 unsigned align
, size
;
483 return trampoline_type
;
485 align
= TRAMPOLINE_ALIGNMENT
;
486 size
= TRAMPOLINE_SIZE
;
488 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
489 then allocate extra space so that we can do dynamic alignment. */
490 if (align
> STACK_BOUNDARY
)
492 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
493 align
= STACK_BOUNDARY
;
496 t
= build_index_type (size_int (size
- 1));
497 t
= build_array_type (char_type_node
, t
);
498 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
499 FIELD_DECL
, get_identifier ("__data"), t
);
500 DECL_ALIGN (t
) = align
;
501 DECL_USER_ALIGN (t
) = 1;
503 trampoline_type
= make_node (RECORD_TYPE
);
504 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
505 TYPE_FIELDS (trampoline_type
) = t
;
506 layout_type (trampoline_type
);
507 DECL_CONTEXT (t
) = trampoline_type
;
509 return trampoline_type
;
512 /* Given DECL, a nested function, find or create a field in the non-local
513 frame structure for a trampoline for this function. */
516 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
517 enum insert_option insert
)
521 if (insert
== NO_INSERT
)
523 slot
= pointer_map_contains (info
->var_map
, decl
);
524 return slot
? (tree
) *slot
: NULL_TREE
;
527 slot
= pointer_map_insert (info
->var_map
, decl
);
530 tree field
= make_node (FIELD_DECL
);
531 DECL_NAME (field
) = DECL_NAME (decl
);
532 TREE_TYPE (field
) = get_trampoline_type (info
);
533 TREE_ADDRESSABLE (field
) = 1;
535 insert_field_into_struct (get_frame_type (info
), field
);
538 info
->any_tramp_created
= true;
544 /* Build or return the field within the non-local frame state that holds
545 the non-local goto "jmp_buf". The buffer itself is maintained by the
546 rtl middle-end as dynamic stack space is allocated. */
549 get_nl_goto_field (struct nesting_info
*info
)
551 tree field
= info
->nl_goto_field
;
557 /* For __builtin_nonlocal_goto, we need N words. The first is the
558 frame pointer, the rest is for the target's stack pointer save
559 area. The number of words is controlled by STACK_SAVEAREA_MODE;
560 not the best interface, but it'll do for now. */
561 if (Pmode
== ptr_mode
)
562 type
= ptr_type_node
;
564 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
566 size
= GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
567 size
= size
/ GET_MODE_SIZE (Pmode
);
570 type
= build_array_type
571 (type
, build_index_type (size_int (size
)));
573 field
= make_node (FIELD_DECL
);
574 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
575 TREE_TYPE (field
) = type
;
576 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
577 TREE_ADDRESSABLE (field
) = 1;
579 insert_field_into_struct (get_frame_type (info
), field
);
581 info
->nl_goto_field
= field
;
587 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
590 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
591 struct nesting_info
*info
, gimple_seq
*pseq
)
593 struct walk_stmt_info wi
;
595 memset (&wi
, 0, sizeof (wi
));
598 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
602 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
605 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
606 struct nesting_info
*info
)
608 gimple_seq body
= gimple_body (info
->context
);
609 walk_body (callback_stmt
, callback_op
, info
, &body
);
610 gimple_set_body (info
->context
, body
);
613 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
616 walk_gimple_omp_for (gimple for_stmt
,
617 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
618 struct nesting_info
*info
)
620 struct walk_stmt_info wi
;
625 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
628 memset (&wi
, 0, sizeof (wi
));
630 wi
.gsi
= gsi_last (seq
);
632 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
635 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
639 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
644 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
647 t
= gimple_omp_for_incr (for_stmt
, i
);
648 gcc_assert (BINARY_CLASS_P (t
));
650 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
653 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
656 seq
= gsi_seq (wi
.gsi
);
657 if (!gimple_seq_empty_p (seq
))
659 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
660 annotate_all_with_location (seq
, gimple_location (for_stmt
));
661 gimple_seq_add_seq (&pre_body
, seq
);
662 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
666 /* Similarly for ROOT and all functions nested underneath, depth first. */
669 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
670 struct nesting_info
*root
)
672 struct nesting_info
*n
;
673 FOR_EACH_NEST_INFO (n
, root
)
674 walk_function (callback_stmt
, callback_op
, n
);
678 /* We have to check for a fairly pathological case. The operands of function
679 nested function are to be interpreted in the context of the enclosing
680 function. So if any are variably-sized, they will get remapped when the
681 enclosing function is inlined. But that remapping would also have to be
682 done in the types of the PARM_DECLs of the nested function, meaning the
683 argument types of that function will disagree with the arguments in the
684 calls to that function. So we'd either have to make a copy of the nested
685 function corresponding to each time the enclosing function was inlined or
686 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
687 function. The former is not practical. The latter would still require
688 detecting this case to know when to add the conversions. So, for now at
689 least, we don't inline such an enclosing function.
691 We have to do that check recursively, so here return indicating whether
692 FNDECL has such a nested function. ORIG_FN is the function we were
693 trying to inline to use for checking whether any argument is variably
694 modified by anything in it.
696 It would be better to do this in tree-inline.c so that we could give
697 the appropriate warning for why a function can't be inlined, but that's
698 too late since the nesting structure has already been flattened and
699 adding a flag just to record this fact seems a waste of a flag. */
702 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
704 struct cgraph_node
*cgn
= cgraph_get_node (fndecl
);
707 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
709 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
710 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
713 if (check_for_nested_with_variably_modified (cgn
->decl
,
721 /* Construct our local datastructure describing the function nesting
722 tree rooted by CGN. */
724 static struct nesting_info
*
725 create_nesting_tree (struct cgraph_node
*cgn
)
727 struct nesting_info
*info
= XCNEW (struct nesting_info
);
728 info
->field_map
= pointer_map_create ();
729 info
->var_map
= pointer_map_create ();
730 info
->mem_refs
= pointer_set_create ();
731 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
732 info
->context
= cgn
->decl
;
734 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
736 struct nesting_info
*sub
= create_nesting_tree (cgn
);
738 sub
->next
= info
->inner
;
742 /* See discussion at check_for_nested_with_variably_modified for a
743 discussion of why this has to be here. */
744 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
745 DECL_UNINLINABLE (info
->context
) = true;
750 /* Return an expression computing the static chain for TARGET_CONTEXT
751 from INFO->CONTEXT. Insert any necessary computations before TSI. */
754 get_static_chain (struct nesting_info
*info
, tree target_context
,
755 gimple_stmt_iterator
*gsi
)
757 struct nesting_info
*i
;
760 if (info
->context
== target_context
)
762 x
= build_addr (info
->frame_decl
, target_context
);
766 x
= get_chain_decl (info
);
768 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
770 tree field
= get_chain_field (i
);
772 x
= build_simple_mem_ref (x
);
773 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
774 x
= init_tmp_var (info
, x
, gsi
);
782 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
783 frame as seen from INFO->CONTEXT. Insert any necessary computations
787 get_frame_field (struct nesting_info
*info
, tree target_context
,
788 tree field
, gimple_stmt_iterator
*gsi
)
790 struct nesting_info
*i
;
793 if (info
->context
== target_context
)
795 /* Make sure frame_decl gets created. */
796 (void) get_frame_type (info
);
797 x
= info
->frame_decl
;
801 x
= get_chain_decl (info
);
803 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
805 tree field
= get_chain_field (i
);
807 x
= build_simple_mem_ref (x
);
808 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
809 x
= init_tmp_var (info
, x
, gsi
);
812 x
= build_simple_mem_ref (x
);
815 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
819 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
821 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
822 in the nested function with DECL_VALUE_EXPR set to reference the true
823 variable in the parent function. This is used both for debug info
824 and in OpenMP lowering. */
827 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
830 struct nesting_info
*i
;
831 tree x
, field
, new_decl
;
834 slot
= pointer_map_insert (info
->var_map
, decl
);
839 target_context
= decl_function_context (decl
);
841 /* A copy of the code in get_frame_field, but without the temporaries. */
842 if (info
->context
== target_context
)
844 /* Make sure frame_decl gets created. */
845 (void) get_frame_type (info
);
846 x
= info
->frame_decl
;
851 x
= get_chain_decl (info
);
852 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
854 field
= get_chain_field (i
);
855 x
= build_simple_mem_ref (x
);
856 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
858 x
= build_simple_mem_ref (x
);
861 field
= lookup_field_for_decl (i
, decl
, INSERT
);
862 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
863 if (use_pointer_in_frame (decl
))
864 x
= build_simple_mem_ref (x
);
866 /* ??? We should be remapping types as well, surely. */
867 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
868 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
869 DECL_CONTEXT (new_decl
) = info
->context
;
870 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
871 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
872 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
873 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
874 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
875 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
876 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
877 if ((TREE_CODE (decl
) == PARM_DECL
878 || TREE_CODE (decl
) == RESULT_DECL
879 || TREE_CODE (decl
) == VAR_DECL
)
880 && DECL_BY_REFERENCE (decl
))
881 DECL_BY_REFERENCE (new_decl
) = 1;
883 SET_DECL_VALUE_EXPR (new_decl
, x
);
884 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
887 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
888 info
->debug_var_chain
= new_decl
;
891 && info
->context
!= target_context
892 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
893 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
899 /* Callback for walk_gimple_stmt, rewrite all references to VAR
900 and PARM_DECLs that belong to outer functions.
902 The rewrite will involve some number of structure accesses back up
903 the static chain. E.g. for a variable FOO up one nesting level it'll
904 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
905 indirections apply to decls for which use_pointer_in_frame is true. */
908 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
910 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
911 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
915 switch (TREE_CODE (t
))
918 /* Non-automatic variables are never processed. */
919 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
924 if (decl_function_context (t
) != info
->context
)
929 x
= get_nonlocal_debug_decl (info
, t
);
930 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
932 tree target_context
= decl_function_context (t
);
933 struct nesting_info
*i
;
934 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
936 x
= lookup_field_for_decl (i
, t
, INSERT
);
937 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
938 if (use_pointer_in_frame (t
))
940 x
= init_tmp_var (info
, x
, &wi
->gsi
);
941 x
= build_simple_mem_ref (x
);
948 x
= save_tmp_var (info
, x
, &wi
->gsi
);
950 x
= init_tmp_var (info
, x
, &wi
->gsi
);
958 /* We're taking the address of a label from a parent function, but
959 this is not itself a non-local goto. Mark the label such that it
960 will not be deleted, much as we would with a label address in
962 if (decl_function_context (t
) != info
->context
)
963 FORCED_LABEL (t
) = 1;
968 bool save_val_only
= wi
->val_only
;
970 wi
->val_only
= false;
973 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
980 /* If we changed anything, we might no longer be directly
981 referencing a decl. */
982 save_context
= current_function_decl
;
983 current_function_decl
= info
->context
;
984 recompute_tree_invariant_for_addr_expr (t
);
985 current_function_decl
= save_context
;
987 /* If the callback converted the address argument in a context
988 where we only accept variables (and min_invariant, presumably),
989 then compute the address into a temporary. */
991 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1001 case ARRAY_RANGE_REF
:
1003 /* Go down this entire nest and just look at the final prefix and
1004 anything that describes the references. Otherwise, we lose track
1005 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1006 wi
->val_only
= true;
1008 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1010 if (TREE_CODE (t
) == COMPONENT_REF
)
1011 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1013 else if (TREE_CODE (t
) == ARRAY_REF
1014 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1016 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1018 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1020 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1024 wi
->val_only
= false;
1025 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1028 case VIEW_CONVERT_EXPR
:
1029 /* Just request to look at the subtrees, leaving val_only and lhs
1030 untouched. This might actually be for !val_only + lhs, in which
1031 case we don't want to force a replacement by a temporary. */
1036 if (!IS_TYPE_OR_DECL_P (t
))
1039 wi
->val_only
= true;
1048 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1049 struct walk_stmt_info
*);
1051 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1052 and PARM_DECLs that belong to outer functions. */
1055 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1057 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1058 bool need_chain
= false, need_stmts
= false;
1061 bitmap new_suppress
;
1063 new_suppress
= BITMAP_GGC_ALLOC ();
1064 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1066 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1068 switch (OMP_CLAUSE_CODE (clause
))
1070 case OMP_CLAUSE_REDUCTION
:
1071 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1073 goto do_decl_clause
;
1075 case OMP_CLAUSE_LASTPRIVATE
:
1076 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1078 goto do_decl_clause
;
1080 case OMP_CLAUSE_PRIVATE
:
1081 case OMP_CLAUSE_FIRSTPRIVATE
:
1082 case OMP_CLAUSE_COPYPRIVATE
:
1083 case OMP_CLAUSE_SHARED
:
1085 decl
= OMP_CLAUSE_DECL (clause
);
1086 if (TREE_CODE (decl
) == VAR_DECL
1087 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1089 if (decl_function_context (decl
) != info
->context
)
1091 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1092 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1093 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1098 case OMP_CLAUSE_SCHEDULE
:
1099 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1102 case OMP_CLAUSE_FINAL
:
1104 case OMP_CLAUSE_NUM_THREADS
:
1105 wi
->val_only
= true;
1107 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1111 case OMP_CLAUSE_NOWAIT
:
1112 case OMP_CLAUSE_ORDERED
:
1113 case OMP_CLAUSE_DEFAULT
:
1114 case OMP_CLAUSE_COPYIN
:
1115 case OMP_CLAUSE_COLLAPSE
:
1116 case OMP_CLAUSE_UNTIED
:
1117 case OMP_CLAUSE_MERGEABLE
:
1125 info
->suppress_expansion
= new_suppress
;
1128 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1129 switch (OMP_CLAUSE_CODE (clause
))
1131 case OMP_CLAUSE_REDUCTION
:
1132 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1135 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1136 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1138 walk_body (convert_nonlocal_reference_stmt
,
1139 convert_nonlocal_reference_op
, info
,
1140 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1141 walk_body (convert_nonlocal_reference_stmt
,
1142 convert_nonlocal_reference_op
, info
,
1143 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1144 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1149 case OMP_CLAUSE_LASTPRIVATE
:
1150 walk_body (convert_nonlocal_reference_stmt
,
1151 convert_nonlocal_reference_op
, info
,
1152 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1162 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1165 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1167 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1168 type
= TREE_TYPE (type
);
1170 if (TYPE_NAME (type
)
1171 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1172 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1173 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1175 while (POINTER_TYPE_P (type
)
1176 || TREE_CODE (type
) == VECTOR_TYPE
1177 || TREE_CODE (type
) == FUNCTION_TYPE
1178 || TREE_CODE (type
) == METHOD_TYPE
)
1179 type
= TREE_TYPE (type
);
1181 if (TREE_CODE (type
) == ARRAY_TYPE
)
1185 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1186 domain
= TYPE_DOMAIN (type
);
1189 t
= TYPE_MIN_VALUE (domain
);
1190 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1191 && decl_function_context (t
) != info
->context
)
1192 get_nonlocal_debug_decl (info
, t
);
1193 t
= TYPE_MAX_VALUE (domain
);
1194 if (t
&& (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
)
1195 && decl_function_context (t
) != info
->context
)
1196 get_nonlocal_debug_decl (info
, t
);
1201 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1205 note_nonlocal_block_vlas (struct nesting_info
*info
, tree block
)
1209 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
1210 if (TREE_CODE (var
) == VAR_DECL
1211 && variably_modified_type_p (TREE_TYPE (var
), NULL
)
1212 && DECL_HAS_VALUE_EXPR_P (var
)
1213 && decl_function_context (var
) != info
->context
)
1214 note_nonlocal_vla_type (info
, TREE_TYPE (var
));
1217 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1218 PARM_DECLs that belong to outer functions. This handles statements
1219 that are not handled via the standard recursion done in
1220 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1221 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1222 operands of STMT have been handled by this function. */
1225 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1226 struct walk_stmt_info
*wi
)
1228 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1229 tree save_local_var_chain
;
1230 bitmap save_suppress
;
1231 gimple stmt
= gsi_stmt (*gsi
);
1233 switch (gimple_code (stmt
))
1236 /* Don't walk non-local gotos for now. */
1237 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1239 wi
->val_only
= true;
1241 *handled_ops_p
= true;
1246 case GIMPLE_OMP_PARALLEL
:
1247 case GIMPLE_OMP_TASK
:
1248 save_suppress
= info
->suppress_expansion
;
1249 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1253 decl
= get_chain_decl (info
);
1254 c
= build_omp_clause (gimple_location (stmt
),
1255 OMP_CLAUSE_FIRSTPRIVATE
);
1256 OMP_CLAUSE_DECL (c
) = decl
;
1257 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1258 gimple_omp_taskreg_set_clauses (stmt
, c
);
1261 save_local_var_chain
= info
->new_local_var_chain
;
1262 info
->new_local_var_chain
= NULL
;
1264 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1265 info
, gimple_omp_body_ptr (stmt
));
1267 if (info
->new_local_var_chain
)
1268 declare_vars (info
->new_local_var_chain
,
1269 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1271 info
->new_local_var_chain
= save_local_var_chain
;
1272 info
->suppress_expansion
= save_suppress
;
1275 case GIMPLE_OMP_FOR
:
1276 save_suppress
= info
->suppress_expansion
;
1277 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1278 walk_gimple_omp_for (stmt
, convert_nonlocal_reference_stmt
,
1279 convert_nonlocal_reference_op
, info
);
1280 walk_body (convert_nonlocal_reference_stmt
,
1281 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1282 info
->suppress_expansion
= save_suppress
;
1285 case GIMPLE_OMP_SECTIONS
:
1286 save_suppress
= info
->suppress_expansion
;
1287 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1288 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1289 info
, gimple_omp_body_ptr (stmt
));
1290 info
->suppress_expansion
= save_suppress
;
1293 case GIMPLE_OMP_SINGLE
:
1294 save_suppress
= info
->suppress_expansion
;
1295 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1296 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1297 info
, gimple_omp_body_ptr (stmt
));
1298 info
->suppress_expansion
= save_suppress
;
1301 case GIMPLE_OMP_TARGET
:
1302 save_suppress
= info
->suppress_expansion
;
1303 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
1304 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1305 info
, gimple_omp_body_ptr (stmt
));
1306 info
->suppress_expansion
= save_suppress
;
1309 case GIMPLE_OMP_TEAMS
:
1310 save_suppress
= info
->suppress_expansion
;
1311 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1312 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1313 info
, gimple_omp_body_ptr (stmt
));
1314 info
->suppress_expansion
= save_suppress
;
1317 case GIMPLE_OMP_SECTION
:
1318 case GIMPLE_OMP_MASTER
:
1319 case GIMPLE_OMP_TASKGROUP
:
1320 case GIMPLE_OMP_ORDERED
:
1321 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1322 info
, gimple_omp_body_ptr (stmt
));
1326 if (!optimize
&& gimple_bind_block (stmt
))
1327 note_nonlocal_block_vlas (info
, gimple_bind_block (stmt
));
1329 *handled_ops_p
= false;
1333 wi
->val_only
= true;
1335 *handled_ops_p
= false;
1339 /* For every other statement that we are not interested in
1340 handling here, let the walker traverse the operands. */
1341 *handled_ops_p
= false;
1345 /* We have handled all of STMT operands, no need to traverse the operands. */
1346 *handled_ops_p
= true;
1351 /* A subroutine of convert_local_reference. Create a local variable
1352 in the parent function with DECL_VALUE_EXPR set to reference the
1353 field in FRAME. This is used both for debug info and in OpenMP
1357 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1362 slot
= pointer_map_insert (info
->var_map
, decl
);
1364 return (tree
) *slot
;
1366 /* Make sure frame_decl gets created. */
1367 (void) get_frame_type (info
);
1368 x
= info
->frame_decl
;
1369 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1371 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1372 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1373 DECL_CONTEXT (new_decl
) = info
->context
;
1374 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1375 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1376 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1377 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1378 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1379 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1380 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1381 if ((TREE_CODE (decl
) == PARM_DECL
1382 || TREE_CODE (decl
) == RESULT_DECL
1383 || TREE_CODE (decl
) == VAR_DECL
)
1384 && DECL_BY_REFERENCE (decl
))
1385 DECL_BY_REFERENCE (new_decl
) = 1;
1387 SET_DECL_VALUE_EXPR (new_decl
, x
);
1388 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1391 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1392 info
->debug_var_chain
= new_decl
;
1394 /* Do not emit debug info twice. */
1395 DECL_IGNORED_P (decl
) = 1;
1401 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1402 and PARM_DECLs that were referenced by inner nested functions.
1403 The rewrite will be a structure reference to the local frame variable. */
1405 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1408 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1410 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1411 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1412 tree t
= *tp
, field
, x
;
1416 switch (TREE_CODE (t
))
1419 /* Non-automatic variables are never processed. */
1420 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1425 if (decl_function_context (t
) == info
->context
)
1427 /* If we copied a pointer to the frame, then the original decl
1428 is used unchanged in the parent function. */
1429 if (use_pointer_in_frame (t
))
1432 /* No need to transform anything if no child references the
1434 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1439 x
= get_local_debug_decl (info
, t
, field
);
1440 if (!bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1441 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1446 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1448 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1456 save_val_only
= wi
->val_only
;
1457 wi
->val_only
= false;
1459 wi
->changed
= false;
1460 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1461 wi
->val_only
= save_val_only
;
1463 /* If we converted anything ... */
1468 /* Then the frame decl is now addressable. */
1469 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1471 save_context
= current_function_decl
;
1472 current_function_decl
= info
->context
;
1473 recompute_tree_invariant_for_addr_expr (t
);
1474 current_function_decl
= save_context
;
1476 /* If we are in a context where we only accept values, then
1477 compute the address into a temporary. */
1479 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1488 case ARRAY_RANGE_REF
:
1490 /* Go down this entire nest and just look at the final prefix and
1491 anything that describes the references. Otherwise, we lose track
1492 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1493 save_val_only
= wi
->val_only
;
1494 wi
->val_only
= true;
1496 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1498 if (TREE_CODE (t
) == COMPONENT_REF
)
1499 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1501 else if (TREE_CODE (t
) == ARRAY_REF
1502 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1504 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1506 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1508 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1512 wi
->val_only
= false;
1513 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1514 wi
->val_only
= save_val_only
;
1518 save_val_only
= wi
->val_only
;
1519 wi
->val_only
= true;
1521 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1523 /* We need to re-fold the MEM_REF as component references as
1524 part of a ADDR_EXPR address are not allowed. But we cannot
1525 fold here, as the chain record type is not yet finalized. */
1526 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1527 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1528 pointer_set_insert (info
->mem_refs
, tp
);
1529 wi
->val_only
= save_val_only
;
1532 case VIEW_CONVERT_EXPR
:
1533 /* Just request to look at the subtrees, leaving val_only and lhs
1534 untouched. This might actually be for !val_only + lhs, in which
1535 case we don't want to force a replacement by a temporary. */
1540 if (!IS_TYPE_OR_DECL_P (t
))
1543 wi
->val_only
= true;
1552 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1553 struct walk_stmt_info
*);
1555 /* Helper for convert_local_reference. Convert all the references in
1556 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1559 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1561 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1562 bool need_frame
= false, need_stmts
= false;
1565 bitmap new_suppress
;
1567 new_suppress
= BITMAP_GGC_ALLOC ();
1568 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1570 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1572 switch (OMP_CLAUSE_CODE (clause
))
1574 case OMP_CLAUSE_REDUCTION
:
1575 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1577 goto do_decl_clause
;
1579 case OMP_CLAUSE_LASTPRIVATE
:
1580 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1582 goto do_decl_clause
;
1584 case OMP_CLAUSE_PRIVATE
:
1585 case OMP_CLAUSE_FIRSTPRIVATE
:
1586 case OMP_CLAUSE_COPYPRIVATE
:
1587 case OMP_CLAUSE_SHARED
:
1589 decl
= OMP_CLAUSE_DECL (clause
);
1590 if (TREE_CODE (decl
) == VAR_DECL
1591 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1593 if (decl_function_context (decl
) == info
->context
1594 && !use_pointer_in_frame (decl
))
1596 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1599 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1600 OMP_CLAUSE_DECL (clause
)
1601 = get_local_debug_decl (info
, decl
, field
);
1607 case OMP_CLAUSE_SCHEDULE
:
1608 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1611 case OMP_CLAUSE_FINAL
:
1613 case OMP_CLAUSE_NUM_THREADS
:
1614 wi
->val_only
= true;
1616 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0), &dummy
,
1620 case OMP_CLAUSE_NOWAIT
:
1621 case OMP_CLAUSE_ORDERED
:
1622 case OMP_CLAUSE_DEFAULT
:
1623 case OMP_CLAUSE_COPYIN
:
1624 case OMP_CLAUSE_COLLAPSE
:
1625 case OMP_CLAUSE_UNTIED
:
1626 case OMP_CLAUSE_MERGEABLE
:
1634 info
->suppress_expansion
= new_suppress
;
1637 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1638 switch (OMP_CLAUSE_CODE (clause
))
1640 case OMP_CLAUSE_REDUCTION
:
1641 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1644 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1645 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1647 walk_body (convert_local_reference_stmt
,
1648 convert_local_reference_op
, info
,
1649 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1650 walk_body (convert_local_reference_stmt
,
1651 convert_local_reference_op
, info
,
1652 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1653 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1658 case OMP_CLAUSE_LASTPRIVATE
:
1659 walk_body (convert_local_reference_stmt
,
1660 convert_local_reference_op
, info
,
1661 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1672 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1673 and PARM_DECLs that were referenced by inner nested functions.
1674 The rewrite will be a structure reference to the local frame variable. */
1677 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1678 struct walk_stmt_info
*wi
)
1680 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1681 tree save_local_var_chain
;
1682 bitmap save_suppress
;
1683 gimple stmt
= gsi_stmt (*gsi
);
1685 switch (gimple_code (stmt
))
1687 case GIMPLE_OMP_PARALLEL
:
1688 case GIMPLE_OMP_TASK
:
1689 save_suppress
= info
->suppress_expansion
;
1690 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1694 (void) get_frame_type (info
);
1695 c
= build_omp_clause (gimple_location (stmt
),
1697 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
1698 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1699 gimple_omp_taskreg_set_clauses (stmt
, c
);
1702 save_local_var_chain
= info
->new_local_var_chain
;
1703 info
->new_local_var_chain
= NULL
;
1705 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
1706 gimple_omp_body_ptr (stmt
));
1708 if (info
->new_local_var_chain
)
1709 declare_vars (info
->new_local_var_chain
,
1710 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
1711 info
->new_local_var_chain
= save_local_var_chain
;
1712 info
->suppress_expansion
= save_suppress
;
1715 case GIMPLE_OMP_FOR
:
1716 save_suppress
= info
->suppress_expansion
;
1717 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1718 walk_gimple_omp_for (stmt
, convert_local_reference_stmt
,
1719 convert_local_reference_op
, info
);
1720 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1721 info
, gimple_omp_body_ptr (stmt
));
1722 info
->suppress_expansion
= save_suppress
;
1725 case GIMPLE_OMP_SECTIONS
:
1726 save_suppress
= info
->suppress_expansion
;
1727 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1728 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1729 info
, gimple_omp_body_ptr (stmt
));
1730 info
->suppress_expansion
= save_suppress
;
1733 case GIMPLE_OMP_SINGLE
:
1734 save_suppress
= info
->suppress_expansion
;
1735 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1736 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1737 info
, gimple_omp_body_ptr (stmt
));
1738 info
->suppress_expansion
= save_suppress
;
1741 case GIMPLE_OMP_TARGET
:
1742 save_suppress
= info
->suppress_expansion
;
1743 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
1744 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1745 info
, gimple_omp_body_ptr (stmt
));
1746 info
->suppress_expansion
= save_suppress
;
1749 case GIMPLE_OMP_TEAMS
:
1750 save_suppress
= info
->suppress_expansion
;
1751 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
1752 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1753 info
, gimple_omp_body_ptr (stmt
));
1754 info
->suppress_expansion
= save_suppress
;
1757 case GIMPLE_OMP_SECTION
:
1758 case GIMPLE_OMP_MASTER
:
1759 case GIMPLE_OMP_TASKGROUP
:
1760 case GIMPLE_OMP_ORDERED
:
1761 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
1762 info
, gimple_omp_body_ptr (stmt
));
1766 wi
->val_only
= true;
1768 *handled_ops_p
= false;
1772 if (gimple_clobber_p (stmt
))
1774 tree lhs
= gimple_assign_lhs (stmt
);
1775 if (!use_pointer_in_frame (lhs
)
1776 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
1778 gsi_replace (gsi
, gimple_build_nop (), true);
1782 *handled_ops_p
= false;
1786 /* For every other statement that we are not interested in
1787 handling here, let the walker traverse the operands. */
1788 *handled_ops_p
= false;
1792 /* Indicate that we have handled all the operands ourselves. */
1793 *handled_ops_p
= true;
1798 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1799 that reference labels from outer functions. The rewrite will be a
1800 call to __builtin_nonlocal_goto. */
1803 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1804 struct walk_stmt_info
*wi
)
1806 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
1807 tree label
, new_label
, target_context
, x
, field
;
1810 gimple stmt
= gsi_stmt (*gsi
);
1812 if (gimple_code (stmt
) != GIMPLE_GOTO
)
1814 *handled_ops_p
= false;
1818 label
= gimple_goto_dest (stmt
);
1819 if (TREE_CODE (label
) != LABEL_DECL
)
1821 *handled_ops_p
= false;
1825 target_context
= decl_function_context (label
);
1826 if (target_context
== info
->context
)
1828 *handled_ops_p
= false;
1832 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
1835 /* The original user label may also be use for a normal goto, therefore
1836 we must create a new label that will actually receive the abnormal
1837 control transfer. This new label will be marked LABEL_NONLOCAL; this
1838 mark will trigger proper behavior in the cfg, as well as cause the
1839 (hairy target-specific) non-local goto receiver code to be generated
1840 when we expand rtl. Enter this association into var_map so that we
1841 can insert the new label into the IL during a second pass. */
1842 slot
= pointer_map_insert (i
->var_map
, label
);
1845 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
1846 DECL_NONLOCAL (new_label
) = 1;
1850 new_label
= (tree
) *slot
;
1852 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1853 field
= get_nl_goto_field (i
);
1854 x
= get_frame_field (info
, target_context
, field
, gsi
);
1855 x
= build_addr (x
, target_context
);
1856 x
= gsi_gimplify_val (info
, x
, gsi
);
1857 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
1858 2, build_addr (new_label
, target_context
), x
);
1859 gsi_replace (gsi
, call
, false);
1861 /* We have handled all of STMT's operands, no need to keep going. */
1862 *handled_ops_p
= true;
1867 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1868 are referenced via nonlocal goto from a nested function. The rewrite
1869 will involve installing a newly generated DECL_NONLOCAL label, and
1870 (potentially) a branch around the rtl gunk that is assumed to be
1871 attached to such a label. */
1874 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1875 struct walk_stmt_info
*wi
)
1877 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1878 tree label
, new_label
;
1879 gimple_stmt_iterator tmp_gsi
;
1881 gimple stmt
= gsi_stmt (*gsi
);
1883 if (gimple_code (stmt
) != GIMPLE_LABEL
)
1885 *handled_ops_p
= false;
1889 label
= gimple_label_label (stmt
);
1891 slot
= pointer_map_contains (info
->var_map
, label
);
1894 *handled_ops_p
= false;
1898 /* If there's any possibility that the previous statement falls through,
1899 then we must branch around the new non-local label. */
1901 gsi_prev (&tmp_gsi
);
1902 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
1904 gimple stmt
= gimple_build_goto (label
);
1905 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1908 new_label
= (tree
) *slot
;
1909 stmt
= gimple_build_label (new_label
);
1910 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1912 *handled_ops_p
= true;
1917 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1918 of nested functions that require the use of trampolines. The rewrite
1919 will involve a reference a trampoline generated for the occasion. */
1922 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1924 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1925 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
1926 tree t
= *tp
, decl
, target_context
, x
, builtin
;
1930 switch (TREE_CODE (t
))
1934 T.1 = &CHAIN->tramp;
1935 T.2 = __builtin_adjust_trampoline (T.1);
1936 T.3 = (func_type)T.2;
1939 decl
= TREE_OPERAND (t
, 0);
1940 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1943 /* Only need to process nested functions. */
1944 target_context
= decl_function_context (decl
);
1945 if (!target_context
)
1948 /* If the nested function doesn't use a static chain, then
1949 it doesn't need a trampoline. */
1950 if (!DECL_STATIC_CHAIN (decl
))
1953 /* If we don't want a trampoline, then don't build one. */
1954 if (TREE_NO_TRAMPOLINE (t
))
1957 /* Lookup the immediate parent of the callee, as that's where
1958 we need to insert the trampoline. */
1959 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
1961 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
1963 /* Compute the address of the field holding the trampoline. */
1964 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1965 x
= build_addr (x
, target_context
);
1966 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
1968 /* Do machine-specific ugliness. Normally this will involve
1969 computing extra alignment, but it can really be anything. */
1970 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
1971 call
= gimple_build_call (builtin
, 1, x
);
1972 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
1974 /* Cast back to the proper function type. */
1975 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
1976 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1982 if (!IS_TYPE_OR_DECL_P (t
))
1991 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1992 to addresses of nested functions that require the use of
1993 trampolines. The rewrite will involve a reference a trampoline
1994 generated for the occasion. */
1997 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1998 struct walk_stmt_info
*wi
)
2000 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2001 gimple stmt
= gsi_stmt (*gsi
);
2003 switch (gimple_code (stmt
))
2007 /* Only walk call arguments, lest we generate trampolines for
2009 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2010 for (i
= 0; i
< nargs
; i
++)
2011 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2016 case GIMPLE_OMP_PARALLEL
:
2017 case GIMPLE_OMP_TASK
:
2019 tree save_local_var_chain
;
2020 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2021 save_local_var_chain
= info
->new_local_var_chain
;
2022 info
->new_local_var_chain
= NULL
;
2023 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2024 info
, gimple_omp_body_ptr (stmt
));
2025 if (info
->new_local_var_chain
)
2026 declare_vars (info
->new_local_var_chain
,
2027 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2029 info
->new_local_var_chain
= save_local_var_chain
;
2034 *handled_ops_p
= false;
2039 *handled_ops_p
= true;
2045 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2046 that reference nested functions to make sure that the static chain
2047 is set up properly for the call. */
2050 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2051 struct walk_stmt_info
*wi
)
2053 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2054 tree decl
, target_context
;
2055 char save_static_chain_added
;
2057 gimple stmt
= gsi_stmt (*gsi
);
2059 switch (gimple_code (stmt
))
2062 if (gimple_call_chain (stmt
))
2064 decl
= gimple_call_fndecl (stmt
);
2067 target_context
= decl_function_context (decl
);
2068 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2070 gimple_call_set_chain (stmt
, get_static_chain (info
, target_context
,
2072 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2076 case GIMPLE_OMP_PARALLEL
:
2077 case GIMPLE_OMP_TASK
:
2078 save_static_chain_added
= info
->static_chain_added
;
2079 info
->static_chain_added
= 0;
2080 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2081 for (i
= 0; i
< 2; i
++)
2084 if ((info
->static_chain_added
& (1 << i
)) == 0)
2086 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2087 /* Don't add CHAIN.* or FRAME.* twice. */
2088 for (c
= gimple_omp_taskreg_clauses (stmt
);
2090 c
= OMP_CLAUSE_CHAIN (c
))
2091 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2092 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2093 && OMP_CLAUSE_DECL (c
) == decl
)
2097 c
= build_omp_clause (gimple_location (stmt
),
2098 i
? OMP_CLAUSE_FIRSTPRIVATE
2099 : OMP_CLAUSE_SHARED
);
2100 OMP_CLAUSE_DECL (c
) = decl
;
2101 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2102 gimple_omp_taskreg_set_clauses (stmt
, c
);
2105 info
->static_chain_added
|= save_static_chain_added
;
2108 case GIMPLE_OMP_FOR
:
2109 walk_body (convert_gimple_call
, NULL
, info
,
2110 gimple_omp_for_pre_body_ptr (stmt
));
2112 case GIMPLE_OMP_SECTIONS
:
2113 case GIMPLE_OMP_SECTION
:
2114 case GIMPLE_OMP_SINGLE
:
2115 case GIMPLE_OMP_TARGET
:
2116 case GIMPLE_OMP_TEAMS
:
2117 case GIMPLE_OMP_MASTER
:
2118 case GIMPLE_OMP_TASKGROUP
:
2119 case GIMPLE_OMP_ORDERED
:
2120 case GIMPLE_OMP_CRITICAL
:
2121 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2125 /* Keep looking for other operands. */
2126 *handled_ops_p
= false;
2130 *handled_ops_p
= true;
2134 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2135 call expressions. At the same time, determine if a nested function
2136 actually uses its static chain; if not, remember that. */
2139 convert_all_function_calls (struct nesting_info
*root
)
2141 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2142 struct nesting_info
*n
;
2144 /* First, optimistically clear static_chain for all decls that haven't
2145 used the static chain already for variable access. */
2146 FOR_EACH_NEST_INFO (n
, root
)
2148 tree decl
= n
->context
;
2149 if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2151 DECL_STATIC_CHAIN (decl
) = 0;
2152 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2153 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2154 lang_hooks
.decl_printable_name (decl
, 2));
2157 DECL_STATIC_CHAIN (decl
) = 1;
2158 chain_count
+= DECL_STATIC_CHAIN (decl
);
2161 /* Walk the functions and perform transformations. Note that these
2162 transformations can induce new uses of the static chain, which in turn
2163 require re-examining all users of the decl. */
2164 /* ??? It would make sense to try to use the call graph to speed this up,
2165 but the call graph hasn't really been built yet. Even if it did, we
2166 would still need to iterate in this loop since address-of references
2167 wouldn't show up in the callgraph anyway. */
2171 old_chain_count
= chain_count
;
2175 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2176 fputc ('\n', dump_file
);
2178 FOR_EACH_NEST_INFO (n
, root
)
2180 tree decl
= n
->context
;
2181 walk_function (convert_tramp_reference_stmt
,
2182 convert_tramp_reference_op
, n
);
2183 walk_function (convert_gimple_call
, NULL
, n
);
2184 chain_count
+= DECL_STATIC_CHAIN (decl
);
2187 while (chain_count
!= old_chain_count
);
2189 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2190 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2194 struct nesting_copy_body_data
2197 struct nesting_info
*root
;
2200 /* A helper subroutine for debug_var_chain type remapping. */
2203 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2205 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2206 void **slot
= pointer_map_contains (nid
->root
->var_map
, decl
);
2209 return (tree
) *slot
;
2211 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2213 tree new_decl
= copy_decl_no_change (decl
, id
);
2214 DECL_ORIGINAL_TYPE (new_decl
)
2215 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
2219 if (TREE_CODE (decl
) == VAR_DECL
2220 || TREE_CODE (decl
) == PARM_DECL
2221 || TREE_CODE (decl
) == RESULT_DECL
)
2224 return copy_decl_no_change (decl
, id
);
2227 /* A helper function for remap_vla_decls. See if *TP contains
2228 some remapped variables. */
2231 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
2233 struct nesting_info
*root
= (struct nesting_info
*) data
;
2240 slot
= pointer_map_contains (root
->var_map
, t
);
2243 return (tree
) *slot
;
2248 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2252 remap_vla_decls (tree block
, struct nesting_info
*root
)
2254 tree var
, subblock
, val
, type
;
2255 struct nesting_copy_body_data id
;
2257 for (subblock
= BLOCK_SUBBLOCKS (block
);
2259 subblock
= BLOCK_CHAIN (subblock
))
2260 remap_vla_decls (subblock
, root
);
2262 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
2263 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2265 val
= DECL_VALUE_EXPR (var
);
2266 type
= TREE_TYPE (var
);
2268 if (!(TREE_CODE (val
) == INDIRECT_REF
2269 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2270 && variably_modified_type_p (type
, NULL
)))
2273 if (pointer_map_contains (root
->var_map
, TREE_OPERAND (val
, 0))
2274 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2278 if (var
== NULL_TREE
)
2281 memset (&id
, 0, sizeof (id
));
2282 id
.cb
.copy_decl
= nesting_copy_decl
;
2283 id
.cb
.decl_map
= pointer_map_create ();
2286 for (; var
; var
= DECL_CHAIN (var
))
2287 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HAS_VALUE_EXPR_P (var
))
2289 struct nesting_info
*i
;
2293 val
= DECL_VALUE_EXPR (var
);
2294 type
= TREE_TYPE (var
);
2296 if (!(TREE_CODE (val
) == INDIRECT_REF
2297 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
2298 && variably_modified_type_p (type
, NULL
)))
2301 slot
= pointer_map_contains (root
->var_map
, TREE_OPERAND (val
, 0));
2302 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
2305 context
= decl_function_context (var
);
2306 for (i
= root
; i
; i
= i
->outer
)
2307 if (i
->context
== context
)
2313 /* Fully expand value expressions. This avoids having debug variables
2314 only referenced from them and that can be swept during GC. */
2317 tree t
= (tree
) *slot
;
2318 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
2319 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
2322 id
.cb
.src_fn
= i
->context
;
2323 id
.cb
.dst_fn
= i
->context
;
2324 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2326 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
2327 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2329 newt
= TREE_TYPE (newt
);
2330 type
= TREE_TYPE (type
);
2332 if (TYPE_NAME (newt
)
2333 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2334 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2336 && TYPE_NAME (newt
) == TYPE_NAME (type
))
2337 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2339 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
2340 if (val
!= DECL_VALUE_EXPR (var
))
2341 SET_DECL_VALUE_EXPR (var
, val
);
2344 pointer_map_destroy (id
.cb
.decl_map
);
2347 /* Fold the MEM_REF *E. */
2349 fold_mem_refs (const void *e
, void *data ATTRIBUTE_UNUSED
)
2351 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
2352 *ref_p
= fold (*ref_p
);
2356 /* Do "everything else" to clean up or complete state collected by the
2357 various walking passes -- lay out the types and decls, generate code
2358 to initialize the frame decl, store critical expressions in the
2359 struct function for rtl to find. */
2362 finalize_nesting_tree_1 (struct nesting_info
*root
)
2364 gimple_seq stmt_list
;
2366 tree context
= root
->context
;
2367 struct function
*sf
;
2371 /* If we created a non-local frame type or decl, we need to lay them
2372 out at this time. */
2373 if (root
->frame_type
)
2375 /* In some cases the frame type will trigger the -Wpadded warning.
2376 This is not helpful; suppress it. */
2377 int save_warn_padded
= warn_padded
;
2381 layout_type (root
->frame_type
);
2382 warn_padded
= save_warn_padded
;
2383 layout_decl (root
->frame_decl
, 0);
2385 /* Remove root->frame_decl from root->new_local_var_chain, so
2386 that we can declare it also in the lexical blocks, which
2387 helps ensure virtual regs that end up appearing in its RTL
2388 expression get substituted in instantiate_virtual_regs(). */
2389 for (adjust
= &root
->new_local_var_chain
;
2390 *adjust
!= root
->frame_decl
;
2391 adjust
= &DECL_CHAIN (*adjust
))
2392 gcc_assert (DECL_CHAIN (*adjust
));
2393 *adjust
= DECL_CHAIN (*adjust
);
2395 DECL_CHAIN (root
->frame_decl
) = NULL_TREE
;
2396 declare_vars (root
->frame_decl
,
2397 gimple_seq_first_stmt (gimple_body (context
)), true);
2400 /* If any parameters were referenced non-locally, then we need to
2401 insert a copy. Likewise, if any variables were referenced by
2402 pointer, we need to initialize the address. */
2403 if (root
->any_parm_remapped
)
2406 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
2410 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
2414 if (use_pointer_in_frame (p
))
2415 x
= build_addr (p
, context
);
2419 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2420 root
->frame_decl
, field
, NULL_TREE
);
2421 stmt
= gimple_build_assign (y
, x
);
2422 gimple_seq_add_stmt (&stmt_list
, stmt
);
2423 /* If the assignment is from a non-register the stmt is
2424 not valid gimple. Make it so by using a temporary instead. */
2425 if (!is_gimple_reg (x
)
2426 && is_gimple_reg_type (TREE_TYPE (x
)))
2428 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
2429 x
= init_tmp_var (root
, x
, &gsi
);
2430 gimple_assign_set_rhs1 (stmt
, x
);
2435 /* If a chain_field was created, then it needs to be initialized
2437 if (root
->chain_field
)
2439 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
2440 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
2441 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
2442 gimple_seq_add_stmt (&stmt_list
, stmt
);
2445 /* If trampolines were created, then we need to initialize them. */
2446 if (root
->any_tramp_created
)
2448 struct nesting_info
*i
;
2449 for (i
= root
->inner
; i
; i
= i
->next
)
2451 tree arg1
, arg2
, arg3
, x
, field
;
2453 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
2457 gcc_assert (DECL_STATIC_CHAIN (i
->context
));
2458 arg3
= build_addr (root
->frame_decl
, context
);
2460 arg2
= build_addr (i
->context
, context
);
2462 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
2463 root
->frame_decl
, field
, NULL_TREE
);
2464 arg1
= build_addr (x
, context
);
2466 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
2467 stmt
= gimple_build_call (x
, 3, arg1
, arg2
, arg3
);
2468 gimple_seq_add_stmt (&stmt_list
, stmt
);
2472 /* If we created initialization statements, insert them. */
2476 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
2477 bind
= gimple_seq_first_stmt (gimple_body (context
));
2478 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
2479 gimple_bind_set_body (bind
, stmt_list
);
2482 /* If a chain_decl was created, then it needs to be registered with
2483 struct function so that it gets initialized from the static chain
2484 register at the beginning of the function. */
2485 sf
= DECL_STRUCT_FUNCTION (root
->context
);
2486 sf
->static_chain_decl
= root
->chain_decl
;
2488 /* Similarly for the non-local goto save area. */
2489 if (root
->nl_goto_field
)
2491 sf
->nonlocal_goto_save_area
2492 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
2493 sf
->has_nonlocal_label
= 1;
2496 /* Make sure all new local variables get inserted into the
2497 proper BIND_EXPR. */
2498 if (root
->new_local_var_chain
)
2499 declare_vars (root
->new_local_var_chain
,
2500 gimple_seq_first_stmt (gimple_body (root
->context
)),
2503 if (root
->debug_var_chain
)
2508 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
2510 for (debug_var
= root
->debug_var_chain
; debug_var
;
2511 debug_var
= DECL_CHAIN (debug_var
))
2512 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2515 /* If there are any debug decls with variable length types,
2516 remap those types using other debug_var_chain variables. */
2519 struct nesting_copy_body_data id
;
2521 memset (&id
, 0, sizeof (id
));
2522 id
.cb
.copy_decl
= nesting_copy_decl
;
2523 id
.cb
.decl_map
= pointer_map_create ();
2526 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
2527 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
2529 tree type
= TREE_TYPE (debug_var
);
2530 tree newt
, t
= type
;
2531 struct nesting_info
*i
;
2533 for (i
= root
; i
; i
= i
->outer
)
2534 if (variably_modified_type_p (type
, i
->context
))
2540 id
.cb
.src_fn
= i
->context
;
2541 id
.cb
.dst_fn
= i
->context
;
2542 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
2544 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
2545 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
2547 newt
= TREE_TYPE (newt
);
2550 if (TYPE_NAME (newt
)
2551 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
2552 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
2554 && TYPE_NAME (newt
) == TYPE_NAME (t
))
2555 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
2558 pointer_map_destroy (id
.cb
.decl_map
);
2561 scope
= gimple_seq_first_stmt (gimple_body (root
->context
));
2562 if (gimple_bind_block (scope
))
2563 declare_vars (root
->debug_var_chain
, scope
, true);
2565 BLOCK_VARS (DECL_INITIAL (root
->context
))
2566 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
2567 root
->debug_var_chain
);
2570 /* Fold the rewritten MEM_REF trees. */
2571 pointer_set_traverse (root
->mem_refs
, fold_mem_refs
, NULL
);
2573 /* Dump the translated tree function. */
2576 fputs ("\n\n", dump_file
);
2577 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
2582 finalize_nesting_tree (struct nesting_info
*root
)
2584 struct nesting_info
*n
;
2585 FOR_EACH_NEST_INFO (n
, root
)
2586 finalize_nesting_tree_1 (n
);
2589 /* Unnest the nodes and pass them to cgraph. */
2592 unnest_nesting_tree_1 (struct nesting_info
*root
)
2594 struct cgraph_node
*node
= cgraph_get_node (root
->context
);
2596 /* For nested functions update the cgraph to reflect unnesting.
2597 We also delay finalizing of these functions up to this point. */
2600 cgraph_unnest_node (node
);
2601 cgraph_finalize_function (root
->context
, true);
2606 unnest_nesting_tree (struct nesting_info
*root
)
2608 struct nesting_info
*n
;
2609 FOR_EACH_NEST_INFO (n
, root
)
2610 unnest_nesting_tree_1 (n
);
2613 /* Free the data structures allocated during this pass. */
2616 free_nesting_tree (struct nesting_info
*root
)
2618 struct nesting_info
*node
, *next
;
2620 node
= iter_nestinfo_start (root
);
2623 next
= iter_nestinfo_next (node
);
2624 pointer_map_destroy (node
->var_map
);
2625 pointer_map_destroy (node
->field_map
);
2626 pointer_set_destroy (node
->mem_refs
);
2633 /* Gimplify a function and all its nested functions. */
2635 gimplify_all_functions (struct cgraph_node
*root
)
2637 struct cgraph_node
*iter
;
2638 if (!gimple_body (root
->decl
))
2639 gimplify_function_tree (root
->decl
);
2640 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
2641 gimplify_all_functions (iter
);
2644 /* Main entry point for this pass. Process FNDECL and all of its nested
2645 subroutines and turn them into something less tightly bound. */
2648 lower_nested_functions (tree fndecl
)
2650 struct cgraph_node
*cgn
;
2651 struct nesting_info
*root
;
2653 /* If there are no nested functions, there's nothing to do. */
2654 cgn
= cgraph_get_node (fndecl
);
2658 gimplify_all_functions (cgn
);
2660 dump_file
= dump_begin (TDI_nested
, &dump_flags
);
2662 fprintf (dump_file
, "\n;; Function %s\n\n",
2663 lang_hooks
.decl_printable_name (fndecl
, 2));
2665 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
2666 root
= create_nesting_tree (cgn
);
2668 walk_all_functions (convert_nonlocal_reference_stmt
,
2669 convert_nonlocal_reference_op
,
2671 walk_all_functions (convert_local_reference_stmt
,
2672 convert_local_reference_op
,
2674 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
2675 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
2677 convert_all_function_calls (root
);
2678 finalize_nesting_tree (root
);
2679 unnest_nesting_tree (root
);
2681 free_nesting_tree (root
);
2682 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
2686 dump_end (TDI_nested
, dump_file
);
2691 #include "gt-tree-nested.h"