Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_9 / gcc / tree-nested.c
blob28753c16c897483335e896cc19a287dfed1267e6
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2014 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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stringpool.h"
26 #include "stor-layout.h"
27 #include "tm_p.h"
28 #include "function.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "pointer-set.h"
32 #include "basic-block.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "cgraph.h"
44 #include "tree-cfg.h"
45 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
46 #include "langhooks.h"
47 #include "gimple-low.h"
50 /* The object of this pass is to lower the representation of a set of nested
51 functions in order to expose all of the gory details of the various
52 nonlocal references. We want to do this sooner rather than later, in
53 order to give us more freedom in emitting all of the functions in question.
55 Back in olden times, when gcc was young, we developed an insanely
56 complicated scheme whereby variables which were referenced nonlocally
57 were forced to live in the stack of the declaring function, and then
58 the nested functions magically discovered where these variables were
59 placed. In order for this scheme to function properly, it required
60 that the outer function be partially expanded, then we switch to
61 compiling the inner function, and once done with those we switch back
62 to compiling the outer function. Such delicate ordering requirements
63 makes it difficult to do whole translation unit optimizations
64 involving such functions.
66 The implementation here is much more direct. Everything that can be
67 referenced by an inner function is a member of an explicitly created
68 structure herein called the "nonlocal frame struct". The incoming
69 static chain for a nested function is a pointer to this struct in
70 the parent. In this way, we settle on known offsets from a known
71 base, and so are decoupled from the logic that places objects in the
72 function's stack frame. More importantly, we don't have to wait for
73 that to happen -- since the compilation of the inner function is no
74 longer tied to a real stack frame, the nonlocal frame struct can be
75 allocated anywhere. Which means that the outer function is now
76 inlinable.
78 Theory of operation here is very simple. Iterate over all the
79 statements in all the functions (depth first) several times,
80 allocating structures and fields on demand. In general we want to
81 examine inner functions first, so that we can avoid making changes
82 to outer functions which are unnecessary.
84 The order of the passes matters a bit, in that later passes will be
85 skipped if it is discovered that the functions don't actually interact
86 at all. That is, they're nested in the lexical sense but could have
87 been written as independent functions without change. */
90 struct nesting_info
92 struct nesting_info *outer;
93 struct nesting_info *inner;
94 struct nesting_info *next;
96 struct pointer_map_t *field_map;
97 struct pointer_map_t *var_map;
98 struct pointer_set_t *mem_refs;
99 bitmap suppress_expansion;
101 tree context;
102 tree new_local_var_chain;
103 tree debug_var_chain;
104 tree frame_type;
105 tree frame_decl;
106 tree chain_field;
107 tree chain_decl;
108 tree nl_goto_field;
110 bool any_parm_remapped;
111 bool any_tramp_created;
112 char static_chain_added;
116 /* Iterate over the nesting tree, starting with ROOT, depth first. */
118 static inline struct nesting_info *
119 iter_nestinfo_start (struct nesting_info *root)
121 while (root->inner)
122 root = root->inner;
123 return root;
126 static inline struct nesting_info *
127 iter_nestinfo_next (struct nesting_info *node)
129 if (node->next)
130 return iter_nestinfo_start (node->next);
131 return node->outer;
134 #define FOR_EACH_NEST_INFO(I, ROOT) \
135 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
137 /* Obstack used for the bitmaps in the struct above. */
138 static struct bitmap_obstack nesting_info_bitmap_obstack;
141 /* We're working in so many different function contexts simultaneously,
142 that create_tmp_var is dangerous. Prevent mishap. */
143 #define create_tmp_var cant_use_create_tmp_var_here_dummy
145 /* Like create_tmp_var, except record the variable for registration at
146 the given nesting level. */
148 static tree
149 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
151 tree tmp_var;
153 /* If the type is of variable size or a type which must be created by the
154 frontend, something is wrong. Note that we explicitly allow
155 incomplete types here, since we create them ourselves here. */
156 gcc_assert (!TREE_ADDRESSABLE (type));
157 gcc_assert (!TYPE_SIZE_UNIT (type)
158 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
160 tmp_var = create_tmp_var_raw (type, prefix);
161 DECL_CONTEXT (tmp_var) = info->context;
162 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
163 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
164 if (TREE_CODE (type) == COMPLEX_TYPE
165 || TREE_CODE (type) == VECTOR_TYPE)
166 DECL_GIMPLE_REG_P (tmp_var) = 1;
168 info->new_local_var_chain = tmp_var;
170 return tmp_var;
173 /* Take the address of EXP to be used within function CONTEXT.
174 Mark it for addressability as necessary. */
176 tree
177 build_addr (tree exp, tree context)
179 tree base = exp;
180 tree save_context;
181 tree retval;
183 while (handled_component_p (base))
184 base = TREE_OPERAND (base, 0);
186 if (DECL_P (base))
187 TREE_ADDRESSABLE (base) = 1;
189 /* Building the ADDR_EXPR will compute a set of properties for
190 that ADDR_EXPR. Those properties are unfortunately context
191 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
193 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
194 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
195 way the properties are for the ADDR_EXPR are computed properly. */
196 save_context = current_function_decl;
197 current_function_decl = context;
198 retval = build_fold_addr_expr (exp);
199 current_function_decl = save_context;
200 return retval;
203 /* Insert FIELD into TYPE, sorted by alignment requirements. */
205 void
206 insert_field_into_struct (tree type, tree field)
208 tree *p;
210 DECL_CONTEXT (field) = type;
212 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
213 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
214 break;
216 DECL_CHAIN (field) = *p;
217 *p = field;
219 /* Set correct alignment for frame struct type. */
220 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
221 TYPE_ALIGN (type) = DECL_ALIGN (field);
224 /* Build or return the RECORD_TYPE that describes the frame state that is
225 shared between INFO->CONTEXT and its nested functions. This record will
226 not be complete until finalize_nesting_tree; up until that point we'll
227 be adding fields as necessary.
229 We also build the DECL that represents this frame in the function. */
231 static tree
232 get_frame_type (struct nesting_info *info)
234 tree type = info->frame_type;
235 if (!type)
237 char *name;
239 type = make_node (RECORD_TYPE);
241 name = concat ("FRAME.",
242 IDENTIFIER_POINTER (DECL_NAME (info->context)),
243 NULL);
244 TYPE_NAME (type) = get_identifier (name);
245 free (name);
247 info->frame_type = type;
248 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
249 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
251 /* ??? Always make it addressable for now, since it is meant to
252 be pointed to by the static chain pointer. This pessimizes
253 when it turns out that no static chains are needed because
254 the nested functions referencing non-local variables are not
255 reachable, but the true pessimization is to create the non-
256 local frame structure in the first place. */
257 TREE_ADDRESSABLE (info->frame_decl) = 1;
259 return type;
262 /* Return true if DECL should be referenced by pointer in the non-local
263 frame structure. */
265 static bool
266 use_pointer_in_frame (tree decl)
268 if (TREE_CODE (decl) == PARM_DECL)
270 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
271 sized decls, and inefficient to copy large aggregates. Don't bother
272 moving anything but scalar variables. */
273 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
275 else
277 /* Variable sized types make things "interesting" in the frame. */
278 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
282 /* Given DECL, a non-locally accessed variable, find or create a field
283 in the non-local frame structure for the given nesting context. */
285 static tree
286 lookup_field_for_decl (struct nesting_info *info, tree decl,
287 enum insert_option insert)
289 void **slot;
291 if (insert == NO_INSERT)
293 slot = pointer_map_contains (info->field_map, decl);
294 return slot ? (tree) *slot : NULL_TREE;
297 slot = pointer_map_insert (info->field_map, decl);
298 if (!*slot)
300 tree field = make_node (FIELD_DECL);
301 DECL_NAME (field) = DECL_NAME (decl);
303 if (use_pointer_in_frame (decl))
305 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
306 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
307 DECL_NONADDRESSABLE_P (field) = 1;
309 else
311 TREE_TYPE (field) = TREE_TYPE (decl);
312 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
313 DECL_ALIGN (field) = DECL_ALIGN (decl);
314 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
315 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
316 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
317 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
320 insert_field_into_struct (get_frame_type (info), field);
321 *slot = field;
323 if (TREE_CODE (decl) == PARM_DECL)
324 info->any_parm_remapped = true;
327 return (tree) *slot;
330 /* Build or return the variable that holds the static chain within
331 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
333 static tree
334 get_chain_decl (struct nesting_info *info)
336 tree decl = info->chain_decl;
338 if (!decl)
340 tree type;
342 type = get_frame_type (info->outer);
343 type = build_pointer_type (type);
345 /* Note that this variable is *not* entered into any BIND_EXPR;
346 the construction of this variable is handled specially in
347 expand_function_start and initialize_inlined_parameters.
348 Note also that it's represented as a parameter. This is more
349 close to the truth, since the initial value does come from
350 the caller. */
351 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
352 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
353 DECL_ARTIFICIAL (decl) = 1;
354 DECL_IGNORED_P (decl) = 1;
355 TREE_USED (decl) = 1;
356 DECL_CONTEXT (decl) = info->context;
357 DECL_ARG_TYPE (decl) = type;
359 /* Tell tree-inline.c that we never write to this variable, so
360 it can copy-prop the replacement value immediately. */
361 TREE_READONLY (decl) = 1;
363 info->chain_decl = decl;
365 if (dump_file
366 && (dump_flags & TDF_DETAILS)
367 && !DECL_STATIC_CHAIN (info->context))
368 fprintf (dump_file, "Setting static-chain for %s\n",
369 lang_hooks.decl_printable_name (info->context, 2));
371 DECL_STATIC_CHAIN (info->context) = 1;
373 return decl;
376 /* Build or return the field within the non-local frame state that holds
377 the static chain for INFO->CONTEXT. This is the way to walk back up
378 multiple nesting levels. */
380 static tree
381 get_chain_field (struct nesting_info *info)
383 tree field = info->chain_field;
385 if (!field)
387 tree type = build_pointer_type (get_frame_type (info->outer));
389 field = make_node (FIELD_DECL);
390 DECL_NAME (field) = get_identifier ("__chain");
391 TREE_TYPE (field) = type;
392 DECL_ALIGN (field) = TYPE_ALIGN (type);
393 DECL_NONADDRESSABLE_P (field) = 1;
395 insert_field_into_struct (get_frame_type (info), field);
397 info->chain_field = field;
399 if (dump_file
400 && (dump_flags & TDF_DETAILS)
401 && !DECL_STATIC_CHAIN (info->context))
402 fprintf (dump_file, "Setting static-chain for %s\n",
403 lang_hooks.decl_printable_name (info->context, 2));
405 DECL_STATIC_CHAIN (info->context) = 1;
407 return field;
410 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
412 static tree
413 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
414 gimple call)
416 tree t;
418 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
419 gimple_call_set_lhs (call, t);
420 if (! gsi_end_p (*gsi))
421 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
422 gsi_insert_before (gsi, call, GSI_SAME_STMT);
424 return t;
428 /* Copy EXP into a temporary. Allocate the temporary in the context of
429 INFO and insert the initialization statement before GSI. */
431 static tree
432 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
434 tree t;
435 gimple stmt;
437 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
438 stmt = gimple_build_assign (t, exp);
439 if (! gsi_end_p (*gsi))
440 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
441 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
443 return t;
447 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
449 static tree
450 gsi_gimplify_val (struct nesting_info *info, tree exp,
451 gimple_stmt_iterator *gsi)
453 if (is_gimple_val (exp))
454 return exp;
455 else
456 return init_tmp_var (info, exp, gsi);
459 /* Similarly, but copy from the temporary and insert the statement
460 after the iterator. */
462 static tree
463 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
465 tree t;
466 gimple stmt;
468 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
469 stmt = gimple_build_assign (exp, t);
470 if (! gsi_end_p (*gsi))
471 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
472 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
474 return t;
477 /* Build or return the type used to represent a nested function trampoline. */
479 static GTY(()) tree trampoline_type;
481 static tree
482 get_trampoline_type (struct nesting_info *info)
484 unsigned align, size;
485 tree t;
487 if (trampoline_type)
488 return trampoline_type;
490 align = TRAMPOLINE_ALIGNMENT;
491 size = TRAMPOLINE_SIZE;
493 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
494 then allocate extra space so that we can do dynamic alignment. */
495 if (align > STACK_BOUNDARY)
497 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
498 align = STACK_BOUNDARY;
501 t = build_index_type (size_int (size - 1));
502 t = build_array_type (char_type_node, t);
503 t = build_decl (DECL_SOURCE_LOCATION (info->context),
504 FIELD_DECL, get_identifier ("__data"), t);
505 DECL_ALIGN (t) = align;
506 DECL_USER_ALIGN (t) = 1;
508 trampoline_type = make_node (RECORD_TYPE);
509 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
510 TYPE_FIELDS (trampoline_type) = t;
511 layout_type (trampoline_type);
512 DECL_CONTEXT (t) = trampoline_type;
514 return trampoline_type;
517 /* Given DECL, a nested function, find or create a field in the non-local
518 frame structure for a trampoline for this function. */
520 static tree
521 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
522 enum insert_option insert)
524 void **slot;
526 if (insert == NO_INSERT)
528 slot = pointer_map_contains (info->var_map, decl);
529 return slot ? (tree) *slot : NULL_TREE;
532 slot = pointer_map_insert (info->var_map, decl);
533 if (!*slot)
535 tree field = make_node (FIELD_DECL);
536 DECL_NAME (field) = DECL_NAME (decl);
537 TREE_TYPE (field) = get_trampoline_type (info);
538 TREE_ADDRESSABLE (field) = 1;
540 insert_field_into_struct (get_frame_type (info), field);
541 *slot = field;
543 info->any_tramp_created = true;
546 return (tree) *slot;
549 /* Build or return the field within the non-local frame state that holds
550 the non-local goto "jmp_buf". The buffer itself is maintained by the
551 rtl middle-end as dynamic stack space is allocated. */
553 static tree
554 get_nl_goto_field (struct nesting_info *info)
556 tree field = info->nl_goto_field;
557 if (!field)
559 unsigned size;
560 tree type;
562 /* For __builtin_nonlocal_goto, we need N words. The first is the
563 frame pointer, the rest is for the target's stack pointer save
564 area. The number of words is controlled by STACK_SAVEAREA_MODE;
565 not the best interface, but it'll do for now. */
566 if (Pmode == ptr_mode)
567 type = ptr_type_node;
568 else
569 type = lang_hooks.types.type_for_mode (Pmode, 1);
571 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
572 size = size / GET_MODE_SIZE (Pmode);
573 size = size + 1;
575 type = build_array_type
576 (type, build_index_type (size_int (size)));
578 field = make_node (FIELD_DECL);
579 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
580 TREE_TYPE (field) = type;
581 DECL_ALIGN (field) = TYPE_ALIGN (type);
582 TREE_ADDRESSABLE (field) = 1;
584 insert_field_into_struct (get_frame_type (info), field);
586 info->nl_goto_field = field;
589 return field;
592 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
594 static void
595 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
596 struct nesting_info *info, gimple_seq *pseq)
598 struct walk_stmt_info wi;
600 memset (&wi, 0, sizeof (wi));
601 wi.info = info;
602 wi.val_only = true;
603 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
607 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
609 static inline void
610 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
611 struct nesting_info *info)
613 gimple_seq body = gimple_body (info->context);
614 walk_body (callback_stmt, callback_op, info, &body);
615 gimple_set_body (info->context, body);
618 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
620 static void
621 walk_gimple_omp_for (gimple for_stmt,
622 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
623 struct nesting_info *info)
625 struct walk_stmt_info wi;
626 gimple_seq seq;
627 tree t;
628 size_t i;
630 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
632 seq = NULL;
633 memset (&wi, 0, sizeof (wi));
634 wi.info = info;
635 wi.gsi = gsi_last (seq);
637 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
639 wi.val_only = false;
640 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
641 &wi, NULL);
642 wi.val_only = true;
643 wi.is_lhs = false;
644 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
645 &wi, NULL);
647 wi.val_only = true;
648 wi.is_lhs = false;
649 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
650 &wi, NULL);
652 t = gimple_omp_for_incr (for_stmt, i);
653 gcc_assert (BINARY_CLASS_P (t));
654 wi.val_only = false;
655 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
656 wi.val_only = true;
657 wi.is_lhs = false;
658 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
661 seq = gsi_seq (wi.gsi);
662 if (!gimple_seq_empty_p (seq))
664 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
665 annotate_all_with_location (seq, gimple_location (for_stmt));
666 gimple_seq_add_seq (&pre_body, seq);
667 gimple_omp_for_set_pre_body (for_stmt, pre_body);
671 /* Similarly for ROOT and all functions nested underneath, depth first. */
673 static void
674 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
675 struct nesting_info *root)
677 struct nesting_info *n;
678 FOR_EACH_NEST_INFO (n, root)
679 walk_function (callback_stmt, callback_op, n);
683 /* We have to check for a fairly pathological case. The operands of function
684 nested function are to be interpreted in the context of the enclosing
685 function. So if any are variably-sized, they will get remapped when the
686 enclosing function is inlined. But that remapping would also have to be
687 done in the types of the PARM_DECLs of the nested function, meaning the
688 argument types of that function will disagree with the arguments in the
689 calls to that function. So we'd either have to make a copy of the nested
690 function corresponding to each time the enclosing function was inlined or
691 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
692 function. The former is not practical. The latter would still require
693 detecting this case to know when to add the conversions. So, for now at
694 least, we don't inline such an enclosing function.
696 We have to do that check recursively, so here return indicating whether
697 FNDECL has such a nested function. ORIG_FN is the function we were
698 trying to inline to use for checking whether any argument is variably
699 modified by anything in it.
701 It would be better to do this in tree-inline.c so that we could give
702 the appropriate warning for why a function can't be inlined, but that's
703 too late since the nesting structure has already been flattened and
704 adding a flag just to record this fact seems a waste of a flag. */
706 static bool
707 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
709 struct cgraph_node *cgn = cgraph_get_node (fndecl);
710 tree arg;
712 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
714 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
715 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
716 return true;
718 if (check_for_nested_with_variably_modified (cgn->decl,
719 orig_fndecl))
720 return true;
723 return false;
726 /* Construct our local datastructure describing the function nesting
727 tree rooted by CGN. */
729 static struct nesting_info *
730 create_nesting_tree (struct cgraph_node *cgn)
732 struct nesting_info *info = XCNEW (struct nesting_info);
733 info->field_map = pointer_map_create ();
734 info->var_map = pointer_map_create ();
735 info->mem_refs = pointer_set_create ();
736 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
737 info->context = cgn->decl;
739 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
741 struct nesting_info *sub = create_nesting_tree (cgn);
742 sub->outer = info;
743 sub->next = info->inner;
744 info->inner = sub;
747 /* See discussion at check_for_nested_with_variably_modified for a
748 discussion of why this has to be here. */
749 if (check_for_nested_with_variably_modified (info->context, info->context))
750 DECL_UNINLINABLE (info->context) = true;
752 return info;
755 /* Return an expression computing the static chain for TARGET_CONTEXT
756 from INFO->CONTEXT. Insert any necessary computations before TSI. */
758 static tree
759 get_static_chain (struct nesting_info *info, tree target_context,
760 gimple_stmt_iterator *gsi)
762 struct nesting_info *i;
763 tree x;
765 if (info->context == target_context)
767 x = build_addr (info->frame_decl, target_context);
769 else
771 x = get_chain_decl (info);
773 for (i = info->outer; i->context != target_context; i = i->outer)
775 tree field = get_chain_field (i);
777 x = build_simple_mem_ref (x);
778 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
779 x = init_tmp_var (info, x, gsi);
783 return x;
787 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
788 frame as seen from INFO->CONTEXT. Insert any necessary computations
789 before GSI. */
791 static tree
792 get_frame_field (struct nesting_info *info, tree target_context,
793 tree field, gimple_stmt_iterator *gsi)
795 struct nesting_info *i;
796 tree x;
798 if (info->context == target_context)
800 /* Make sure frame_decl gets created. */
801 (void) get_frame_type (info);
802 x = info->frame_decl;
804 else
806 x = get_chain_decl (info);
808 for (i = info->outer; i->context != target_context; i = i->outer)
810 tree field = get_chain_field (i);
812 x = build_simple_mem_ref (x);
813 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
814 x = init_tmp_var (info, x, gsi);
817 x = build_simple_mem_ref (x);
820 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
821 return x;
824 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
826 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
827 in the nested function with DECL_VALUE_EXPR set to reference the true
828 variable in the parent function. This is used both for debug info
829 and in OpenMP lowering. */
831 static tree
832 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
834 tree target_context;
835 struct nesting_info *i;
836 tree x, field, new_decl;
837 void **slot;
839 slot = pointer_map_insert (info->var_map, decl);
841 if (*slot)
842 return (tree) *slot;
844 target_context = decl_function_context (decl);
846 /* A copy of the code in get_frame_field, but without the temporaries. */
847 if (info->context == target_context)
849 /* Make sure frame_decl gets created. */
850 (void) get_frame_type (info);
851 x = info->frame_decl;
852 i = info;
854 else
856 x = get_chain_decl (info);
857 for (i = info->outer; i->context != target_context; i = i->outer)
859 field = get_chain_field (i);
860 x = build_simple_mem_ref (x);
861 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
863 x = build_simple_mem_ref (x);
866 field = lookup_field_for_decl (i, decl, INSERT);
867 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
868 if (use_pointer_in_frame (decl))
869 x = build_simple_mem_ref (x);
871 /* ??? We should be remapping types as well, surely. */
872 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
873 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
874 DECL_CONTEXT (new_decl) = info->context;
875 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
876 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
877 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
878 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
879 TREE_READONLY (new_decl) = TREE_READONLY (decl);
880 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
881 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
882 if ((TREE_CODE (decl) == PARM_DECL
883 || TREE_CODE (decl) == RESULT_DECL
884 || TREE_CODE (decl) == VAR_DECL)
885 && DECL_BY_REFERENCE (decl))
886 DECL_BY_REFERENCE (new_decl) = 1;
888 SET_DECL_VALUE_EXPR (new_decl, x);
889 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
891 *slot = new_decl;
892 DECL_CHAIN (new_decl) = info->debug_var_chain;
893 info->debug_var_chain = new_decl;
895 if (!optimize
896 && info->context != target_context
897 && variably_modified_type_p (TREE_TYPE (decl), NULL))
898 note_nonlocal_vla_type (info, TREE_TYPE (decl));
900 return new_decl;
904 /* Callback for walk_gimple_stmt, rewrite all references to VAR
905 and PARM_DECLs that belong to outer functions.
907 The rewrite will involve some number of structure accesses back up
908 the static chain. E.g. for a variable FOO up one nesting level it'll
909 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
910 indirections apply to decls for which use_pointer_in_frame is true. */
912 static tree
913 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
915 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
916 struct nesting_info *const info = (struct nesting_info *) wi->info;
917 tree t = *tp;
919 *walk_subtrees = 0;
920 switch (TREE_CODE (t))
922 case VAR_DECL:
923 /* Non-automatic variables are never processed. */
924 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
925 break;
926 /* FALLTHRU */
928 case PARM_DECL:
929 if (decl_function_context (t) != info->context)
931 tree x;
932 wi->changed = true;
934 x = get_nonlocal_debug_decl (info, t);
935 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
937 tree target_context = decl_function_context (t);
938 struct nesting_info *i;
939 for (i = info->outer; i->context != target_context; i = i->outer)
940 continue;
941 x = lookup_field_for_decl (i, t, INSERT);
942 x = get_frame_field (info, target_context, x, &wi->gsi);
943 if (use_pointer_in_frame (t))
945 x = init_tmp_var (info, x, &wi->gsi);
946 x = build_simple_mem_ref (x);
950 if (wi->val_only)
952 if (wi->is_lhs)
953 x = save_tmp_var (info, x, &wi->gsi);
954 else
955 x = init_tmp_var (info, x, &wi->gsi);
958 *tp = x;
960 break;
962 case LABEL_DECL:
963 /* We're taking the address of a label from a parent function, but
964 this is not itself a non-local goto. Mark the label such that it
965 will not be deleted, much as we would with a label address in
966 static storage. */
967 if (decl_function_context (t) != info->context)
968 FORCED_LABEL (t) = 1;
969 break;
971 case ADDR_EXPR:
973 bool save_val_only = wi->val_only;
975 wi->val_only = false;
976 wi->is_lhs = false;
977 wi->changed = false;
978 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
979 wi->val_only = true;
981 if (wi->changed)
983 tree save_context;
985 /* If we changed anything, we might no longer be directly
986 referencing a decl. */
987 save_context = current_function_decl;
988 current_function_decl = info->context;
989 recompute_tree_invariant_for_addr_expr (t);
990 current_function_decl = save_context;
992 /* If the callback converted the address argument in a context
993 where we only accept variables (and min_invariant, presumably),
994 then compute the address into a temporary. */
995 if (save_val_only)
996 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
997 t, &wi->gsi);
1000 break;
1002 case REALPART_EXPR:
1003 case IMAGPART_EXPR:
1004 case COMPONENT_REF:
1005 case ARRAY_REF:
1006 case ARRAY_RANGE_REF:
1007 case BIT_FIELD_REF:
1008 /* Go down this entire nest and just look at the final prefix and
1009 anything that describes the references. Otherwise, we lose track
1010 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1011 wi->val_only = true;
1012 wi->is_lhs = false;
1013 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1015 if (TREE_CODE (t) == COMPONENT_REF)
1016 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1017 NULL);
1018 else if (TREE_CODE (t) == ARRAY_REF
1019 || TREE_CODE (t) == ARRAY_RANGE_REF)
1021 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1022 wi, NULL);
1023 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1024 wi, NULL);
1025 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1026 wi, NULL);
1029 wi->val_only = false;
1030 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1031 break;
1033 case VIEW_CONVERT_EXPR:
1034 /* Just request to look at the subtrees, leaving val_only and lhs
1035 untouched. This might actually be for !val_only + lhs, in which
1036 case we don't want to force a replacement by a temporary. */
1037 *walk_subtrees = 1;
1038 break;
1040 default:
1041 if (!IS_TYPE_OR_DECL_P (t))
1043 *walk_subtrees = 1;
1044 wi->val_only = true;
1045 wi->is_lhs = false;
1047 break;
1050 return NULL_TREE;
1053 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1054 struct walk_stmt_info *);
1056 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1057 and PARM_DECLs that belong to outer functions. */
1059 static bool
1060 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1062 struct nesting_info *const info = (struct nesting_info *) wi->info;
1063 bool need_chain = false, need_stmts = false;
1064 tree clause, decl;
1065 int dummy;
1066 bitmap new_suppress;
1068 new_suppress = BITMAP_GGC_ALLOC ();
1069 bitmap_copy (new_suppress, info->suppress_expansion);
1071 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1073 switch (OMP_CLAUSE_CODE (clause))
1075 case OMP_CLAUSE_REDUCTION:
1076 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1077 need_stmts = true;
1078 goto do_decl_clause;
1080 case OMP_CLAUSE_LASTPRIVATE:
1081 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1082 need_stmts = true;
1083 goto do_decl_clause;
1085 case OMP_CLAUSE_LINEAR:
1086 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1087 need_stmts = true;
1088 wi->val_only = true;
1089 wi->is_lhs = false;
1090 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1091 &dummy, wi);
1092 goto do_decl_clause;
1094 case OMP_CLAUSE_PRIVATE:
1095 case OMP_CLAUSE_FIRSTPRIVATE:
1096 case OMP_CLAUSE_COPYPRIVATE:
1097 case OMP_CLAUSE_SHARED:
1098 do_decl_clause:
1099 decl = OMP_CLAUSE_DECL (clause);
1100 if (TREE_CODE (decl) == VAR_DECL
1101 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1102 break;
1103 if (decl_function_context (decl) != info->context)
1105 bitmap_set_bit (new_suppress, DECL_UID (decl));
1106 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1107 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1108 need_chain = true;
1110 break;
1112 case OMP_CLAUSE_SCHEDULE:
1113 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1114 break;
1115 /* FALLTHRU */
1116 case OMP_CLAUSE_FINAL:
1117 case OMP_CLAUSE_IF:
1118 case OMP_CLAUSE_NUM_THREADS:
1119 case OMP_CLAUSE_DEPEND:
1120 case OMP_CLAUSE_DEVICE:
1121 case OMP_CLAUSE_NUM_TEAMS:
1122 case OMP_CLAUSE_THREAD_LIMIT:
1123 case OMP_CLAUSE_SAFELEN:
1124 wi->val_only = true;
1125 wi->is_lhs = false;
1126 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1127 &dummy, wi);
1128 break;
1130 case OMP_CLAUSE_DIST_SCHEDULE:
1131 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1133 wi->val_only = true;
1134 wi->is_lhs = false;
1135 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1136 &dummy, wi);
1138 break;
1140 case OMP_CLAUSE_MAP:
1141 case OMP_CLAUSE_TO:
1142 case OMP_CLAUSE_FROM:
1143 if (OMP_CLAUSE_SIZE (clause))
1145 wi->val_only = true;
1146 wi->is_lhs = false;
1147 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1148 &dummy, wi);
1150 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1151 goto do_decl_clause;
1152 wi->val_only = true;
1153 wi->is_lhs = false;
1154 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1155 wi, NULL);
1156 break;
1158 case OMP_CLAUSE_ALIGNED:
1159 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1161 wi->val_only = true;
1162 wi->is_lhs = false;
1163 convert_nonlocal_reference_op
1164 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1166 /* Like do_decl_clause, but don't add any suppression. */
1167 decl = OMP_CLAUSE_DECL (clause);
1168 if (TREE_CODE (decl) == VAR_DECL
1169 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1170 break;
1171 if (decl_function_context (decl) != info->context)
1173 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1174 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1175 need_chain = true;
1177 break;
1179 case OMP_CLAUSE_NOWAIT:
1180 case OMP_CLAUSE_ORDERED:
1181 case OMP_CLAUSE_DEFAULT:
1182 case OMP_CLAUSE_COPYIN:
1183 case OMP_CLAUSE_COLLAPSE:
1184 case OMP_CLAUSE_UNTIED:
1185 case OMP_CLAUSE_MERGEABLE:
1186 case OMP_CLAUSE_PROC_BIND:
1187 break;
1189 default:
1190 gcc_unreachable ();
1194 info->suppress_expansion = new_suppress;
1196 if (need_stmts)
1197 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1198 switch (OMP_CLAUSE_CODE (clause))
1200 case OMP_CLAUSE_REDUCTION:
1201 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1203 tree old_context
1204 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1205 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1206 = info->context;
1207 walk_body (convert_nonlocal_reference_stmt,
1208 convert_nonlocal_reference_op, info,
1209 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1210 walk_body (convert_nonlocal_reference_stmt,
1211 convert_nonlocal_reference_op, info,
1212 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1213 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1214 = old_context;
1216 break;
1218 case OMP_CLAUSE_LASTPRIVATE:
1219 walk_body (convert_nonlocal_reference_stmt,
1220 convert_nonlocal_reference_op, info,
1221 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1222 break;
1224 case OMP_CLAUSE_LINEAR:
1225 walk_body (convert_nonlocal_reference_stmt,
1226 convert_nonlocal_reference_op, info,
1227 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1228 break;
1230 default:
1231 break;
1234 return need_chain;
1237 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1239 static void
1240 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1242 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1243 type = TREE_TYPE (type);
1245 if (TYPE_NAME (type)
1246 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1247 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1248 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1250 while (POINTER_TYPE_P (type)
1251 || TREE_CODE (type) == VECTOR_TYPE
1252 || TREE_CODE (type) == FUNCTION_TYPE
1253 || TREE_CODE (type) == METHOD_TYPE)
1254 type = TREE_TYPE (type);
1256 if (TREE_CODE (type) == ARRAY_TYPE)
1258 tree domain, t;
1260 note_nonlocal_vla_type (info, TREE_TYPE (type));
1261 domain = TYPE_DOMAIN (type);
1262 if (domain)
1264 t = TYPE_MIN_VALUE (domain);
1265 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1266 && decl_function_context (t) != info->context)
1267 get_nonlocal_debug_decl (info, t);
1268 t = TYPE_MAX_VALUE (domain);
1269 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1270 && decl_function_context (t) != info->context)
1271 get_nonlocal_debug_decl (info, t);
1276 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1277 in BLOCK. */
1279 static void
1280 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1282 tree var;
1284 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1285 if (TREE_CODE (var) == VAR_DECL
1286 && variably_modified_type_p (TREE_TYPE (var), NULL)
1287 && DECL_HAS_VALUE_EXPR_P (var)
1288 && decl_function_context (var) != info->context)
1289 note_nonlocal_vla_type (info, TREE_TYPE (var));
1292 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1293 PARM_DECLs that belong to outer functions. This handles statements
1294 that are not handled via the standard recursion done in
1295 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1296 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1297 operands of STMT have been handled by this function. */
1299 static tree
1300 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1301 struct walk_stmt_info *wi)
1303 struct nesting_info *info = (struct nesting_info *) wi->info;
1304 tree save_local_var_chain;
1305 bitmap save_suppress;
1306 gimple stmt = gsi_stmt (*gsi);
1308 switch (gimple_code (stmt))
1310 case GIMPLE_GOTO:
1311 /* Don't walk non-local gotos for now. */
1312 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1314 wi->val_only = true;
1315 wi->is_lhs = false;
1316 *handled_ops_p = true;
1317 return NULL_TREE;
1319 break;
1321 case GIMPLE_OMP_PARALLEL:
1322 case GIMPLE_OMP_TASK:
1323 save_suppress = info->suppress_expansion;
1324 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1325 wi))
1327 tree c, decl;
1328 decl = get_chain_decl (info);
1329 c = build_omp_clause (gimple_location (stmt),
1330 OMP_CLAUSE_FIRSTPRIVATE);
1331 OMP_CLAUSE_DECL (c) = decl;
1332 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1333 gimple_omp_taskreg_set_clauses (stmt, c);
1336 save_local_var_chain = info->new_local_var_chain;
1337 info->new_local_var_chain = NULL;
1339 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1340 info, gimple_omp_body_ptr (stmt));
1342 if (info->new_local_var_chain)
1343 declare_vars (info->new_local_var_chain,
1344 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1345 false);
1346 info->new_local_var_chain = save_local_var_chain;
1347 info->suppress_expansion = save_suppress;
1348 break;
1350 case GIMPLE_OMP_FOR:
1351 save_suppress = info->suppress_expansion;
1352 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1353 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1354 convert_nonlocal_reference_op, info);
1355 walk_body (convert_nonlocal_reference_stmt,
1356 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1357 info->suppress_expansion = save_suppress;
1358 break;
1360 case GIMPLE_OMP_SECTIONS:
1361 save_suppress = info->suppress_expansion;
1362 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1363 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1364 info, gimple_omp_body_ptr (stmt));
1365 info->suppress_expansion = save_suppress;
1366 break;
1368 case GIMPLE_OMP_SINGLE:
1369 save_suppress = info->suppress_expansion;
1370 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1371 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1372 info, gimple_omp_body_ptr (stmt));
1373 info->suppress_expansion = save_suppress;
1374 break;
1376 case GIMPLE_OMP_TARGET:
1377 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1379 save_suppress = info->suppress_expansion;
1380 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1381 wi);
1382 info->suppress_expansion = save_suppress;
1383 walk_body (convert_nonlocal_reference_stmt,
1384 convert_nonlocal_reference_op, info,
1385 gimple_omp_body_ptr (stmt));
1386 break;
1388 save_suppress = info->suppress_expansion;
1389 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1390 wi))
1392 tree c, decl;
1393 decl = get_chain_decl (info);
1394 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1395 OMP_CLAUSE_DECL (c) = decl;
1396 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TO;
1397 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1398 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1399 gimple_omp_target_set_clauses (stmt, c);
1402 save_local_var_chain = info->new_local_var_chain;
1403 info->new_local_var_chain = NULL;
1405 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1406 info, gimple_omp_body_ptr (stmt));
1408 if (info->new_local_var_chain)
1409 declare_vars (info->new_local_var_chain,
1410 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1411 false);
1412 info->new_local_var_chain = save_local_var_chain;
1413 info->suppress_expansion = save_suppress;
1414 break;
1416 case GIMPLE_OMP_TEAMS:
1417 save_suppress = info->suppress_expansion;
1418 convert_nonlocal_omp_clauses (gimple_omp_teams_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;
1422 break;
1424 case GIMPLE_OMP_SECTION:
1425 case GIMPLE_OMP_MASTER:
1426 case GIMPLE_OMP_TASKGROUP:
1427 case GIMPLE_OMP_ORDERED:
1428 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1429 info, gimple_omp_body_ptr (stmt));
1430 break;
1432 case GIMPLE_BIND:
1433 if (!optimize && gimple_bind_block (stmt))
1434 note_nonlocal_block_vlas (info, gimple_bind_block (stmt));
1436 for (tree var = gimple_bind_vars (stmt); var; var = DECL_CHAIN (var))
1437 if (TREE_CODE (var) == NAMELIST_DECL)
1439 /* Adjust decls mentioned in NAMELIST_DECL. */
1440 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1441 tree decl;
1442 unsigned int i;
1444 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1446 if (TREE_CODE (decl) == VAR_DECL
1447 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1448 continue;
1449 if (decl_function_context (decl) != info->context)
1450 CONSTRUCTOR_ELT (decls, i)->value
1451 = get_nonlocal_debug_decl (info, decl);
1455 *handled_ops_p = false;
1456 return NULL_TREE;
1458 case GIMPLE_COND:
1459 wi->val_only = true;
1460 wi->is_lhs = false;
1461 *handled_ops_p = false;
1462 return NULL_TREE;
1464 default:
1465 /* For every other statement that we are not interested in
1466 handling here, let the walker traverse the operands. */
1467 *handled_ops_p = false;
1468 return NULL_TREE;
1471 /* We have handled all of STMT operands, no need to traverse the operands. */
1472 *handled_ops_p = true;
1473 return NULL_TREE;
1477 /* A subroutine of convert_local_reference. Create a local variable
1478 in the parent function with DECL_VALUE_EXPR set to reference the
1479 field in FRAME. This is used both for debug info and in OpenMP
1480 lowering. */
1482 static tree
1483 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1485 tree x, new_decl;
1486 void **slot;
1488 slot = pointer_map_insert (info->var_map, decl);
1489 if (*slot)
1490 return (tree) *slot;
1492 /* Make sure frame_decl gets created. */
1493 (void) get_frame_type (info);
1494 x = info->frame_decl;
1495 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1497 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1498 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1499 DECL_CONTEXT (new_decl) = info->context;
1500 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1501 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1502 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1503 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1504 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1505 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1506 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1507 if ((TREE_CODE (decl) == PARM_DECL
1508 || TREE_CODE (decl) == RESULT_DECL
1509 || TREE_CODE (decl) == VAR_DECL)
1510 && DECL_BY_REFERENCE (decl))
1511 DECL_BY_REFERENCE (new_decl) = 1;
1513 SET_DECL_VALUE_EXPR (new_decl, x);
1514 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1515 *slot = new_decl;
1517 DECL_CHAIN (new_decl) = info->debug_var_chain;
1518 info->debug_var_chain = new_decl;
1520 /* Do not emit debug info twice. */
1521 DECL_IGNORED_P (decl) = 1;
1523 return new_decl;
1527 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1528 and PARM_DECLs that were referenced by inner nested functions.
1529 The rewrite will be a structure reference to the local frame variable. */
1531 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1533 static tree
1534 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1536 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1537 struct nesting_info *const info = (struct nesting_info *) wi->info;
1538 tree t = *tp, field, x;
1539 bool save_val_only;
1541 *walk_subtrees = 0;
1542 switch (TREE_CODE (t))
1544 case VAR_DECL:
1545 /* Non-automatic variables are never processed. */
1546 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1547 break;
1548 /* FALLTHRU */
1550 case PARM_DECL:
1551 if (decl_function_context (t) == info->context)
1553 /* If we copied a pointer to the frame, then the original decl
1554 is used unchanged in the parent function. */
1555 if (use_pointer_in_frame (t))
1556 break;
1558 /* No need to transform anything if no child references the
1559 variable. */
1560 field = lookup_field_for_decl (info, t, NO_INSERT);
1561 if (!field)
1562 break;
1563 wi->changed = true;
1565 x = get_local_debug_decl (info, t, field);
1566 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1567 x = get_frame_field (info, info->context, field, &wi->gsi);
1569 if (wi->val_only)
1571 if (wi->is_lhs)
1572 x = save_tmp_var (info, x, &wi->gsi);
1573 else
1574 x = init_tmp_var (info, x, &wi->gsi);
1577 *tp = x;
1579 break;
1581 case ADDR_EXPR:
1582 save_val_only = wi->val_only;
1583 wi->val_only = false;
1584 wi->is_lhs = false;
1585 wi->changed = false;
1586 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1587 wi->val_only = save_val_only;
1589 /* If we converted anything ... */
1590 if (wi->changed)
1592 tree save_context;
1594 /* Then the frame decl is now addressable. */
1595 TREE_ADDRESSABLE (info->frame_decl) = 1;
1597 save_context = current_function_decl;
1598 current_function_decl = info->context;
1599 recompute_tree_invariant_for_addr_expr (t);
1600 current_function_decl = save_context;
1602 /* If we are in a context where we only accept values, then
1603 compute the address into a temporary. */
1604 if (save_val_only)
1605 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1606 t, &wi->gsi);
1608 break;
1610 case REALPART_EXPR:
1611 case IMAGPART_EXPR:
1612 case COMPONENT_REF:
1613 case ARRAY_REF:
1614 case ARRAY_RANGE_REF:
1615 case BIT_FIELD_REF:
1616 /* Go down this entire nest and just look at the final prefix and
1617 anything that describes the references. Otherwise, we lose track
1618 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1619 save_val_only = wi->val_only;
1620 wi->val_only = true;
1621 wi->is_lhs = false;
1622 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1624 if (TREE_CODE (t) == COMPONENT_REF)
1625 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1626 NULL);
1627 else if (TREE_CODE (t) == ARRAY_REF
1628 || TREE_CODE (t) == ARRAY_RANGE_REF)
1630 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1631 NULL);
1632 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1633 NULL);
1634 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1635 NULL);
1638 wi->val_only = false;
1639 walk_tree (tp, convert_local_reference_op, wi, NULL);
1640 wi->val_only = save_val_only;
1641 break;
1643 case MEM_REF:
1644 save_val_only = wi->val_only;
1645 wi->val_only = true;
1646 wi->is_lhs = false;
1647 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1648 wi, NULL);
1649 /* We need to re-fold the MEM_REF as component references as
1650 part of a ADDR_EXPR address are not allowed. But we cannot
1651 fold here, as the chain record type is not yet finalized. */
1652 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1653 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1654 pointer_set_insert (info->mem_refs, tp);
1655 wi->val_only = save_val_only;
1656 break;
1658 case VIEW_CONVERT_EXPR:
1659 /* Just request to look at the subtrees, leaving val_only and lhs
1660 untouched. This might actually be for !val_only + lhs, in which
1661 case we don't want to force a replacement by a temporary. */
1662 *walk_subtrees = 1;
1663 break;
1665 default:
1666 if (!IS_TYPE_OR_DECL_P (t))
1668 *walk_subtrees = 1;
1669 wi->val_only = true;
1670 wi->is_lhs = false;
1672 break;
1675 return NULL_TREE;
1678 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1679 struct walk_stmt_info *);
1681 /* Helper for convert_local_reference. Convert all the references in
1682 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1684 static bool
1685 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1687 struct nesting_info *const info = (struct nesting_info *) wi->info;
1688 bool need_frame = false, need_stmts = false;
1689 tree clause, decl;
1690 int dummy;
1691 bitmap new_suppress;
1693 new_suppress = BITMAP_GGC_ALLOC ();
1694 bitmap_copy (new_suppress, info->suppress_expansion);
1696 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1698 switch (OMP_CLAUSE_CODE (clause))
1700 case OMP_CLAUSE_REDUCTION:
1701 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1702 need_stmts = true;
1703 goto do_decl_clause;
1705 case OMP_CLAUSE_LASTPRIVATE:
1706 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1707 need_stmts = true;
1708 goto do_decl_clause;
1710 case OMP_CLAUSE_LINEAR:
1711 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1712 need_stmts = true;
1713 wi->val_only = true;
1714 wi->is_lhs = false;
1715 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1716 wi);
1717 goto do_decl_clause;
1719 case OMP_CLAUSE_PRIVATE:
1720 case OMP_CLAUSE_FIRSTPRIVATE:
1721 case OMP_CLAUSE_COPYPRIVATE:
1722 case OMP_CLAUSE_SHARED:
1723 do_decl_clause:
1724 decl = OMP_CLAUSE_DECL (clause);
1725 if (TREE_CODE (decl) == VAR_DECL
1726 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1727 break;
1728 if (decl_function_context (decl) == info->context
1729 && !use_pointer_in_frame (decl))
1731 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1732 if (field)
1734 bitmap_set_bit (new_suppress, DECL_UID (decl));
1735 OMP_CLAUSE_DECL (clause)
1736 = get_local_debug_decl (info, decl, field);
1737 need_frame = true;
1740 break;
1742 case OMP_CLAUSE_SCHEDULE:
1743 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1744 break;
1745 /* FALLTHRU */
1746 case OMP_CLAUSE_FINAL:
1747 case OMP_CLAUSE_IF:
1748 case OMP_CLAUSE_NUM_THREADS:
1749 case OMP_CLAUSE_DEPEND:
1750 case OMP_CLAUSE_DEVICE:
1751 case OMP_CLAUSE_NUM_TEAMS:
1752 case OMP_CLAUSE_THREAD_LIMIT:
1753 case OMP_CLAUSE_SAFELEN:
1754 wi->val_only = true;
1755 wi->is_lhs = false;
1756 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1757 wi);
1758 break;
1760 case OMP_CLAUSE_DIST_SCHEDULE:
1761 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1763 wi->val_only = true;
1764 wi->is_lhs = false;
1765 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1766 &dummy, wi);
1768 break;
1770 case OMP_CLAUSE_MAP:
1771 case OMP_CLAUSE_TO:
1772 case OMP_CLAUSE_FROM:
1773 if (OMP_CLAUSE_SIZE (clause))
1775 wi->val_only = true;
1776 wi->is_lhs = false;
1777 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1778 &dummy, wi);
1780 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1781 goto do_decl_clause;
1782 wi->val_only = true;
1783 wi->is_lhs = false;
1784 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1785 wi, NULL);
1786 break;
1788 case OMP_CLAUSE_ALIGNED:
1789 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1791 wi->val_only = true;
1792 wi->is_lhs = false;
1793 convert_local_reference_op
1794 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1796 /* Like do_decl_clause, but don't add any suppression. */
1797 decl = OMP_CLAUSE_DECL (clause);
1798 if (TREE_CODE (decl) == VAR_DECL
1799 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1800 break;
1801 if (decl_function_context (decl) == info->context
1802 && !use_pointer_in_frame (decl))
1804 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1805 if (field)
1807 OMP_CLAUSE_DECL (clause)
1808 = get_local_debug_decl (info, decl, field);
1809 need_frame = true;
1812 break;
1814 case OMP_CLAUSE_NOWAIT:
1815 case OMP_CLAUSE_ORDERED:
1816 case OMP_CLAUSE_DEFAULT:
1817 case OMP_CLAUSE_COPYIN:
1818 case OMP_CLAUSE_COLLAPSE:
1819 case OMP_CLAUSE_UNTIED:
1820 case OMP_CLAUSE_MERGEABLE:
1821 case OMP_CLAUSE_PROC_BIND:
1822 break;
1824 default:
1825 gcc_unreachable ();
1829 info->suppress_expansion = new_suppress;
1831 if (need_stmts)
1832 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1833 switch (OMP_CLAUSE_CODE (clause))
1835 case OMP_CLAUSE_REDUCTION:
1836 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1838 tree old_context
1839 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1840 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1841 = info->context;
1842 walk_body (convert_local_reference_stmt,
1843 convert_local_reference_op, info,
1844 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1845 walk_body (convert_local_reference_stmt,
1846 convert_local_reference_op, info,
1847 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1848 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1849 = old_context;
1851 break;
1853 case OMP_CLAUSE_LASTPRIVATE:
1854 walk_body (convert_local_reference_stmt,
1855 convert_local_reference_op, info,
1856 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1857 break;
1859 case OMP_CLAUSE_LINEAR:
1860 walk_body (convert_local_reference_stmt,
1861 convert_local_reference_op, info,
1862 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1863 break;
1865 default:
1866 break;
1869 return need_frame;
1873 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1874 and PARM_DECLs that were referenced by inner nested functions.
1875 The rewrite will be a structure reference to the local frame variable. */
1877 static tree
1878 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1879 struct walk_stmt_info *wi)
1881 struct nesting_info *info = (struct nesting_info *) wi->info;
1882 tree save_local_var_chain;
1883 bitmap save_suppress;
1884 gimple stmt = gsi_stmt (*gsi);
1886 switch (gimple_code (stmt))
1888 case GIMPLE_OMP_PARALLEL:
1889 case GIMPLE_OMP_TASK:
1890 save_suppress = info->suppress_expansion;
1891 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1892 wi))
1894 tree c;
1895 (void) get_frame_type (info);
1896 c = build_omp_clause (gimple_location (stmt),
1897 OMP_CLAUSE_SHARED);
1898 OMP_CLAUSE_DECL (c) = info->frame_decl;
1899 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1900 gimple_omp_taskreg_set_clauses (stmt, c);
1903 save_local_var_chain = info->new_local_var_chain;
1904 info->new_local_var_chain = NULL;
1906 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1907 gimple_omp_body_ptr (stmt));
1909 if (info->new_local_var_chain)
1910 declare_vars (info->new_local_var_chain,
1911 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1912 info->new_local_var_chain = save_local_var_chain;
1913 info->suppress_expansion = save_suppress;
1914 break;
1916 case GIMPLE_OMP_FOR:
1917 save_suppress = info->suppress_expansion;
1918 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1919 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1920 convert_local_reference_op, info);
1921 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1922 info, gimple_omp_body_ptr (stmt));
1923 info->suppress_expansion = save_suppress;
1924 break;
1926 case GIMPLE_OMP_SECTIONS:
1927 save_suppress = info->suppress_expansion;
1928 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1929 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1930 info, gimple_omp_body_ptr (stmt));
1931 info->suppress_expansion = save_suppress;
1932 break;
1934 case GIMPLE_OMP_SINGLE:
1935 save_suppress = info->suppress_expansion;
1936 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1937 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1938 info, gimple_omp_body_ptr (stmt));
1939 info->suppress_expansion = save_suppress;
1940 break;
1942 case GIMPLE_OMP_TARGET:
1943 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1945 save_suppress = info->suppress_expansion;
1946 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1947 info->suppress_expansion = save_suppress;
1948 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1949 info, gimple_omp_body_ptr (stmt));
1950 break;
1952 save_suppress = info->suppress_expansion;
1953 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1955 tree c;
1956 (void) get_frame_type (info);
1957 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1958 OMP_CLAUSE_DECL (c) = info->frame_decl;
1959 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TOFROM;
1960 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1961 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1962 gimple_omp_target_set_clauses (stmt, c);
1965 save_local_var_chain = info->new_local_var_chain;
1966 info->new_local_var_chain = NULL;
1968 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1969 gimple_omp_body_ptr (stmt));
1971 if (info->new_local_var_chain)
1972 declare_vars (info->new_local_var_chain,
1973 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1974 info->new_local_var_chain = save_local_var_chain;
1975 info->suppress_expansion = save_suppress;
1976 break;
1978 case GIMPLE_OMP_TEAMS:
1979 save_suppress = info->suppress_expansion;
1980 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1981 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1982 info, gimple_omp_body_ptr (stmt));
1983 info->suppress_expansion = save_suppress;
1984 break;
1986 case GIMPLE_OMP_SECTION:
1987 case GIMPLE_OMP_MASTER:
1988 case GIMPLE_OMP_TASKGROUP:
1989 case GIMPLE_OMP_ORDERED:
1990 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1991 info, gimple_omp_body_ptr (stmt));
1992 break;
1994 case GIMPLE_COND:
1995 wi->val_only = true;
1996 wi->is_lhs = false;
1997 *handled_ops_p = false;
1998 return NULL_TREE;
2000 case GIMPLE_ASSIGN:
2001 if (gimple_clobber_p (stmt))
2003 tree lhs = gimple_assign_lhs (stmt);
2004 if (!use_pointer_in_frame (lhs)
2005 && lookup_field_for_decl (info, lhs, NO_INSERT))
2007 gsi_replace (gsi, gimple_build_nop (), true);
2008 break;
2011 *handled_ops_p = false;
2012 return NULL_TREE;
2014 case GIMPLE_BIND:
2015 for (tree var = gimple_bind_vars (stmt); var; var = DECL_CHAIN (var))
2016 if (TREE_CODE (var) == NAMELIST_DECL)
2018 /* Adjust decls mentioned in NAMELIST_DECL. */
2019 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2020 tree decl;
2021 unsigned int i;
2023 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2025 if (TREE_CODE (decl) == VAR_DECL
2026 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2027 continue;
2028 if (decl_function_context (decl) == info->context
2029 && !use_pointer_in_frame (decl))
2031 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2032 if (field)
2034 CONSTRUCTOR_ELT (decls, i)->value
2035 = get_local_debug_decl (info, decl, field);
2041 *handled_ops_p = false;
2042 return NULL_TREE;
2044 default:
2045 /* For every other statement that we are not interested in
2046 handling here, let the walker traverse the operands. */
2047 *handled_ops_p = false;
2048 return NULL_TREE;
2051 /* Indicate that we have handled all the operands ourselves. */
2052 *handled_ops_p = true;
2053 return NULL_TREE;
2057 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2058 that reference labels from outer functions. The rewrite will be a
2059 call to __builtin_nonlocal_goto. */
2061 static tree
2062 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2063 struct walk_stmt_info *wi)
2065 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2066 tree label, new_label, target_context, x, field;
2067 void **slot;
2068 gimple call;
2069 gimple stmt = gsi_stmt (*gsi);
2071 if (gimple_code (stmt) != GIMPLE_GOTO)
2073 *handled_ops_p = false;
2074 return NULL_TREE;
2077 label = gimple_goto_dest (stmt);
2078 if (TREE_CODE (label) != LABEL_DECL)
2080 *handled_ops_p = false;
2081 return NULL_TREE;
2084 target_context = decl_function_context (label);
2085 if (target_context == info->context)
2087 *handled_ops_p = false;
2088 return NULL_TREE;
2091 for (i = info->outer; target_context != i->context; i = i->outer)
2092 continue;
2094 /* The original user label may also be use for a normal goto, therefore
2095 we must create a new label that will actually receive the abnormal
2096 control transfer. This new label will be marked LABEL_NONLOCAL; this
2097 mark will trigger proper behavior in the cfg, as well as cause the
2098 (hairy target-specific) non-local goto receiver code to be generated
2099 when we expand rtl. Enter this association into var_map so that we
2100 can insert the new label into the IL during a second pass. */
2101 slot = pointer_map_insert (i->var_map, label);
2102 if (*slot == NULL)
2104 new_label = create_artificial_label (UNKNOWN_LOCATION);
2105 DECL_NONLOCAL (new_label) = 1;
2106 *slot = new_label;
2108 else
2109 new_label = (tree) *slot;
2111 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2112 field = get_nl_goto_field (i);
2113 x = get_frame_field (info, target_context, field, gsi);
2114 x = build_addr (x, target_context);
2115 x = gsi_gimplify_val (info, x, gsi);
2116 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2117 2, build_addr (new_label, target_context), x);
2118 gsi_replace (gsi, call, false);
2120 /* We have handled all of STMT's operands, no need to keep going. */
2121 *handled_ops_p = true;
2122 return NULL_TREE;
2126 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2127 are referenced via nonlocal goto from a nested function. The rewrite
2128 will involve installing a newly generated DECL_NONLOCAL label, and
2129 (potentially) a branch around the rtl gunk that is assumed to be
2130 attached to such a label. */
2132 static tree
2133 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2134 struct walk_stmt_info *wi)
2136 struct nesting_info *const info = (struct nesting_info *) wi->info;
2137 tree label, new_label;
2138 gimple_stmt_iterator tmp_gsi;
2139 void **slot;
2140 gimple stmt = gsi_stmt (*gsi);
2142 if (gimple_code (stmt) != GIMPLE_LABEL)
2144 *handled_ops_p = false;
2145 return NULL_TREE;
2148 label = gimple_label_label (stmt);
2150 slot = pointer_map_contains (info->var_map, label);
2151 if (!slot)
2153 *handled_ops_p = false;
2154 return NULL_TREE;
2157 /* If there's any possibility that the previous statement falls through,
2158 then we must branch around the new non-local label. */
2159 tmp_gsi = wi->gsi;
2160 gsi_prev (&tmp_gsi);
2161 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2163 gimple stmt = gimple_build_goto (label);
2164 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2167 new_label = (tree) *slot;
2168 stmt = gimple_build_label (new_label);
2169 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2171 *handled_ops_p = true;
2172 return NULL_TREE;
2176 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2177 of nested functions that require the use of trampolines. The rewrite
2178 will involve a reference a trampoline generated for the occasion. */
2180 static tree
2181 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2183 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2184 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2185 tree t = *tp, decl, target_context, x, builtin;
2186 gimple call;
2188 *walk_subtrees = 0;
2189 switch (TREE_CODE (t))
2191 case ADDR_EXPR:
2192 /* Build
2193 T.1 = &CHAIN->tramp;
2194 T.2 = __builtin_adjust_trampoline (T.1);
2195 T.3 = (func_type)T.2;
2198 decl = TREE_OPERAND (t, 0);
2199 if (TREE_CODE (decl) != FUNCTION_DECL)
2200 break;
2202 /* Only need to process nested functions. */
2203 target_context = decl_function_context (decl);
2204 if (!target_context)
2205 break;
2207 /* If the nested function doesn't use a static chain, then
2208 it doesn't need a trampoline. */
2209 if (!DECL_STATIC_CHAIN (decl))
2210 break;
2212 /* If we don't want a trampoline, then don't build one. */
2213 if (TREE_NO_TRAMPOLINE (t))
2214 break;
2216 /* Lookup the immediate parent of the callee, as that's where
2217 we need to insert the trampoline. */
2218 for (i = info; i->context != target_context; i = i->outer)
2219 continue;
2220 x = lookup_tramp_for_decl (i, decl, INSERT);
2222 /* Compute the address of the field holding the trampoline. */
2223 x = get_frame_field (info, target_context, x, &wi->gsi);
2224 x = build_addr (x, target_context);
2225 x = gsi_gimplify_val (info, x, &wi->gsi);
2227 /* Do machine-specific ugliness. Normally this will involve
2228 computing extra alignment, but it can really be anything. */
2229 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2230 call = gimple_build_call (builtin, 1, x);
2231 x = init_tmp_var_with_call (info, &wi->gsi, call);
2233 /* Cast back to the proper function type. */
2234 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2235 x = init_tmp_var (info, x, &wi->gsi);
2237 *tp = x;
2238 break;
2240 default:
2241 if (!IS_TYPE_OR_DECL_P (t))
2242 *walk_subtrees = 1;
2243 break;
2246 return NULL_TREE;
2250 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2251 to addresses of nested functions that require the use of
2252 trampolines. The rewrite will involve a reference a trampoline
2253 generated for the occasion. */
2255 static tree
2256 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2257 struct walk_stmt_info *wi)
2259 struct nesting_info *info = (struct nesting_info *) wi->info;
2260 gimple stmt = gsi_stmt (*gsi);
2262 switch (gimple_code (stmt))
2264 case GIMPLE_CALL:
2266 /* Only walk call arguments, lest we generate trampolines for
2267 direct calls. */
2268 unsigned long i, nargs = gimple_call_num_args (stmt);
2269 for (i = 0; i < nargs; i++)
2270 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2271 wi, NULL);
2272 break;
2275 case GIMPLE_OMP_TARGET:
2276 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2278 *handled_ops_p = false;
2279 return NULL_TREE;
2281 /* FALLTHRU */
2282 case GIMPLE_OMP_PARALLEL:
2283 case GIMPLE_OMP_TASK:
2285 tree save_local_var_chain;
2286 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2287 save_local_var_chain = info->new_local_var_chain;
2288 info->new_local_var_chain = NULL;
2289 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2290 info, gimple_omp_body_ptr (stmt));
2291 if (info->new_local_var_chain)
2292 declare_vars (info->new_local_var_chain,
2293 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2294 false);
2295 info->new_local_var_chain = save_local_var_chain;
2297 break;
2299 default:
2300 *handled_ops_p = false;
2301 return NULL_TREE;
2304 *handled_ops_p = true;
2305 return NULL_TREE;
2310 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2311 that reference nested functions to make sure that the static chain
2312 is set up properly for the call. */
2314 static tree
2315 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2316 struct walk_stmt_info *wi)
2318 struct nesting_info *const info = (struct nesting_info *) wi->info;
2319 tree decl, target_context;
2320 char save_static_chain_added;
2321 int i;
2322 gimple stmt = gsi_stmt (*gsi);
2324 switch (gimple_code (stmt))
2326 case GIMPLE_CALL:
2327 if (gimple_call_chain (stmt))
2328 break;
2329 decl = gimple_call_fndecl (stmt);
2330 if (!decl)
2331 break;
2332 target_context = decl_function_context (decl);
2333 if (target_context && DECL_STATIC_CHAIN (decl))
2335 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
2336 &wi->gsi));
2337 info->static_chain_added |= (1 << (info->context != target_context));
2339 break;
2341 case GIMPLE_OMP_PARALLEL:
2342 case GIMPLE_OMP_TASK:
2343 save_static_chain_added = info->static_chain_added;
2344 info->static_chain_added = 0;
2345 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2346 for (i = 0; i < 2; i++)
2348 tree c, decl;
2349 if ((info->static_chain_added & (1 << i)) == 0)
2350 continue;
2351 decl = i ? get_chain_decl (info) : info->frame_decl;
2352 /* Don't add CHAIN.* or FRAME.* twice. */
2353 for (c = gimple_omp_taskreg_clauses (stmt);
2355 c = OMP_CLAUSE_CHAIN (c))
2356 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2357 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2358 && OMP_CLAUSE_DECL (c) == decl)
2359 break;
2360 if (c == NULL)
2362 c = build_omp_clause (gimple_location (stmt),
2363 i ? OMP_CLAUSE_FIRSTPRIVATE
2364 : OMP_CLAUSE_SHARED);
2365 OMP_CLAUSE_DECL (c) = decl;
2366 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2367 gimple_omp_taskreg_set_clauses (stmt, c);
2370 info->static_chain_added |= save_static_chain_added;
2371 break;
2373 case GIMPLE_OMP_TARGET:
2374 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2376 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2377 break;
2379 save_static_chain_added = info->static_chain_added;
2380 info->static_chain_added = 0;
2381 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2382 for (i = 0; i < 2; i++)
2384 tree c, decl;
2385 if ((info->static_chain_added & (1 << i)) == 0)
2386 continue;
2387 decl = i ? get_chain_decl (info) : info->frame_decl;
2388 /* Don't add CHAIN.* or FRAME.* twice. */
2389 for (c = gimple_omp_target_clauses (stmt);
2391 c = OMP_CLAUSE_CHAIN (c))
2392 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2393 && OMP_CLAUSE_DECL (c) == decl)
2394 break;
2395 if (c == NULL)
2397 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2398 OMP_CLAUSE_DECL (c) = decl;
2399 OMP_CLAUSE_MAP_KIND (c)
2400 = i ? OMP_CLAUSE_MAP_TO : OMP_CLAUSE_MAP_TOFROM;
2401 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2402 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2403 gimple_omp_target_set_clauses (stmt, c);
2406 info->static_chain_added |= save_static_chain_added;
2407 break;
2409 case GIMPLE_OMP_FOR:
2410 walk_body (convert_gimple_call, NULL, info,
2411 gimple_omp_for_pre_body_ptr (stmt));
2412 /* FALLTHRU */
2413 case GIMPLE_OMP_SECTIONS:
2414 case GIMPLE_OMP_SECTION:
2415 case GIMPLE_OMP_SINGLE:
2416 case GIMPLE_OMP_TEAMS:
2417 case GIMPLE_OMP_MASTER:
2418 case GIMPLE_OMP_TASKGROUP:
2419 case GIMPLE_OMP_ORDERED:
2420 case GIMPLE_OMP_CRITICAL:
2421 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2422 break;
2424 default:
2425 /* Keep looking for other operands. */
2426 *handled_ops_p = false;
2427 return NULL_TREE;
2430 *handled_ops_p = true;
2431 return NULL_TREE;
2434 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2435 call expressions. At the same time, determine if a nested function
2436 actually uses its static chain; if not, remember that. */
2438 static void
2439 convert_all_function_calls (struct nesting_info *root)
2441 unsigned int chain_count = 0, old_chain_count, iter_count;
2442 struct nesting_info *n;
2444 /* First, optimistically clear static_chain for all decls that haven't
2445 used the static chain already for variable access. */
2446 FOR_EACH_NEST_INFO (n, root)
2448 tree decl = n->context;
2449 if (!n->outer || (!n->chain_decl && !n->chain_field))
2451 DECL_STATIC_CHAIN (decl) = 0;
2452 if (dump_file && (dump_flags & TDF_DETAILS))
2453 fprintf (dump_file, "Guessing no static-chain for %s\n",
2454 lang_hooks.decl_printable_name (decl, 2));
2456 else
2457 DECL_STATIC_CHAIN (decl) = 1;
2458 chain_count += DECL_STATIC_CHAIN (decl);
2461 /* Walk the functions and perform transformations. Note that these
2462 transformations can induce new uses of the static chain, which in turn
2463 require re-examining all users of the decl. */
2464 /* ??? It would make sense to try to use the call graph to speed this up,
2465 but the call graph hasn't really been built yet. Even if it did, we
2466 would still need to iterate in this loop since address-of references
2467 wouldn't show up in the callgraph anyway. */
2468 iter_count = 0;
2471 old_chain_count = chain_count;
2472 chain_count = 0;
2473 iter_count++;
2475 if (dump_file && (dump_flags & TDF_DETAILS))
2476 fputc ('\n', dump_file);
2478 FOR_EACH_NEST_INFO (n, root)
2480 tree decl = n->context;
2481 walk_function (convert_tramp_reference_stmt,
2482 convert_tramp_reference_op, n);
2483 walk_function (convert_gimple_call, NULL, n);
2484 chain_count += DECL_STATIC_CHAIN (decl);
2487 while (chain_count != old_chain_count);
2489 if (dump_file && (dump_flags & TDF_DETAILS))
2490 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2491 iter_count);
2494 struct nesting_copy_body_data
2496 copy_body_data cb;
2497 struct nesting_info *root;
2500 /* A helper subroutine for debug_var_chain type remapping. */
2502 static tree
2503 nesting_copy_decl (tree decl, copy_body_data *id)
2505 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2506 void **slot = pointer_map_contains (nid->root->var_map, decl);
2508 if (slot)
2509 return (tree) *slot;
2511 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2513 tree new_decl = copy_decl_no_change (decl, id);
2514 DECL_ORIGINAL_TYPE (new_decl)
2515 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2516 return new_decl;
2519 if (TREE_CODE (decl) == VAR_DECL
2520 || TREE_CODE (decl) == PARM_DECL
2521 || TREE_CODE (decl) == RESULT_DECL)
2522 return decl;
2524 return copy_decl_no_change (decl, id);
2527 /* A helper function for remap_vla_decls. See if *TP contains
2528 some remapped variables. */
2530 static tree
2531 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2533 struct nesting_info *root = (struct nesting_info *) data;
2534 tree t = *tp;
2535 void **slot;
2537 if (DECL_P (t))
2539 *walk_subtrees = 0;
2540 slot = pointer_map_contains (root->var_map, t);
2542 if (slot)
2543 return (tree) *slot;
2545 return NULL;
2548 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2549 involved. */
2551 static void
2552 remap_vla_decls (tree block, struct nesting_info *root)
2554 tree var, subblock, val, type;
2555 struct nesting_copy_body_data id;
2557 for (subblock = BLOCK_SUBBLOCKS (block);
2558 subblock;
2559 subblock = BLOCK_CHAIN (subblock))
2560 remap_vla_decls (subblock, root);
2562 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2563 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2565 val = DECL_VALUE_EXPR (var);
2566 type = TREE_TYPE (var);
2568 if (!(TREE_CODE (val) == INDIRECT_REF
2569 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2570 && variably_modified_type_p (type, NULL)))
2571 continue;
2573 if (pointer_map_contains (root->var_map, TREE_OPERAND (val, 0))
2574 || walk_tree (&type, contains_remapped_vars, root, NULL))
2575 break;
2578 if (var == NULL_TREE)
2579 return;
2581 memset (&id, 0, sizeof (id));
2582 id.cb.copy_decl = nesting_copy_decl;
2583 id.cb.decl_map = pointer_map_create ();
2584 id.root = root;
2586 for (; var; var = DECL_CHAIN (var))
2587 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2589 struct nesting_info *i;
2590 tree newt, context;
2591 void **slot;
2593 val = DECL_VALUE_EXPR (var);
2594 type = TREE_TYPE (var);
2596 if (!(TREE_CODE (val) == INDIRECT_REF
2597 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2598 && variably_modified_type_p (type, NULL)))
2599 continue;
2601 slot = pointer_map_contains (root->var_map, TREE_OPERAND (val, 0));
2602 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2603 continue;
2605 context = decl_function_context (var);
2606 for (i = root; i; i = i->outer)
2607 if (i->context == context)
2608 break;
2610 if (i == NULL)
2611 continue;
2613 /* Fully expand value expressions. This avoids having debug variables
2614 only referenced from them and that can be swept during GC. */
2615 if (slot)
2617 tree t = (tree) *slot;
2618 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2619 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2622 id.cb.src_fn = i->context;
2623 id.cb.dst_fn = i->context;
2624 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2626 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2627 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2629 newt = TREE_TYPE (newt);
2630 type = TREE_TYPE (type);
2632 if (TYPE_NAME (newt)
2633 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2634 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2635 && newt != type
2636 && TYPE_NAME (newt) == TYPE_NAME (type))
2637 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2639 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2640 if (val != DECL_VALUE_EXPR (var))
2641 SET_DECL_VALUE_EXPR (var, val);
2644 pointer_map_destroy (id.cb.decl_map);
2647 /* Fold the MEM_REF *E. */
2648 static bool
2649 fold_mem_refs (const void *e, void *data ATTRIBUTE_UNUSED)
2651 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2652 *ref_p = fold (*ref_p);
2653 return true;
2656 /* Do "everything else" to clean up or complete state collected by the
2657 various walking passes -- lay out the types and decls, generate code
2658 to initialize the frame decl, store critical expressions in the
2659 struct function for rtl to find. */
2661 static void
2662 finalize_nesting_tree_1 (struct nesting_info *root)
2664 gimple_seq stmt_list;
2665 gimple stmt;
2666 tree context = root->context;
2667 struct function *sf;
2669 stmt_list = NULL;
2671 /* If we created a non-local frame type or decl, we need to lay them
2672 out at this time. */
2673 if (root->frame_type)
2675 /* In some cases the frame type will trigger the -Wpadded warning.
2676 This is not helpful; suppress it. */
2677 int save_warn_padded = warn_padded;
2678 tree *adjust;
2680 warn_padded = 0;
2681 layout_type (root->frame_type);
2682 warn_padded = save_warn_padded;
2683 layout_decl (root->frame_decl, 0);
2685 /* Remove root->frame_decl from root->new_local_var_chain, so
2686 that we can declare it also in the lexical blocks, which
2687 helps ensure virtual regs that end up appearing in its RTL
2688 expression get substituted in instantiate_virtual_regs(). */
2689 for (adjust = &root->new_local_var_chain;
2690 *adjust != root->frame_decl;
2691 adjust = &DECL_CHAIN (*adjust))
2692 gcc_assert (DECL_CHAIN (*adjust));
2693 *adjust = DECL_CHAIN (*adjust);
2695 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2696 declare_vars (root->frame_decl,
2697 gimple_seq_first_stmt (gimple_body (context)), true);
2700 /* If any parameters were referenced non-locally, then we need to
2701 insert a copy. Likewise, if any variables were referenced by
2702 pointer, we need to initialize the address. */
2703 if (root->any_parm_remapped)
2705 tree p;
2706 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2708 tree field, x, y;
2710 field = lookup_field_for_decl (root, p, NO_INSERT);
2711 if (!field)
2712 continue;
2714 if (use_pointer_in_frame (p))
2715 x = build_addr (p, context);
2716 else
2717 x = p;
2719 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2720 root->frame_decl, field, NULL_TREE);
2721 stmt = gimple_build_assign (y, x);
2722 gimple_seq_add_stmt (&stmt_list, stmt);
2723 /* If the assignment is from a non-register the stmt is
2724 not valid gimple. Make it so by using a temporary instead. */
2725 if (!is_gimple_reg (x)
2726 && is_gimple_reg_type (TREE_TYPE (x)))
2728 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2729 x = init_tmp_var (root, x, &gsi);
2730 gimple_assign_set_rhs1 (stmt, x);
2735 /* If a chain_field was created, then it needs to be initialized
2736 from chain_decl. */
2737 if (root->chain_field)
2739 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2740 root->frame_decl, root->chain_field, NULL_TREE);
2741 stmt = gimple_build_assign (x, get_chain_decl (root));
2742 gimple_seq_add_stmt (&stmt_list, stmt);
2745 /* If trampolines were created, then we need to initialize them. */
2746 if (root->any_tramp_created)
2748 struct nesting_info *i;
2749 for (i = root->inner; i ; i = i->next)
2751 tree arg1, arg2, arg3, x, field;
2753 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2754 if (!field)
2755 continue;
2757 gcc_assert (DECL_STATIC_CHAIN (i->context));
2758 arg3 = build_addr (root->frame_decl, context);
2760 arg2 = build_addr (i->context, context);
2762 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2763 root->frame_decl, field, NULL_TREE);
2764 arg1 = build_addr (x, context);
2766 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2767 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2768 gimple_seq_add_stmt (&stmt_list, stmt);
2772 /* If we created initialization statements, insert them. */
2773 if (stmt_list)
2775 gimple bind;
2776 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2777 bind = gimple_seq_first_stmt (gimple_body (context));
2778 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2779 gimple_bind_set_body (bind, stmt_list);
2782 /* If a chain_decl was created, then it needs to be registered with
2783 struct function so that it gets initialized from the static chain
2784 register at the beginning of the function. */
2785 sf = DECL_STRUCT_FUNCTION (root->context);
2786 sf->static_chain_decl = root->chain_decl;
2788 /* Similarly for the non-local goto save area. */
2789 if (root->nl_goto_field)
2791 sf->nonlocal_goto_save_area
2792 = get_frame_field (root, context, root->nl_goto_field, NULL);
2793 sf->has_nonlocal_label = 1;
2796 /* Make sure all new local variables get inserted into the
2797 proper BIND_EXPR. */
2798 if (root->new_local_var_chain)
2799 declare_vars (root->new_local_var_chain,
2800 gimple_seq_first_stmt (gimple_body (root->context)),
2801 false);
2803 if (root->debug_var_chain)
2805 tree debug_var;
2806 gimple scope;
2808 remap_vla_decls (DECL_INITIAL (root->context), root);
2810 for (debug_var = root->debug_var_chain; debug_var;
2811 debug_var = DECL_CHAIN (debug_var))
2812 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2813 break;
2815 /* If there are any debug decls with variable length types,
2816 remap those types using other debug_var_chain variables. */
2817 if (debug_var)
2819 struct nesting_copy_body_data id;
2821 memset (&id, 0, sizeof (id));
2822 id.cb.copy_decl = nesting_copy_decl;
2823 id.cb.decl_map = pointer_map_create ();
2824 id.root = root;
2826 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2827 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2829 tree type = TREE_TYPE (debug_var);
2830 tree newt, t = type;
2831 struct nesting_info *i;
2833 for (i = root; i; i = i->outer)
2834 if (variably_modified_type_p (type, i->context))
2835 break;
2837 if (i == NULL)
2838 continue;
2840 id.cb.src_fn = i->context;
2841 id.cb.dst_fn = i->context;
2842 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2844 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2845 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2847 newt = TREE_TYPE (newt);
2848 t = TREE_TYPE (t);
2850 if (TYPE_NAME (newt)
2851 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2852 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2853 && newt != t
2854 && TYPE_NAME (newt) == TYPE_NAME (t))
2855 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2858 pointer_map_destroy (id.cb.decl_map);
2861 scope = gimple_seq_first_stmt (gimple_body (root->context));
2862 if (gimple_bind_block (scope))
2863 declare_vars (root->debug_var_chain, scope, true);
2864 else
2865 BLOCK_VARS (DECL_INITIAL (root->context))
2866 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2867 root->debug_var_chain);
2870 /* Fold the rewritten MEM_REF trees. */
2871 pointer_set_traverse (root->mem_refs, fold_mem_refs, NULL);
2873 /* Dump the translated tree function. */
2874 if (dump_file)
2876 fputs ("\n\n", dump_file);
2877 dump_function_to_file (root->context, dump_file, dump_flags);
2881 static void
2882 finalize_nesting_tree (struct nesting_info *root)
2884 struct nesting_info *n;
2885 FOR_EACH_NEST_INFO (n, root)
2886 finalize_nesting_tree_1 (n);
2889 /* Unnest the nodes and pass them to cgraph. */
2891 static void
2892 unnest_nesting_tree_1 (struct nesting_info *root)
2894 struct cgraph_node *node = cgraph_get_node (root->context);
2896 /* For nested functions update the cgraph to reflect unnesting.
2897 We also delay finalizing of these functions up to this point. */
2898 if (node->origin)
2900 cgraph_unnest_node (node);
2901 cgraph_finalize_function (root->context, true);
2905 static void
2906 unnest_nesting_tree (struct nesting_info *root)
2908 struct nesting_info *n;
2909 FOR_EACH_NEST_INFO (n, root)
2910 unnest_nesting_tree_1 (n);
2913 /* Free the data structures allocated during this pass. */
2915 static void
2916 free_nesting_tree (struct nesting_info *root)
2918 struct nesting_info *node, *next;
2920 node = iter_nestinfo_start (root);
2923 next = iter_nestinfo_next (node);
2924 pointer_map_destroy (node->var_map);
2925 pointer_map_destroy (node->field_map);
2926 pointer_set_destroy (node->mem_refs);
2927 free (node);
2928 node = next;
2930 while (node);
2933 /* Gimplify a function and all its nested functions. */
2934 static void
2935 gimplify_all_functions (struct cgraph_node *root)
2937 struct cgraph_node *iter;
2938 if (!gimple_body (root->decl))
2939 gimplify_function_tree (root->decl);
2940 for (iter = root->nested; iter; iter = iter->next_nested)
2941 gimplify_all_functions (iter);
2944 /* Main entry point for this pass. Process FNDECL and all of its nested
2945 subroutines and turn them into something less tightly bound. */
2947 void
2948 lower_nested_functions (tree fndecl)
2950 struct cgraph_node *cgn;
2951 struct nesting_info *root;
2953 /* If there are no nested functions, there's nothing to do. */
2954 cgn = cgraph_get_node (fndecl);
2955 if (!cgn->nested)
2956 return;
2958 gimplify_all_functions (cgn);
2960 dump_file = dump_begin (TDI_nested, &dump_flags);
2961 if (dump_file)
2962 fprintf (dump_file, "\n;; Function %s\n\n",
2963 lang_hooks.decl_printable_name (fndecl, 2));
2965 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2966 root = create_nesting_tree (cgn);
2968 walk_all_functions (convert_nonlocal_reference_stmt,
2969 convert_nonlocal_reference_op,
2970 root);
2971 walk_all_functions (convert_local_reference_stmt,
2972 convert_local_reference_op,
2973 root);
2974 walk_all_functions (convert_nl_goto_reference, NULL, root);
2975 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2977 convert_all_function_calls (root);
2978 finalize_nesting_tree (root);
2979 unnest_nesting_tree (root);
2981 free_nesting_tree (root);
2982 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2984 if (dump_file)
2986 dump_end (TDI_nested, dump_file);
2987 dump_file = NULL;
2991 #include "gt-tree-nested.h"