1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
35 #include "langhooks.h"
36 #include "pointer-set.h"
40 /* The object of this pass is to lower the representation of a set of nested
41 functions in order to expose all of the gory details of the various
42 nonlocal references. We want to do this sooner rather than later, in
43 order to give us more freedom in emitting all of the functions in question.
45 Back in olden times, when gcc was young, we developed an insanely
46 complicated scheme whereby variables which were referenced nonlocally
47 were forced to live in the stack of the declaring function, and then
48 the nested functions magically discovered where these variables were
49 placed. In order for this scheme to function properly, it required
50 that the outer function be partially expanded, then we switch to
51 compiling the inner function, and once done with those we switch back
52 to compiling the outer function. Such delicate ordering requirements
53 makes it difficult to do whole translation unit optimizations
54 involving such functions.
56 The implementation here is much more direct. Everything that can be
57 referenced by an inner function is a member of an explicitly created
58 structure herein called the "nonlocal frame struct". The incoming
59 static chain for a nested function is a pointer to this struct in
60 the parent. In this way, we settle on known offsets from a known
61 base, and so are decoupled from the logic that places objects in the
62 function's stack frame. More importantly, we don't have to wait for
63 that to happen -- since the compilation of the inner function is no
64 longer tied to a real stack frame, the nonlocal frame struct can be
65 allocated anywhere. Which means that the outer function is now
68 Theory of operation here is very simple. Iterate over all the
69 statements in all the functions (depth first) several times,
70 allocating structures and fields on demand. In general we want to
71 examine inner functions first, so that we can avoid making changes
72 to outer functions which are unnecessary.
74 The order of the passes matters a bit, in that later passes will be
75 skipped if it is discovered that the functions don't actually interact
76 at all. That is, they're nested in the lexical sense but could have
77 been written as independent functions without change. */
82 struct nesting_info *outer;
83 struct nesting_info *inner;
84 struct nesting_info *next;
86 struct pointer_map_t *field_map;
87 struct pointer_map_t *var_map;
88 bitmap suppress_expansion;
91 tree new_local_var_chain;
99 bool any_parm_remapped;
100 bool any_tramp_created;
101 char static_chain_added;
105 /* Obstack used for the bitmaps in the struct above. */
106 static struct bitmap_obstack nesting_info_bitmap_obstack;
109 /* We're working in so many different function contexts simultaneously,
110 that create_tmp_var is dangerous. Prevent mishap. */
111 #define create_tmp_var cant_use_create_tmp_var_here_dummy
113 /* Like create_tmp_var, except record the variable for registration at
114 the given nesting level. */
117 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
121 /* If the type is of variable size or a type which must be created by the
122 frontend, something is wrong. Note that we explicitly allow
123 incomplete types here, since we create them ourselves here. */
124 gcc_assert (!TREE_ADDRESSABLE (type));
125 gcc_assert (!TYPE_SIZE_UNIT (type)
126 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
128 tmp_var = create_tmp_var_raw (type, prefix);
129 DECL_CONTEXT (tmp_var) = info->context;
130 TREE_CHAIN (tmp_var) = info->new_local_var_chain;
131 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
132 if (TREE_CODE (type) == COMPLEX_TYPE
133 || TREE_CODE (type) == VECTOR_TYPE)
134 DECL_GIMPLE_REG_P (tmp_var) = 1;
136 info->new_local_var_chain = tmp_var;
141 /* Take the address of EXP to be used within function CONTEXT.
142 Mark it for addressability as necessary. */
145 build_addr (tree exp, tree context)
151 while (handled_component_p (base))
152 base = TREE_OPERAND (base, 0);
155 TREE_ADDRESSABLE (base) = 1;
157 /* Building the ADDR_EXPR will compute a set of properties for
158 that ADDR_EXPR. Those properties are unfortunately context
159 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
161 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
162 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
163 way the properties are for the ADDR_EXPR are computed properly. */
164 save_context = current_function_decl;
165 current_function_decl = context;
166 retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
167 current_function_decl = save_context;
171 /* Insert FIELD into TYPE, sorted by alignment requirements. */
174 insert_field_into_struct (tree type, tree field)
178 DECL_CONTEXT (field) = type;
180 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
181 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
184 TREE_CHAIN (field) = *p;
187 /* Set correct alignment for frame struct type. */
188 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
189 TYPE_ALIGN (type) = DECL_ALIGN (field);
192 /* Build or return the RECORD_TYPE that describes the frame state that is
193 shared between INFO->CONTEXT and its nested functions. This record will
194 not be complete until finalize_nesting_tree; up until that point we'll
195 be adding fields as necessary.
197 We also build the DECL that represents this frame in the function. */
200 get_frame_type (struct nesting_info *info)
202 tree type = info->frame_type;
207 type = make_node (RECORD_TYPE);
209 name = concat ("FRAME.",
210 IDENTIFIER_POINTER (DECL_NAME (info->context)),
212 TYPE_NAME (type) = get_identifier (name);
215 info->frame_type = type;
216 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
218 /* ??? Always make it addressable for now, since it is meant to
219 be pointed to by the static chain pointer. This pessimizes
220 when it turns out that no static chains are needed because
221 the nested functions referencing non-local variables are not
222 reachable, but the true pessimization is to create the non-
223 local frame structure in the first place. */
224 TREE_ADDRESSABLE (info->frame_decl) = 1;
229 /* Return true if DECL should be referenced by pointer in the non-local
233 use_pointer_in_frame (tree decl)
235 if (TREE_CODE (decl) == PARM_DECL)
237 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
238 sized decls, and inefficient to copy large aggregates. Don't bother
239 moving anything but scalar variables. */
240 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
244 /* Variable sized types make things "interesting" in the frame. */
245 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
249 /* Given DECL, a non-locally accessed variable, find or create a field
250 in the non-local frame structure for the given nesting context. */
253 lookup_field_for_decl (struct nesting_info *info, tree decl,
254 enum insert_option insert)
258 if (insert == NO_INSERT)
260 slot = pointer_map_contains (info->field_map, decl);
261 return slot ? (tree) *slot : NULL_TREE;
264 slot = pointer_map_insert (info->field_map, decl);
267 tree field = make_node (FIELD_DECL);
268 DECL_NAME (field) = DECL_NAME (decl);
270 if (use_pointer_in_frame (decl))
272 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
273 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
274 DECL_NONADDRESSABLE_P (field) = 1;
278 TREE_TYPE (field) = TREE_TYPE (decl);
279 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
280 DECL_ALIGN (field) = DECL_ALIGN (decl);
281 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
282 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
283 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
284 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
287 insert_field_into_struct (get_frame_type (info), field);
290 if (TREE_CODE (decl) == PARM_DECL)
291 info->any_parm_remapped = true;
297 /* Build or return the variable that holds the static chain within
298 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
301 get_chain_decl (struct nesting_info *info)
303 tree decl = info->chain_decl;
308 type = get_frame_type (info->outer);
309 type = build_pointer_type (type);
311 /* Note that this variable is *not* entered into any BIND_EXPR;
312 the construction of this variable is handled specially in
313 expand_function_start and initialize_inlined_parameters.
314 Note also that it's represented as a parameter. This is more
315 close to the truth, since the initial value does come from
317 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
318 DECL_ARTIFICIAL (decl) = 1;
319 DECL_IGNORED_P (decl) = 1;
320 TREE_USED (decl) = 1;
321 DECL_CONTEXT (decl) = info->context;
322 DECL_ARG_TYPE (decl) = type;
324 /* Tell tree-inline.c that we never write to this variable, so
325 it can copy-prop the replacement value immediately. */
326 TREE_READONLY (decl) = 1;
328 info->chain_decl = decl;
333 /* Build or return the field within the non-local frame state that holds
334 the static chain for INFO->CONTEXT. This is the way to walk back up
335 multiple nesting levels. */
338 get_chain_field (struct nesting_info *info)
340 tree field = info->chain_field;
343 tree type = build_pointer_type (get_frame_type (info->outer));
345 field = make_node (FIELD_DECL);
346 DECL_NAME (field) = get_identifier ("__chain");
347 TREE_TYPE (field) = type;
348 DECL_ALIGN (field) = TYPE_ALIGN (type);
349 DECL_NONADDRESSABLE_P (field) = 1;
351 insert_field_into_struct (get_frame_type (info), field);
353 info->chain_field = field;
358 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
361 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
366 t = create_tmp_var_for (info, TREE_TYPE (TREE_TYPE (gimple_call_fn (call))),
368 gimple_call_set_lhs (call, t);
369 if (! gsi_end_p (*gsi))
370 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
371 gsi_insert_before (gsi, call, GSI_SAME_STMT);
377 /* Copy EXP into a temporary. Allocate the temporary in the context of
378 INFO and insert the initialization statement before GSI. */
381 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
386 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
387 stmt = gimple_build_assign (t, exp);
388 if (! gsi_end_p (*gsi))
389 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
390 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
396 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
399 gsi_gimplify_val (struct nesting_info *info, tree exp,
400 gimple_stmt_iterator *gsi)
402 if (is_gimple_val (exp))
405 return init_tmp_var (info, exp, gsi);
408 /* Similarly, but copy from the temporary and insert the statement
409 after the iterator. */
412 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
417 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
418 stmt = gimple_build_assign (exp, t);
419 if (! gsi_end_p (*gsi))
420 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
421 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
426 /* Build or return the type used to represent a nested function trampoline. */
428 static GTY(()) tree trampoline_type;
431 get_trampoline_type (void)
433 unsigned align, size;
437 return trampoline_type;
439 align = TRAMPOLINE_ALIGNMENT;
440 size = TRAMPOLINE_SIZE;
442 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
443 then allocate extra space so that we can do dynamic alignment. */
444 if (align > STACK_BOUNDARY)
446 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
447 align = STACK_BOUNDARY;
450 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
451 t = build_array_type (char_type_node, t);
452 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
453 DECL_ALIGN (t) = align;
454 DECL_USER_ALIGN (t) = 1;
456 trampoline_type = make_node (RECORD_TYPE);
457 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
458 TYPE_FIELDS (trampoline_type) = t;
459 layout_type (trampoline_type);
460 DECL_CONTEXT (t) = trampoline_type;
462 return trampoline_type;
465 /* Given DECL, a nested function, find or create a field in the non-local
466 frame structure for a trampoline for this function. */
469 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
470 enum insert_option insert)
474 if (insert == NO_INSERT)
476 slot = pointer_map_contains (info->var_map, decl);
477 return slot ? (tree) *slot : NULL_TREE;
480 slot = pointer_map_insert (info->var_map, decl);
483 tree field = make_node (FIELD_DECL);
484 DECL_NAME (field) = DECL_NAME (decl);
485 TREE_TYPE (field) = get_trampoline_type ();
486 TREE_ADDRESSABLE (field) = 1;
488 insert_field_into_struct (get_frame_type (info), field);
491 info->any_tramp_created = true;
497 /* Build or return the field within the non-local frame state that holds
498 the non-local goto "jmp_buf". The buffer itself is maintained by the
499 rtl middle-end as dynamic stack space is allocated. */
502 get_nl_goto_field (struct nesting_info *info)
504 tree field = info->nl_goto_field;
510 /* For __builtin_nonlocal_goto, we need N words. The first is the
511 frame pointer, the rest is for the target's stack pointer save
512 area. The number of words is controlled by STACK_SAVEAREA_MODE;
513 not the best interface, but it'll do for now. */
514 if (Pmode == ptr_mode)
515 type = ptr_type_node;
517 type = lang_hooks.types.type_for_mode (Pmode, 1);
519 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
520 size = size / GET_MODE_SIZE (Pmode);
523 type = build_array_type
524 (type, build_index_type (build_int_cst (NULL_TREE, size)));
526 field = make_node (FIELD_DECL);
527 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
528 TREE_TYPE (field) = type;
529 DECL_ALIGN (field) = TYPE_ALIGN (type);
530 TREE_ADDRESSABLE (field) = 1;
532 insert_field_into_struct (get_frame_type (info), field);
534 info->nl_goto_field = field;
540 /* Invoke CALLBACK on all statements of GIMPLE sequence SEQ. */
543 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
544 struct nesting_info *info, gimple_seq seq)
546 struct walk_stmt_info wi;
548 memset (&wi, 0, sizeof (wi));
551 walk_gimple_seq (seq, callback_stmt, callback_op, &wi);
555 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
558 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
559 struct nesting_info *info)
561 walk_body (callback_stmt, callback_op, info, gimple_body (info->context));
564 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
567 walk_gimple_omp_for (gimple for_stmt,
568 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
569 struct nesting_info *info)
571 struct walk_stmt_info wi;
576 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body (for_stmt));
578 seq = gimple_seq_alloc ();
579 memset (&wi, 0, sizeof (wi));
581 wi.gsi = gsi_last (seq);
583 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
586 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
590 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
595 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
598 t = gimple_omp_for_incr (for_stmt, i);
599 gcc_assert (BINARY_CLASS_P (t));
601 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
604 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
607 if (gimple_seq_empty_p (seq))
608 gimple_seq_free (seq);
611 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
612 annotate_all_with_location (seq, gimple_location (for_stmt));
613 gimple_seq_add_seq (&pre_body, seq);
614 gimple_omp_for_set_pre_body (for_stmt, pre_body);
618 /* Similarly for ROOT and all functions nested underneath, depth first. */
621 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
622 struct nesting_info *root)
627 walk_all_functions (callback_stmt, callback_op, root->inner);
628 walk_function (callback_stmt, callback_op, root);
635 /* We have to check for a fairly pathological case. The operands of function
636 nested function are to be interpreted in the context of the enclosing
637 function. So if any are variably-sized, they will get remapped when the
638 enclosing function is inlined. But that remapping would also have to be
639 done in the types of the PARM_DECLs of the nested function, meaning the
640 argument types of that function will disagree with the arguments in the
641 calls to that function. So we'd either have to make a copy of the nested
642 function corresponding to each time the enclosing function was inlined or
643 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
644 function. The former is not practical. The latter would still require
645 detecting this case to know when to add the conversions. So, for now at
646 least, we don't inline such an enclosing function.
648 We have to do that check recursively, so here return indicating whether
649 FNDECL has such a nested function. ORIG_FN is the function we were
650 trying to inline to use for checking whether any argument is variably
651 modified by anything in it.
653 It would be better to do this in tree-inline.c so that we could give
654 the appropriate warning for why a function can't be inlined, but that's
655 too late since the nesting structure has already been flattened and
656 adding a flag just to record this fact seems a waste of a flag. */
659 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
661 struct cgraph_node *cgn = cgraph_node (fndecl);
664 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
666 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
667 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
670 if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
677 /* Construct our local datastructure describing the function nesting
678 tree rooted by CGN. */
680 static struct nesting_info *
681 create_nesting_tree (struct cgraph_node *cgn)
683 struct nesting_info *info = XCNEW (struct nesting_info);
684 info->field_map = pointer_map_create ();
685 info->var_map = pointer_map_create ();
686 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
687 info->context = cgn->decl;
689 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
691 struct nesting_info *sub = create_nesting_tree (cgn);
693 sub->next = info->inner;
697 /* See discussion at check_for_nested_with_variably_modified for a
698 discussion of why this has to be here. */
699 if (check_for_nested_with_variably_modified (info->context, info->context))
700 DECL_UNINLINABLE (info->context) = true;
705 /* Return an expression computing the static chain for TARGET_CONTEXT
706 from INFO->CONTEXT. Insert any necessary computations before TSI. */
709 get_static_chain (struct nesting_info *info, tree target_context,
710 gimple_stmt_iterator *gsi)
712 struct nesting_info *i;
715 if (info->context == target_context)
717 x = build_addr (info->frame_decl, target_context);
721 x = get_chain_decl (info);
723 for (i = info->outer; i->context != target_context; i = i->outer)
725 tree field = get_chain_field (i);
727 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
728 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
729 x = init_tmp_var (info, x, gsi);
737 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
738 frame as seen from INFO->CONTEXT. Insert any necessary computations
742 get_frame_field (struct nesting_info *info, tree target_context,
743 tree field, gimple_stmt_iterator *gsi)
745 struct nesting_info *i;
748 if (info->context == target_context)
750 /* Make sure frame_decl gets created. */
751 (void) get_frame_type (info);
752 x = info->frame_decl;
756 x = get_chain_decl (info);
758 for (i = info->outer; i->context != target_context; i = i->outer)
760 tree field = get_chain_field (i);
762 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
763 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
764 x = init_tmp_var (info, x, gsi);
767 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
770 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
775 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
776 in the nested function with DECL_VALUE_EXPR set to reference the true
777 variable in the parent function. This is used both for debug info
778 and in OpenMP lowering. */
781 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
784 struct nesting_info *i;
785 tree x, field, new_decl;
788 slot = pointer_map_insert (info->var_map, decl);
793 target_context = decl_function_context (decl);
795 /* A copy of the code in get_frame_field, but without the temporaries. */
796 if (info->context == target_context)
798 /* Make sure frame_decl gets created. */
799 (void) get_frame_type (info);
800 x = info->frame_decl;
805 x = get_chain_decl (info);
806 for (i = info->outer; i->context != target_context; i = i->outer)
808 field = get_chain_field (i);
809 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
810 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
812 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
815 field = lookup_field_for_decl (i, decl, INSERT);
816 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
817 if (use_pointer_in_frame (decl))
818 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
820 /* ??? We should be remapping types as well, surely. */
821 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
822 DECL_CONTEXT (new_decl) = info->context;
823 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
824 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
825 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
826 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
827 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
828 TREE_READONLY (new_decl) = TREE_READONLY (decl);
829 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
830 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
832 SET_DECL_VALUE_EXPR (new_decl, x);
833 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
836 TREE_CHAIN (new_decl) = info->debug_var_chain;
837 info->debug_var_chain = new_decl;
843 /* Callback for walk_gimple_stmt, rewrite all references to VAR
844 and PARM_DECLs that belong to outer functions.
846 The rewrite will involve some number of structure accesses back up
847 the static chain. E.g. for a variable FOO up one nesting level it'll
848 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
849 indirections apply to decls for which use_pointer_in_frame is true. */
852 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
854 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
855 struct nesting_info *const info = (struct nesting_info *) wi->info;
859 switch (TREE_CODE (t))
862 /* Non-automatic variables are never processed. */
863 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
868 if (decl_function_context (t) != info->context)
873 x = get_nonlocal_debug_decl (info, t);
874 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
876 tree target_context = decl_function_context (t);
877 struct nesting_info *i;
878 for (i = info->outer; i->context != target_context; i = i->outer)
880 x = lookup_field_for_decl (i, t, INSERT);
881 x = get_frame_field (info, target_context, x, &wi->gsi);
882 if (use_pointer_in_frame (t))
884 x = init_tmp_var (info, x, &wi->gsi);
885 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
892 x = save_tmp_var (info, x, &wi->gsi);
894 x = init_tmp_var (info, x, &wi->gsi);
902 /* We're taking the address of a label from a parent function, but
903 this is not itself a non-local goto. Mark the label such that it
904 will not be deleted, much as we would with a label address in
906 if (decl_function_context (t) != info->context)
907 FORCED_LABEL (t) = 1;
912 bool save_val_only = wi->val_only;
914 wi->val_only = false;
917 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
924 /* If we changed anything, we might no longer be directly
925 referencing a decl. */
926 save_context = current_function_decl;
927 current_function_decl = info->context;
928 recompute_tree_invariant_for_addr_expr (t);
929 current_function_decl = save_context;
931 /* If the callback converted the address argument in a context
932 where we only accept variables (and min_invariant, presumably),
933 then compute the address into a temporary. */
935 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
945 case ARRAY_RANGE_REF:
947 /* Go down this entire nest and just look at the final prefix and
948 anything that describes the references. Otherwise, we lose track
949 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
952 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
954 if (TREE_CODE (t) == COMPONENT_REF)
955 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
957 else if (TREE_CODE (t) == ARRAY_REF
958 || TREE_CODE (t) == ARRAY_RANGE_REF)
960 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
962 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
964 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
967 else if (TREE_CODE (t) == BIT_FIELD_REF)
969 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
971 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
975 wi->val_only = false;
976 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
979 case VIEW_CONVERT_EXPR:
980 /* Just request to look at the subtrees, leaving val_only and lhs
981 untouched. This might actually be for !val_only + lhs, in which
982 case we don't want to force a replacement by a temporary. */
987 if (!IS_TYPE_OR_DECL_P (t))
999 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1000 struct walk_stmt_info *);
1002 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1003 and PARM_DECLs that belong to outer functions. */
1006 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1008 struct nesting_info *const info = (struct nesting_info *) wi->info;
1009 bool need_chain = false, need_stmts = false;
1012 bitmap new_suppress;
1014 new_suppress = BITMAP_GGC_ALLOC ();
1015 bitmap_copy (new_suppress, info->suppress_expansion);
1017 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1019 switch (OMP_CLAUSE_CODE (clause))
1021 case OMP_CLAUSE_REDUCTION:
1022 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1024 goto do_decl_clause;
1026 case OMP_CLAUSE_LASTPRIVATE:
1027 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1029 goto do_decl_clause;
1031 case OMP_CLAUSE_PRIVATE:
1032 case OMP_CLAUSE_FIRSTPRIVATE:
1033 case OMP_CLAUSE_COPYPRIVATE:
1034 case OMP_CLAUSE_SHARED:
1036 decl = OMP_CLAUSE_DECL (clause);
1037 if (TREE_CODE (decl) == VAR_DECL
1038 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1040 if (decl_function_context (decl) != info->context)
1042 bitmap_set_bit (new_suppress, DECL_UID (decl));
1043 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1048 case OMP_CLAUSE_SCHEDULE:
1049 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1053 case OMP_CLAUSE_NUM_THREADS:
1054 wi->val_only = true;
1056 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1060 case OMP_CLAUSE_NOWAIT:
1061 case OMP_CLAUSE_ORDERED:
1062 case OMP_CLAUSE_DEFAULT:
1063 case OMP_CLAUSE_COPYIN:
1064 case OMP_CLAUSE_COLLAPSE:
1065 case OMP_CLAUSE_UNTIED:
1073 info->suppress_expansion = new_suppress;
1076 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1077 switch (OMP_CLAUSE_CODE (clause))
1079 case OMP_CLAUSE_REDUCTION:
1080 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1083 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1084 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1086 walk_body (convert_nonlocal_reference_stmt,
1087 convert_nonlocal_reference_op, info,
1088 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1089 walk_body (convert_nonlocal_reference_stmt,
1090 convert_nonlocal_reference_op, info,
1091 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1092 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1097 case OMP_CLAUSE_LASTPRIVATE:
1098 walk_body (convert_nonlocal_reference_stmt,
1099 convert_nonlocal_reference_op, info,
1100 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1111 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1112 PARM_DECLs that belong to outer functions. This handles statements
1113 that are not handled via the standard recursion done in
1114 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1115 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1116 operands of STMT have been handled by this function. */
1119 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1120 struct walk_stmt_info *wi)
1122 struct nesting_info *info = (struct nesting_info *) wi->info;
1123 tree save_local_var_chain;
1124 bitmap save_suppress;
1125 gimple stmt = gsi_stmt (*gsi);
1127 switch (gimple_code (stmt))
1130 /* Don't walk non-local gotos for now. */
1131 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1133 wi->val_only = true;
1135 *handled_ops_p = true;
1140 case GIMPLE_OMP_PARALLEL:
1141 case GIMPLE_OMP_TASK:
1142 save_suppress = info->suppress_expansion;
1143 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1147 decl = get_chain_decl (info);
1148 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1149 OMP_CLAUSE_DECL (c) = decl;
1150 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1151 gimple_omp_taskreg_set_clauses (stmt, c);
1154 save_local_var_chain = info->new_local_var_chain;
1155 info->new_local_var_chain = NULL;
1157 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1158 info, gimple_omp_body (stmt));
1160 if (info->new_local_var_chain)
1161 declare_vars (info->new_local_var_chain,
1162 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1164 info->new_local_var_chain = save_local_var_chain;
1165 info->suppress_expansion = save_suppress;
1168 case GIMPLE_OMP_FOR:
1169 save_suppress = info->suppress_expansion;
1170 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1171 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1172 convert_nonlocal_reference_op, info);
1173 walk_body (convert_nonlocal_reference_stmt,
1174 convert_nonlocal_reference_op, info, gimple_omp_body (stmt));
1175 info->suppress_expansion = save_suppress;
1178 case GIMPLE_OMP_SECTIONS:
1179 save_suppress = info->suppress_expansion;
1180 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1181 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1182 info, gimple_omp_body (stmt));
1183 info->suppress_expansion = save_suppress;
1186 case GIMPLE_OMP_SINGLE:
1187 save_suppress = info->suppress_expansion;
1188 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1189 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1190 info, gimple_omp_body (stmt));
1191 info->suppress_expansion = save_suppress;
1194 case GIMPLE_OMP_SECTION:
1195 case GIMPLE_OMP_MASTER:
1196 case GIMPLE_OMP_ORDERED:
1197 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1198 info, gimple_omp_body (stmt));
1202 /* For every other statement that we are not interested in
1203 handling here, let the walker traverse the operands. */
1204 *handled_ops_p = false;
1208 /* We have handled all of STMT operands, no need to traverse the operands. */
1209 *handled_ops_p = true;
1214 /* A subroutine of convert_local_reference. Create a local variable
1215 in the parent function with DECL_VALUE_EXPR set to reference the
1216 field in FRAME. This is used both for debug info and in OpenMP
1220 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1225 slot = pointer_map_insert (info->var_map, decl);
1227 return (tree) *slot;
1229 /* Make sure frame_decl gets created. */
1230 (void) get_frame_type (info);
1231 x = info->frame_decl;
1232 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1234 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1235 DECL_CONTEXT (new_decl) = info->context;
1236 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1237 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1238 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1239 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1240 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1241 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1242 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1243 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1245 SET_DECL_VALUE_EXPR (new_decl, x);
1246 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1249 TREE_CHAIN (new_decl) = info->debug_var_chain;
1250 info->debug_var_chain = new_decl;
1252 /* Do not emit debug info twice. */
1253 DECL_IGNORED_P (decl) = 1;
1259 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1260 and PARM_DECLs that were referenced by inner nested functions.
1261 The rewrite will be a structure reference to the local frame variable. */
1263 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1266 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1268 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1269 struct nesting_info *const info = (struct nesting_info *) wi->info;
1270 tree t = *tp, field, x;
1274 switch (TREE_CODE (t))
1277 /* Non-automatic variables are never processed. */
1278 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1283 if (decl_function_context (t) == info->context)
1285 /* If we copied a pointer to the frame, then the original decl
1286 is used unchanged in the parent function. */
1287 if (use_pointer_in_frame (t))
1290 /* No need to transform anything if no child references the
1292 field = lookup_field_for_decl (info, t, NO_INSERT);
1297 x = get_local_debug_decl (info, t, field);
1298 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1299 x = get_frame_field (info, info->context, field, &wi->gsi);
1304 x = save_tmp_var (info, x, &wi->gsi);
1306 x = init_tmp_var (info, x, &wi->gsi);
1314 save_val_only = wi->val_only;
1315 wi->val_only = false;
1317 wi->changed = false;
1318 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1319 wi->val_only = save_val_only;
1321 /* If we converted anything ... */
1326 /* Then the frame decl is now addressable. */
1327 TREE_ADDRESSABLE (info->frame_decl) = 1;
1329 save_context = current_function_decl;
1330 current_function_decl = info->context;
1331 recompute_tree_invariant_for_addr_expr (t);
1332 current_function_decl = save_context;
1334 /* If we are in a context where we only accept values, then
1335 compute the address into a temporary. */
1337 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1346 case ARRAY_RANGE_REF:
1348 /* Go down this entire nest and just look at the final prefix and
1349 anything that describes the references. Otherwise, we lose track
1350 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1351 save_val_only = wi->val_only;
1352 wi->val_only = true;
1354 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1356 if (TREE_CODE (t) == COMPONENT_REF)
1357 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1359 else if (TREE_CODE (t) == ARRAY_REF
1360 || TREE_CODE (t) == ARRAY_RANGE_REF)
1362 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1364 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1366 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1369 else if (TREE_CODE (t) == BIT_FIELD_REF)
1371 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1373 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1377 wi->val_only = false;
1378 walk_tree (tp, convert_local_reference_op, wi, NULL);
1379 wi->val_only = save_val_only;
1382 case VIEW_CONVERT_EXPR:
1383 /* Just request to look at the subtrees, leaving val_only and lhs
1384 untouched. This might actually be for !val_only + lhs, in which
1385 case we don't want to force a replacement by a temporary. */
1390 if (!IS_TYPE_OR_DECL_P (t))
1393 wi->val_only = true;
1402 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1403 struct walk_stmt_info *);
1405 /* Helper for convert_local_reference. Convert all the references in
1406 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1409 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1411 struct nesting_info *const info = (struct nesting_info *) wi->info;
1412 bool need_frame = false, need_stmts = false;
1415 bitmap new_suppress;
1417 new_suppress = BITMAP_GGC_ALLOC ();
1418 bitmap_copy (new_suppress, info->suppress_expansion);
1420 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1422 switch (OMP_CLAUSE_CODE (clause))
1424 case OMP_CLAUSE_REDUCTION:
1425 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1427 goto do_decl_clause;
1429 case OMP_CLAUSE_LASTPRIVATE:
1430 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1432 goto do_decl_clause;
1434 case OMP_CLAUSE_PRIVATE:
1435 case OMP_CLAUSE_FIRSTPRIVATE:
1436 case OMP_CLAUSE_COPYPRIVATE:
1437 case OMP_CLAUSE_SHARED:
1439 decl = OMP_CLAUSE_DECL (clause);
1440 if (TREE_CODE (decl) == VAR_DECL
1441 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1443 if (decl_function_context (decl) == info->context
1444 && !use_pointer_in_frame (decl))
1446 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1449 bitmap_set_bit (new_suppress, DECL_UID (decl));
1450 OMP_CLAUSE_DECL (clause)
1451 = get_local_debug_decl (info, decl, field);
1457 case OMP_CLAUSE_SCHEDULE:
1458 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1462 case OMP_CLAUSE_NUM_THREADS:
1463 wi->val_only = true;
1465 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1469 case OMP_CLAUSE_NOWAIT:
1470 case OMP_CLAUSE_ORDERED:
1471 case OMP_CLAUSE_DEFAULT:
1472 case OMP_CLAUSE_COPYIN:
1473 case OMP_CLAUSE_COLLAPSE:
1474 case OMP_CLAUSE_UNTIED:
1482 info->suppress_expansion = new_suppress;
1485 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1486 switch (OMP_CLAUSE_CODE (clause))
1488 case OMP_CLAUSE_REDUCTION:
1489 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1492 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1493 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1495 walk_body (convert_local_reference_stmt,
1496 convert_local_reference_op, info,
1497 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1498 walk_body (convert_local_reference_stmt,
1499 convert_local_reference_op, info,
1500 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1501 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1506 case OMP_CLAUSE_LASTPRIVATE:
1507 walk_body (convert_local_reference_stmt,
1508 convert_local_reference_op, info,
1509 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1520 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1521 and PARM_DECLs that were referenced by inner nested functions.
1522 The rewrite will be a structure reference to the local frame variable. */
1525 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1526 struct walk_stmt_info *wi)
1528 struct nesting_info *info = (struct nesting_info *) wi->info;
1529 tree save_local_var_chain;
1530 bitmap save_suppress;
1531 gimple stmt = gsi_stmt (*gsi);
1533 switch (gimple_code (stmt))
1535 case GIMPLE_OMP_PARALLEL:
1536 case GIMPLE_OMP_TASK:
1537 save_suppress = info->suppress_expansion;
1538 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1542 (void) get_frame_type (info);
1543 c = build_omp_clause (OMP_CLAUSE_SHARED);
1544 OMP_CLAUSE_DECL (c) = info->frame_decl;
1545 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1546 gimple_omp_taskreg_set_clauses (stmt, c);
1549 save_local_var_chain = info->new_local_var_chain;
1550 info->new_local_var_chain = NULL;
1552 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1553 gimple_omp_body (stmt));
1555 if (info->new_local_var_chain)
1556 declare_vars (info->new_local_var_chain,
1557 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1558 info->new_local_var_chain = save_local_var_chain;
1559 info->suppress_expansion = save_suppress;
1562 case GIMPLE_OMP_FOR:
1563 save_suppress = info->suppress_expansion;
1564 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1565 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1566 convert_local_reference_op, info);
1567 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1568 info, gimple_omp_body (stmt));
1569 info->suppress_expansion = save_suppress;
1572 case GIMPLE_OMP_SECTIONS:
1573 save_suppress = info->suppress_expansion;
1574 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1575 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1576 info, gimple_omp_body (stmt));
1577 info->suppress_expansion = save_suppress;
1580 case GIMPLE_OMP_SINGLE:
1581 save_suppress = info->suppress_expansion;
1582 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1583 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1584 info, gimple_omp_body (stmt));
1585 info->suppress_expansion = save_suppress;
1588 case GIMPLE_OMP_SECTION:
1589 case GIMPLE_OMP_MASTER:
1590 case GIMPLE_OMP_ORDERED:
1591 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1592 info, gimple_omp_body (stmt));
1596 /* For every other statement that we are not interested in
1597 handling here, let the walker traverse the operands. */
1598 *handled_ops_p = false;
1602 /* Indicate that we have handled all the operands ourselves. */
1603 *handled_ops_p = true;
1608 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1609 that reference labels from outer functions. The rewrite will be a
1610 call to __builtin_nonlocal_goto. */
1613 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1614 struct walk_stmt_info *wi)
1616 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1617 tree label, new_label, target_context, x, field;
1620 gimple stmt = gsi_stmt (*gsi);
1622 if (gimple_code (stmt) != GIMPLE_GOTO)
1624 *handled_ops_p = false;
1628 label = gimple_goto_dest (stmt);
1629 if (TREE_CODE (label) != LABEL_DECL)
1631 *handled_ops_p = false;
1635 target_context = decl_function_context (label);
1636 if (target_context == info->context)
1638 *handled_ops_p = false;
1642 for (i = info->outer; target_context != i->context; i = i->outer)
1645 /* The original user label may also be use for a normal goto, therefore
1646 we must create a new label that will actually receive the abnormal
1647 control transfer. This new label will be marked LABEL_NONLOCAL; this
1648 mark will trigger proper behavior in the cfg, as well as cause the
1649 (hairy target-specific) non-local goto receiver code to be generated
1650 when we expand rtl. Enter this association into var_map so that we
1651 can insert the new label into the IL during a second pass. */
1652 slot = pointer_map_insert (i->var_map, label);
1655 new_label = create_artificial_label ();
1656 DECL_NONLOCAL (new_label) = 1;
1660 new_label = (tree) *slot;
1662 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1663 field = get_nl_goto_field (i);
1664 x = get_frame_field (info, target_context, field, &wi->gsi);
1665 x = build_addr (x, target_context);
1666 x = gsi_gimplify_val (info, x, &wi->gsi);
1667 call = gimple_build_call (implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO], 2,
1668 build_addr (new_label, target_context), x);
1669 gsi_replace (&wi->gsi, call, false);
1671 /* We have handled all of STMT's operands, no need to keep going. */
1672 *handled_ops_p = true;
1677 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1678 are referenced via nonlocal goto from a nested function. The rewrite
1679 will involve installing a newly generated DECL_NONLOCAL label, and
1680 (potentially) a branch around the rtl gunk that is assumed to be
1681 attached to such a label. */
1684 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1685 struct walk_stmt_info *wi)
1687 struct nesting_info *const info = (struct nesting_info *) wi->info;
1688 tree label, new_label;
1689 gimple_stmt_iterator tmp_gsi;
1691 gimple stmt = gsi_stmt (*gsi);
1693 if (gimple_code (stmt) != GIMPLE_LABEL)
1695 *handled_ops_p = false;
1699 label = gimple_label_label (stmt);
1701 slot = pointer_map_contains (info->var_map, label);
1704 *handled_ops_p = false;
1708 /* If there's any possibility that the previous statement falls through,
1709 then we must branch around the new non-local label. */
1711 gsi_prev (&tmp_gsi);
1712 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
1714 gimple stmt = gimple_build_goto (label);
1715 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1718 new_label = (tree) *slot;
1719 stmt = gimple_build_label (new_label);
1720 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1722 *handled_ops_p = true;
1727 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1728 of nested functions that require the use of trampolines. The rewrite
1729 will involve a reference a trampoline generated for the occasion. */
1732 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
1734 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1735 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1736 tree t = *tp, decl, target_context, x, builtin;
1740 switch (TREE_CODE (t))
1744 T.1 = &CHAIN->tramp;
1745 T.2 = __builtin_adjust_trampoline (T.1);
1746 T.3 = (func_type)T.2;
1749 decl = TREE_OPERAND (t, 0);
1750 if (TREE_CODE (decl) != FUNCTION_DECL)
1753 /* Only need to process nested functions. */
1754 target_context = decl_function_context (decl);
1755 if (!target_context)
1758 /* If the nested function doesn't use a static chain, then
1759 it doesn't need a trampoline. */
1760 if (DECL_NO_STATIC_CHAIN (decl))
1763 /* If we don't want a trampoline, then don't build one. */
1764 if (TREE_NO_TRAMPOLINE (t))
1767 /* Lookup the immediate parent of the callee, as that's where
1768 we need to insert the trampoline. */
1769 for (i = info; i->context != target_context; i = i->outer)
1771 x = lookup_tramp_for_decl (i, decl, INSERT);
1773 /* Compute the address of the field holding the trampoline. */
1774 x = get_frame_field (info, target_context, x, &wi->gsi);
1775 x = build_addr (x, target_context);
1776 x = gsi_gimplify_val (info, x, &wi->gsi);
1778 /* Do machine-specific ugliness. Normally this will involve
1779 computing extra alignment, but it can really be anything. */
1780 builtin = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1781 call = gimple_build_call (builtin, 1, x);
1782 x = init_tmp_var_with_call (info, &wi->gsi, call);
1784 /* Cast back to the proper function type. */
1785 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1786 x = init_tmp_var (info, x, &wi->gsi);
1792 if (!IS_TYPE_OR_DECL_P (t))
1801 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1802 to addresses of nested functions that require the use of
1803 trampolines. The rewrite will involve a reference a trampoline
1804 generated for the occasion. */
1807 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1808 struct walk_stmt_info *wi)
1810 gimple stmt = gsi_stmt (*gsi);
1812 switch (gimple_code (stmt))
1816 /* Only walk call arguments, lest we generate trampolines for
1818 unsigned long i, nargs = gimple_call_num_args (stmt);
1819 for (i = 0; i < nargs; i++)
1820 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
1823 *handled_ops_p = true;
1831 *handled_ops_p = false;
1837 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
1838 that reference nested functions to make sure that the static chain
1839 is set up properly for the call. */
1842 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1843 struct walk_stmt_info *wi)
1845 struct nesting_info *const info = (struct nesting_info *) wi->info;
1846 tree decl, target_context;
1847 char save_static_chain_added;
1849 gimple stmt = gsi_stmt (*gsi);
1851 switch (gimple_code (stmt))
1854 decl = gimple_call_fn (stmt);
1855 if (TREE_CODE (decl) != FUNCTION_DECL)
1857 target_context = decl_function_context (decl);
1858 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1860 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
1862 info->static_chain_added |= (1 << (info->context != target_context));
1866 case GIMPLE_OMP_PARALLEL:
1867 case GIMPLE_OMP_TASK:
1868 save_static_chain_added = info->static_chain_added;
1869 info->static_chain_added = 0;
1870 walk_body (convert_gimple_call, NULL, info, gimple_omp_body (stmt));
1871 for (i = 0; i < 2; i++)
1874 if ((info->static_chain_added & (1 << i)) == 0)
1876 decl = i ? get_chain_decl (info) : info->frame_decl;
1877 /* Don't add CHAIN.* or FRAME.* twice. */
1878 for (c = gimple_omp_taskreg_clauses (stmt);
1880 c = OMP_CLAUSE_CHAIN (c))
1881 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1882 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1883 && OMP_CLAUSE_DECL (c) == decl)
1887 c = build_omp_clause (i ? OMP_CLAUSE_FIRSTPRIVATE
1888 : OMP_CLAUSE_SHARED);
1889 OMP_CLAUSE_DECL (c) = decl;
1890 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1891 gimple_omp_taskreg_set_clauses (stmt, c);
1894 info->static_chain_added |= save_static_chain_added;
1897 case GIMPLE_OMP_FOR:
1898 walk_body (convert_gimple_call, NULL, info,
1899 gimple_omp_for_pre_body (stmt));
1901 case GIMPLE_OMP_SECTIONS:
1902 case GIMPLE_OMP_SECTION:
1903 case GIMPLE_OMP_SINGLE:
1904 case GIMPLE_OMP_MASTER:
1905 case GIMPLE_OMP_ORDERED:
1906 case GIMPLE_OMP_CRITICAL:
1907 walk_body (convert_gimple_call, NULL, info, gimple_omp_body (stmt));
1911 /* Keep looking for other operands. */
1912 *handled_ops_p = false;
1916 *handled_ops_p = true;
1921 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1922 trampolines and call expressions. On the way back up, determine if
1923 a nested function actually uses its static chain; if not, remember that. */
1926 convert_all_function_calls (struct nesting_info *root)
1931 convert_all_function_calls (root->inner);
1933 walk_function (convert_tramp_reference_stmt, convert_tramp_reference_op,
1935 walk_function (convert_gimple_call, NULL, root);
1937 /* If the function does not use a static chain, then remember that. */
1938 if (root->outer && !root->chain_decl && !root->chain_field)
1939 DECL_NO_STATIC_CHAIN (root->context) = 1;
1941 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1948 /* Do "everything else" to clean up or complete state collected by the
1949 various walking passes -- lay out the types and decls, generate code
1950 to initialize the frame decl, store critical expressions in the
1951 struct function for rtl to find. */
1954 finalize_nesting_tree_1 (struct nesting_info *root)
1956 gimple_seq stmt_list;
1958 tree context = root->context;
1959 struct function *sf;
1963 /* If we created a non-local frame type or decl, we need to lay them
1964 out at this time. */
1965 if (root->frame_type)
1967 /* In some cases the frame type will trigger the -Wpadded warning.
1968 This is not helpful; suppress it. */
1969 int save_warn_padded = warn_padded;
1971 layout_type (root->frame_type);
1972 warn_padded = save_warn_padded;
1973 layout_decl (root->frame_decl, 0);
1976 /* If any parameters were referenced non-locally, then we need to
1977 insert a copy. Likewise, if any variables were referenced by
1978 pointer, we need to initialize the address. */
1979 if (root->any_parm_remapped)
1982 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1986 field = lookup_field_for_decl (root, p, NO_INSERT);
1990 if (use_pointer_in_frame (p))
1991 x = build_addr (p, context);
1995 y = build3 (COMPONENT_REF, TREE_TYPE (field),
1996 root->frame_decl, field, NULL_TREE);
1997 stmt = gimple_build_assign (y, x);
1998 gimple_seq_add_stmt (&stmt_list, stmt);
2002 /* If a chain_field was created, then it needs to be initialized
2004 if (root->chain_field)
2006 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2007 root->frame_decl, root->chain_field, NULL_TREE);
2008 stmt = gimple_build_assign (x, get_chain_decl (root));
2009 gimple_seq_add_stmt (&stmt_list, stmt);
2012 /* If trampolines were created, then we need to initialize them. */
2013 if (root->any_tramp_created)
2015 struct nesting_info *i;
2016 for (i = root->inner; i ; i = i->next)
2018 tree arg1, arg2, arg3, x, field;
2020 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2024 if (DECL_NO_STATIC_CHAIN (i->context))
2025 arg3 = null_pointer_node;
2027 arg3 = build_addr (root->frame_decl, context);
2029 arg2 = build_addr (i->context, context);
2031 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2032 root->frame_decl, field, NULL_TREE);
2033 arg1 = build_addr (x, context);
2035 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
2036 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2037 gimple_seq_add_stmt (&stmt_list, stmt);
2041 /* If we created initialization statements, insert them. */
2045 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2046 bind = gimple_seq_first_stmt (gimple_body (context));
2047 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2048 gimple_bind_set_body (bind, stmt_list);
2051 /* If a chain_decl was created, then it needs to be registered with
2052 struct function so that it gets initialized from the static chain
2053 register at the beginning of the function. */
2054 sf = DECL_STRUCT_FUNCTION (root->context);
2055 sf->static_chain_decl = root->chain_decl;
2057 /* Similarly for the non-local goto save area. */
2058 if (root->nl_goto_field)
2060 sf->nonlocal_goto_save_area
2061 = get_frame_field (root, context, root->nl_goto_field, NULL);
2062 sf->has_nonlocal_label = 1;
2065 /* Make sure all new local variables get inserted into the
2066 proper BIND_EXPR. */
2067 if (root->new_local_var_chain)
2068 declare_vars (root->new_local_var_chain,
2069 gimple_seq_first_stmt (gimple_body (root->context)),
2071 if (root->debug_var_chain)
2072 declare_vars (root->debug_var_chain,
2073 gimple_seq_first_stmt (gimple_body (root->context)),
2076 /* Dump the translated tree function. */
2077 dump_function (TDI_nested, root->context);
2081 finalize_nesting_tree (struct nesting_info *root)
2086 finalize_nesting_tree (root->inner);
2087 finalize_nesting_tree_1 (root);
2093 /* Unnest the nodes and pass them to cgraph. */
2096 unnest_nesting_tree_1 (struct nesting_info *root)
2098 struct cgraph_node *node = cgraph_node (root->context);
2100 /* For nested functions update the cgraph to reflect unnesting.
2101 We also delay finalizing of these functions up to this point. */
2104 cgraph_unnest_node (cgraph_node (root->context));
2105 cgraph_finalize_function (root->context, true);
2110 unnest_nesting_tree (struct nesting_info *root)
2115 unnest_nesting_tree (root->inner);
2116 unnest_nesting_tree_1 (root);
2122 /* Free the data structures allocated during this pass. */
2125 free_nesting_tree (struct nesting_info *root)
2127 struct nesting_info *next;
2131 free_nesting_tree (root->inner);
2132 pointer_map_destroy (root->var_map);
2133 pointer_map_destroy (root->field_map);
2141 /* Main entry point for this pass. Process FNDECL and all of its nested
2142 subroutines and turn them into something less tightly bound. */
2145 lower_nested_functions (tree fndecl)
2147 struct cgraph_node *cgn;
2148 struct nesting_info *root;
2150 /* If there are no nested functions, there's nothing to do. */
2151 cgn = cgraph_node (fndecl);
2155 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2156 root = create_nesting_tree (cgn);
2157 walk_all_functions (convert_nonlocal_reference_stmt,
2158 convert_nonlocal_reference_op,
2160 walk_all_functions (convert_local_reference_stmt,
2161 convert_local_reference_op,
2163 walk_all_functions (convert_nl_goto_reference, NULL, root);
2164 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2165 convert_all_function_calls (root);
2166 finalize_nesting_tree (root);
2167 unnest_nesting_tree (root);
2168 free_nesting_tree (root);
2169 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2172 #include "gt-tree-nested.h"