PR middle-end/59175
[official-gcc.git] / gcc / tree-nested.c
blob9b4493bade0152fc971968fe6507083ed0249f14
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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 "tm_p.h"
26 #include "function.h"
27 #include "tree-dump.h"
28 #include "tree-inline.h"
29 #include "gimple.h"
30 #include "gimplify.h"
31 #include "gimple-iterator.h"
32 #include "gimple-walk.h"
33 #include "tree-iterator.h"
34 #include "bitmap.h"
35 #include "cgraph.h"
36 #include "tree-cfg.h"
37 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
38 #include "langhooks.h"
39 #include "pointer-set.h"
40 #include "gimple-low.h"
43 /* The object of this pass is to lower the representation of a set of nested
44 functions in order to expose all of the gory details of the various
45 nonlocal references. We want to do this sooner rather than later, in
46 order to give us more freedom in emitting all of the functions in question.
48 Back in olden times, when gcc was young, we developed an insanely
49 complicated scheme whereby variables which were referenced nonlocally
50 were forced to live in the stack of the declaring function, and then
51 the nested functions magically discovered where these variables were
52 placed. In order for this scheme to function properly, it required
53 that the outer function be partially expanded, then we switch to
54 compiling the inner function, and once done with those we switch back
55 to compiling the outer function. Such delicate ordering requirements
56 makes it difficult to do whole translation unit optimizations
57 involving such functions.
59 The implementation here is much more direct. Everything that can be
60 referenced by an inner function is a member of an explicitly created
61 structure herein called the "nonlocal frame struct". The incoming
62 static chain for a nested function is a pointer to this struct in
63 the parent. In this way, we settle on known offsets from a known
64 base, and so are decoupled from the logic that places objects in the
65 function's stack frame. More importantly, we don't have to wait for
66 that to happen -- since the compilation of the inner function is no
67 longer tied to a real stack frame, the nonlocal frame struct can be
68 allocated anywhere. Which means that the outer function is now
69 inlinable.
71 Theory of operation here is very simple. Iterate over all the
72 statements in all the functions (depth first) several times,
73 allocating structures and fields on demand. In general we want to
74 examine inner functions first, so that we can avoid making changes
75 to outer functions which are unnecessary.
77 The order of the passes matters a bit, in that later passes will be
78 skipped if it is discovered that the functions don't actually interact
79 at all. That is, they're nested in the lexical sense but could have
80 been written as independent functions without change. */
83 struct nesting_info
85 struct nesting_info *outer;
86 struct nesting_info *inner;
87 struct nesting_info *next;
89 struct pointer_map_t *field_map;
90 struct pointer_map_t *var_map;
91 struct pointer_set_t *mem_refs;
92 bitmap suppress_expansion;
94 tree context;
95 tree new_local_var_chain;
96 tree debug_var_chain;
97 tree frame_type;
98 tree frame_decl;
99 tree chain_field;
100 tree chain_decl;
101 tree nl_goto_field;
103 bool any_parm_remapped;
104 bool any_tramp_created;
105 char static_chain_added;
109 /* Iterate over the nesting tree, starting with ROOT, depth first. */
111 static inline struct nesting_info *
112 iter_nestinfo_start (struct nesting_info *root)
114 while (root->inner)
115 root = root->inner;
116 return root;
119 static inline struct nesting_info *
120 iter_nestinfo_next (struct nesting_info *node)
122 if (node->next)
123 return iter_nestinfo_start (node->next);
124 return node->outer;
127 #define FOR_EACH_NEST_INFO(I, ROOT) \
128 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
130 /* Obstack used for the bitmaps in the struct above. */
131 static struct bitmap_obstack nesting_info_bitmap_obstack;
134 /* We're working in so many different function contexts simultaneously,
135 that create_tmp_var is dangerous. Prevent mishap. */
136 #define create_tmp_var cant_use_create_tmp_var_here_dummy
138 /* Like create_tmp_var, except record the variable for registration at
139 the given nesting level. */
141 static tree
142 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
144 tree tmp_var;
146 /* If the type is of variable size or a type which must be created by the
147 frontend, something is wrong. Note that we explicitly allow
148 incomplete types here, since we create them ourselves here. */
149 gcc_assert (!TREE_ADDRESSABLE (type));
150 gcc_assert (!TYPE_SIZE_UNIT (type)
151 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
153 tmp_var = create_tmp_var_raw (type, prefix);
154 DECL_CONTEXT (tmp_var) = info->context;
155 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
156 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
157 if (TREE_CODE (type) == COMPLEX_TYPE
158 || TREE_CODE (type) == VECTOR_TYPE)
159 DECL_GIMPLE_REG_P (tmp_var) = 1;
161 info->new_local_var_chain = tmp_var;
163 return tmp_var;
166 /* Take the address of EXP to be used within function CONTEXT.
167 Mark it for addressability as necessary. */
169 tree
170 build_addr (tree exp, tree context)
172 tree base = exp;
173 tree save_context;
174 tree retval;
176 while (handled_component_p (base))
177 base = TREE_OPERAND (base, 0);
179 if (DECL_P (base))
180 TREE_ADDRESSABLE (base) = 1;
182 /* Building the ADDR_EXPR will compute a set of properties for
183 that ADDR_EXPR. Those properties are unfortunately context
184 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
186 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
187 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
188 way the properties are for the ADDR_EXPR are computed properly. */
189 save_context = current_function_decl;
190 current_function_decl = context;
191 retval = build_fold_addr_expr (exp);
192 current_function_decl = save_context;
193 return retval;
196 /* Insert FIELD into TYPE, sorted by alignment requirements. */
198 void
199 insert_field_into_struct (tree type, tree field)
201 tree *p;
203 DECL_CONTEXT (field) = type;
205 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
206 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
207 break;
209 DECL_CHAIN (field) = *p;
210 *p = field;
212 /* Set correct alignment for frame struct type. */
213 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
214 TYPE_ALIGN (type) = DECL_ALIGN (field);
217 /* Build or return the RECORD_TYPE that describes the frame state that is
218 shared between INFO->CONTEXT and its nested functions. This record will
219 not be complete until finalize_nesting_tree; up until that point we'll
220 be adding fields as necessary.
222 We also build the DECL that represents this frame in the function. */
224 static tree
225 get_frame_type (struct nesting_info *info)
227 tree type = info->frame_type;
228 if (!type)
230 char *name;
232 type = make_node (RECORD_TYPE);
234 name = concat ("FRAME.",
235 IDENTIFIER_POINTER (DECL_NAME (info->context)),
236 NULL);
237 TYPE_NAME (type) = get_identifier (name);
238 free (name);
240 info->frame_type = type;
241 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
242 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
244 /* ??? Always make it addressable for now, since it is meant to
245 be pointed to by the static chain pointer. This pessimizes
246 when it turns out that no static chains are needed because
247 the nested functions referencing non-local variables are not
248 reachable, but the true pessimization is to create the non-
249 local frame structure in the first place. */
250 TREE_ADDRESSABLE (info->frame_decl) = 1;
252 return type;
255 /* Return true if DECL should be referenced by pointer in the non-local
256 frame structure. */
258 static bool
259 use_pointer_in_frame (tree decl)
261 if (TREE_CODE (decl) == PARM_DECL)
263 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
264 sized decls, and inefficient to copy large aggregates. Don't bother
265 moving anything but scalar variables. */
266 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
268 else
270 /* Variable sized types make things "interesting" in the frame. */
271 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
275 /* Given DECL, a non-locally accessed variable, find or create a field
276 in the non-local frame structure for the given nesting context. */
278 static tree
279 lookup_field_for_decl (struct nesting_info *info, tree decl,
280 enum insert_option insert)
282 void **slot;
284 if (insert == NO_INSERT)
286 slot = pointer_map_contains (info->field_map, decl);
287 return slot ? (tree) *slot : NULL_TREE;
290 slot = pointer_map_insert (info->field_map, decl);
291 if (!*slot)
293 tree field = make_node (FIELD_DECL);
294 DECL_NAME (field) = DECL_NAME (decl);
296 if (use_pointer_in_frame (decl))
298 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
299 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
300 DECL_NONADDRESSABLE_P (field) = 1;
302 else
304 TREE_TYPE (field) = TREE_TYPE (decl);
305 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
306 DECL_ALIGN (field) = DECL_ALIGN (decl);
307 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
308 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
309 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
310 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
313 insert_field_into_struct (get_frame_type (info), field);
314 *slot = field;
316 if (TREE_CODE (decl) == PARM_DECL)
317 info->any_parm_remapped = true;
320 return (tree) *slot;
323 /* Build or return the variable that holds the static chain within
324 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
326 static tree
327 get_chain_decl (struct nesting_info *info)
329 tree decl = info->chain_decl;
331 if (!decl)
333 tree type;
335 type = get_frame_type (info->outer);
336 type = build_pointer_type (type);
338 /* Note that this variable is *not* entered into any BIND_EXPR;
339 the construction of this variable is handled specially in
340 expand_function_start and initialize_inlined_parameters.
341 Note also that it's represented as a parameter. This is more
342 close to the truth, since the initial value does come from
343 the caller. */
344 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
345 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
346 DECL_ARTIFICIAL (decl) = 1;
347 DECL_IGNORED_P (decl) = 1;
348 TREE_USED (decl) = 1;
349 DECL_CONTEXT (decl) = info->context;
350 DECL_ARG_TYPE (decl) = type;
352 /* Tell tree-inline.c that we never write to this variable, so
353 it can copy-prop the replacement value immediately. */
354 TREE_READONLY (decl) = 1;
356 info->chain_decl = decl;
358 if (dump_file
359 && (dump_flags & TDF_DETAILS)
360 && !DECL_STATIC_CHAIN (info->context))
361 fprintf (dump_file, "Setting static-chain for %s\n",
362 lang_hooks.decl_printable_name (info->context, 2));
364 DECL_STATIC_CHAIN (info->context) = 1;
366 return decl;
369 /* Build or return the field within the non-local frame state that holds
370 the static chain for INFO->CONTEXT. This is the way to walk back up
371 multiple nesting levels. */
373 static tree
374 get_chain_field (struct nesting_info *info)
376 tree field = info->chain_field;
378 if (!field)
380 tree type = build_pointer_type (get_frame_type (info->outer));
382 field = make_node (FIELD_DECL);
383 DECL_NAME (field) = get_identifier ("__chain");
384 TREE_TYPE (field) = type;
385 DECL_ALIGN (field) = TYPE_ALIGN (type);
386 DECL_NONADDRESSABLE_P (field) = 1;
388 insert_field_into_struct (get_frame_type (info), field);
390 info->chain_field = field;
392 if (dump_file
393 && (dump_flags & TDF_DETAILS)
394 && !DECL_STATIC_CHAIN (info->context))
395 fprintf (dump_file, "Setting static-chain for %s\n",
396 lang_hooks.decl_printable_name (info->context, 2));
398 DECL_STATIC_CHAIN (info->context) = 1;
400 return field;
403 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
405 static tree
406 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
407 gimple call)
409 tree t;
411 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
412 gimple_call_set_lhs (call, t);
413 if (! gsi_end_p (*gsi))
414 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
415 gsi_insert_before (gsi, call, GSI_SAME_STMT);
417 return t;
421 /* Copy EXP into a temporary. Allocate the temporary in the context of
422 INFO and insert the initialization statement before GSI. */
424 static tree
425 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
427 tree t;
428 gimple stmt;
430 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
431 stmt = gimple_build_assign (t, exp);
432 if (! gsi_end_p (*gsi))
433 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
434 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
436 return t;
440 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
442 static tree
443 gsi_gimplify_val (struct nesting_info *info, tree exp,
444 gimple_stmt_iterator *gsi)
446 if (is_gimple_val (exp))
447 return exp;
448 else
449 return init_tmp_var (info, exp, gsi);
452 /* Similarly, but copy from the temporary and insert the statement
453 after the iterator. */
455 static tree
456 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
458 tree t;
459 gimple stmt;
461 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
462 stmt = gimple_build_assign (exp, t);
463 if (! gsi_end_p (*gsi))
464 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
465 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
467 return t;
470 /* Build or return the type used to represent a nested function trampoline. */
472 static GTY(()) tree trampoline_type;
474 static tree
475 get_trampoline_type (struct nesting_info *info)
477 unsigned align, size;
478 tree t;
480 if (trampoline_type)
481 return trampoline_type;
483 align = TRAMPOLINE_ALIGNMENT;
484 size = TRAMPOLINE_SIZE;
486 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
487 then allocate extra space so that we can do dynamic alignment. */
488 if (align > STACK_BOUNDARY)
490 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
491 align = STACK_BOUNDARY;
494 t = build_index_type (size_int (size - 1));
495 t = build_array_type (char_type_node, t);
496 t = build_decl (DECL_SOURCE_LOCATION (info->context),
497 FIELD_DECL, get_identifier ("__data"), t);
498 DECL_ALIGN (t) = align;
499 DECL_USER_ALIGN (t) = 1;
501 trampoline_type = make_node (RECORD_TYPE);
502 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
503 TYPE_FIELDS (trampoline_type) = t;
504 layout_type (trampoline_type);
505 DECL_CONTEXT (t) = trampoline_type;
507 return trampoline_type;
510 /* Given DECL, a nested function, find or create a field in the non-local
511 frame structure for a trampoline for this function. */
513 static tree
514 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
515 enum insert_option insert)
517 void **slot;
519 if (insert == NO_INSERT)
521 slot = pointer_map_contains (info->var_map, decl);
522 return slot ? (tree) *slot : NULL_TREE;
525 slot = pointer_map_insert (info->var_map, decl);
526 if (!*slot)
528 tree field = make_node (FIELD_DECL);
529 DECL_NAME (field) = DECL_NAME (decl);
530 TREE_TYPE (field) = get_trampoline_type (info);
531 TREE_ADDRESSABLE (field) = 1;
533 insert_field_into_struct (get_frame_type (info), field);
534 *slot = field;
536 info->any_tramp_created = true;
539 return (tree) *slot;
542 /* Build or return the field within the non-local frame state that holds
543 the non-local goto "jmp_buf". The buffer itself is maintained by the
544 rtl middle-end as dynamic stack space is allocated. */
546 static tree
547 get_nl_goto_field (struct nesting_info *info)
549 tree field = info->nl_goto_field;
550 if (!field)
552 unsigned size;
553 tree type;
555 /* For __builtin_nonlocal_goto, we need N words. The first is the
556 frame pointer, the rest is for the target's stack pointer save
557 area. The number of words is controlled by STACK_SAVEAREA_MODE;
558 not the best interface, but it'll do for now. */
559 if (Pmode == ptr_mode)
560 type = ptr_type_node;
561 else
562 type = lang_hooks.types.type_for_mode (Pmode, 1);
564 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
565 size = size / GET_MODE_SIZE (Pmode);
566 size = size + 1;
568 type = build_array_type
569 (type, build_index_type (size_int (size)));
571 field = make_node (FIELD_DECL);
572 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
573 TREE_TYPE (field) = type;
574 DECL_ALIGN (field) = TYPE_ALIGN (type);
575 TREE_ADDRESSABLE (field) = 1;
577 insert_field_into_struct (get_frame_type (info), field);
579 info->nl_goto_field = field;
582 return field;
585 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
587 static void
588 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
589 struct nesting_info *info, gimple_seq *pseq)
591 struct walk_stmt_info wi;
593 memset (&wi, 0, sizeof (wi));
594 wi.info = info;
595 wi.val_only = true;
596 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
600 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
602 static inline void
603 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
604 struct nesting_info *info)
606 gimple_seq body = gimple_body (info->context);
607 walk_body (callback_stmt, callback_op, info, &body);
608 gimple_set_body (info->context, body);
611 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
613 static void
614 walk_gimple_omp_for (gimple for_stmt,
615 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
616 struct nesting_info *info)
618 struct walk_stmt_info wi;
619 gimple_seq seq;
620 tree t;
621 size_t i;
623 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
625 seq = NULL;
626 memset (&wi, 0, sizeof (wi));
627 wi.info = info;
628 wi.gsi = gsi_last (seq);
630 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
632 wi.val_only = false;
633 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
634 &wi, NULL);
635 wi.val_only = true;
636 wi.is_lhs = false;
637 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
638 &wi, NULL);
640 wi.val_only = true;
641 wi.is_lhs = false;
642 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
643 &wi, NULL);
645 t = gimple_omp_for_incr (for_stmt, i);
646 gcc_assert (BINARY_CLASS_P (t));
647 wi.val_only = false;
648 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
649 wi.val_only = true;
650 wi.is_lhs = false;
651 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
654 seq = gsi_seq (wi.gsi);
655 if (!gimple_seq_empty_p (seq))
657 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
658 annotate_all_with_location (seq, gimple_location (for_stmt));
659 gimple_seq_add_seq (&pre_body, seq);
660 gimple_omp_for_set_pre_body (for_stmt, pre_body);
664 /* Similarly for ROOT and all functions nested underneath, depth first. */
666 static void
667 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
668 struct nesting_info *root)
670 struct nesting_info *n;
671 FOR_EACH_NEST_INFO (n, root)
672 walk_function (callback_stmt, callback_op, n);
676 /* We have to check for a fairly pathological case. The operands of function
677 nested function are to be interpreted in the context of the enclosing
678 function. So if any are variably-sized, they will get remapped when the
679 enclosing function is inlined. But that remapping would also have to be
680 done in the types of the PARM_DECLs of the nested function, meaning the
681 argument types of that function will disagree with the arguments in the
682 calls to that function. So we'd either have to make a copy of the nested
683 function corresponding to each time the enclosing function was inlined or
684 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
685 function. The former is not practical. The latter would still require
686 detecting this case to know when to add the conversions. So, for now at
687 least, we don't inline such an enclosing function.
689 We have to do that check recursively, so here return indicating whether
690 FNDECL has such a nested function. ORIG_FN is the function we were
691 trying to inline to use for checking whether any argument is variably
692 modified by anything in it.
694 It would be better to do this in tree-inline.c so that we could give
695 the appropriate warning for why a function can't be inlined, but that's
696 too late since the nesting structure has already been flattened and
697 adding a flag just to record this fact seems a waste of a flag. */
699 static bool
700 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
702 struct cgraph_node *cgn = cgraph_get_node (fndecl);
703 tree arg;
705 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
707 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
708 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
709 return true;
711 if (check_for_nested_with_variably_modified (cgn->decl,
712 orig_fndecl))
713 return true;
716 return false;
719 /* Construct our local datastructure describing the function nesting
720 tree rooted by CGN. */
722 static struct nesting_info *
723 create_nesting_tree (struct cgraph_node *cgn)
725 struct nesting_info *info = XCNEW (struct nesting_info);
726 info->field_map = pointer_map_create ();
727 info->var_map = pointer_map_create ();
728 info->mem_refs = pointer_set_create ();
729 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
730 info->context = cgn->decl;
732 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
734 struct nesting_info *sub = create_nesting_tree (cgn);
735 sub->outer = info;
736 sub->next = info->inner;
737 info->inner = sub;
740 /* See discussion at check_for_nested_with_variably_modified for a
741 discussion of why this has to be here. */
742 if (check_for_nested_with_variably_modified (info->context, info->context))
743 DECL_UNINLINABLE (info->context) = true;
745 return info;
748 /* Return an expression computing the static chain for TARGET_CONTEXT
749 from INFO->CONTEXT. Insert any necessary computations before TSI. */
751 static tree
752 get_static_chain (struct nesting_info *info, tree target_context,
753 gimple_stmt_iterator *gsi)
755 struct nesting_info *i;
756 tree x;
758 if (info->context == target_context)
760 x = build_addr (info->frame_decl, target_context);
762 else
764 x = get_chain_decl (info);
766 for (i = info->outer; i->context != target_context; i = i->outer)
768 tree field = get_chain_field (i);
770 x = build_simple_mem_ref (x);
771 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
772 x = init_tmp_var (info, x, gsi);
776 return x;
780 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
781 frame as seen from INFO->CONTEXT. Insert any necessary computations
782 before GSI. */
784 static tree
785 get_frame_field (struct nesting_info *info, tree target_context,
786 tree field, gimple_stmt_iterator *gsi)
788 struct nesting_info *i;
789 tree x;
791 if (info->context == target_context)
793 /* Make sure frame_decl gets created. */
794 (void) get_frame_type (info);
795 x = info->frame_decl;
797 else
799 x = get_chain_decl (info);
801 for (i = info->outer; i->context != target_context; i = i->outer)
803 tree field = get_chain_field (i);
805 x = build_simple_mem_ref (x);
806 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
807 x = init_tmp_var (info, x, gsi);
810 x = build_simple_mem_ref (x);
813 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
814 return x;
817 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
819 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
820 in the nested function with DECL_VALUE_EXPR set to reference the true
821 variable in the parent function. This is used both for debug info
822 and in OpenMP lowering. */
824 static tree
825 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
827 tree target_context;
828 struct nesting_info *i;
829 tree x, field, new_decl;
830 void **slot;
832 slot = pointer_map_insert (info->var_map, decl);
834 if (*slot)
835 return (tree) *slot;
837 target_context = decl_function_context (decl);
839 /* A copy of the code in get_frame_field, but without the temporaries. */
840 if (info->context == target_context)
842 /* Make sure frame_decl gets created. */
843 (void) get_frame_type (info);
844 x = info->frame_decl;
845 i = info;
847 else
849 x = get_chain_decl (info);
850 for (i = info->outer; i->context != target_context; i = i->outer)
852 field = get_chain_field (i);
853 x = build_simple_mem_ref (x);
854 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
856 x = build_simple_mem_ref (x);
859 field = lookup_field_for_decl (i, decl, INSERT);
860 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
861 if (use_pointer_in_frame (decl))
862 x = build_simple_mem_ref (x);
864 /* ??? We should be remapping types as well, surely. */
865 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
866 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
867 DECL_CONTEXT (new_decl) = info->context;
868 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
869 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
870 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
871 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
872 TREE_READONLY (new_decl) = TREE_READONLY (decl);
873 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
874 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
875 if ((TREE_CODE (decl) == PARM_DECL
876 || TREE_CODE (decl) == RESULT_DECL
877 || TREE_CODE (decl) == VAR_DECL)
878 && DECL_BY_REFERENCE (decl))
879 DECL_BY_REFERENCE (new_decl) = 1;
881 SET_DECL_VALUE_EXPR (new_decl, x);
882 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
884 *slot = new_decl;
885 DECL_CHAIN (new_decl) = info->debug_var_chain;
886 info->debug_var_chain = new_decl;
888 if (!optimize
889 && info->context != target_context
890 && variably_modified_type_p (TREE_TYPE (decl), NULL))
891 note_nonlocal_vla_type (info, TREE_TYPE (decl));
893 return new_decl;
897 /* Callback for walk_gimple_stmt, rewrite all references to VAR
898 and PARM_DECLs that belong to outer functions.
900 The rewrite will involve some number of structure accesses back up
901 the static chain. E.g. for a variable FOO up one nesting level it'll
902 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
903 indirections apply to decls for which use_pointer_in_frame is true. */
905 static tree
906 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
908 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
909 struct nesting_info *const info = (struct nesting_info *) wi->info;
910 tree t = *tp;
912 *walk_subtrees = 0;
913 switch (TREE_CODE (t))
915 case VAR_DECL:
916 /* Non-automatic variables are never processed. */
917 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
918 break;
919 /* FALLTHRU */
921 case PARM_DECL:
922 if (decl_function_context (t) != info->context)
924 tree x;
925 wi->changed = true;
927 x = get_nonlocal_debug_decl (info, t);
928 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
930 tree target_context = decl_function_context (t);
931 struct nesting_info *i;
932 for (i = info->outer; i->context != target_context; i = i->outer)
933 continue;
934 x = lookup_field_for_decl (i, t, INSERT);
935 x = get_frame_field (info, target_context, x, &wi->gsi);
936 if (use_pointer_in_frame (t))
938 x = init_tmp_var (info, x, &wi->gsi);
939 x = build_simple_mem_ref (x);
943 if (wi->val_only)
945 if (wi->is_lhs)
946 x = save_tmp_var (info, x, &wi->gsi);
947 else
948 x = init_tmp_var (info, x, &wi->gsi);
951 *tp = x;
953 break;
955 case LABEL_DECL:
956 /* We're taking the address of a label from a parent function, but
957 this is not itself a non-local goto. Mark the label such that it
958 will not be deleted, much as we would with a label address in
959 static storage. */
960 if (decl_function_context (t) != info->context)
961 FORCED_LABEL (t) = 1;
962 break;
964 case ADDR_EXPR:
966 bool save_val_only = wi->val_only;
968 wi->val_only = false;
969 wi->is_lhs = false;
970 wi->changed = false;
971 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
972 wi->val_only = true;
974 if (wi->changed)
976 tree save_context;
978 /* If we changed anything, we might no longer be directly
979 referencing a decl. */
980 save_context = current_function_decl;
981 current_function_decl = info->context;
982 recompute_tree_invariant_for_addr_expr (t);
983 current_function_decl = save_context;
985 /* If the callback converted the address argument in a context
986 where we only accept variables (and min_invariant, presumably),
987 then compute the address into a temporary. */
988 if (save_val_only)
989 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
990 t, &wi->gsi);
993 break;
995 case REALPART_EXPR:
996 case IMAGPART_EXPR:
997 case COMPONENT_REF:
998 case ARRAY_REF:
999 case ARRAY_RANGE_REF:
1000 case BIT_FIELD_REF:
1001 /* Go down this entire nest and just look at the final prefix and
1002 anything that describes the references. Otherwise, we lose track
1003 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1004 wi->val_only = true;
1005 wi->is_lhs = false;
1006 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1008 if (TREE_CODE (t) == COMPONENT_REF)
1009 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1010 NULL);
1011 else if (TREE_CODE (t) == ARRAY_REF
1012 || TREE_CODE (t) == ARRAY_RANGE_REF)
1014 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1015 wi, NULL);
1016 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1017 wi, NULL);
1018 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1019 wi, NULL);
1022 wi->val_only = false;
1023 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1024 break;
1026 case VIEW_CONVERT_EXPR:
1027 /* Just request to look at the subtrees, leaving val_only and lhs
1028 untouched. This might actually be for !val_only + lhs, in which
1029 case we don't want to force a replacement by a temporary. */
1030 *walk_subtrees = 1;
1031 break;
1033 default:
1034 if (!IS_TYPE_OR_DECL_P (t))
1036 *walk_subtrees = 1;
1037 wi->val_only = true;
1038 wi->is_lhs = false;
1040 break;
1043 return NULL_TREE;
1046 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1047 struct walk_stmt_info *);
1049 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1050 and PARM_DECLs that belong to outer functions. */
1052 static bool
1053 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1055 struct nesting_info *const info = (struct nesting_info *) wi->info;
1056 bool need_chain = false, need_stmts = false;
1057 tree clause, decl;
1058 int dummy;
1059 bitmap new_suppress;
1061 new_suppress = BITMAP_GGC_ALLOC ();
1062 bitmap_copy (new_suppress, info->suppress_expansion);
1064 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1066 switch (OMP_CLAUSE_CODE (clause))
1068 case OMP_CLAUSE_REDUCTION:
1069 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1070 need_stmts = true;
1071 goto do_decl_clause;
1073 case OMP_CLAUSE_LASTPRIVATE:
1074 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1075 need_stmts = true;
1076 goto do_decl_clause;
1078 case OMP_CLAUSE_PRIVATE:
1079 case OMP_CLAUSE_FIRSTPRIVATE:
1080 case OMP_CLAUSE_COPYPRIVATE:
1081 case OMP_CLAUSE_SHARED:
1082 do_decl_clause:
1083 decl = OMP_CLAUSE_DECL (clause);
1084 if (TREE_CODE (decl) == VAR_DECL
1085 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1086 break;
1087 if (decl_function_context (decl) != info->context)
1089 bitmap_set_bit (new_suppress, DECL_UID (decl));
1090 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1091 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1092 need_chain = true;
1094 break;
1096 case OMP_CLAUSE_SCHEDULE:
1097 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1098 break;
1099 /* FALLTHRU */
1100 case OMP_CLAUSE_FINAL:
1101 case OMP_CLAUSE_IF:
1102 case OMP_CLAUSE_NUM_THREADS:
1103 wi->val_only = true;
1104 wi->is_lhs = false;
1105 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1106 &dummy, wi);
1107 break;
1109 case OMP_CLAUSE_NOWAIT:
1110 case OMP_CLAUSE_ORDERED:
1111 case OMP_CLAUSE_DEFAULT:
1112 case OMP_CLAUSE_COPYIN:
1113 case OMP_CLAUSE_COLLAPSE:
1114 case OMP_CLAUSE_UNTIED:
1115 case OMP_CLAUSE_MERGEABLE:
1116 break;
1118 default:
1119 gcc_unreachable ();
1123 info->suppress_expansion = new_suppress;
1125 if (need_stmts)
1126 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1127 switch (OMP_CLAUSE_CODE (clause))
1129 case OMP_CLAUSE_REDUCTION:
1130 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1132 tree old_context
1133 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1134 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1135 = info->context;
1136 walk_body (convert_nonlocal_reference_stmt,
1137 convert_nonlocal_reference_op, info,
1138 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1139 walk_body (convert_nonlocal_reference_stmt,
1140 convert_nonlocal_reference_op, info,
1141 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1142 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1143 = old_context;
1145 break;
1147 case OMP_CLAUSE_LASTPRIVATE:
1148 walk_body (convert_nonlocal_reference_stmt,
1149 convert_nonlocal_reference_op, info,
1150 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1151 break;
1153 default:
1154 break;
1157 return need_chain;
1160 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1162 static void
1163 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1165 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1166 type = TREE_TYPE (type);
1168 if (TYPE_NAME (type)
1169 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1170 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1171 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1173 while (POINTER_TYPE_P (type)
1174 || TREE_CODE (type) == VECTOR_TYPE
1175 || TREE_CODE (type) == FUNCTION_TYPE
1176 || TREE_CODE (type) == METHOD_TYPE)
1177 type = TREE_TYPE (type);
1179 if (TREE_CODE (type) == ARRAY_TYPE)
1181 tree domain, t;
1183 note_nonlocal_vla_type (info, TREE_TYPE (type));
1184 domain = TYPE_DOMAIN (type);
1185 if (domain)
1187 t = TYPE_MIN_VALUE (domain);
1188 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1189 && decl_function_context (t) != info->context)
1190 get_nonlocal_debug_decl (info, t);
1191 t = TYPE_MAX_VALUE (domain);
1192 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1193 && decl_function_context (t) != info->context)
1194 get_nonlocal_debug_decl (info, t);
1199 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1200 in BLOCK. */
1202 static void
1203 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1205 tree var;
1207 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1208 if (TREE_CODE (var) == VAR_DECL
1209 && variably_modified_type_p (TREE_TYPE (var), NULL)
1210 && DECL_HAS_VALUE_EXPR_P (var)
1211 && decl_function_context (var) != info->context)
1212 note_nonlocal_vla_type (info, TREE_TYPE (var));
1215 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1216 PARM_DECLs that belong to outer functions. This handles statements
1217 that are not handled via the standard recursion done in
1218 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1219 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1220 operands of STMT have been handled by this function. */
1222 static tree
1223 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1224 struct walk_stmt_info *wi)
1226 struct nesting_info *info = (struct nesting_info *) wi->info;
1227 tree save_local_var_chain;
1228 bitmap save_suppress;
1229 gimple stmt = gsi_stmt (*gsi);
1231 switch (gimple_code (stmt))
1233 case GIMPLE_GOTO:
1234 /* Don't walk non-local gotos for now. */
1235 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1237 wi->val_only = true;
1238 wi->is_lhs = false;
1239 *handled_ops_p = true;
1240 return NULL_TREE;
1242 break;
1244 case GIMPLE_OMP_PARALLEL:
1245 case GIMPLE_OMP_TASK:
1246 save_suppress = info->suppress_expansion;
1247 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1248 wi))
1250 tree c, decl;
1251 decl = get_chain_decl (info);
1252 c = build_omp_clause (gimple_location (stmt),
1253 OMP_CLAUSE_FIRSTPRIVATE);
1254 OMP_CLAUSE_DECL (c) = decl;
1255 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1256 gimple_omp_taskreg_set_clauses (stmt, c);
1259 save_local_var_chain = info->new_local_var_chain;
1260 info->new_local_var_chain = NULL;
1262 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1263 info, gimple_omp_body_ptr (stmt));
1265 if (info->new_local_var_chain)
1266 declare_vars (info->new_local_var_chain,
1267 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1268 false);
1269 info->new_local_var_chain = save_local_var_chain;
1270 info->suppress_expansion = save_suppress;
1271 break;
1273 case GIMPLE_OMP_FOR:
1274 save_suppress = info->suppress_expansion;
1275 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1276 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1277 convert_nonlocal_reference_op, info);
1278 walk_body (convert_nonlocal_reference_stmt,
1279 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1280 info->suppress_expansion = save_suppress;
1281 break;
1283 case GIMPLE_OMP_SECTIONS:
1284 save_suppress = info->suppress_expansion;
1285 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1286 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1287 info, gimple_omp_body_ptr (stmt));
1288 info->suppress_expansion = save_suppress;
1289 break;
1291 case GIMPLE_OMP_SINGLE:
1292 save_suppress = info->suppress_expansion;
1293 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1294 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1295 info, gimple_omp_body_ptr (stmt));
1296 info->suppress_expansion = save_suppress;
1297 break;
1299 case GIMPLE_OMP_TARGET:
1300 save_suppress = info->suppress_expansion;
1301 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1302 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1303 info, gimple_omp_body_ptr (stmt));
1304 info->suppress_expansion = save_suppress;
1305 break;
1307 case GIMPLE_OMP_TEAMS:
1308 save_suppress = info->suppress_expansion;
1309 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1310 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1311 info, gimple_omp_body_ptr (stmt));
1312 info->suppress_expansion = save_suppress;
1313 break;
1315 case GIMPLE_OMP_SECTION:
1316 case GIMPLE_OMP_MASTER:
1317 case GIMPLE_OMP_TASKGROUP:
1318 case GIMPLE_OMP_ORDERED:
1319 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1320 info, gimple_omp_body_ptr (stmt));
1321 break;
1323 case GIMPLE_BIND:
1324 if (!optimize && gimple_bind_block (stmt))
1325 note_nonlocal_block_vlas (info, gimple_bind_block (stmt));
1327 *handled_ops_p = false;
1328 return NULL_TREE;
1330 case GIMPLE_COND:
1331 wi->val_only = true;
1332 wi->is_lhs = false;
1333 *handled_ops_p = false;
1334 return NULL_TREE;
1336 default:
1337 /* For every other statement that we are not interested in
1338 handling here, let the walker traverse the operands. */
1339 *handled_ops_p = false;
1340 return NULL_TREE;
1343 /* We have handled all of STMT operands, no need to traverse the operands. */
1344 *handled_ops_p = true;
1345 return NULL_TREE;
1349 /* A subroutine of convert_local_reference. Create a local variable
1350 in the parent function with DECL_VALUE_EXPR set to reference the
1351 field in FRAME. This is used both for debug info and in OpenMP
1352 lowering. */
1354 static tree
1355 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1357 tree x, new_decl;
1358 void **slot;
1360 slot = pointer_map_insert (info->var_map, decl);
1361 if (*slot)
1362 return (tree) *slot;
1364 /* Make sure frame_decl gets created. */
1365 (void) get_frame_type (info);
1366 x = info->frame_decl;
1367 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1369 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1370 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1371 DECL_CONTEXT (new_decl) = info->context;
1372 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1373 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1374 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1375 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1376 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1377 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1378 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1379 if ((TREE_CODE (decl) == PARM_DECL
1380 || TREE_CODE (decl) == RESULT_DECL
1381 || TREE_CODE (decl) == VAR_DECL)
1382 && DECL_BY_REFERENCE (decl))
1383 DECL_BY_REFERENCE (new_decl) = 1;
1385 SET_DECL_VALUE_EXPR (new_decl, x);
1386 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1387 *slot = new_decl;
1389 DECL_CHAIN (new_decl) = info->debug_var_chain;
1390 info->debug_var_chain = new_decl;
1392 /* Do not emit debug info twice. */
1393 DECL_IGNORED_P (decl) = 1;
1395 return new_decl;
1399 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1400 and PARM_DECLs that were referenced by inner nested functions.
1401 The rewrite will be a structure reference to the local frame variable. */
1403 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1405 static tree
1406 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1408 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1409 struct nesting_info *const info = (struct nesting_info *) wi->info;
1410 tree t = *tp, field, x;
1411 bool save_val_only;
1413 *walk_subtrees = 0;
1414 switch (TREE_CODE (t))
1416 case VAR_DECL:
1417 /* Non-automatic variables are never processed. */
1418 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1419 break;
1420 /* FALLTHRU */
1422 case PARM_DECL:
1423 if (decl_function_context (t) == info->context)
1425 /* If we copied a pointer to the frame, then the original decl
1426 is used unchanged in the parent function. */
1427 if (use_pointer_in_frame (t))
1428 break;
1430 /* No need to transform anything if no child references the
1431 variable. */
1432 field = lookup_field_for_decl (info, t, NO_INSERT);
1433 if (!field)
1434 break;
1435 wi->changed = true;
1437 x = get_local_debug_decl (info, t, field);
1438 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1439 x = get_frame_field (info, info->context, field, &wi->gsi);
1441 if (wi->val_only)
1443 if (wi->is_lhs)
1444 x = save_tmp_var (info, x, &wi->gsi);
1445 else
1446 x = init_tmp_var (info, x, &wi->gsi);
1449 *tp = x;
1451 break;
1453 case ADDR_EXPR:
1454 save_val_only = wi->val_only;
1455 wi->val_only = false;
1456 wi->is_lhs = false;
1457 wi->changed = false;
1458 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1459 wi->val_only = save_val_only;
1461 /* If we converted anything ... */
1462 if (wi->changed)
1464 tree save_context;
1466 /* Then the frame decl is now addressable. */
1467 TREE_ADDRESSABLE (info->frame_decl) = 1;
1469 save_context = current_function_decl;
1470 current_function_decl = info->context;
1471 recompute_tree_invariant_for_addr_expr (t);
1472 current_function_decl = save_context;
1474 /* If we are in a context where we only accept values, then
1475 compute the address into a temporary. */
1476 if (save_val_only)
1477 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1478 t, &wi->gsi);
1480 break;
1482 case REALPART_EXPR:
1483 case IMAGPART_EXPR:
1484 case COMPONENT_REF:
1485 case ARRAY_REF:
1486 case ARRAY_RANGE_REF:
1487 case BIT_FIELD_REF:
1488 /* Go down this entire nest and just look at the final prefix and
1489 anything that describes the references. Otherwise, we lose track
1490 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1491 save_val_only = wi->val_only;
1492 wi->val_only = true;
1493 wi->is_lhs = false;
1494 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1496 if (TREE_CODE (t) == COMPONENT_REF)
1497 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1498 NULL);
1499 else if (TREE_CODE (t) == ARRAY_REF
1500 || TREE_CODE (t) == ARRAY_RANGE_REF)
1502 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1503 NULL);
1504 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1505 NULL);
1506 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1507 NULL);
1510 wi->val_only = false;
1511 walk_tree (tp, convert_local_reference_op, wi, NULL);
1512 wi->val_only = save_val_only;
1513 break;
1515 case MEM_REF:
1516 save_val_only = wi->val_only;
1517 wi->val_only = true;
1518 wi->is_lhs = false;
1519 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1520 wi, NULL);
1521 /* We need to re-fold the MEM_REF as component references as
1522 part of a ADDR_EXPR address are not allowed. But we cannot
1523 fold here, as the chain record type is not yet finalized. */
1524 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1525 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1526 pointer_set_insert (info->mem_refs, tp);
1527 wi->val_only = save_val_only;
1528 break;
1530 case VIEW_CONVERT_EXPR:
1531 /* Just request to look at the subtrees, leaving val_only and lhs
1532 untouched. This might actually be for !val_only + lhs, in which
1533 case we don't want to force a replacement by a temporary. */
1534 *walk_subtrees = 1;
1535 break;
1537 default:
1538 if (!IS_TYPE_OR_DECL_P (t))
1540 *walk_subtrees = 1;
1541 wi->val_only = true;
1542 wi->is_lhs = false;
1544 break;
1547 return NULL_TREE;
1550 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1551 struct walk_stmt_info *);
1553 /* Helper for convert_local_reference. Convert all the references in
1554 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1556 static bool
1557 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1559 struct nesting_info *const info = (struct nesting_info *) wi->info;
1560 bool need_frame = false, need_stmts = false;
1561 tree clause, decl;
1562 int dummy;
1563 bitmap new_suppress;
1565 new_suppress = BITMAP_GGC_ALLOC ();
1566 bitmap_copy (new_suppress, info->suppress_expansion);
1568 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1570 switch (OMP_CLAUSE_CODE (clause))
1572 case OMP_CLAUSE_REDUCTION:
1573 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1574 need_stmts = true;
1575 goto do_decl_clause;
1577 case OMP_CLAUSE_LASTPRIVATE:
1578 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1579 need_stmts = true;
1580 goto do_decl_clause;
1582 case OMP_CLAUSE_PRIVATE:
1583 case OMP_CLAUSE_FIRSTPRIVATE:
1584 case OMP_CLAUSE_COPYPRIVATE:
1585 case OMP_CLAUSE_SHARED:
1586 do_decl_clause:
1587 decl = OMP_CLAUSE_DECL (clause);
1588 if (TREE_CODE (decl) == VAR_DECL
1589 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1590 break;
1591 if (decl_function_context (decl) == info->context
1592 && !use_pointer_in_frame (decl))
1594 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1595 if (field)
1597 bitmap_set_bit (new_suppress, DECL_UID (decl));
1598 OMP_CLAUSE_DECL (clause)
1599 = get_local_debug_decl (info, decl, field);
1600 need_frame = true;
1603 break;
1605 case OMP_CLAUSE_SCHEDULE:
1606 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1607 break;
1608 /* FALLTHRU */
1609 case OMP_CLAUSE_FINAL:
1610 case OMP_CLAUSE_IF:
1611 case OMP_CLAUSE_NUM_THREADS:
1612 wi->val_only = true;
1613 wi->is_lhs = false;
1614 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1615 wi);
1616 break;
1618 case OMP_CLAUSE_NOWAIT:
1619 case OMP_CLAUSE_ORDERED:
1620 case OMP_CLAUSE_DEFAULT:
1621 case OMP_CLAUSE_COPYIN:
1622 case OMP_CLAUSE_COLLAPSE:
1623 case OMP_CLAUSE_UNTIED:
1624 case OMP_CLAUSE_MERGEABLE:
1625 break;
1627 default:
1628 gcc_unreachable ();
1632 info->suppress_expansion = new_suppress;
1634 if (need_stmts)
1635 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1636 switch (OMP_CLAUSE_CODE (clause))
1638 case OMP_CLAUSE_REDUCTION:
1639 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1641 tree old_context
1642 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1643 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1644 = info->context;
1645 walk_body (convert_local_reference_stmt,
1646 convert_local_reference_op, info,
1647 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1648 walk_body (convert_local_reference_stmt,
1649 convert_local_reference_op, info,
1650 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1651 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1652 = old_context;
1654 break;
1656 case OMP_CLAUSE_LASTPRIVATE:
1657 walk_body (convert_local_reference_stmt,
1658 convert_local_reference_op, info,
1659 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1660 break;
1662 default:
1663 break;
1666 return need_frame;
1670 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1671 and PARM_DECLs that were referenced by inner nested functions.
1672 The rewrite will be a structure reference to the local frame variable. */
1674 static tree
1675 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1676 struct walk_stmt_info *wi)
1678 struct nesting_info *info = (struct nesting_info *) wi->info;
1679 tree save_local_var_chain;
1680 bitmap save_suppress;
1681 gimple stmt = gsi_stmt (*gsi);
1683 switch (gimple_code (stmt))
1685 case GIMPLE_OMP_PARALLEL:
1686 case GIMPLE_OMP_TASK:
1687 save_suppress = info->suppress_expansion;
1688 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1689 wi))
1691 tree c;
1692 (void) get_frame_type (info);
1693 c = build_omp_clause (gimple_location (stmt),
1694 OMP_CLAUSE_SHARED);
1695 OMP_CLAUSE_DECL (c) = info->frame_decl;
1696 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1697 gimple_omp_taskreg_set_clauses (stmt, c);
1700 save_local_var_chain = info->new_local_var_chain;
1701 info->new_local_var_chain = NULL;
1703 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1704 gimple_omp_body_ptr (stmt));
1706 if (info->new_local_var_chain)
1707 declare_vars (info->new_local_var_chain,
1708 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1709 info->new_local_var_chain = save_local_var_chain;
1710 info->suppress_expansion = save_suppress;
1711 break;
1713 case GIMPLE_OMP_FOR:
1714 save_suppress = info->suppress_expansion;
1715 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1716 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1717 convert_local_reference_op, info);
1718 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1719 info, gimple_omp_body_ptr (stmt));
1720 info->suppress_expansion = save_suppress;
1721 break;
1723 case GIMPLE_OMP_SECTIONS:
1724 save_suppress = info->suppress_expansion;
1725 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1726 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1727 info, gimple_omp_body_ptr (stmt));
1728 info->suppress_expansion = save_suppress;
1729 break;
1731 case GIMPLE_OMP_SINGLE:
1732 save_suppress = info->suppress_expansion;
1733 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1734 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1735 info, gimple_omp_body_ptr (stmt));
1736 info->suppress_expansion = save_suppress;
1737 break;
1739 case GIMPLE_OMP_TARGET:
1740 save_suppress = info->suppress_expansion;
1741 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1742 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1743 info, gimple_omp_body_ptr (stmt));
1744 info->suppress_expansion = save_suppress;
1745 break;
1747 case GIMPLE_OMP_TEAMS:
1748 save_suppress = info->suppress_expansion;
1749 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1750 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1751 info, gimple_omp_body_ptr (stmt));
1752 info->suppress_expansion = save_suppress;
1753 break;
1755 case GIMPLE_OMP_SECTION:
1756 case GIMPLE_OMP_MASTER:
1757 case GIMPLE_OMP_TASKGROUP:
1758 case GIMPLE_OMP_ORDERED:
1759 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1760 info, gimple_omp_body_ptr (stmt));
1761 break;
1763 case GIMPLE_COND:
1764 wi->val_only = true;
1765 wi->is_lhs = false;
1766 *handled_ops_p = false;
1767 return NULL_TREE;
1769 case GIMPLE_ASSIGN:
1770 if (gimple_clobber_p (stmt))
1772 tree lhs = gimple_assign_lhs (stmt);
1773 if (!use_pointer_in_frame (lhs)
1774 && lookup_field_for_decl (info, lhs, NO_INSERT))
1776 gsi_replace (gsi, gimple_build_nop (), true);
1777 break;
1780 *handled_ops_p = false;
1781 return NULL_TREE;
1783 default:
1784 /* For every other statement that we are not interested in
1785 handling here, let the walker traverse the operands. */
1786 *handled_ops_p = false;
1787 return NULL_TREE;
1790 /* Indicate that we have handled all the operands ourselves. */
1791 *handled_ops_p = true;
1792 return NULL_TREE;
1796 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1797 that reference labels from outer functions. The rewrite will be a
1798 call to __builtin_nonlocal_goto. */
1800 static tree
1801 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1802 struct walk_stmt_info *wi)
1804 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1805 tree label, new_label, target_context, x, field;
1806 void **slot;
1807 gimple call;
1808 gimple stmt = gsi_stmt (*gsi);
1810 if (gimple_code (stmt) != GIMPLE_GOTO)
1812 *handled_ops_p = false;
1813 return NULL_TREE;
1816 label = gimple_goto_dest (stmt);
1817 if (TREE_CODE (label) != LABEL_DECL)
1819 *handled_ops_p = false;
1820 return NULL_TREE;
1823 target_context = decl_function_context (label);
1824 if (target_context == info->context)
1826 *handled_ops_p = false;
1827 return NULL_TREE;
1830 for (i = info->outer; target_context != i->context; i = i->outer)
1831 continue;
1833 /* The original user label may also be use for a normal goto, therefore
1834 we must create a new label that will actually receive the abnormal
1835 control transfer. This new label will be marked LABEL_NONLOCAL; this
1836 mark will trigger proper behavior in the cfg, as well as cause the
1837 (hairy target-specific) non-local goto receiver code to be generated
1838 when we expand rtl. Enter this association into var_map so that we
1839 can insert the new label into the IL during a second pass. */
1840 slot = pointer_map_insert (i->var_map, label);
1841 if (*slot == NULL)
1843 new_label = create_artificial_label (UNKNOWN_LOCATION);
1844 DECL_NONLOCAL (new_label) = 1;
1845 *slot = new_label;
1847 else
1848 new_label = (tree) *slot;
1850 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1851 field = get_nl_goto_field (i);
1852 x = get_frame_field (info, target_context, field, gsi);
1853 x = build_addr (x, target_context);
1854 x = gsi_gimplify_val (info, x, gsi);
1855 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
1856 2, build_addr (new_label, target_context), x);
1857 gsi_replace (gsi, call, false);
1859 /* We have handled all of STMT's operands, no need to keep going. */
1860 *handled_ops_p = true;
1861 return NULL_TREE;
1865 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1866 are referenced via nonlocal goto from a nested function. The rewrite
1867 will involve installing a newly generated DECL_NONLOCAL label, and
1868 (potentially) a branch around the rtl gunk that is assumed to be
1869 attached to such a label. */
1871 static tree
1872 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1873 struct walk_stmt_info *wi)
1875 struct nesting_info *const info = (struct nesting_info *) wi->info;
1876 tree label, new_label;
1877 gimple_stmt_iterator tmp_gsi;
1878 void **slot;
1879 gimple stmt = gsi_stmt (*gsi);
1881 if (gimple_code (stmt) != GIMPLE_LABEL)
1883 *handled_ops_p = false;
1884 return NULL_TREE;
1887 label = gimple_label_label (stmt);
1889 slot = pointer_map_contains (info->var_map, label);
1890 if (!slot)
1892 *handled_ops_p = false;
1893 return NULL_TREE;
1896 /* If there's any possibility that the previous statement falls through,
1897 then we must branch around the new non-local label. */
1898 tmp_gsi = wi->gsi;
1899 gsi_prev (&tmp_gsi);
1900 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
1902 gimple stmt = gimple_build_goto (label);
1903 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1906 new_label = (tree) *slot;
1907 stmt = gimple_build_label (new_label);
1908 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1910 *handled_ops_p = true;
1911 return NULL_TREE;
1915 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1916 of nested functions that require the use of trampolines. The rewrite
1917 will involve a reference a trampoline generated for the occasion. */
1919 static tree
1920 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
1922 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1923 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1924 tree t = *tp, decl, target_context, x, builtin;
1925 gimple call;
1927 *walk_subtrees = 0;
1928 switch (TREE_CODE (t))
1930 case ADDR_EXPR:
1931 /* Build
1932 T.1 = &CHAIN->tramp;
1933 T.2 = __builtin_adjust_trampoline (T.1);
1934 T.3 = (func_type)T.2;
1937 decl = TREE_OPERAND (t, 0);
1938 if (TREE_CODE (decl) != FUNCTION_DECL)
1939 break;
1941 /* Only need to process nested functions. */
1942 target_context = decl_function_context (decl);
1943 if (!target_context)
1944 break;
1946 /* If the nested function doesn't use a static chain, then
1947 it doesn't need a trampoline. */
1948 if (!DECL_STATIC_CHAIN (decl))
1949 break;
1951 /* If we don't want a trampoline, then don't build one. */
1952 if (TREE_NO_TRAMPOLINE (t))
1953 break;
1955 /* Lookup the immediate parent of the callee, as that's where
1956 we need to insert the trampoline. */
1957 for (i = info; i->context != target_context; i = i->outer)
1958 continue;
1959 x = lookup_tramp_for_decl (i, decl, INSERT);
1961 /* Compute the address of the field holding the trampoline. */
1962 x = get_frame_field (info, target_context, x, &wi->gsi);
1963 x = build_addr (x, target_context);
1964 x = gsi_gimplify_val (info, x, &wi->gsi);
1966 /* Do machine-specific ugliness. Normally this will involve
1967 computing extra alignment, but it can really be anything. */
1968 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
1969 call = gimple_build_call (builtin, 1, x);
1970 x = init_tmp_var_with_call (info, &wi->gsi, call);
1972 /* Cast back to the proper function type. */
1973 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1974 x = init_tmp_var (info, x, &wi->gsi);
1976 *tp = x;
1977 break;
1979 default:
1980 if (!IS_TYPE_OR_DECL_P (t))
1981 *walk_subtrees = 1;
1982 break;
1985 return NULL_TREE;
1989 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1990 to addresses of nested functions that require the use of
1991 trampolines. The rewrite will involve a reference a trampoline
1992 generated for the occasion. */
1994 static tree
1995 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1996 struct walk_stmt_info *wi)
1998 struct nesting_info *info = (struct nesting_info *) wi->info;
1999 gimple stmt = gsi_stmt (*gsi);
2001 switch (gimple_code (stmt))
2003 case GIMPLE_CALL:
2005 /* Only walk call arguments, lest we generate trampolines for
2006 direct calls. */
2007 unsigned long i, nargs = gimple_call_num_args (stmt);
2008 for (i = 0; i < nargs; i++)
2009 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2010 wi, NULL);
2011 break;
2014 case GIMPLE_OMP_PARALLEL:
2015 case GIMPLE_OMP_TASK:
2017 tree save_local_var_chain;
2018 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2019 save_local_var_chain = info->new_local_var_chain;
2020 info->new_local_var_chain = NULL;
2021 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2022 info, gimple_omp_body_ptr (stmt));
2023 if (info->new_local_var_chain)
2024 declare_vars (info->new_local_var_chain,
2025 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2026 false);
2027 info->new_local_var_chain = save_local_var_chain;
2029 break;
2031 default:
2032 *handled_ops_p = false;
2033 return NULL_TREE;
2034 break;
2037 *handled_ops_p = true;
2038 return NULL_TREE;
2043 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2044 that reference nested functions to make sure that the static chain
2045 is set up properly for the call. */
2047 static tree
2048 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2049 struct walk_stmt_info *wi)
2051 struct nesting_info *const info = (struct nesting_info *) wi->info;
2052 tree decl, target_context;
2053 char save_static_chain_added;
2054 int i;
2055 gimple stmt = gsi_stmt (*gsi);
2057 switch (gimple_code (stmt))
2059 case GIMPLE_CALL:
2060 if (gimple_call_chain (stmt))
2061 break;
2062 decl = gimple_call_fndecl (stmt);
2063 if (!decl)
2064 break;
2065 target_context = decl_function_context (decl);
2066 if (target_context && DECL_STATIC_CHAIN (decl))
2068 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
2069 &wi->gsi));
2070 info->static_chain_added |= (1 << (info->context != target_context));
2072 break;
2074 case GIMPLE_OMP_PARALLEL:
2075 case GIMPLE_OMP_TASK:
2076 save_static_chain_added = info->static_chain_added;
2077 info->static_chain_added = 0;
2078 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2079 for (i = 0; i < 2; i++)
2081 tree c, decl;
2082 if ((info->static_chain_added & (1 << i)) == 0)
2083 continue;
2084 decl = i ? get_chain_decl (info) : info->frame_decl;
2085 /* Don't add CHAIN.* or FRAME.* twice. */
2086 for (c = gimple_omp_taskreg_clauses (stmt);
2088 c = OMP_CLAUSE_CHAIN (c))
2089 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2090 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2091 && OMP_CLAUSE_DECL (c) == decl)
2092 break;
2093 if (c == NULL)
2095 c = build_omp_clause (gimple_location (stmt),
2096 i ? OMP_CLAUSE_FIRSTPRIVATE
2097 : OMP_CLAUSE_SHARED);
2098 OMP_CLAUSE_DECL (c) = decl;
2099 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2100 gimple_omp_taskreg_set_clauses (stmt, c);
2103 info->static_chain_added |= save_static_chain_added;
2104 break;
2106 case GIMPLE_OMP_FOR:
2107 walk_body (convert_gimple_call, NULL, info,
2108 gimple_omp_for_pre_body_ptr (stmt));
2109 /* FALLTHRU */
2110 case GIMPLE_OMP_SECTIONS:
2111 case GIMPLE_OMP_SECTION:
2112 case GIMPLE_OMP_SINGLE:
2113 case GIMPLE_OMP_TARGET:
2114 case GIMPLE_OMP_TEAMS:
2115 case GIMPLE_OMP_MASTER:
2116 case GIMPLE_OMP_TASKGROUP:
2117 case GIMPLE_OMP_ORDERED:
2118 case GIMPLE_OMP_CRITICAL:
2119 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2120 break;
2122 default:
2123 /* Keep looking for other operands. */
2124 *handled_ops_p = false;
2125 return NULL_TREE;
2128 *handled_ops_p = true;
2129 return NULL_TREE;
2132 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2133 call expressions. At the same time, determine if a nested function
2134 actually uses its static chain; if not, remember that. */
2136 static void
2137 convert_all_function_calls (struct nesting_info *root)
2139 unsigned int chain_count = 0, old_chain_count, iter_count;
2140 struct nesting_info *n;
2142 /* First, optimistically clear static_chain for all decls that haven't
2143 used the static chain already for variable access. */
2144 FOR_EACH_NEST_INFO (n, root)
2146 tree decl = n->context;
2147 if (!n->outer || (!n->chain_decl && !n->chain_field))
2149 DECL_STATIC_CHAIN (decl) = 0;
2150 if (dump_file && (dump_flags & TDF_DETAILS))
2151 fprintf (dump_file, "Guessing no static-chain for %s\n",
2152 lang_hooks.decl_printable_name (decl, 2));
2154 else
2155 DECL_STATIC_CHAIN (decl) = 1;
2156 chain_count += DECL_STATIC_CHAIN (decl);
2159 /* Walk the functions and perform transformations. Note that these
2160 transformations can induce new uses of the static chain, which in turn
2161 require re-examining all users of the decl. */
2162 /* ??? It would make sense to try to use the call graph to speed this up,
2163 but the call graph hasn't really been built yet. Even if it did, we
2164 would still need to iterate in this loop since address-of references
2165 wouldn't show up in the callgraph anyway. */
2166 iter_count = 0;
2169 old_chain_count = chain_count;
2170 chain_count = 0;
2171 iter_count++;
2173 if (dump_file && (dump_flags & TDF_DETAILS))
2174 fputc ('\n', dump_file);
2176 FOR_EACH_NEST_INFO (n, root)
2178 tree decl = n->context;
2179 walk_function (convert_tramp_reference_stmt,
2180 convert_tramp_reference_op, n);
2181 walk_function (convert_gimple_call, NULL, n);
2182 chain_count += DECL_STATIC_CHAIN (decl);
2185 while (chain_count != old_chain_count);
2187 if (dump_file && (dump_flags & TDF_DETAILS))
2188 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2189 iter_count);
2192 struct nesting_copy_body_data
2194 copy_body_data cb;
2195 struct nesting_info *root;
2198 /* A helper subroutine for debug_var_chain type remapping. */
2200 static tree
2201 nesting_copy_decl (tree decl, copy_body_data *id)
2203 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2204 void **slot = pointer_map_contains (nid->root->var_map, decl);
2206 if (slot)
2207 return (tree) *slot;
2209 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2211 tree new_decl = copy_decl_no_change (decl, id);
2212 DECL_ORIGINAL_TYPE (new_decl)
2213 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2214 return new_decl;
2217 if (TREE_CODE (decl) == VAR_DECL
2218 || TREE_CODE (decl) == PARM_DECL
2219 || TREE_CODE (decl) == RESULT_DECL)
2220 return decl;
2222 return copy_decl_no_change (decl, id);
2225 /* A helper function for remap_vla_decls. See if *TP contains
2226 some remapped variables. */
2228 static tree
2229 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2231 struct nesting_info *root = (struct nesting_info *) data;
2232 tree t = *tp;
2233 void **slot;
2235 if (DECL_P (t))
2237 *walk_subtrees = 0;
2238 slot = pointer_map_contains (root->var_map, t);
2240 if (slot)
2241 return (tree) *slot;
2243 return NULL;
2246 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2247 involved. */
2249 static void
2250 remap_vla_decls (tree block, struct nesting_info *root)
2252 tree var, subblock, val, type;
2253 struct nesting_copy_body_data id;
2255 for (subblock = BLOCK_SUBBLOCKS (block);
2256 subblock;
2257 subblock = BLOCK_CHAIN (subblock))
2258 remap_vla_decls (subblock, root);
2260 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2261 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2263 val = DECL_VALUE_EXPR (var);
2264 type = TREE_TYPE (var);
2266 if (!(TREE_CODE (val) == INDIRECT_REF
2267 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2268 && variably_modified_type_p (type, NULL)))
2269 continue;
2271 if (pointer_map_contains (root->var_map, TREE_OPERAND (val, 0))
2272 || walk_tree (&type, contains_remapped_vars, root, NULL))
2273 break;
2276 if (var == NULL_TREE)
2277 return;
2279 memset (&id, 0, sizeof (id));
2280 id.cb.copy_decl = nesting_copy_decl;
2281 id.cb.decl_map = pointer_map_create ();
2282 id.root = root;
2284 for (; var; var = DECL_CHAIN (var))
2285 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2287 struct nesting_info *i;
2288 tree newt, context;
2289 void **slot;
2291 val = DECL_VALUE_EXPR (var);
2292 type = TREE_TYPE (var);
2294 if (!(TREE_CODE (val) == INDIRECT_REF
2295 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2296 && variably_modified_type_p (type, NULL)))
2297 continue;
2299 slot = pointer_map_contains (root->var_map, TREE_OPERAND (val, 0));
2300 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2301 continue;
2303 context = decl_function_context (var);
2304 for (i = root; i; i = i->outer)
2305 if (i->context == context)
2306 break;
2308 if (i == NULL)
2309 continue;
2311 /* Fully expand value expressions. This avoids having debug variables
2312 only referenced from them and that can be swept during GC. */
2313 if (slot)
2315 tree t = (tree) *slot;
2316 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2317 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2320 id.cb.src_fn = i->context;
2321 id.cb.dst_fn = i->context;
2322 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2324 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2325 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2327 newt = TREE_TYPE (newt);
2328 type = TREE_TYPE (type);
2330 if (TYPE_NAME (newt)
2331 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2332 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2333 && newt != type
2334 && TYPE_NAME (newt) == TYPE_NAME (type))
2335 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2337 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2338 if (val != DECL_VALUE_EXPR (var))
2339 SET_DECL_VALUE_EXPR (var, val);
2342 pointer_map_destroy (id.cb.decl_map);
2345 /* Fold the MEM_REF *E. */
2346 static bool
2347 fold_mem_refs (const void *e, void *data ATTRIBUTE_UNUSED)
2349 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2350 *ref_p = fold (*ref_p);
2351 return true;
2354 /* Do "everything else" to clean up or complete state collected by the
2355 various walking passes -- lay out the types and decls, generate code
2356 to initialize the frame decl, store critical expressions in the
2357 struct function for rtl to find. */
2359 static void
2360 finalize_nesting_tree_1 (struct nesting_info *root)
2362 gimple_seq stmt_list;
2363 gimple stmt;
2364 tree context = root->context;
2365 struct function *sf;
2367 stmt_list = NULL;
2369 /* If we created a non-local frame type or decl, we need to lay them
2370 out at this time. */
2371 if (root->frame_type)
2373 /* In some cases the frame type will trigger the -Wpadded warning.
2374 This is not helpful; suppress it. */
2375 int save_warn_padded = warn_padded;
2376 tree *adjust;
2378 warn_padded = 0;
2379 layout_type (root->frame_type);
2380 warn_padded = save_warn_padded;
2381 layout_decl (root->frame_decl, 0);
2383 /* Remove root->frame_decl from root->new_local_var_chain, so
2384 that we can declare it also in the lexical blocks, which
2385 helps ensure virtual regs that end up appearing in its RTL
2386 expression get substituted in instantiate_virtual_regs(). */
2387 for (adjust = &root->new_local_var_chain;
2388 *adjust != root->frame_decl;
2389 adjust = &DECL_CHAIN (*adjust))
2390 gcc_assert (DECL_CHAIN (*adjust));
2391 *adjust = DECL_CHAIN (*adjust);
2393 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2394 declare_vars (root->frame_decl,
2395 gimple_seq_first_stmt (gimple_body (context)), true);
2398 /* If any parameters were referenced non-locally, then we need to
2399 insert a copy. Likewise, if any variables were referenced by
2400 pointer, we need to initialize the address. */
2401 if (root->any_parm_remapped)
2403 tree p;
2404 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2406 tree field, x, y;
2408 field = lookup_field_for_decl (root, p, NO_INSERT);
2409 if (!field)
2410 continue;
2412 if (use_pointer_in_frame (p))
2413 x = build_addr (p, context);
2414 else
2415 x = p;
2417 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2418 root->frame_decl, field, NULL_TREE);
2419 stmt = gimple_build_assign (y, x);
2420 gimple_seq_add_stmt (&stmt_list, stmt);
2421 /* If the assignment is from a non-register the stmt is
2422 not valid gimple. Make it so by using a temporary instead. */
2423 if (!is_gimple_reg (x)
2424 && is_gimple_reg_type (TREE_TYPE (x)))
2426 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2427 x = init_tmp_var (root, x, &gsi);
2428 gimple_assign_set_rhs1 (stmt, x);
2433 /* If a chain_field was created, then it needs to be initialized
2434 from chain_decl. */
2435 if (root->chain_field)
2437 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2438 root->frame_decl, root->chain_field, NULL_TREE);
2439 stmt = gimple_build_assign (x, get_chain_decl (root));
2440 gimple_seq_add_stmt (&stmt_list, stmt);
2443 /* If trampolines were created, then we need to initialize them. */
2444 if (root->any_tramp_created)
2446 struct nesting_info *i;
2447 for (i = root->inner; i ; i = i->next)
2449 tree arg1, arg2, arg3, x, field;
2451 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2452 if (!field)
2453 continue;
2455 gcc_assert (DECL_STATIC_CHAIN (i->context));
2456 arg3 = build_addr (root->frame_decl, context);
2458 arg2 = build_addr (i->context, context);
2460 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2461 root->frame_decl, field, NULL_TREE);
2462 arg1 = build_addr (x, context);
2464 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2465 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2466 gimple_seq_add_stmt (&stmt_list, stmt);
2470 /* If we created initialization statements, insert them. */
2471 if (stmt_list)
2473 gimple bind;
2474 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2475 bind = gimple_seq_first_stmt (gimple_body (context));
2476 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2477 gimple_bind_set_body (bind, stmt_list);
2480 /* If a chain_decl was created, then it needs to be registered with
2481 struct function so that it gets initialized from the static chain
2482 register at the beginning of the function. */
2483 sf = DECL_STRUCT_FUNCTION (root->context);
2484 sf->static_chain_decl = root->chain_decl;
2486 /* Similarly for the non-local goto save area. */
2487 if (root->nl_goto_field)
2489 sf->nonlocal_goto_save_area
2490 = get_frame_field (root, context, root->nl_goto_field, NULL);
2491 sf->has_nonlocal_label = 1;
2494 /* Make sure all new local variables get inserted into the
2495 proper BIND_EXPR. */
2496 if (root->new_local_var_chain)
2497 declare_vars (root->new_local_var_chain,
2498 gimple_seq_first_stmt (gimple_body (root->context)),
2499 false);
2501 if (root->debug_var_chain)
2503 tree debug_var;
2504 gimple scope;
2506 remap_vla_decls (DECL_INITIAL (root->context), root);
2508 for (debug_var = root->debug_var_chain; debug_var;
2509 debug_var = DECL_CHAIN (debug_var))
2510 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2511 break;
2513 /* If there are any debug decls with variable length types,
2514 remap those types using other debug_var_chain variables. */
2515 if (debug_var)
2517 struct nesting_copy_body_data id;
2519 memset (&id, 0, sizeof (id));
2520 id.cb.copy_decl = nesting_copy_decl;
2521 id.cb.decl_map = pointer_map_create ();
2522 id.root = root;
2524 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2525 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2527 tree type = TREE_TYPE (debug_var);
2528 tree newt, t = type;
2529 struct nesting_info *i;
2531 for (i = root; i; i = i->outer)
2532 if (variably_modified_type_p (type, i->context))
2533 break;
2535 if (i == NULL)
2536 continue;
2538 id.cb.src_fn = i->context;
2539 id.cb.dst_fn = i->context;
2540 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2542 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2543 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2545 newt = TREE_TYPE (newt);
2546 t = TREE_TYPE (t);
2548 if (TYPE_NAME (newt)
2549 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2550 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2551 && newt != t
2552 && TYPE_NAME (newt) == TYPE_NAME (t))
2553 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2556 pointer_map_destroy (id.cb.decl_map);
2559 scope = gimple_seq_first_stmt (gimple_body (root->context));
2560 if (gimple_bind_block (scope))
2561 declare_vars (root->debug_var_chain, scope, true);
2562 else
2563 BLOCK_VARS (DECL_INITIAL (root->context))
2564 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2565 root->debug_var_chain);
2568 /* Fold the rewritten MEM_REF trees. */
2569 pointer_set_traverse (root->mem_refs, fold_mem_refs, NULL);
2571 /* Dump the translated tree function. */
2572 if (dump_file)
2574 fputs ("\n\n", dump_file);
2575 dump_function_to_file (root->context, dump_file, dump_flags);
2579 static void
2580 finalize_nesting_tree (struct nesting_info *root)
2582 struct nesting_info *n;
2583 FOR_EACH_NEST_INFO (n, root)
2584 finalize_nesting_tree_1 (n);
2587 /* Unnest the nodes and pass them to cgraph. */
2589 static void
2590 unnest_nesting_tree_1 (struct nesting_info *root)
2592 struct cgraph_node *node = cgraph_get_node (root->context);
2594 /* For nested functions update the cgraph to reflect unnesting.
2595 We also delay finalizing of these functions up to this point. */
2596 if (node->origin)
2598 cgraph_unnest_node (node);
2599 cgraph_finalize_function (root->context, true);
2603 static void
2604 unnest_nesting_tree (struct nesting_info *root)
2606 struct nesting_info *n;
2607 FOR_EACH_NEST_INFO (n, root)
2608 unnest_nesting_tree_1 (n);
2611 /* Free the data structures allocated during this pass. */
2613 static void
2614 free_nesting_tree (struct nesting_info *root)
2616 struct nesting_info *node, *next;
2618 node = iter_nestinfo_start (root);
2621 next = iter_nestinfo_next (node);
2622 pointer_map_destroy (node->var_map);
2623 pointer_map_destroy (node->field_map);
2624 pointer_set_destroy (node->mem_refs);
2625 free (node);
2626 node = next;
2628 while (node);
2631 /* Gimplify a function and all its nested functions. */
2632 static void
2633 gimplify_all_functions (struct cgraph_node *root)
2635 struct cgraph_node *iter;
2636 if (!gimple_body (root->decl))
2637 gimplify_function_tree (root->decl);
2638 for (iter = root->nested; iter; iter = iter->next_nested)
2639 gimplify_all_functions (iter);
2642 /* Main entry point for this pass. Process FNDECL and all of its nested
2643 subroutines and turn them into something less tightly bound. */
2645 void
2646 lower_nested_functions (tree fndecl)
2648 struct cgraph_node *cgn;
2649 struct nesting_info *root;
2651 /* If there are no nested functions, there's nothing to do. */
2652 cgn = cgraph_get_node (fndecl);
2653 if (!cgn->nested)
2654 return;
2656 gimplify_all_functions (cgn);
2658 dump_file = dump_begin (TDI_nested, &dump_flags);
2659 if (dump_file)
2660 fprintf (dump_file, "\n;; Function %s\n\n",
2661 lang_hooks.decl_printable_name (fndecl, 2));
2663 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2664 root = create_nesting_tree (cgn);
2666 walk_all_functions (convert_nonlocal_reference_stmt,
2667 convert_nonlocal_reference_op,
2668 root);
2669 walk_all_functions (convert_local_reference_stmt,
2670 convert_local_reference_op,
2671 root);
2672 walk_all_functions (convert_nl_goto_reference, NULL, root);
2673 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2675 convert_all_function_calls (root);
2676 finalize_nesting_tree (root);
2677 unnest_nesting_tree (root);
2679 free_nesting_tree (root);
2680 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2682 if (dump_file)
2684 dump_end (TDI_nested, dump_file);
2685 dump_file = NULL;
2689 #include "gt-tree-nested.h"