1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2016 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"
29 #include "stringpool.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "tree-dump.h"
34 #include "tree-inline.h"
36 #include "gimple-iterator.h"
37 #include "gimple-walk.h"
40 #include "langhooks.h"
41 #include "gimple-low.h"
42 #include "gomp-constants.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 hash_map<tree, tree> *field_map;
92 hash_map<tree, tree> *var_map;
93 hash_set<tree *> *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)
174 mark_addressable (exp);
175 return build_fold_addr_expr (exp);
178 /* Insert FIELD into TYPE, sorted by alignment requirements. */
181 insert_field_into_struct (tree type, tree field)
185 DECL_CONTEXT (field) = type;
187 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
188 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
191 DECL_CHAIN (field) = *p;
194 /* Set correct alignment for frame struct type. */
195 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
196 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
199 /* Build or return the RECORD_TYPE that describes the frame state that is
200 shared between INFO->CONTEXT and its nested functions. This record will
201 not be complete until finalize_nesting_tree; up until that point we'll
202 be adding fields as necessary.
204 We also build the DECL that represents this frame in the function. */
207 get_frame_type (struct nesting_info *info)
209 tree type = info->frame_type;
214 type = make_node (RECORD_TYPE);
216 name = concat ("FRAME.",
217 IDENTIFIER_POINTER (DECL_NAME (info->context)),
219 TYPE_NAME (type) = get_identifier (name);
222 info->frame_type = type;
223 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
224 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
226 /* ??? Always make it addressable for now, since it is meant to
227 be pointed to by the static chain pointer. This pessimizes
228 when it turns out that no static chains are needed because
229 the nested functions referencing non-local variables are not
230 reachable, but the true pessimization is to create the non-
231 local frame structure in the first place. */
232 TREE_ADDRESSABLE (info->frame_decl) = 1;
237 /* Return true if DECL should be referenced by pointer in the non-local
241 use_pointer_in_frame (tree decl)
243 if (TREE_CODE (decl) == PARM_DECL)
245 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
246 sized decls, and inefficient to copy large aggregates. Don't bother
247 moving anything but scalar variables. */
248 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
252 /* Variable sized types make things "interesting" in the frame. */
253 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
257 /* Given DECL, a non-locally accessed variable, find or create a field
258 in the non-local frame structure for the given nesting context. */
261 lookup_field_for_decl (struct nesting_info *info, tree decl,
262 enum insert_option insert)
264 if (insert == NO_INSERT)
266 tree *slot = info->field_map->get (decl);
267 return slot ? *slot : NULL_TREE;
270 tree *slot = &info->field_map->get_or_insert (decl);
273 tree field = make_node (FIELD_DECL);
274 DECL_NAME (field) = DECL_NAME (decl);
276 if (use_pointer_in_frame (decl))
278 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
279 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
280 DECL_NONADDRESSABLE_P (field) = 1;
284 TREE_TYPE (field) = TREE_TYPE (decl);
285 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
286 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
287 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
288 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
289 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
290 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
293 insert_field_into_struct (get_frame_type (info), field);
296 if (TREE_CODE (decl) == PARM_DECL)
297 info->any_parm_remapped = true;
303 /* Build or return the variable that holds the static chain within
304 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
307 get_chain_decl (struct nesting_info *info)
309 tree decl = info->chain_decl;
315 type = get_frame_type (info->outer);
316 type = build_pointer_type (type);
318 /* Note that this variable is *not* entered into any BIND_EXPR;
319 the construction of this variable is handled specially in
320 expand_function_start and initialize_inlined_parameters.
321 Note also that it's represented as a parameter. This is more
322 close to the truth, since the initial value does come from
324 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
325 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
326 DECL_ARTIFICIAL (decl) = 1;
327 DECL_IGNORED_P (decl) = 1;
328 TREE_USED (decl) = 1;
329 DECL_CONTEXT (decl) = info->context;
330 DECL_ARG_TYPE (decl) = type;
332 /* Tell tree-inline.c that we never write to this variable, so
333 it can copy-prop the replacement value immediately. */
334 TREE_READONLY (decl) = 1;
336 info->chain_decl = decl;
339 && (dump_flags & TDF_DETAILS)
340 && !DECL_STATIC_CHAIN (info->context))
341 fprintf (dump_file, "Setting static-chain for %s\n",
342 lang_hooks.decl_printable_name (info->context, 2));
344 DECL_STATIC_CHAIN (info->context) = 1;
349 /* Build or return the field within the non-local frame state that holds
350 the static chain for INFO->CONTEXT. This is the way to walk back up
351 multiple nesting levels. */
354 get_chain_field (struct nesting_info *info)
356 tree field = info->chain_field;
360 tree type = build_pointer_type (get_frame_type (info->outer));
362 field = make_node (FIELD_DECL);
363 DECL_NAME (field) = get_identifier ("__chain");
364 TREE_TYPE (field) = type;
365 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
366 DECL_NONADDRESSABLE_P (field) = 1;
368 insert_field_into_struct (get_frame_type (info), field);
370 info->chain_field = field;
373 && (dump_flags & TDF_DETAILS)
374 && !DECL_STATIC_CHAIN (info->context))
375 fprintf (dump_file, "Setting static-chain for %s\n",
376 lang_hooks.decl_printable_name (info->context, 2));
378 DECL_STATIC_CHAIN (info->context) = 1;
383 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
386 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
391 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
392 gimple_call_set_lhs (call, t);
393 if (! gsi_end_p (*gsi))
394 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
395 gsi_insert_before (gsi, call, GSI_SAME_STMT);
401 /* Copy EXP into a temporary. Allocate the temporary in the context of
402 INFO and insert the initialization statement before GSI. */
405 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
410 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
411 stmt = gimple_build_assign (t, exp);
412 if (! gsi_end_p (*gsi))
413 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
414 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
420 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
423 gsi_gimplify_val (struct nesting_info *info, tree exp,
424 gimple_stmt_iterator *gsi)
426 if (is_gimple_val (exp))
429 return init_tmp_var (info, exp, gsi);
432 /* Similarly, but copy from the temporary and insert the statement
433 after the iterator. */
436 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
441 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
442 stmt = gimple_build_assign (exp, t);
443 if (! gsi_end_p (*gsi))
444 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
445 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
450 /* Build or return the type used to represent a nested function trampoline. */
452 static GTY(()) tree trampoline_type;
455 get_trampoline_type (struct nesting_info *info)
457 unsigned align, size;
461 return trampoline_type;
463 align = TRAMPOLINE_ALIGNMENT;
464 size = TRAMPOLINE_SIZE;
466 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
467 then allocate extra space so that we can do dynamic alignment. */
468 if (align > STACK_BOUNDARY)
470 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
471 align = STACK_BOUNDARY;
474 t = build_index_type (size_int (size - 1));
475 t = build_array_type (char_type_node, t);
476 t = build_decl (DECL_SOURCE_LOCATION (info->context),
477 FIELD_DECL, get_identifier ("__data"), t);
478 SET_DECL_ALIGN (t, align);
479 DECL_USER_ALIGN (t) = 1;
481 trampoline_type = make_node (RECORD_TYPE);
482 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
483 TYPE_FIELDS (trampoline_type) = t;
484 layout_type (trampoline_type);
485 DECL_CONTEXT (t) = trampoline_type;
487 return trampoline_type;
490 /* Given DECL, a nested function, find or create a field in the non-local
491 frame structure for a trampoline for this function. */
494 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
495 enum insert_option insert)
497 if (insert == NO_INSERT)
499 tree *slot = info->var_map->get (decl);
500 return slot ? *slot : NULL_TREE;
503 tree *slot = &info->var_map->get_or_insert (decl);
506 tree field = make_node (FIELD_DECL);
507 DECL_NAME (field) = DECL_NAME (decl);
508 TREE_TYPE (field) = get_trampoline_type (info);
509 TREE_ADDRESSABLE (field) = 1;
511 insert_field_into_struct (get_frame_type (info), field);
514 info->any_tramp_created = true;
520 /* Build or return the field within the non-local frame state that holds
521 the non-local goto "jmp_buf". The buffer itself is maintained by the
522 rtl middle-end as dynamic stack space is allocated. */
525 get_nl_goto_field (struct nesting_info *info)
527 tree field = info->nl_goto_field;
533 /* For __builtin_nonlocal_goto, we need N words. The first is the
534 frame pointer, the rest is for the target's stack pointer save
535 area. The number of words is controlled by STACK_SAVEAREA_MODE;
536 not the best interface, but it'll do for now. */
537 if (Pmode == ptr_mode)
538 type = ptr_type_node;
540 type = lang_hooks.types.type_for_mode (Pmode, 1);
542 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
543 size = size / GET_MODE_SIZE (Pmode);
546 type = build_array_type
547 (type, build_index_type (size_int (size)));
549 field = make_node (FIELD_DECL);
550 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
551 TREE_TYPE (field) = type;
552 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
553 TREE_ADDRESSABLE (field) = 1;
555 insert_field_into_struct (get_frame_type (info), field);
557 info->nl_goto_field = field;
563 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
566 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
567 struct nesting_info *info, gimple_seq *pseq)
569 struct walk_stmt_info wi;
571 memset (&wi, 0, sizeof (wi));
574 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
578 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
581 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
582 struct nesting_info *info)
584 gimple_seq body = gimple_body (info->context);
585 walk_body (callback_stmt, callback_op, info, &body);
586 gimple_set_body (info->context, body);
589 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
592 walk_gimple_omp_for (gomp_for *for_stmt,
593 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
594 struct nesting_info *info)
596 struct walk_stmt_info wi;
601 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
604 memset (&wi, 0, sizeof (wi));
606 wi.gsi = gsi_last (seq);
608 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
611 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
615 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
620 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
623 t = gimple_omp_for_incr (for_stmt, i);
624 gcc_assert (BINARY_CLASS_P (t));
626 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
629 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
632 seq = gsi_seq (wi.gsi);
633 if (!gimple_seq_empty_p (seq))
635 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
636 annotate_all_with_location (seq, gimple_location (for_stmt));
637 gimple_seq_add_seq (&pre_body, seq);
638 gimple_omp_for_set_pre_body (for_stmt, pre_body);
642 /* Similarly for ROOT and all functions nested underneath, depth first. */
645 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
646 struct nesting_info *root)
648 struct nesting_info *n;
649 FOR_EACH_NEST_INFO (n, root)
650 walk_function (callback_stmt, callback_op, n);
654 /* We have to check for a fairly pathological case. The operands of function
655 nested function are to be interpreted in the context of the enclosing
656 function. So if any are variably-sized, they will get remapped when the
657 enclosing function is inlined. But that remapping would also have to be
658 done in the types of the PARM_DECLs of the nested function, meaning the
659 argument types of that function will disagree with the arguments in the
660 calls to that function. So we'd either have to make a copy of the nested
661 function corresponding to each time the enclosing function was inlined or
662 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
663 function. The former is not practical. The latter would still require
664 detecting this case to know when to add the conversions. So, for now at
665 least, we don't inline such an enclosing function.
667 We have to do that check recursively, so here return indicating whether
668 FNDECL has such a nested function. ORIG_FN is the function we were
669 trying to inline to use for checking whether any argument is variably
670 modified by anything in it.
672 It would be better to do this in tree-inline.c so that we could give
673 the appropriate warning for why a function can't be inlined, but that's
674 too late since the nesting structure has already been flattened and
675 adding a flag just to record this fact seems a waste of a flag. */
678 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
680 struct cgraph_node *cgn = cgraph_node::get (fndecl);
683 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
685 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
686 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
689 if (check_for_nested_with_variably_modified (cgn->decl,
697 /* Construct our local datastructure describing the function nesting
698 tree rooted by CGN. */
700 static struct nesting_info *
701 create_nesting_tree (struct cgraph_node *cgn)
703 struct nesting_info *info = XCNEW (struct nesting_info);
704 info->field_map = new hash_map<tree, tree>;
705 info->var_map = new hash_map<tree, tree>;
706 info->mem_refs = new hash_set<tree *>;
707 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
708 info->context = cgn->decl;
710 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
712 struct nesting_info *sub = create_nesting_tree (cgn);
714 sub->next = info->inner;
718 /* See discussion at check_for_nested_with_variably_modified for a
719 discussion of why this has to be here. */
720 if (check_for_nested_with_variably_modified (info->context, info->context))
721 DECL_UNINLINABLE (info->context) = true;
726 /* Return an expression computing the static chain for TARGET_CONTEXT
727 from INFO->CONTEXT. Insert any necessary computations before TSI. */
730 get_static_chain (struct nesting_info *info, tree target_context,
731 gimple_stmt_iterator *gsi)
733 struct nesting_info *i;
736 if (info->context == target_context)
738 x = build_addr (info->frame_decl);
739 info->static_chain_added |= 1;
743 x = get_chain_decl (info);
744 info->static_chain_added |= 2;
746 for (i = info->outer; i->context != target_context; i = i->outer)
748 tree field = get_chain_field (i);
750 x = build_simple_mem_ref (x);
751 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
752 x = init_tmp_var (info, x, gsi);
760 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
761 frame as seen from INFO->CONTEXT. Insert any necessary computations
765 get_frame_field (struct nesting_info *info, tree target_context,
766 tree field, gimple_stmt_iterator *gsi)
768 struct nesting_info *i;
771 if (info->context == target_context)
773 /* Make sure frame_decl gets created. */
774 (void) get_frame_type (info);
775 x = info->frame_decl;
776 info->static_chain_added |= 1;
780 x = get_chain_decl (info);
781 info->static_chain_added |= 2;
783 for (i = info->outer; i->context != target_context; i = i->outer)
785 tree field = get_chain_field (i);
787 x = build_simple_mem_ref (x);
788 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
789 x = init_tmp_var (info, x, gsi);
792 x = build_simple_mem_ref (x);
795 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
799 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
801 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
802 in the nested function with DECL_VALUE_EXPR set to reference the true
803 variable in the parent function. This is used both for debug info
804 and in OMP lowering. */
807 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
810 struct nesting_info *i;
811 tree x, field, new_decl;
813 tree *slot = &info->var_map->get_or_insert (decl);
818 target_context = decl_function_context (decl);
820 /* A copy of the code in get_frame_field, but without the temporaries. */
821 if (info->context == target_context)
823 /* Make sure frame_decl gets created. */
824 (void) get_frame_type (info);
825 x = info->frame_decl;
827 info->static_chain_added |= 1;
831 x = get_chain_decl (info);
832 info->static_chain_added |= 2;
833 for (i = info->outer; i->context != target_context; i = i->outer)
835 field = get_chain_field (i);
836 x = build_simple_mem_ref (x);
837 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
839 x = build_simple_mem_ref (x);
842 field = lookup_field_for_decl (i, decl, INSERT);
843 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
844 if (use_pointer_in_frame (decl))
845 x = build_simple_mem_ref (x);
847 /* ??? We should be remapping types as well, surely. */
848 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
849 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
850 DECL_CONTEXT (new_decl) = info->context;
851 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
852 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
853 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
854 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
855 TREE_READONLY (new_decl) = TREE_READONLY (decl);
856 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
857 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
858 if ((TREE_CODE (decl) == PARM_DECL
859 || TREE_CODE (decl) == RESULT_DECL
861 && DECL_BY_REFERENCE (decl))
862 DECL_BY_REFERENCE (new_decl) = 1;
864 SET_DECL_VALUE_EXPR (new_decl, x);
865 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
868 DECL_CHAIN (new_decl) = info->debug_var_chain;
869 info->debug_var_chain = new_decl;
872 && info->context != target_context
873 && variably_modified_type_p (TREE_TYPE (decl), NULL))
874 note_nonlocal_vla_type (info, TREE_TYPE (decl));
880 /* Callback for walk_gimple_stmt, rewrite all references to VAR
881 and PARM_DECLs that belong to outer functions.
883 The rewrite will involve some number of structure accesses back up
884 the static chain. E.g. for a variable FOO up one nesting level it'll
885 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
886 indirections apply to decls for which use_pointer_in_frame is true. */
889 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
891 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
892 struct nesting_info *const info = (struct nesting_info *) wi->info;
896 switch (TREE_CODE (t))
899 /* Non-automatic variables are never processed. */
900 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
905 if (decl_function_context (t) != info->context)
910 x = get_nonlocal_debug_decl (info, t);
911 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
913 tree target_context = decl_function_context (t);
914 struct nesting_info *i;
915 for (i = info->outer; i->context != target_context; i = i->outer)
917 x = lookup_field_for_decl (i, t, INSERT);
918 x = get_frame_field (info, target_context, x, &wi->gsi);
919 if (use_pointer_in_frame (t))
921 x = init_tmp_var (info, x, &wi->gsi);
922 x = build_simple_mem_ref (x);
929 x = save_tmp_var (info, x, &wi->gsi);
931 x = init_tmp_var (info, x, &wi->gsi);
939 /* We're taking the address of a label from a parent function, but
940 this is not itself a non-local goto. Mark the label such that it
941 will not be deleted, much as we would with a label address in
943 if (decl_function_context (t) != info->context)
944 FORCED_LABEL (t) = 1;
949 bool save_val_only = wi->val_only;
951 wi->val_only = false;
954 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
961 /* If we changed anything, we might no longer be directly
962 referencing a decl. */
963 save_context = current_function_decl;
964 current_function_decl = info->context;
965 recompute_tree_invariant_for_addr_expr (t);
966 current_function_decl = save_context;
968 /* If the callback converted the address argument in a context
969 where we only accept variables (and min_invariant, presumably),
970 then compute the address into a temporary. */
972 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
982 case ARRAY_RANGE_REF:
984 /* Go down this entire nest and just look at the final prefix and
985 anything that describes the references. Otherwise, we lose track
986 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
989 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
991 if (TREE_CODE (t) == COMPONENT_REF)
992 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
994 else if (TREE_CODE (t) == ARRAY_REF
995 || TREE_CODE (t) == ARRAY_RANGE_REF)
997 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
999 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1001 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1005 wi->val_only = false;
1006 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1009 case VIEW_CONVERT_EXPR:
1010 /* Just request to look at the subtrees, leaving val_only and lhs
1011 untouched. This might actually be for !val_only + lhs, in which
1012 case we don't want to force a replacement by a temporary. */
1017 if (!IS_TYPE_OR_DECL_P (t))
1020 wi->val_only = true;
1029 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1030 struct walk_stmt_info *);
1032 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1033 and PARM_DECLs that belong to outer functions. */
1036 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1038 struct nesting_info *const info = (struct nesting_info *) wi->info;
1039 bool need_chain = false, need_stmts = false;
1042 bitmap new_suppress;
1044 new_suppress = BITMAP_GGC_ALLOC ();
1045 bitmap_copy (new_suppress, info->suppress_expansion);
1047 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1049 switch (OMP_CLAUSE_CODE (clause))
1051 case OMP_CLAUSE_REDUCTION:
1052 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1054 goto do_decl_clause;
1056 case OMP_CLAUSE_LASTPRIVATE:
1057 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1059 goto do_decl_clause;
1061 case OMP_CLAUSE_LINEAR:
1062 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1064 wi->val_only = true;
1066 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1068 goto do_decl_clause;
1070 case OMP_CLAUSE_PRIVATE:
1071 case OMP_CLAUSE_FIRSTPRIVATE:
1072 case OMP_CLAUSE_COPYPRIVATE:
1073 case OMP_CLAUSE_SHARED:
1074 case OMP_CLAUSE_TO_DECLARE:
1075 case OMP_CLAUSE_LINK:
1076 case OMP_CLAUSE_USE_DEVICE_PTR:
1077 case OMP_CLAUSE_IS_DEVICE_PTR:
1079 decl = OMP_CLAUSE_DECL (clause);
1081 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1083 if (decl_function_context (decl) != info->context)
1085 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1086 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1087 bitmap_set_bit (new_suppress, DECL_UID (decl));
1088 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1089 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1094 case OMP_CLAUSE_SCHEDULE:
1095 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1098 case OMP_CLAUSE_FINAL:
1100 case OMP_CLAUSE_NUM_THREADS:
1101 case OMP_CLAUSE_DEPEND:
1102 case OMP_CLAUSE_DEVICE:
1103 case OMP_CLAUSE_NUM_TEAMS:
1104 case OMP_CLAUSE_THREAD_LIMIT:
1105 case OMP_CLAUSE_SAFELEN:
1106 case OMP_CLAUSE_SIMDLEN:
1107 case OMP_CLAUSE_PRIORITY:
1108 case OMP_CLAUSE_GRAINSIZE:
1109 case OMP_CLAUSE_NUM_TASKS:
1110 case OMP_CLAUSE_HINT:
1111 case OMP_CLAUSE__CILK_FOR_COUNT_:
1112 case OMP_CLAUSE_NUM_GANGS:
1113 case OMP_CLAUSE_NUM_WORKERS:
1114 case OMP_CLAUSE_VECTOR_LENGTH:
1115 case OMP_CLAUSE_GANG:
1116 case OMP_CLAUSE_WORKER:
1117 case OMP_CLAUSE_VECTOR:
1118 case OMP_CLAUSE_ASYNC:
1119 case OMP_CLAUSE_WAIT:
1120 /* Several OpenACC clauses have optional arguments. Check if they
1122 if (OMP_CLAUSE_OPERAND (clause, 0))
1124 wi->val_only = true;
1126 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1130 /* The gang clause accepts two arguments. */
1131 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1132 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1134 wi->val_only = true;
1136 convert_nonlocal_reference_op
1137 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1141 case OMP_CLAUSE_DIST_SCHEDULE:
1142 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1144 wi->val_only = true;
1146 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1151 case OMP_CLAUSE_MAP:
1153 case OMP_CLAUSE_FROM:
1154 if (OMP_CLAUSE_SIZE (clause))
1156 wi->val_only = true;
1158 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1161 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1162 goto do_decl_clause;
1163 wi->val_only = true;
1165 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1169 case OMP_CLAUSE_ALIGNED:
1170 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1172 wi->val_only = true;
1174 convert_nonlocal_reference_op
1175 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1177 /* Like do_decl_clause, but don't add any suppression. */
1178 decl = OMP_CLAUSE_DECL (clause);
1180 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1182 if (decl_function_context (decl) != info->context)
1184 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1185 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1190 case OMP_CLAUSE_NOWAIT:
1191 case OMP_CLAUSE_ORDERED:
1192 case OMP_CLAUSE_DEFAULT:
1193 case OMP_CLAUSE_COPYIN:
1194 case OMP_CLAUSE_COLLAPSE:
1195 case OMP_CLAUSE_UNTIED:
1196 case OMP_CLAUSE_MERGEABLE:
1197 case OMP_CLAUSE_PROC_BIND:
1198 case OMP_CLAUSE_NOGROUP:
1199 case OMP_CLAUSE_THREADS:
1200 case OMP_CLAUSE_SIMD:
1201 case OMP_CLAUSE_DEFAULTMAP:
1202 case OMP_CLAUSE_SEQ:
1203 case OMP_CLAUSE_INDEPENDENT:
1204 case OMP_CLAUSE_AUTO:
1207 /* OpenACC tile clauses are discarded during gimplification. */
1208 case OMP_CLAUSE_TILE:
1209 /* The following clause belongs to the OpenACC cache directive, which
1210 is discarded during gimplification. */
1211 case OMP_CLAUSE__CACHE_:
1212 /* The following clauses are only allowed in the OpenMP declare simd
1213 directive, so not seen here. */
1214 case OMP_CLAUSE_UNIFORM:
1215 case OMP_CLAUSE_INBRANCH:
1216 case OMP_CLAUSE_NOTINBRANCH:
1217 /* The following clauses are only allowed on OpenMP cancel and
1218 cancellation point directives, which at this point have already
1219 been lowered into a function call. */
1220 case OMP_CLAUSE_FOR:
1221 case OMP_CLAUSE_PARALLEL:
1222 case OMP_CLAUSE_SECTIONS:
1223 case OMP_CLAUSE_TASKGROUP:
1224 /* The following clauses are only added during OMP lowering; nested
1225 function decomposition happens before that. */
1226 case OMP_CLAUSE__LOOPTEMP_:
1227 case OMP_CLAUSE__SIMDUID_:
1228 case OMP_CLAUSE__GRIDDIM_:
1229 /* Anything else. */
1235 info->suppress_expansion = new_suppress;
1238 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1239 switch (OMP_CLAUSE_CODE (clause))
1241 case OMP_CLAUSE_REDUCTION:
1242 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1245 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1246 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1248 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1249 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1251 walk_body (convert_nonlocal_reference_stmt,
1252 convert_nonlocal_reference_op, info,
1253 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1254 walk_body (convert_nonlocal_reference_stmt,
1255 convert_nonlocal_reference_op, info,
1256 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1257 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1259 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1260 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1265 case OMP_CLAUSE_LASTPRIVATE:
1266 walk_body (convert_nonlocal_reference_stmt,
1267 convert_nonlocal_reference_op, info,
1268 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1271 case OMP_CLAUSE_LINEAR:
1272 walk_body (convert_nonlocal_reference_stmt,
1273 convert_nonlocal_reference_op, info,
1274 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1284 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1287 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1289 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1290 type = TREE_TYPE (type);
1292 if (TYPE_NAME (type)
1293 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1294 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1295 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1297 while (POINTER_TYPE_P (type)
1298 || TREE_CODE (type) == VECTOR_TYPE
1299 || TREE_CODE (type) == FUNCTION_TYPE
1300 || TREE_CODE (type) == METHOD_TYPE)
1301 type = TREE_TYPE (type);
1303 if (TREE_CODE (type) == ARRAY_TYPE)
1307 note_nonlocal_vla_type (info, TREE_TYPE (type));
1308 domain = TYPE_DOMAIN (type);
1311 t = TYPE_MIN_VALUE (domain);
1312 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1313 && decl_function_context (t) != info->context)
1314 get_nonlocal_debug_decl (info, t);
1315 t = TYPE_MAX_VALUE (domain);
1316 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1317 && decl_function_context (t) != info->context)
1318 get_nonlocal_debug_decl (info, t);
1323 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1327 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1331 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1333 && variably_modified_type_p (TREE_TYPE (var), NULL)
1334 && DECL_HAS_VALUE_EXPR_P (var)
1335 && decl_function_context (var) != info->context)
1336 note_nonlocal_vla_type (info, TREE_TYPE (var));
1339 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1340 PARM_DECLs that belong to outer functions. This handles statements
1341 that are not handled via the standard recursion done in
1342 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1343 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1344 operands of STMT have been handled by this function. */
1347 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1348 struct walk_stmt_info *wi)
1350 struct nesting_info *info = (struct nesting_info *) wi->info;
1351 tree save_local_var_chain;
1352 bitmap save_suppress;
1353 gimple *stmt = gsi_stmt (*gsi);
1355 switch (gimple_code (stmt))
1358 /* Don't walk non-local gotos for now. */
1359 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1361 wi->val_only = true;
1363 *handled_ops_p = false;
1368 case GIMPLE_OMP_PARALLEL:
1369 case GIMPLE_OMP_TASK:
1370 save_suppress = info->suppress_expansion;
1371 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1375 decl = get_chain_decl (info);
1376 c = build_omp_clause (gimple_location (stmt),
1377 OMP_CLAUSE_FIRSTPRIVATE);
1378 OMP_CLAUSE_DECL (c) = decl;
1379 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1380 gimple_omp_taskreg_set_clauses (stmt, c);
1383 save_local_var_chain = info->new_local_var_chain;
1384 info->new_local_var_chain = NULL;
1386 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1387 info, gimple_omp_body_ptr (stmt));
1389 if (info->new_local_var_chain)
1390 declare_vars (info->new_local_var_chain,
1391 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1393 info->new_local_var_chain = save_local_var_chain;
1394 info->suppress_expansion = save_suppress;
1397 case GIMPLE_OMP_FOR:
1398 save_suppress = info->suppress_expansion;
1399 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1400 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1401 convert_nonlocal_reference_stmt,
1402 convert_nonlocal_reference_op, info);
1403 walk_body (convert_nonlocal_reference_stmt,
1404 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1405 info->suppress_expansion = save_suppress;
1408 case GIMPLE_OMP_SECTIONS:
1409 save_suppress = info->suppress_expansion;
1410 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1411 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1412 info, gimple_omp_body_ptr (stmt));
1413 info->suppress_expansion = save_suppress;
1416 case GIMPLE_OMP_SINGLE:
1417 save_suppress = info->suppress_expansion;
1418 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1419 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1420 info, gimple_omp_body_ptr (stmt));
1421 info->suppress_expansion = save_suppress;
1424 case GIMPLE_OMP_TARGET:
1425 if (!is_gimple_omp_offloaded (stmt))
1427 save_suppress = info->suppress_expansion;
1428 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1430 info->suppress_expansion = save_suppress;
1431 walk_body (convert_nonlocal_reference_stmt,
1432 convert_nonlocal_reference_op, info,
1433 gimple_omp_body_ptr (stmt));
1436 save_suppress = info->suppress_expansion;
1437 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1441 decl = get_chain_decl (info);
1442 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1443 OMP_CLAUSE_DECL (c) = decl;
1444 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1445 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1446 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1447 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1450 save_local_var_chain = info->new_local_var_chain;
1451 info->new_local_var_chain = NULL;
1453 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1454 info, gimple_omp_body_ptr (stmt));
1456 if (info->new_local_var_chain)
1457 declare_vars (info->new_local_var_chain,
1458 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1460 info->new_local_var_chain = save_local_var_chain;
1461 info->suppress_expansion = save_suppress;
1464 case GIMPLE_OMP_TEAMS:
1465 save_suppress = info->suppress_expansion;
1466 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1467 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1468 info, gimple_omp_body_ptr (stmt));
1469 info->suppress_expansion = save_suppress;
1472 case GIMPLE_OMP_SECTION:
1473 case GIMPLE_OMP_MASTER:
1474 case GIMPLE_OMP_TASKGROUP:
1475 case GIMPLE_OMP_ORDERED:
1476 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1477 info, gimple_omp_body_ptr (stmt));
1482 gbind *bind_stmt = as_a <gbind *> (stmt);
1483 if (!optimize && gimple_bind_block (bind_stmt))
1484 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1486 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1487 if (TREE_CODE (var) == NAMELIST_DECL)
1489 /* Adjust decls mentioned in NAMELIST_DECL. */
1490 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1494 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1497 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1499 if (decl_function_context (decl) != info->context)
1500 CONSTRUCTOR_ELT (decls, i)->value
1501 = get_nonlocal_debug_decl (info, decl);
1505 *handled_ops_p = false;
1509 wi->val_only = true;
1511 *handled_ops_p = false;
1515 /* For every other statement that we are not interested in
1516 handling here, let the walker traverse the operands. */
1517 *handled_ops_p = false;
1521 /* We have handled all of STMT operands, no need to traverse the operands. */
1522 *handled_ops_p = true;
1527 /* A subroutine of convert_local_reference. Create a local variable
1528 in the parent function with DECL_VALUE_EXPR set to reference the
1529 field in FRAME. This is used both for debug info and in OMP
1533 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1537 tree *slot = &info->var_map->get_or_insert (decl);
1541 /* Make sure frame_decl gets created. */
1542 (void) get_frame_type (info);
1543 x = info->frame_decl;
1544 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1546 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1547 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1548 DECL_CONTEXT (new_decl) = info->context;
1549 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1550 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1551 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1552 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1553 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1554 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1555 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1556 if ((TREE_CODE (decl) == PARM_DECL
1557 || TREE_CODE (decl) == RESULT_DECL
1559 && DECL_BY_REFERENCE (decl))
1560 DECL_BY_REFERENCE (new_decl) = 1;
1562 SET_DECL_VALUE_EXPR (new_decl, x);
1563 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1566 DECL_CHAIN (new_decl) = info->debug_var_chain;
1567 info->debug_var_chain = new_decl;
1569 /* Do not emit debug info twice. */
1570 DECL_IGNORED_P (decl) = 1;
1576 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1577 and PARM_DECLs that were referenced by inner nested functions.
1578 The rewrite will be a structure reference to the local frame variable. */
1580 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1583 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1585 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1586 struct nesting_info *const info = (struct nesting_info *) wi->info;
1587 tree t = *tp, field, x;
1591 switch (TREE_CODE (t))
1594 /* Non-automatic variables are never processed. */
1595 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1600 if (decl_function_context (t) == info->context)
1602 /* If we copied a pointer to the frame, then the original decl
1603 is used unchanged in the parent function. */
1604 if (use_pointer_in_frame (t))
1607 /* No need to transform anything if no child references the
1609 field = lookup_field_for_decl (info, t, NO_INSERT);
1614 x = get_local_debug_decl (info, t, field);
1615 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1616 x = get_frame_field (info, info->context, field, &wi->gsi);
1621 x = save_tmp_var (info, x, &wi->gsi);
1623 x = init_tmp_var (info, x, &wi->gsi);
1631 save_val_only = wi->val_only;
1632 wi->val_only = false;
1634 wi->changed = false;
1635 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1636 wi->val_only = save_val_only;
1638 /* If we converted anything ... */
1643 /* Then the frame decl is now addressable. */
1644 TREE_ADDRESSABLE (info->frame_decl) = 1;
1646 save_context = current_function_decl;
1647 current_function_decl = info->context;
1648 recompute_tree_invariant_for_addr_expr (t);
1649 current_function_decl = save_context;
1651 /* If we are in a context where we only accept values, then
1652 compute the address into a temporary. */
1654 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1663 case ARRAY_RANGE_REF:
1665 /* Go down this entire nest and just look at the final prefix and
1666 anything that describes the references. Otherwise, we lose track
1667 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1668 save_val_only = wi->val_only;
1669 wi->val_only = true;
1671 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1673 if (TREE_CODE (t) == COMPONENT_REF)
1674 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1676 else if (TREE_CODE (t) == ARRAY_REF
1677 || TREE_CODE (t) == ARRAY_RANGE_REF)
1679 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1681 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1683 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1687 wi->val_only = false;
1688 walk_tree (tp, convert_local_reference_op, wi, NULL);
1689 wi->val_only = save_val_only;
1693 save_val_only = wi->val_only;
1694 wi->val_only = true;
1696 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1698 /* We need to re-fold the MEM_REF as component references as
1699 part of a ADDR_EXPR address are not allowed. But we cannot
1700 fold here, as the chain record type is not yet finalized. */
1701 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1702 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1703 info->mem_refs->add (tp);
1704 wi->val_only = save_val_only;
1707 case VIEW_CONVERT_EXPR:
1708 /* Just request to look at the subtrees, leaving val_only and lhs
1709 untouched. This might actually be for !val_only + lhs, in which
1710 case we don't want to force a replacement by a temporary. */
1715 if (!IS_TYPE_OR_DECL_P (t))
1718 wi->val_only = true;
1727 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1728 struct walk_stmt_info *);
1730 /* Helper for convert_local_reference. Convert all the references in
1731 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1734 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1736 struct nesting_info *const info = (struct nesting_info *) wi->info;
1737 bool need_frame = false, need_stmts = false;
1740 bitmap new_suppress;
1742 new_suppress = BITMAP_GGC_ALLOC ();
1743 bitmap_copy (new_suppress, info->suppress_expansion);
1745 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1747 switch (OMP_CLAUSE_CODE (clause))
1749 case OMP_CLAUSE_REDUCTION:
1750 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1752 goto do_decl_clause;
1754 case OMP_CLAUSE_LASTPRIVATE:
1755 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1757 goto do_decl_clause;
1759 case OMP_CLAUSE_LINEAR:
1760 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1762 wi->val_only = true;
1764 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1766 goto do_decl_clause;
1768 case OMP_CLAUSE_PRIVATE:
1769 case OMP_CLAUSE_FIRSTPRIVATE:
1770 case OMP_CLAUSE_COPYPRIVATE:
1771 case OMP_CLAUSE_SHARED:
1772 case OMP_CLAUSE_TO_DECLARE:
1773 case OMP_CLAUSE_LINK:
1774 case OMP_CLAUSE_USE_DEVICE_PTR:
1775 case OMP_CLAUSE_IS_DEVICE_PTR:
1777 decl = OMP_CLAUSE_DECL (clause);
1779 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1781 if (decl_function_context (decl) == info->context
1782 && !use_pointer_in_frame (decl))
1784 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1787 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1788 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1789 bitmap_set_bit (new_suppress, DECL_UID (decl));
1790 OMP_CLAUSE_DECL (clause)
1791 = get_local_debug_decl (info, decl, field);
1797 case OMP_CLAUSE_SCHEDULE:
1798 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1801 case OMP_CLAUSE_FINAL:
1803 case OMP_CLAUSE_NUM_THREADS:
1804 case OMP_CLAUSE_DEPEND:
1805 case OMP_CLAUSE_DEVICE:
1806 case OMP_CLAUSE_NUM_TEAMS:
1807 case OMP_CLAUSE_THREAD_LIMIT:
1808 case OMP_CLAUSE_SAFELEN:
1809 case OMP_CLAUSE_SIMDLEN:
1810 case OMP_CLAUSE_PRIORITY:
1811 case OMP_CLAUSE_GRAINSIZE:
1812 case OMP_CLAUSE_NUM_TASKS:
1813 case OMP_CLAUSE_HINT:
1814 case OMP_CLAUSE__CILK_FOR_COUNT_:
1815 case OMP_CLAUSE_NUM_GANGS:
1816 case OMP_CLAUSE_NUM_WORKERS:
1817 case OMP_CLAUSE_VECTOR_LENGTH:
1818 case OMP_CLAUSE_GANG:
1819 case OMP_CLAUSE_WORKER:
1820 case OMP_CLAUSE_VECTOR:
1821 case OMP_CLAUSE_ASYNC:
1822 case OMP_CLAUSE_WAIT:
1823 /* Several OpenACC clauses have optional arguments. Check if they
1825 if (OMP_CLAUSE_OPERAND (clause, 0))
1827 wi->val_only = true;
1829 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1833 /* The gang clause accepts two arguments. */
1834 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1835 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1837 wi->val_only = true;
1839 convert_nonlocal_reference_op
1840 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1844 case OMP_CLAUSE_DIST_SCHEDULE:
1845 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1847 wi->val_only = true;
1849 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1854 case OMP_CLAUSE_MAP:
1856 case OMP_CLAUSE_FROM:
1857 if (OMP_CLAUSE_SIZE (clause))
1859 wi->val_only = true;
1861 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1864 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1865 goto do_decl_clause;
1866 wi->val_only = true;
1868 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1872 case OMP_CLAUSE_ALIGNED:
1873 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1875 wi->val_only = true;
1877 convert_local_reference_op
1878 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1880 /* Like do_decl_clause, but don't add any suppression. */
1881 decl = OMP_CLAUSE_DECL (clause);
1883 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1885 if (decl_function_context (decl) == info->context
1886 && !use_pointer_in_frame (decl))
1888 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1891 OMP_CLAUSE_DECL (clause)
1892 = get_local_debug_decl (info, decl, field);
1898 case OMP_CLAUSE_NOWAIT:
1899 case OMP_CLAUSE_ORDERED:
1900 case OMP_CLAUSE_DEFAULT:
1901 case OMP_CLAUSE_COPYIN:
1902 case OMP_CLAUSE_COLLAPSE:
1903 case OMP_CLAUSE_UNTIED:
1904 case OMP_CLAUSE_MERGEABLE:
1905 case OMP_CLAUSE_PROC_BIND:
1906 case OMP_CLAUSE_NOGROUP:
1907 case OMP_CLAUSE_THREADS:
1908 case OMP_CLAUSE_SIMD:
1909 case OMP_CLAUSE_DEFAULTMAP:
1910 case OMP_CLAUSE_SEQ:
1911 case OMP_CLAUSE_INDEPENDENT:
1912 case OMP_CLAUSE_AUTO:
1915 /* OpenACC tile clauses are discarded during gimplification. */
1916 case OMP_CLAUSE_TILE:
1917 /* The following clause belongs to the OpenACC cache directive, which
1918 is discarded during gimplification. */
1919 case OMP_CLAUSE__CACHE_:
1920 /* The following clauses are only allowed in the OpenMP declare simd
1921 directive, so not seen here. */
1922 case OMP_CLAUSE_UNIFORM:
1923 case OMP_CLAUSE_INBRANCH:
1924 case OMP_CLAUSE_NOTINBRANCH:
1925 /* The following clauses are only allowed on OpenMP cancel and
1926 cancellation point directives, which at this point have already
1927 been lowered into a function call. */
1928 case OMP_CLAUSE_FOR:
1929 case OMP_CLAUSE_PARALLEL:
1930 case OMP_CLAUSE_SECTIONS:
1931 case OMP_CLAUSE_TASKGROUP:
1932 /* The following clauses are only added during OMP lowering; nested
1933 function decomposition happens before that. */
1934 case OMP_CLAUSE__LOOPTEMP_:
1935 case OMP_CLAUSE__SIMDUID_:
1936 case OMP_CLAUSE__GRIDDIM_:
1937 /* Anything else. */
1943 info->suppress_expansion = new_suppress;
1946 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1947 switch (OMP_CLAUSE_CODE (clause))
1949 case OMP_CLAUSE_REDUCTION:
1950 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1953 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1954 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1956 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1957 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1959 walk_body (convert_local_reference_stmt,
1960 convert_local_reference_op, info,
1961 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1962 walk_body (convert_local_reference_stmt,
1963 convert_local_reference_op, info,
1964 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1965 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1967 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1968 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1973 case OMP_CLAUSE_LASTPRIVATE:
1974 walk_body (convert_local_reference_stmt,
1975 convert_local_reference_op, info,
1976 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1979 case OMP_CLAUSE_LINEAR:
1980 walk_body (convert_local_reference_stmt,
1981 convert_local_reference_op, info,
1982 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1993 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1994 and PARM_DECLs that were referenced by inner nested functions.
1995 The rewrite will be a structure reference to the local frame variable. */
1998 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1999 struct walk_stmt_info *wi)
2001 struct nesting_info *info = (struct nesting_info *) wi->info;
2002 tree save_local_var_chain;
2003 bitmap save_suppress;
2004 gimple *stmt = gsi_stmt (*gsi);
2006 switch (gimple_code (stmt))
2008 case GIMPLE_OMP_PARALLEL:
2009 case GIMPLE_OMP_TASK:
2010 save_suppress = info->suppress_expansion;
2011 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2015 (void) get_frame_type (info);
2016 c = build_omp_clause (gimple_location (stmt),
2018 OMP_CLAUSE_DECL (c) = info->frame_decl;
2019 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2020 gimple_omp_taskreg_set_clauses (stmt, c);
2023 save_local_var_chain = info->new_local_var_chain;
2024 info->new_local_var_chain = NULL;
2026 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2027 gimple_omp_body_ptr (stmt));
2029 if (info->new_local_var_chain)
2030 declare_vars (info->new_local_var_chain,
2031 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2032 info->new_local_var_chain = save_local_var_chain;
2033 info->suppress_expansion = save_suppress;
2036 case GIMPLE_OMP_FOR:
2037 save_suppress = info->suppress_expansion;
2038 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2039 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2040 convert_local_reference_stmt,
2041 convert_local_reference_op, info);
2042 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2043 info, gimple_omp_body_ptr (stmt));
2044 info->suppress_expansion = save_suppress;
2047 case GIMPLE_OMP_SECTIONS:
2048 save_suppress = info->suppress_expansion;
2049 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2050 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2051 info, gimple_omp_body_ptr (stmt));
2052 info->suppress_expansion = save_suppress;
2055 case GIMPLE_OMP_SINGLE:
2056 save_suppress = info->suppress_expansion;
2057 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2058 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2059 info, gimple_omp_body_ptr (stmt));
2060 info->suppress_expansion = save_suppress;
2063 case GIMPLE_OMP_TARGET:
2064 if (!is_gimple_omp_offloaded (stmt))
2066 save_suppress = info->suppress_expansion;
2067 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2068 info->suppress_expansion = save_suppress;
2069 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2070 info, gimple_omp_body_ptr (stmt));
2073 save_suppress = info->suppress_expansion;
2074 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2077 (void) get_frame_type (info);
2078 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2079 OMP_CLAUSE_DECL (c) = info->frame_decl;
2080 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2081 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2082 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2083 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2086 save_local_var_chain = info->new_local_var_chain;
2087 info->new_local_var_chain = NULL;
2089 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2090 gimple_omp_body_ptr (stmt));
2092 if (info->new_local_var_chain)
2093 declare_vars (info->new_local_var_chain,
2094 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2095 info->new_local_var_chain = save_local_var_chain;
2096 info->suppress_expansion = save_suppress;
2099 case GIMPLE_OMP_TEAMS:
2100 save_suppress = info->suppress_expansion;
2101 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2102 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2103 info, gimple_omp_body_ptr (stmt));
2104 info->suppress_expansion = save_suppress;
2107 case GIMPLE_OMP_SECTION:
2108 case GIMPLE_OMP_MASTER:
2109 case GIMPLE_OMP_TASKGROUP:
2110 case GIMPLE_OMP_ORDERED:
2111 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2112 info, gimple_omp_body_ptr (stmt));
2116 wi->val_only = true;
2118 *handled_ops_p = false;
2122 if (gimple_clobber_p (stmt))
2124 tree lhs = gimple_assign_lhs (stmt);
2125 if (!use_pointer_in_frame (lhs)
2126 && lookup_field_for_decl (info, lhs, NO_INSERT))
2128 gsi_replace (gsi, gimple_build_nop (), true);
2132 *handled_ops_p = false;
2136 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2138 var = DECL_CHAIN (var))
2139 if (TREE_CODE (var) == NAMELIST_DECL)
2141 /* Adjust decls mentioned in NAMELIST_DECL. */
2142 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2146 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2149 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2151 if (decl_function_context (decl) == info->context
2152 && !use_pointer_in_frame (decl))
2154 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2157 CONSTRUCTOR_ELT (decls, i)->value
2158 = get_local_debug_decl (info, decl, field);
2164 *handled_ops_p = false;
2168 /* For every other statement that we are not interested in
2169 handling here, let the walker traverse the operands. */
2170 *handled_ops_p = false;
2174 /* Indicate that we have handled all the operands ourselves. */
2175 *handled_ops_p = true;
2180 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2181 that reference labels from outer functions. The rewrite will be a
2182 call to __builtin_nonlocal_goto. */
2185 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2186 struct walk_stmt_info *wi)
2188 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2189 tree label, new_label, target_context, x, field;
2191 gimple *stmt = gsi_stmt (*gsi);
2193 if (gimple_code (stmt) != GIMPLE_GOTO)
2195 *handled_ops_p = false;
2199 label = gimple_goto_dest (stmt);
2200 if (TREE_CODE (label) != LABEL_DECL)
2202 *handled_ops_p = false;
2206 target_context = decl_function_context (label);
2207 if (target_context == info->context)
2209 *handled_ops_p = false;
2213 for (i = info->outer; target_context != i->context; i = i->outer)
2216 /* The original user label may also be use for a normal goto, therefore
2217 we must create a new label that will actually receive the abnormal
2218 control transfer. This new label will be marked LABEL_NONLOCAL; this
2219 mark will trigger proper behavior in the cfg, as well as cause the
2220 (hairy target-specific) non-local goto receiver code to be generated
2221 when we expand rtl. Enter this association into var_map so that we
2222 can insert the new label into the IL during a second pass. */
2223 tree *slot = &i->var_map->get_or_insert (label);
2226 new_label = create_artificial_label (UNKNOWN_LOCATION);
2227 DECL_NONLOCAL (new_label) = 1;
2233 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2234 field = get_nl_goto_field (i);
2235 x = get_frame_field (info, target_context, field, gsi);
2237 x = gsi_gimplify_val (info, x, gsi);
2238 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2239 2, build_addr (new_label), x);
2240 gsi_replace (gsi, call, false);
2242 /* We have handled all of STMT's operands, no need to keep going. */
2243 *handled_ops_p = true;
2248 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2249 are referenced via nonlocal goto from a nested function. The rewrite
2250 will involve installing a newly generated DECL_NONLOCAL label, and
2251 (potentially) a branch around the rtl gunk that is assumed to be
2252 attached to such a label. */
2255 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2256 struct walk_stmt_info *wi)
2258 struct nesting_info *const info = (struct nesting_info *) wi->info;
2259 tree label, new_label;
2260 gimple_stmt_iterator tmp_gsi;
2261 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2265 *handled_ops_p = false;
2269 label = gimple_label_label (stmt);
2271 tree *slot = info->var_map->get (label);
2274 *handled_ops_p = false;
2278 /* If there's any possibility that the previous statement falls through,
2279 then we must branch around the new non-local label. */
2281 gsi_prev (&tmp_gsi);
2282 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2284 gimple *stmt = gimple_build_goto (label);
2285 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2288 new_label = (tree) *slot;
2289 stmt = gimple_build_label (new_label);
2290 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2292 *handled_ops_p = true;
2297 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2298 of nested functions that require the use of trampolines. The rewrite
2299 will involve a reference a trampoline generated for the occasion. */
2302 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2304 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2305 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2306 tree t = *tp, decl, target_context, x, builtin;
2310 switch (TREE_CODE (t))
2314 T.1 = &CHAIN->tramp;
2315 T.2 = __builtin_adjust_trampoline (T.1);
2316 T.3 = (func_type)T.2;
2319 decl = TREE_OPERAND (t, 0);
2320 if (TREE_CODE (decl) != FUNCTION_DECL)
2323 /* Only need to process nested functions. */
2324 target_context = decl_function_context (decl);
2325 if (!target_context)
2328 /* If the nested function doesn't use a static chain, then
2329 it doesn't need a trampoline. */
2330 if (!DECL_STATIC_CHAIN (decl))
2333 /* If we don't want a trampoline, then don't build one. */
2334 if (TREE_NO_TRAMPOLINE (t))
2337 /* Lookup the immediate parent of the callee, as that's where
2338 we need to insert the trampoline. */
2339 for (i = info; i->context != target_context; i = i->outer)
2341 x = lookup_tramp_for_decl (i, decl, INSERT);
2343 /* Compute the address of the field holding the trampoline. */
2344 x = get_frame_field (info, target_context, x, &wi->gsi);
2346 x = gsi_gimplify_val (info, x, &wi->gsi);
2348 /* Do machine-specific ugliness. Normally this will involve
2349 computing extra alignment, but it can really be anything. */
2350 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2351 call = gimple_build_call (builtin, 1, x);
2352 x = init_tmp_var_with_call (info, &wi->gsi, call);
2354 /* Cast back to the proper function type. */
2355 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2356 x = init_tmp_var (info, x, &wi->gsi);
2362 if (!IS_TYPE_OR_DECL_P (t))
2371 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2372 to addresses of nested functions that require the use of
2373 trampolines. The rewrite will involve a reference a trampoline
2374 generated for the occasion. */
2377 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2378 struct walk_stmt_info *wi)
2380 struct nesting_info *info = (struct nesting_info *) wi->info;
2381 gimple *stmt = gsi_stmt (*gsi);
2383 switch (gimple_code (stmt))
2387 /* Only walk call arguments, lest we generate trampolines for
2389 unsigned long i, nargs = gimple_call_num_args (stmt);
2390 for (i = 0; i < nargs; i++)
2391 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2396 case GIMPLE_OMP_TARGET:
2397 if (!is_gimple_omp_offloaded (stmt))
2399 *handled_ops_p = false;
2403 case GIMPLE_OMP_PARALLEL:
2404 case GIMPLE_OMP_TASK:
2406 tree save_local_var_chain = info->new_local_var_chain;
2407 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2408 info->new_local_var_chain = NULL;
2409 char save_static_chain_added = info->static_chain_added;
2410 info->static_chain_added = 0;
2411 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2412 info, gimple_omp_body_ptr (stmt));
2413 if (info->new_local_var_chain)
2414 declare_vars (info->new_local_var_chain,
2415 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2417 for (int i = 0; i < 2; i++)
2420 if ((info->static_chain_added & (1 << i)) == 0)
2422 decl = i ? get_chain_decl (info) : info->frame_decl;
2423 /* Don't add CHAIN.* or FRAME.* twice. */
2424 for (c = gimple_omp_taskreg_clauses (stmt);
2426 c = OMP_CLAUSE_CHAIN (c))
2427 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2428 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2429 && OMP_CLAUSE_DECL (c) == decl)
2431 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2433 c = build_omp_clause (gimple_location (stmt),
2434 i ? OMP_CLAUSE_FIRSTPRIVATE
2435 : OMP_CLAUSE_SHARED);
2436 OMP_CLAUSE_DECL (c) = decl;
2437 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2438 gimple_omp_taskreg_set_clauses (stmt, c);
2442 c = build_omp_clause (gimple_location (stmt),
2444 OMP_CLAUSE_DECL (c) = decl;
2445 OMP_CLAUSE_SET_MAP_KIND (c,
2446 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2447 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2448 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2449 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2453 info->new_local_var_chain = save_local_var_chain;
2454 info->static_chain_added |= save_static_chain_added;
2459 *handled_ops_p = false;
2463 *handled_ops_p = true;
2469 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2470 that reference nested functions to make sure that the static chain
2471 is set up properly for the call. */
2474 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2475 struct walk_stmt_info *wi)
2477 struct nesting_info *const info = (struct nesting_info *) wi->info;
2478 tree decl, target_context;
2479 char save_static_chain_added;
2481 gimple *stmt = gsi_stmt (*gsi);
2483 switch (gimple_code (stmt))
2486 if (gimple_call_chain (stmt))
2488 decl = gimple_call_fndecl (stmt);
2491 target_context = decl_function_context (decl);
2492 if (target_context && DECL_STATIC_CHAIN (decl))
2494 gimple_call_set_chain (as_a <gcall *> (stmt),
2495 get_static_chain (info, target_context,
2497 info->static_chain_added |= (1 << (info->context != target_context));
2501 case GIMPLE_OMP_PARALLEL:
2502 case GIMPLE_OMP_TASK:
2503 save_static_chain_added = info->static_chain_added;
2504 info->static_chain_added = 0;
2505 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2506 for (i = 0; i < 2; i++)
2509 if ((info->static_chain_added & (1 << i)) == 0)
2511 decl = i ? get_chain_decl (info) : info->frame_decl;
2512 /* Don't add CHAIN.* or FRAME.* twice. */
2513 for (c = gimple_omp_taskreg_clauses (stmt);
2515 c = OMP_CLAUSE_CHAIN (c))
2516 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2517 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2518 && OMP_CLAUSE_DECL (c) == decl)
2522 c = build_omp_clause (gimple_location (stmt),
2523 i ? OMP_CLAUSE_FIRSTPRIVATE
2524 : OMP_CLAUSE_SHARED);
2525 OMP_CLAUSE_DECL (c) = decl;
2526 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2527 gimple_omp_taskreg_set_clauses (stmt, c);
2530 info->static_chain_added |= save_static_chain_added;
2533 case GIMPLE_OMP_TARGET:
2534 if (!is_gimple_omp_offloaded (stmt))
2536 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2539 save_static_chain_added = info->static_chain_added;
2540 info->static_chain_added = 0;
2541 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2542 for (i = 0; i < 2; i++)
2545 if ((info->static_chain_added & (1 << i)) == 0)
2547 decl = i ? get_chain_decl (info) : info->frame_decl;
2548 /* Don't add CHAIN.* or FRAME.* twice. */
2549 for (c = gimple_omp_target_clauses (stmt);
2551 c = OMP_CLAUSE_CHAIN (c))
2552 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2553 && OMP_CLAUSE_DECL (c) == decl)
2557 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2558 OMP_CLAUSE_DECL (c) = decl;
2559 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2560 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2561 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2562 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2566 info->static_chain_added |= save_static_chain_added;
2569 case GIMPLE_OMP_FOR:
2570 walk_body (convert_gimple_call, NULL, info,
2571 gimple_omp_for_pre_body_ptr (stmt));
2573 case GIMPLE_OMP_SECTIONS:
2574 case GIMPLE_OMP_SECTION:
2575 case GIMPLE_OMP_SINGLE:
2576 case GIMPLE_OMP_TEAMS:
2577 case GIMPLE_OMP_MASTER:
2578 case GIMPLE_OMP_TASKGROUP:
2579 case GIMPLE_OMP_ORDERED:
2580 case GIMPLE_OMP_CRITICAL:
2581 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2585 /* Keep looking for other operands. */
2586 *handled_ops_p = false;
2590 *handled_ops_p = true;
2594 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2595 call expressions. At the same time, determine if a nested function
2596 actually uses its static chain; if not, remember that. */
2599 convert_all_function_calls (struct nesting_info *root)
2601 unsigned int chain_count = 0, old_chain_count, iter_count;
2602 struct nesting_info *n;
2604 /* First, optimistically clear static_chain for all decls that haven't
2605 used the static chain already for variable access. But always create
2606 it if not optimizing. This makes it possible to reconstruct the static
2607 nesting tree at run time and thus to resolve up-level references from
2608 within the debugger. */
2609 FOR_EACH_NEST_INFO (n, root)
2611 tree decl = n->context;
2615 (void) get_frame_type (n);
2617 (void) get_chain_decl (n);
2619 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2621 DECL_STATIC_CHAIN (decl) = 0;
2622 if (dump_file && (dump_flags & TDF_DETAILS))
2623 fprintf (dump_file, "Guessing no static-chain for %s\n",
2624 lang_hooks.decl_printable_name (decl, 2));
2627 DECL_STATIC_CHAIN (decl) = 1;
2628 chain_count += DECL_STATIC_CHAIN (decl);
2631 /* Walk the functions and perform transformations. Note that these
2632 transformations can induce new uses of the static chain, which in turn
2633 require re-examining all users of the decl. */
2634 /* ??? It would make sense to try to use the call graph to speed this up,
2635 but the call graph hasn't really been built yet. Even if it did, we
2636 would still need to iterate in this loop since address-of references
2637 wouldn't show up in the callgraph anyway. */
2641 old_chain_count = chain_count;
2645 if (dump_file && (dump_flags & TDF_DETAILS))
2646 fputc ('\n', dump_file);
2648 FOR_EACH_NEST_INFO (n, root)
2650 tree decl = n->context;
2651 walk_function (convert_tramp_reference_stmt,
2652 convert_tramp_reference_op, n);
2653 walk_function (convert_gimple_call, NULL, n);
2654 chain_count += DECL_STATIC_CHAIN (decl);
2657 while (chain_count != old_chain_count);
2659 if (dump_file && (dump_flags & TDF_DETAILS))
2660 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2664 struct nesting_copy_body_data
2667 struct nesting_info *root;
2670 /* A helper subroutine for debug_var_chain type remapping. */
2673 nesting_copy_decl (tree decl, copy_body_data *id)
2675 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2676 tree *slot = nid->root->var_map->get (decl);
2679 return (tree) *slot;
2681 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2683 tree new_decl = copy_decl_no_change (decl, id);
2684 DECL_ORIGINAL_TYPE (new_decl)
2685 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2690 || TREE_CODE (decl) == PARM_DECL
2691 || TREE_CODE (decl) == RESULT_DECL)
2694 return copy_decl_no_change (decl, id);
2697 /* A helper function for remap_vla_decls. See if *TP contains
2698 some remapped variables. */
2701 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2703 struct nesting_info *root = (struct nesting_info *) data;
2709 tree *slot = root->var_map->get (t);
2717 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2721 remap_vla_decls (tree block, struct nesting_info *root)
2723 tree var, subblock, val, type;
2724 struct nesting_copy_body_data id;
2726 for (subblock = BLOCK_SUBBLOCKS (block);
2728 subblock = BLOCK_CHAIN (subblock))
2729 remap_vla_decls (subblock, root);
2731 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2732 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2734 val = DECL_VALUE_EXPR (var);
2735 type = TREE_TYPE (var);
2737 if (!(TREE_CODE (val) == INDIRECT_REF
2738 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2739 && variably_modified_type_p (type, NULL)))
2742 if (root->var_map->get (TREE_OPERAND (val, 0))
2743 || walk_tree (&type, contains_remapped_vars, root, NULL))
2747 if (var == NULL_TREE)
2750 memset (&id, 0, sizeof (id));
2751 id.cb.copy_decl = nesting_copy_decl;
2752 id.cb.decl_map = new hash_map<tree, tree>;
2755 for (; var; var = DECL_CHAIN (var))
2756 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2758 struct nesting_info *i;
2761 val = DECL_VALUE_EXPR (var);
2762 type = TREE_TYPE (var);
2764 if (!(TREE_CODE (val) == INDIRECT_REF
2765 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2766 && variably_modified_type_p (type, NULL)))
2769 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2770 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2773 context = decl_function_context (var);
2774 for (i = root; i; i = i->outer)
2775 if (i->context == context)
2781 /* Fully expand value expressions. This avoids having debug variables
2782 only referenced from them and that can be swept during GC. */
2785 tree t = (tree) *slot;
2786 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2787 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2790 id.cb.src_fn = i->context;
2791 id.cb.dst_fn = i->context;
2792 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2794 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2795 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2797 newt = TREE_TYPE (newt);
2798 type = TREE_TYPE (type);
2800 if (TYPE_NAME (newt)
2801 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2802 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2804 && TYPE_NAME (newt) == TYPE_NAME (type))
2805 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2807 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2808 if (val != DECL_VALUE_EXPR (var))
2809 SET_DECL_VALUE_EXPR (var, val);
2812 delete id.cb.decl_map;
2815 /* Fold the MEM_REF *E. */
2817 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2819 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2820 *ref_p = fold (*ref_p);
2824 /* Do "everything else" to clean up or complete state collected by the various
2825 walking passes -- create a field to hold the frame base address, lay out the
2826 types and decls, generate code to initialize the frame decl, store critical
2827 expressions in the struct function for rtl to find. */
2830 finalize_nesting_tree_1 (struct nesting_info *root)
2832 gimple_seq stmt_list;
2834 tree context = root->context;
2835 struct function *sf;
2839 /* If we created a non-local frame type or decl, we need to lay them
2840 out at this time. */
2841 if (root->frame_type)
2843 /* Debugging information needs to compute the frame base address of the
2844 parent frame out of the static chain from the nested frame.
2846 The static chain is the address of the FRAME record, so one could
2847 imagine it would be possible to compute the frame base address just
2848 adding a constant offset to this address. Unfortunately, this is not
2849 possible: if the FRAME object has alignment constraints that are
2850 stronger than the stack, then the offset between the frame base and
2851 the FRAME object will be dynamic.
2853 What we do instead is to append a field to the FRAME object that holds
2854 the frame base address: then debug info just has to fetch this
2857 /* Debugging information will refer to the CFA as the frame base
2858 address: we will do the same here. */
2859 const tree frame_addr_fndecl
2860 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
2862 /* Create a field in the FRAME record to hold the frame base address for
2863 this stack frame. Since it will be used only by the debugger, put it
2864 at the end of the record in order not to shift all other offsets. */
2865 tree fb_decl = make_node (FIELD_DECL);
2867 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
2868 TREE_TYPE (fb_decl) = ptr_type_node;
2869 TREE_ADDRESSABLE (fb_decl) = 1;
2870 DECL_CONTEXT (fb_decl) = root->frame_type;
2871 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
2874 /* In some cases the frame type will trigger the -Wpadded warning.
2875 This is not helpful; suppress it. */
2876 int save_warn_padded = warn_padded;
2878 layout_type (root->frame_type);
2879 warn_padded = save_warn_padded;
2880 layout_decl (root->frame_decl, 0);
2882 /* Initialize the frame base address field. If the builtin we need is
2883 not available, set it to NULL so that debugging information does not
2885 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
2886 root->frame_decl, fb_decl, NULL_TREE);
2889 if (frame_addr_fndecl != NULL_TREE)
2891 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
2893 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2895 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
2898 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
2899 gimple_seq_add_stmt (&stmt_list,
2900 gimple_build_assign (fb_ref, fb_tmp));
2902 /* Remove root->frame_decl from root->new_local_var_chain, so
2903 that we can declare it also in the lexical blocks, which
2904 helps ensure virtual regs that end up appearing in its RTL
2905 expression get substituted in instantiate_virtual_regs(). */
2907 for (adjust = &root->new_local_var_chain;
2908 *adjust != root->frame_decl;
2909 adjust = &DECL_CHAIN (*adjust))
2910 gcc_assert (DECL_CHAIN (*adjust));
2911 *adjust = DECL_CHAIN (*adjust);
2913 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2914 declare_vars (root->frame_decl,
2915 gimple_seq_first_stmt (gimple_body (context)), true);
2918 /* If any parameters were referenced non-locally, then we need to
2919 insert a copy. Likewise, if any variables were referenced by
2920 pointer, we need to initialize the address. */
2921 if (root->any_parm_remapped)
2924 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2928 field = lookup_field_for_decl (root, p, NO_INSERT);
2932 if (use_pointer_in_frame (p))
2937 /* If the assignment is from a non-register the stmt is
2938 not valid gimple. Make it so by using a temporary instead. */
2939 if (!is_gimple_reg (x)
2940 && is_gimple_reg_type (TREE_TYPE (x)))
2942 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2943 x = init_tmp_var (root, x, &gsi);
2946 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2947 root->frame_decl, field, NULL_TREE);
2948 stmt = gimple_build_assign (y, x);
2949 gimple_seq_add_stmt (&stmt_list, stmt);
2953 /* If a chain_field was created, then it needs to be initialized
2955 if (root->chain_field)
2957 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2958 root->frame_decl, root->chain_field, NULL_TREE);
2959 stmt = gimple_build_assign (x, get_chain_decl (root));
2960 gimple_seq_add_stmt (&stmt_list, stmt);
2963 /* If trampolines were created, then we need to initialize them. */
2964 if (root->any_tramp_created)
2966 struct nesting_info *i;
2967 for (i = root->inner; i ; i = i->next)
2969 tree arg1, arg2, arg3, x, field;
2971 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2975 gcc_assert (DECL_STATIC_CHAIN (i->context));
2976 arg3 = build_addr (root->frame_decl);
2978 arg2 = build_addr (i->context);
2980 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2981 root->frame_decl, field, NULL_TREE);
2982 arg1 = build_addr (x);
2984 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2985 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2986 gimple_seq_add_stmt (&stmt_list, stmt);
2990 /* If we created initialization statements, insert them. */
2994 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2995 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2996 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2997 gimple_bind_set_body (bind, stmt_list);
3000 /* If a chain_decl was created, then it needs to be registered with
3001 struct function so that it gets initialized from the static chain
3002 register at the beginning of the function. */
3003 sf = DECL_STRUCT_FUNCTION (root->context);
3004 sf->static_chain_decl = root->chain_decl;
3006 /* Similarly for the non-local goto save area. */
3007 if (root->nl_goto_field)
3009 sf->nonlocal_goto_save_area
3010 = get_frame_field (root, context, root->nl_goto_field, NULL);
3011 sf->has_nonlocal_label = 1;
3014 /* Make sure all new local variables get inserted into the
3015 proper BIND_EXPR. */
3016 if (root->new_local_var_chain)
3017 declare_vars (root->new_local_var_chain,
3018 gimple_seq_first_stmt (gimple_body (root->context)),
3021 if (root->debug_var_chain)
3026 remap_vla_decls (DECL_INITIAL (root->context), root);
3028 for (debug_var = root->debug_var_chain; debug_var;
3029 debug_var = DECL_CHAIN (debug_var))
3030 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3033 /* If there are any debug decls with variable length types,
3034 remap those types using other debug_var_chain variables. */
3037 struct nesting_copy_body_data id;
3039 memset (&id, 0, sizeof (id));
3040 id.cb.copy_decl = nesting_copy_decl;
3041 id.cb.decl_map = new hash_map<tree, tree>;
3044 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3045 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3047 tree type = TREE_TYPE (debug_var);
3048 tree newt, t = type;
3049 struct nesting_info *i;
3051 for (i = root; i; i = i->outer)
3052 if (variably_modified_type_p (type, i->context))
3058 id.cb.src_fn = i->context;
3059 id.cb.dst_fn = i->context;
3060 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3062 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3063 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3065 newt = TREE_TYPE (newt);
3068 if (TYPE_NAME (newt)
3069 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3070 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3072 && TYPE_NAME (newt) == TYPE_NAME (t))
3073 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3076 delete id.cb.decl_map;
3079 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3080 if (gimple_bind_block (scope))
3081 declare_vars (root->debug_var_chain, scope, true);
3083 BLOCK_VARS (DECL_INITIAL (root->context))
3084 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3085 root->debug_var_chain);
3088 /* Fold the rewritten MEM_REF trees. */
3089 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3091 /* Dump the translated tree function. */
3094 fputs ("\n\n", dump_file);
3095 dump_function_to_file (root->context, dump_file, dump_flags);
3100 finalize_nesting_tree (struct nesting_info *root)
3102 struct nesting_info *n;
3103 FOR_EACH_NEST_INFO (n, root)
3104 finalize_nesting_tree_1 (n);
3107 /* Unnest the nodes and pass them to cgraph. */
3110 unnest_nesting_tree_1 (struct nesting_info *root)
3112 struct cgraph_node *node = cgraph_node::get (root->context);
3114 /* For nested functions update the cgraph to reflect unnesting.
3115 We also delay finalizing of these functions up to this point. */
3119 cgraph_node::finalize_function (root->context, true);
3124 unnest_nesting_tree (struct nesting_info *root)
3126 struct nesting_info *n;
3127 FOR_EACH_NEST_INFO (n, root)
3128 unnest_nesting_tree_1 (n);
3131 /* Free the data structures allocated during this pass. */
3134 free_nesting_tree (struct nesting_info *root)
3136 struct nesting_info *node, *next;
3138 node = iter_nestinfo_start (root);
3141 next = iter_nestinfo_next (node);
3142 delete node->var_map;
3143 delete node->field_map;
3144 delete node->mem_refs;
3151 /* Gimplify a function and all its nested functions. */
3153 gimplify_all_functions (struct cgraph_node *root)
3155 struct cgraph_node *iter;
3156 if (!gimple_body (root->decl))
3157 gimplify_function_tree (root->decl);
3158 for (iter = root->nested; iter; iter = iter->next_nested)
3159 gimplify_all_functions (iter);
3162 /* Main entry point for this pass. Process FNDECL and all of its nested
3163 subroutines and turn them into something less tightly bound. */
3166 lower_nested_functions (tree fndecl)
3168 struct cgraph_node *cgn;
3169 struct nesting_info *root;
3171 /* If there are no nested functions, there's nothing to do. */
3172 cgn = cgraph_node::get (fndecl);
3176 gimplify_all_functions (cgn);
3178 dump_file = dump_begin (TDI_nested, &dump_flags);
3180 fprintf (dump_file, "\n;; Function %s\n\n",
3181 lang_hooks.decl_printable_name (fndecl, 2));
3183 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3184 root = create_nesting_tree (cgn);
3186 walk_all_functions (convert_nonlocal_reference_stmt,
3187 convert_nonlocal_reference_op,
3189 walk_all_functions (convert_local_reference_stmt,
3190 convert_local_reference_op,
3192 walk_all_functions (convert_nl_goto_reference, NULL, root);
3193 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3195 convert_all_function_calls (root);
3196 finalize_nesting_tree (root);
3197 unnest_nesting_tree (root);
3199 free_nesting_tree (root);
3200 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3204 dump_end (TDI_nested, dump_file);
3209 #include "gt-tree-nested.h"