2014-08-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-nested.c
blob07d6c79a271a74273c6c5f70785b80af04d8e70d
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 "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "gimplify.h"
38 #include "gimple-iterator.h"
39 #include "gimple-walk.h"
40 #include "tree-iterator.h"
41 #include "bitmap.h"
42 #include "cgraph.h"
43 #include "tree-cfg.h"
44 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
45 #include "langhooks.h"
46 #include "gimple-low.h"
49 /* The object of this pass is to lower the representation of a set of nested
50 functions in order to expose all of the gory details of the various
51 nonlocal references. We want to do this sooner rather than later, in
52 order to give us more freedom in emitting all of the functions in question.
54 Back in olden times, when gcc was young, we developed an insanely
55 complicated scheme whereby variables which were referenced nonlocally
56 were forced to live in the stack of the declaring function, and then
57 the nested functions magically discovered where these variables were
58 placed. In order for this scheme to function properly, it required
59 that the outer function be partially expanded, then we switch to
60 compiling the inner function, and once done with those we switch back
61 to compiling the outer function. Such delicate ordering requirements
62 makes it difficult to do whole translation unit optimizations
63 involving such functions.
65 The implementation here is much more direct. Everything that can be
66 referenced by an inner function is a member of an explicitly created
67 structure herein called the "nonlocal frame struct". The incoming
68 static chain for a nested function is a pointer to this struct in
69 the parent. In this way, we settle on known offsets from a known
70 base, and so are decoupled from the logic that places objects in the
71 function's stack frame. More importantly, we don't have to wait for
72 that to happen -- since the compilation of the inner function is no
73 longer tied to a real stack frame, the nonlocal frame struct can be
74 allocated anywhere. Which means that the outer function is now
75 inlinable.
77 Theory of operation here is very simple. Iterate over all the
78 statements in all the functions (depth first) several times,
79 allocating structures and fields on demand. In general we want to
80 examine inner functions first, so that we can avoid making changes
81 to outer functions which are unnecessary.
83 The order of the passes matters a bit, in that later passes will be
84 skipped if it is discovered that the functions don't actually interact
85 at all. That is, they're nested in the lexical sense but could have
86 been written as independent functions without change. */
89 struct nesting_info
91 struct nesting_info *outer;
92 struct nesting_info *inner;
93 struct nesting_info *next;
95 hash_map<tree, tree> *field_map;
96 hash_map<tree, tree> *var_map;
97 hash_set<tree *> *mem_refs;
98 bitmap suppress_expansion;
100 tree context;
101 tree new_local_var_chain;
102 tree debug_var_chain;
103 tree frame_type;
104 tree frame_decl;
105 tree chain_field;
106 tree chain_decl;
107 tree nl_goto_field;
109 bool any_parm_remapped;
110 bool any_tramp_created;
111 char static_chain_added;
115 /* Iterate over the nesting tree, starting with ROOT, depth first. */
117 static inline struct nesting_info *
118 iter_nestinfo_start (struct nesting_info *root)
120 while (root->inner)
121 root = root->inner;
122 return root;
125 static inline struct nesting_info *
126 iter_nestinfo_next (struct nesting_info *node)
128 if (node->next)
129 return iter_nestinfo_start (node->next);
130 return node->outer;
133 #define FOR_EACH_NEST_INFO(I, ROOT) \
134 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
136 /* Obstack used for the bitmaps in the struct above. */
137 static struct bitmap_obstack nesting_info_bitmap_obstack;
140 /* We're working in so many different function contexts simultaneously,
141 that create_tmp_var is dangerous. Prevent mishap. */
142 #define create_tmp_var cant_use_create_tmp_var_here_dummy
144 /* Like create_tmp_var, except record the variable for registration at
145 the given nesting level. */
147 static tree
148 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
150 tree tmp_var;
152 /* If the type is of variable size or a type which must be created by the
153 frontend, something is wrong. Note that we explicitly allow
154 incomplete types here, since we create them ourselves here. */
155 gcc_assert (!TREE_ADDRESSABLE (type));
156 gcc_assert (!TYPE_SIZE_UNIT (type)
157 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
159 tmp_var = create_tmp_var_raw (type, prefix);
160 DECL_CONTEXT (tmp_var) = info->context;
161 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
162 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
163 if (TREE_CODE (type) == COMPLEX_TYPE
164 || TREE_CODE (type) == VECTOR_TYPE)
165 DECL_GIMPLE_REG_P (tmp_var) = 1;
167 info->new_local_var_chain = tmp_var;
169 return tmp_var;
172 /* Take the address of EXP to be used within function CONTEXT.
173 Mark it for addressability as necessary. */
175 tree
176 build_addr (tree exp, tree context)
178 tree base = exp;
179 tree save_context;
180 tree retval;
182 while (handled_component_p (base))
183 base = TREE_OPERAND (base, 0);
185 if (DECL_P (base))
186 TREE_ADDRESSABLE (base) = 1;
188 /* Building the ADDR_EXPR will compute a set of properties for
189 that ADDR_EXPR. Those properties are unfortunately context
190 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
192 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
193 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
194 way the properties are for the ADDR_EXPR are computed properly. */
195 save_context = current_function_decl;
196 current_function_decl = context;
197 retval = build_fold_addr_expr (exp);
198 current_function_decl = save_context;
199 return retval;
202 /* Insert FIELD into TYPE, sorted by alignment requirements. */
204 void
205 insert_field_into_struct (tree type, tree field)
207 tree *p;
209 DECL_CONTEXT (field) = type;
211 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
212 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
213 break;
215 DECL_CHAIN (field) = *p;
216 *p = field;
218 /* Set correct alignment for frame struct type. */
219 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
220 TYPE_ALIGN (type) = DECL_ALIGN (field);
223 /* Build or return the RECORD_TYPE that describes the frame state that is
224 shared between INFO->CONTEXT and its nested functions. This record will
225 not be complete until finalize_nesting_tree; up until that point we'll
226 be adding fields as necessary.
228 We also build the DECL that represents this frame in the function. */
230 static tree
231 get_frame_type (struct nesting_info *info)
233 tree type = info->frame_type;
234 if (!type)
236 char *name;
238 type = make_node (RECORD_TYPE);
240 name = concat ("FRAME.",
241 IDENTIFIER_POINTER (DECL_NAME (info->context)),
242 NULL);
243 TYPE_NAME (type) = get_identifier (name);
244 free (name);
246 info->frame_type = type;
247 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
248 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
250 /* ??? Always make it addressable for now, since it is meant to
251 be pointed to by the static chain pointer. This pessimizes
252 when it turns out that no static chains are needed because
253 the nested functions referencing non-local variables are not
254 reachable, but the true pessimization is to create the non-
255 local frame structure in the first place. */
256 TREE_ADDRESSABLE (info->frame_decl) = 1;
258 return type;
261 /* Return true if DECL should be referenced by pointer in the non-local
262 frame structure. */
264 static bool
265 use_pointer_in_frame (tree decl)
267 if (TREE_CODE (decl) == PARM_DECL)
269 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
270 sized decls, and inefficient to copy large aggregates. Don't bother
271 moving anything but scalar variables. */
272 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
274 else
276 /* Variable sized types make things "interesting" in the frame. */
277 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
281 /* Given DECL, a non-locally accessed variable, find or create a field
282 in the non-local frame structure for the given nesting context. */
284 static tree
285 lookup_field_for_decl (struct nesting_info *info, tree decl,
286 enum insert_option insert)
288 if (insert == NO_INSERT)
290 tree *slot = info->field_map->get (decl);
291 return slot ? *slot : NULL_TREE;
294 tree *slot = &info->field_map->get_or_insert (decl);
295 if (!*slot)
297 tree field = make_node (FIELD_DECL);
298 DECL_NAME (field) = DECL_NAME (decl);
300 if (use_pointer_in_frame (decl))
302 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
303 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
304 DECL_NONADDRESSABLE_P (field) = 1;
306 else
308 TREE_TYPE (field) = TREE_TYPE (decl);
309 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
310 DECL_ALIGN (field) = DECL_ALIGN (decl);
311 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
312 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
313 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
314 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
317 insert_field_into_struct (get_frame_type (info), field);
318 *slot = field;
320 if (TREE_CODE (decl) == PARM_DECL)
321 info->any_parm_remapped = true;
324 return *slot;
327 /* Build or return the variable that holds the static chain within
328 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
330 static tree
331 get_chain_decl (struct nesting_info *info)
333 tree decl = info->chain_decl;
335 if (!decl)
337 tree type;
339 type = get_frame_type (info->outer);
340 type = build_pointer_type (type);
342 /* Note that this variable is *not* entered into any BIND_EXPR;
343 the construction of this variable is handled specially in
344 expand_function_start and initialize_inlined_parameters.
345 Note also that it's represented as a parameter. This is more
346 close to the truth, since the initial value does come from
347 the caller. */
348 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
349 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
350 DECL_ARTIFICIAL (decl) = 1;
351 DECL_IGNORED_P (decl) = 1;
352 TREE_USED (decl) = 1;
353 DECL_CONTEXT (decl) = info->context;
354 DECL_ARG_TYPE (decl) = type;
356 /* Tell tree-inline.c that we never write to this variable, so
357 it can copy-prop the replacement value immediately. */
358 TREE_READONLY (decl) = 1;
360 info->chain_decl = decl;
362 if (dump_file
363 && (dump_flags & TDF_DETAILS)
364 && !DECL_STATIC_CHAIN (info->context))
365 fprintf (dump_file, "Setting static-chain for %s\n",
366 lang_hooks.decl_printable_name (info->context, 2));
368 DECL_STATIC_CHAIN (info->context) = 1;
370 return decl;
373 /* Build or return the field within the non-local frame state that holds
374 the static chain for INFO->CONTEXT. This is the way to walk back up
375 multiple nesting levels. */
377 static tree
378 get_chain_field (struct nesting_info *info)
380 tree field = info->chain_field;
382 if (!field)
384 tree type = build_pointer_type (get_frame_type (info->outer));
386 field = make_node (FIELD_DECL);
387 DECL_NAME (field) = get_identifier ("__chain");
388 TREE_TYPE (field) = type;
389 DECL_ALIGN (field) = TYPE_ALIGN (type);
390 DECL_NONADDRESSABLE_P (field) = 1;
392 insert_field_into_struct (get_frame_type (info), field);
394 info->chain_field = field;
396 if (dump_file
397 && (dump_flags & TDF_DETAILS)
398 && !DECL_STATIC_CHAIN (info->context))
399 fprintf (dump_file, "Setting static-chain for %s\n",
400 lang_hooks.decl_printable_name (info->context, 2));
402 DECL_STATIC_CHAIN (info->context) = 1;
404 return field;
407 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
409 static tree
410 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
411 gimple call)
413 tree t;
415 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
416 gimple_call_set_lhs (call, t);
417 if (! gsi_end_p (*gsi))
418 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
419 gsi_insert_before (gsi, call, GSI_SAME_STMT);
421 return t;
425 /* Copy EXP into a temporary. Allocate the temporary in the context of
426 INFO and insert the initialization statement before GSI. */
428 static tree
429 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
431 tree t;
432 gimple stmt;
434 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
435 stmt = gimple_build_assign (t, exp);
436 if (! gsi_end_p (*gsi))
437 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
438 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
440 return t;
444 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
446 static tree
447 gsi_gimplify_val (struct nesting_info *info, tree exp,
448 gimple_stmt_iterator *gsi)
450 if (is_gimple_val (exp))
451 return exp;
452 else
453 return init_tmp_var (info, exp, gsi);
456 /* Similarly, but copy from the temporary and insert the statement
457 after the iterator. */
459 static tree
460 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
462 tree t;
463 gimple stmt;
465 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
466 stmt = gimple_build_assign (exp, t);
467 if (! gsi_end_p (*gsi))
468 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
469 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
471 return t;
474 /* Build or return the type used to represent a nested function trampoline. */
476 static GTY(()) tree trampoline_type;
478 static tree
479 get_trampoline_type (struct nesting_info *info)
481 unsigned align, size;
482 tree t;
484 if (trampoline_type)
485 return trampoline_type;
487 align = TRAMPOLINE_ALIGNMENT;
488 size = TRAMPOLINE_SIZE;
490 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
491 then allocate extra space so that we can do dynamic alignment. */
492 if (align > STACK_BOUNDARY)
494 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
495 align = STACK_BOUNDARY;
498 t = build_index_type (size_int (size - 1));
499 t = build_array_type (char_type_node, t);
500 t = build_decl (DECL_SOURCE_LOCATION (info->context),
501 FIELD_DECL, get_identifier ("__data"), t);
502 DECL_ALIGN (t) = align;
503 DECL_USER_ALIGN (t) = 1;
505 trampoline_type = make_node (RECORD_TYPE);
506 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
507 TYPE_FIELDS (trampoline_type) = t;
508 layout_type (trampoline_type);
509 DECL_CONTEXT (t) = trampoline_type;
511 return trampoline_type;
514 /* Given DECL, a nested function, find or create a field in the non-local
515 frame structure for a trampoline for this function. */
517 static tree
518 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
519 enum insert_option insert)
521 if (insert == NO_INSERT)
523 tree *slot = info->var_map->get (decl);
524 return slot ? *slot : NULL_TREE;
527 tree *slot = &info->var_map->get_or_insert (decl);
528 if (!*slot)
530 tree field = make_node (FIELD_DECL);
531 DECL_NAME (field) = DECL_NAME (decl);
532 TREE_TYPE (field) = get_trampoline_type (info);
533 TREE_ADDRESSABLE (field) = 1;
535 insert_field_into_struct (get_frame_type (info), field);
536 *slot = field;
538 info->any_tramp_created = true;
541 return *slot;
544 /* Build or return the field within the non-local frame state that holds
545 the non-local goto "jmp_buf". The buffer itself is maintained by the
546 rtl middle-end as dynamic stack space is allocated. */
548 static tree
549 get_nl_goto_field (struct nesting_info *info)
551 tree field = info->nl_goto_field;
552 if (!field)
554 unsigned size;
555 tree type;
557 /* For __builtin_nonlocal_goto, we need N words. The first is the
558 frame pointer, the rest is for the target's stack pointer save
559 area. The number of words is controlled by STACK_SAVEAREA_MODE;
560 not the best interface, but it'll do for now. */
561 if (Pmode == ptr_mode)
562 type = ptr_type_node;
563 else
564 type = lang_hooks.types.type_for_mode (Pmode, 1);
566 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
567 size = size / GET_MODE_SIZE (Pmode);
568 size = size + 1;
570 type = build_array_type
571 (type, build_index_type (size_int (size)));
573 field = make_node (FIELD_DECL);
574 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
575 TREE_TYPE (field) = type;
576 DECL_ALIGN (field) = TYPE_ALIGN (type);
577 TREE_ADDRESSABLE (field) = 1;
579 insert_field_into_struct (get_frame_type (info), field);
581 info->nl_goto_field = field;
584 return field;
587 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
589 static void
590 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
591 struct nesting_info *info, gimple_seq *pseq)
593 struct walk_stmt_info wi;
595 memset (&wi, 0, sizeof (wi));
596 wi.info = info;
597 wi.val_only = true;
598 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
602 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
604 static inline void
605 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
606 struct nesting_info *info)
608 gimple_seq body = gimple_body (info->context);
609 walk_body (callback_stmt, callback_op, info, &body);
610 gimple_set_body (info->context, body);
613 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
615 static void
616 walk_gimple_omp_for (gimple for_stmt,
617 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
618 struct nesting_info *info)
620 struct walk_stmt_info wi;
621 gimple_seq seq;
622 tree t;
623 size_t i;
625 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
627 seq = NULL;
628 memset (&wi, 0, sizeof (wi));
629 wi.info = info;
630 wi.gsi = gsi_last (seq);
632 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
634 wi.val_only = false;
635 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
636 &wi, NULL);
637 wi.val_only = true;
638 wi.is_lhs = false;
639 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
640 &wi, NULL);
642 wi.val_only = true;
643 wi.is_lhs = false;
644 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
645 &wi, NULL);
647 t = gimple_omp_for_incr (for_stmt, i);
648 gcc_assert (BINARY_CLASS_P (t));
649 wi.val_only = false;
650 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
651 wi.val_only = true;
652 wi.is_lhs = false;
653 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
656 seq = gsi_seq (wi.gsi);
657 if (!gimple_seq_empty_p (seq))
659 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
660 annotate_all_with_location (seq, gimple_location (for_stmt));
661 gimple_seq_add_seq (&pre_body, seq);
662 gimple_omp_for_set_pre_body (for_stmt, pre_body);
666 /* Similarly for ROOT and all functions nested underneath, depth first. */
668 static void
669 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
670 struct nesting_info *root)
672 struct nesting_info *n;
673 FOR_EACH_NEST_INFO (n, root)
674 walk_function (callback_stmt, callback_op, n);
678 /* We have to check for a fairly pathological case. The operands of function
679 nested function are to be interpreted in the context of the enclosing
680 function. So if any are variably-sized, they will get remapped when the
681 enclosing function is inlined. But that remapping would also have to be
682 done in the types of the PARM_DECLs of the nested function, meaning the
683 argument types of that function will disagree with the arguments in the
684 calls to that function. So we'd either have to make a copy of the nested
685 function corresponding to each time the enclosing function was inlined or
686 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
687 function. The former is not practical. The latter would still require
688 detecting this case to know when to add the conversions. So, for now at
689 least, we don't inline such an enclosing function.
691 We have to do that check recursively, so here return indicating whether
692 FNDECL has such a nested function. ORIG_FN is the function we were
693 trying to inline to use for checking whether any argument is variably
694 modified by anything in it.
696 It would be better to do this in tree-inline.c so that we could give
697 the appropriate warning for why a function can't be inlined, but that's
698 too late since the nesting structure has already been flattened and
699 adding a flag just to record this fact seems a waste of a flag. */
701 static bool
702 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
704 struct cgraph_node *cgn = cgraph_node::get (fndecl);
705 tree arg;
707 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
709 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
710 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
711 return true;
713 if (check_for_nested_with_variably_modified (cgn->decl,
714 orig_fndecl))
715 return true;
718 return false;
721 /* Construct our local datastructure describing the function nesting
722 tree rooted by CGN. */
724 static struct nesting_info *
725 create_nesting_tree (struct cgraph_node *cgn)
727 struct nesting_info *info = XCNEW (struct nesting_info);
728 info->field_map = new hash_map<tree, tree>;
729 info->var_map = new hash_map<tree, tree>;
730 info->mem_refs = new hash_set<tree *>;
731 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
732 info->context = cgn->decl;
734 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
736 struct nesting_info *sub = create_nesting_tree (cgn);
737 sub->outer = info;
738 sub->next = info->inner;
739 info->inner = sub;
742 /* See discussion at check_for_nested_with_variably_modified for a
743 discussion of why this has to be here. */
744 if (check_for_nested_with_variably_modified (info->context, info->context))
745 DECL_UNINLINABLE (info->context) = true;
747 return info;
750 /* Return an expression computing the static chain for TARGET_CONTEXT
751 from INFO->CONTEXT. Insert any necessary computations before TSI. */
753 static tree
754 get_static_chain (struct nesting_info *info, tree target_context,
755 gimple_stmt_iterator *gsi)
757 struct nesting_info *i;
758 tree x;
760 if (info->context == target_context)
762 x = build_addr (info->frame_decl, target_context);
764 else
766 x = get_chain_decl (info);
768 for (i = info->outer; i->context != target_context; i = i->outer)
770 tree field = get_chain_field (i);
772 x = build_simple_mem_ref (x);
773 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
774 x = init_tmp_var (info, x, gsi);
778 return x;
782 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
783 frame as seen from INFO->CONTEXT. Insert any necessary computations
784 before GSI. */
786 static tree
787 get_frame_field (struct nesting_info *info, tree target_context,
788 tree field, gimple_stmt_iterator *gsi)
790 struct nesting_info *i;
791 tree x;
793 if (info->context == target_context)
795 /* Make sure frame_decl gets created. */
796 (void) get_frame_type (info);
797 x = info->frame_decl;
799 else
801 x = get_chain_decl (info);
803 for (i = info->outer; i->context != target_context; i = i->outer)
805 tree field = get_chain_field (i);
807 x = build_simple_mem_ref (x);
808 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
809 x = init_tmp_var (info, x, gsi);
812 x = build_simple_mem_ref (x);
815 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
816 return x;
819 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
821 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
822 in the nested function with DECL_VALUE_EXPR set to reference the true
823 variable in the parent function. This is used both for debug info
824 and in OpenMP lowering. */
826 static tree
827 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
829 tree target_context;
830 struct nesting_info *i;
831 tree x, field, new_decl;
833 tree *slot = &info->var_map->get_or_insert (decl);
835 if (*slot)
836 return *slot;
838 target_context = decl_function_context (decl);
840 /* A copy of the code in get_frame_field, but without the temporaries. */
841 if (info->context == target_context)
843 /* Make sure frame_decl gets created. */
844 (void) get_frame_type (info);
845 x = info->frame_decl;
846 i = info;
848 else
850 x = get_chain_decl (info);
851 for (i = info->outer; i->context != target_context; i = i->outer)
853 field = get_chain_field (i);
854 x = build_simple_mem_ref (x);
855 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
857 x = build_simple_mem_ref (x);
860 field = lookup_field_for_decl (i, decl, INSERT);
861 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
862 if (use_pointer_in_frame (decl))
863 x = build_simple_mem_ref (x);
865 /* ??? We should be remapping types as well, surely. */
866 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
867 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
868 DECL_CONTEXT (new_decl) = info->context;
869 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
870 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
871 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
872 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
873 TREE_READONLY (new_decl) = TREE_READONLY (decl);
874 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
875 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
876 if ((TREE_CODE (decl) == PARM_DECL
877 || TREE_CODE (decl) == RESULT_DECL
878 || TREE_CODE (decl) == VAR_DECL)
879 && DECL_BY_REFERENCE (decl))
880 DECL_BY_REFERENCE (new_decl) = 1;
882 SET_DECL_VALUE_EXPR (new_decl, x);
883 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
885 *slot = new_decl;
886 DECL_CHAIN (new_decl) = info->debug_var_chain;
887 info->debug_var_chain = new_decl;
889 if (!optimize
890 && info->context != target_context
891 && variably_modified_type_p (TREE_TYPE (decl), NULL))
892 note_nonlocal_vla_type (info, TREE_TYPE (decl));
894 return new_decl;
898 /* Callback for walk_gimple_stmt, rewrite all references to VAR
899 and PARM_DECLs that belong to outer functions.
901 The rewrite will involve some number of structure accesses back up
902 the static chain. E.g. for a variable FOO up one nesting level it'll
903 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
904 indirections apply to decls for which use_pointer_in_frame is true. */
906 static tree
907 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
909 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
910 struct nesting_info *const info = (struct nesting_info *) wi->info;
911 tree t = *tp;
913 *walk_subtrees = 0;
914 switch (TREE_CODE (t))
916 case VAR_DECL:
917 /* Non-automatic variables are never processed. */
918 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
919 break;
920 /* FALLTHRU */
922 case PARM_DECL:
923 if (decl_function_context (t) != info->context)
925 tree x;
926 wi->changed = true;
928 x = get_nonlocal_debug_decl (info, t);
929 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
931 tree target_context = decl_function_context (t);
932 struct nesting_info *i;
933 for (i = info->outer; i->context != target_context; i = i->outer)
934 continue;
935 x = lookup_field_for_decl (i, t, INSERT);
936 x = get_frame_field (info, target_context, x, &wi->gsi);
937 if (use_pointer_in_frame (t))
939 x = init_tmp_var (info, x, &wi->gsi);
940 x = build_simple_mem_ref (x);
944 if (wi->val_only)
946 if (wi->is_lhs)
947 x = save_tmp_var (info, x, &wi->gsi);
948 else
949 x = init_tmp_var (info, x, &wi->gsi);
952 *tp = x;
954 break;
956 case LABEL_DECL:
957 /* We're taking the address of a label from a parent function, but
958 this is not itself a non-local goto. Mark the label such that it
959 will not be deleted, much as we would with a label address in
960 static storage. */
961 if (decl_function_context (t) != info->context)
962 FORCED_LABEL (t) = 1;
963 break;
965 case ADDR_EXPR:
967 bool save_val_only = wi->val_only;
969 wi->val_only = false;
970 wi->is_lhs = false;
971 wi->changed = false;
972 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
973 wi->val_only = true;
975 if (wi->changed)
977 tree save_context;
979 /* If we changed anything, we might no longer be directly
980 referencing a decl. */
981 save_context = current_function_decl;
982 current_function_decl = info->context;
983 recompute_tree_invariant_for_addr_expr (t);
984 current_function_decl = save_context;
986 /* If the callback converted the address argument in a context
987 where we only accept variables (and min_invariant, presumably),
988 then compute the address into a temporary. */
989 if (save_val_only)
990 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
991 t, &wi->gsi);
994 break;
996 case REALPART_EXPR:
997 case IMAGPART_EXPR:
998 case COMPONENT_REF:
999 case ARRAY_REF:
1000 case ARRAY_RANGE_REF:
1001 case BIT_FIELD_REF:
1002 /* Go down this entire nest and just look at the final prefix and
1003 anything that describes the references. Otherwise, we lose track
1004 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1005 wi->val_only = true;
1006 wi->is_lhs = false;
1007 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1009 if (TREE_CODE (t) == COMPONENT_REF)
1010 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1011 NULL);
1012 else if (TREE_CODE (t) == ARRAY_REF
1013 || TREE_CODE (t) == ARRAY_RANGE_REF)
1015 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1016 wi, NULL);
1017 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1018 wi, NULL);
1019 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1020 wi, NULL);
1023 wi->val_only = false;
1024 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1025 break;
1027 case VIEW_CONVERT_EXPR:
1028 /* Just request to look at the subtrees, leaving val_only and lhs
1029 untouched. This might actually be for !val_only + lhs, in which
1030 case we don't want to force a replacement by a temporary. */
1031 *walk_subtrees = 1;
1032 break;
1034 default:
1035 if (!IS_TYPE_OR_DECL_P (t))
1037 *walk_subtrees = 1;
1038 wi->val_only = true;
1039 wi->is_lhs = false;
1041 break;
1044 return NULL_TREE;
1047 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1048 struct walk_stmt_info *);
1050 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1051 and PARM_DECLs that belong to outer functions. */
1053 static bool
1054 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1056 struct nesting_info *const info = (struct nesting_info *) wi->info;
1057 bool need_chain = false, need_stmts = false;
1058 tree clause, decl;
1059 int dummy;
1060 bitmap new_suppress;
1062 new_suppress = BITMAP_GGC_ALLOC ();
1063 bitmap_copy (new_suppress, info->suppress_expansion);
1065 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1067 switch (OMP_CLAUSE_CODE (clause))
1069 case OMP_CLAUSE_REDUCTION:
1070 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1071 need_stmts = true;
1072 goto do_decl_clause;
1074 case OMP_CLAUSE_LASTPRIVATE:
1075 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1076 need_stmts = true;
1077 goto do_decl_clause;
1079 case OMP_CLAUSE_LINEAR:
1080 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1081 need_stmts = true;
1082 wi->val_only = true;
1083 wi->is_lhs = false;
1084 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1085 &dummy, wi);
1086 goto do_decl_clause;
1088 case OMP_CLAUSE_PRIVATE:
1089 case OMP_CLAUSE_FIRSTPRIVATE:
1090 case OMP_CLAUSE_COPYPRIVATE:
1091 case OMP_CLAUSE_SHARED:
1092 do_decl_clause:
1093 decl = OMP_CLAUSE_DECL (clause);
1094 if (TREE_CODE (decl) == VAR_DECL
1095 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1096 break;
1097 if (decl_function_context (decl) != info->context)
1099 bitmap_set_bit (new_suppress, DECL_UID (decl));
1100 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1101 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1102 need_chain = true;
1104 break;
1106 case OMP_CLAUSE_SCHEDULE:
1107 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1108 break;
1109 /* FALLTHRU */
1110 case OMP_CLAUSE_FINAL:
1111 case OMP_CLAUSE_IF:
1112 case OMP_CLAUSE_NUM_THREADS:
1113 case OMP_CLAUSE_DEPEND:
1114 case OMP_CLAUSE_DEVICE:
1115 case OMP_CLAUSE_NUM_TEAMS:
1116 case OMP_CLAUSE_THREAD_LIMIT:
1117 case OMP_CLAUSE_SAFELEN:
1118 wi->val_only = true;
1119 wi->is_lhs = false;
1120 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1121 &dummy, wi);
1122 break;
1124 case OMP_CLAUSE_DIST_SCHEDULE:
1125 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1127 wi->val_only = true;
1128 wi->is_lhs = false;
1129 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1130 &dummy, wi);
1132 break;
1134 case OMP_CLAUSE_MAP:
1135 case OMP_CLAUSE_TO:
1136 case OMP_CLAUSE_FROM:
1137 if (OMP_CLAUSE_SIZE (clause))
1139 wi->val_only = true;
1140 wi->is_lhs = false;
1141 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1142 &dummy, wi);
1144 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1145 goto do_decl_clause;
1146 wi->val_only = true;
1147 wi->is_lhs = false;
1148 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1149 wi, NULL);
1150 break;
1152 case OMP_CLAUSE_ALIGNED:
1153 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1155 wi->val_only = true;
1156 wi->is_lhs = false;
1157 convert_nonlocal_reference_op
1158 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1160 /* Like do_decl_clause, but don't add any suppression. */
1161 decl = OMP_CLAUSE_DECL (clause);
1162 if (TREE_CODE (decl) == VAR_DECL
1163 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1164 break;
1165 if (decl_function_context (decl) != info->context)
1167 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1168 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1169 need_chain = true;
1171 break;
1173 case OMP_CLAUSE_NOWAIT:
1174 case OMP_CLAUSE_ORDERED:
1175 case OMP_CLAUSE_DEFAULT:
1176 case OMP_CLAUSE_COPYIN:
1177 case OMP_CLAUSE_COLLAPSE:
1178 case OMP_CLAUSE_UNTIED:
1179 case OMP_CLAUSE_MERGEABLE:
1180 case OMP_CLAUSE_PROC_BIND:
1181 break;
1183 default:
1184 gcc_unreachable ();
1188 info->suppress_expansion = new_suppress;
1190 if (need_stmts)
1191 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1192 switch (OMP_CLAUSE_CODE (clause))
1194 case OMP_CLAUSE_REDUCTION:
1195 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1197 tree old_context
1198 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1199 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1200 = info->context;
1201 walk_body (convert_nonlocal_reference_stmt,
1202 convert_nonlocal_reference_op, info,
1203 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1204 walk_body (convert_nonlocal_reference_stmt,
1205 convert_nonlocal_reference_op, info,
1206 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1207 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1208 = old_context;
1210 break;
1212 case OMP_CLAUSE_LASTPRIVATE:
1213 walk_body (convert_nonlocal_reference_stmt,
1214 convert_nonlocal_reference_op, info,
1215 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1216 break;
1218 case OMP_CLAUSE_LINEAR:
1219 walk_body (convert_nonlocal_reference_stmt,
1220 convert_nonlocal_reference_op, info,
1221 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1222 break;
1224 default:
1225 break;
1228 return need_chain;
1231 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1233 static void
1234 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1236 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1237 type = TREE_TYPE (type);
1239 if (TYPE_NAME (type)
1240 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1241 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1242 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1244 while (POINTER_TYPE_P (type)
1245 || TREE_CODE (type) == VECTOR_TYPE
1246 || TREE_CODE (type) == FUNCTION_TYPE
1247 || TREE_CODE (type) == METHOD_TYPE)
1248 type = TREE_TYPE (type);
1250 if (TREE_CODE (type) == ARRAY_TYPE)
1252 tree domain, t;
1254 note_nonlocal_vla_type (info, TREE_TYPE (type));
1255 domain = TYPE_DOMAIN (type);
1256 if (domain)
1258 t = TYPE_MIN_VALUE (domain);
1259 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1260 && decl_function_context (t) != info->context)
1261 get_nonlocal_debug_decl (info, t);
1262 t = TYPE_MAX_VALUE (domain);
1263 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1264 && decl_function_context (t) != info->context)
1265 get_nonlocal_debug_decl (info, t);
1270 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1271 in BLOCK. */
1273 static void
1274 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1276 tree var;
1278 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1279 if (TREE_CODE (var) == VAR_DECL
1280 && variably_modified_type_p (TREE_TYPE (var), NULL)
1281 && DECL_HAS_VALUE_EXPR_P (var)
1282 && decl_function_context (var) != info->context)
1283 note_nonlocal_vla_type (info, TREE_TYPE (var));
1286 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1287 PARM_DECLs that belong to outer functions. This handles statements
1288 that are not handled via the standard recursion done in
1289 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1290 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1291 operands of STMT have been handled by this function. */
1293 static tree
1294 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1295 struct walk_stmt_info *wi)
1297 struct nesting_info *info = (struct nesting_info *) wi->info;
1298 tree save_local_var_chain;
1299 bitmap save_suppress;
1300 gimple stmt = gsi_stmt (*gsi);
1302 switch (gimple_code (stmt))
1304 case GIMPLE_GOTO:
1305 /* Don't walk non-local gotos for now. */
1306 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1308 wi->val_only = true;
1309 wi->is_lhs = false;
1310 *handled_ops_p = true;
1311 return NULL_TREE;
1313 break;
1315 case GIMPLE_OMP_PARALLEL:
1316 case GIMPLE_OMP_TASK:
1317 save_suppress = info->suppress_expansion;
1318 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1319 wi))
1321 tree c, decl;
1322 decl = get_chain_decl (info);
1323 c = build_omp_clause (gimple_location (stmt),
1324 OMP_CLAUSE_FIRSTPRIVATE);
1325 OMP_CLAUSE_DECL (c) = decl;
1326 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1327 gimple_omp_taskreg_set_clauses (stmt, c);
1330 save_local_var_chain = info->new_local_var_chain;
1331 info->new_local_var_chain = NULL;
1333 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1334 info, gimple_omp_body_ptr (stmt));
1336 if (info->new_local_var_chain)
1337 declare_vars (info->new_local_var_chain,
1338 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1339 false);
1340 info->new_local_var_chain = save_local_var_chain;
1341 info->suppress_expansion = save_suppress;
1342 break;
1344 case GIMPLE_OMP_FOR:
1345 save_suppress = info->suppress_expansion;
1346 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1347 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1348 convert_nonlocal_reference_op, info);
1349 walk_body (convert_nonlocal_reference_stmt,
1350 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1351 info->suppress_expansion = save_suppress;
1352 break;
1354 case GIMPLE_OMP_SECTIONS:
1355 save_suppress = info->suppress_expansion;
1356 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1357 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1358 info, gimple_omp_body_ptr (stmt));
1359 info->suppress_expansion = save_suppress;
1360 break;
1362 case GIMPLE_OMP_SINGLE:
1363 save_suppress = info->suppress_expansion;
1364 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1365 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1366 info, gimple_omp_body_ptr (stmt));
1367 info->suppress_expansion = save_suppress;
1368 break;
1370 case GIMPLE_OMP_TARGET:
1371 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1373 save_suppress = info->suppress_expansion;
1374 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1375 wi);
1376 info->suppress_expansion = save_suppress;
1377 walk_body (convert_nonlocal_reference_stmt,
1378 convert_nonlocal_reference_op, info,
1379 gimple_omp_body_ptr (stmt));
1380 break;
1382 save_suppress = info->suppress_expansion;
1383 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1384 wi))
1386 tree c, decl;
1387 decl = get_chain_decl (info);
1388 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1389 OMP_CLAUSE_DECL (c) = decl;
1390 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TO;
1391 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1392 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1393 gimple_omp_target_set_clauses (stmt, c);
1396 save_local_var_chain = info->new_local_var_chain;
1397 info->new_local_var_chain = NULL;
1399 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1400 info, gimple_omp_body_ptr (stmt));
1402 if (info->new_local_var_chain)
1403 declare_vars (info->new_local_var_chain,
1404 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1405 false);
1406 info->new_local_var_chain = save_local_var_chain;
1407 info->suppress_expansion = save_suppress;
1408 break;
1410 case GIMPLE_OMP_TEAMS:
1411 save_suppress = info->suppress_expansion;
1412 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1413 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1414 info, gimple_omp_body_ptr (stmt));
1415 info->suppress_expansion = save_suppress;
1416 break;
1418 case GIMPLE_OMP_SECTION:
1419 case GIMPLE_OMP_MASTER:
1420 case GIMPLE_OMP_TASKGROUP:
1421 case GIMPLE_OMP_ORDERED:
1422 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1423 info, gimple_omp_body_ptr (stmt));
1424 break;
1426 case GIMPLE_BIND:
1427 if (!optimize && gimple_bind_block (stmt))
1428 note_nonlocal_block_vlas (info, gimple_bind_block (stmt));
1430 for (tree var = gimple_bind_vars (stmt); var; var = DECL_CHAIN (var))
1431 if (TREE_CODE (var) == NAMELIST_DECL)
1433 /* Adjust decls mentioned in NAMELIST_DECL. */
1434 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1435 tree decl;
1436 unsigned int i;
1438 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1440 if (TREE_CODE (decl) == VAR_DECL
1441 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1442 continue;
1443 if (decl_function_context (decl) != info->context)
1444 CONSTRUCTOR_ELT (decls, i)->value
1445 = get_nonlocal_debug_decl (info, decl);
1449 *handled_ops_p = false;
1450 return NULL_TREE;
1452 case GIMPLE_COND:
1453 wi->val_only = true;
1454 wi->is_lhs = false;
1455 *handled_ops_p = false;
1456 return NULL_TREE;
1458 default:
1459 /* For every other statement that we are not interested in
1460 handling here, let the walker traverse the operands. */
1461 *handled_ops_p = false;
1462 return NULL_TREE;
1465 /* We have handled all of STMT operands, no need to traverse the operands. */
1466 *handled_ops_p = true;
1467 return NULL_TREE;
1471 /* A subroutine of convert_local_reference. Create a local variable
1472 in the parent function with DECL_VALUE_EXPR set to reference the
1473 field in FRAME. This is used both for debug info and in OpenMP
1474 lowering. */
1476 static tree
1477 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1479 tree x, new_decl;
1481 tree *slot = &info->var_map->get_or_insert (decl);
1482 if (*slot)
1483 return *slot;
1485 /* Make sure frame_decl gets created. */
1486 (void) get_frame_type (info);
1487 x = info->frame_decl;
1488 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1490 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1491 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1492 DECL_CONTEXT (new_decl) = info->context;
1493 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1494 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1495 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1496 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1497 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1498 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1499 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1500 if ((TREE_CODE (decl) == PARM_DECL
1501 || TREE_CODE (decl) == RESULT_DECL
1502 || TREE_CODE (decl) == VAR_DECL)
1503 && DECL_BY_REFERENCE (decl))
1504 DECL_BY_REFERENCE (new_decl) = 1;
1506 SET_DECL_VALUE_EXPR (new_decl, x);
1507 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1508 *slot = new_decl;
1510 DECL_CHAIN (new_decl) = info->debug_var_chain;
1511 info->debug_var_chain = new_decl;
1513 /* Do not emit debug info twice. */
1514 DECL_IGNORED_P (decl) = 1;
1516 return new_decl;
1520 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1521 and PARM_DECLs that were referenced by inner nested functions.
1522 The rewrite will be a structure reference to the local frame variable. */
1524 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1526 static tree
1527 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1529 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1530 struct nesting_info *const info = (struct nesting_info *) wi->info;
1531 tree t = *tp, field, x;
1532 bool save_val_only;
1534 *walk_subtrees = 0;
1535 switch (TREE_CODE (t))
1537 case VAR_DECL:
1538 /* Non-automatic variables are never processed. */
1539 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1540 break;
1541 /* FALLTHRU */
1543 case PARM_DECL:
1544 if (decl_function_context (t) == info->context)
1546 /* If we copied a pointer to the frame, then the original decl
1547 is used unchanged in the parent function. */
1548 if (use_pointer_in_frame (t))
1549 break;
1551 /* No need to transform anything if no child references the
1552 variable. */
1553 field = lookup_field_for_decl (info, t, NO_INSERT);
1554 if (!field)
1555 break;
1556 wi->changed = true;
1558 x = get_local_debug_decl (info, t, field);
1559 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1560 x = get_frame_field (info, info->context, field, &wi->gsi);
1562 if (wi->val_only)
1564 if (wi->is_lhs)
1565 x = save_tmp_var (info, x, &wi->gsi);
1566 else
1567 x = init_tmp_var (info, x, &wi->gsi);
1570 *tp = x;
1572 break;
1574 case ADDR_EXPR:
1575 save_val_only = wi->val_only;
1576 wi->val_only = false;
1577 wi->is_lhs = false;
1578 wi->changed = false;
1579 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1580 wi->val_only = save_val_only;
1582 /* If we converted anything ... */
1583 if (wi->changed)
1585 tree save_context;
1587 /* Then the frame decl is now addressable. */
1588 TREE_ADDRESSABLE (info->frame_decl) = 1;
1590 save_context = current_function_decl;
1591 current_function_decl = info->context;
1592 recompute_tree_invariant_for_addr_expr (t);
1593 current_function_decl = save_context;
1595 /* If we are in a context where we only accept values, then
1596 compute the address into a temporary. */
1597 if (save_val_only)
1598 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1599 t, &wi->gsi);
1601 break;
1603 case REALPART_EXPR:
1604 case IMAGPART_EXPR:
1605 case COMPONENT_REF:
1606 case ARRAY_REF:
1607 case ARRAY_RANGE_REF:
1608 case BIT_FIELD_REF:
1609 /* Go down this entire nest and just look at the final prefix and
1610 anything that describes the references. Otherwise, we lose track
1611 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1612 save_val_only = wi->val_only;
1613 wi->val_only = true;
1614 wi->is_lhs = false;
1615 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1617 if (TREE_CODE (t) == COMPONENT_REF)
1618 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1619 NULL);
1620 else if (TREE_CODE (t) == ARRAY_REF
1621 || TREE_CODE (t) == ARRAY_RANGE_REF)
1623 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1624 NULL);
1625 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1626 NULL);
1627 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1628 NULL);
1631 wi->val_only = false;
1632 walk_tree (tp, convert_local_reference_op, wi, NULL);
1633 wi->val_only = save_val_only;
1634 break;
1636 case MEM_REF:
1637 save_val_only = wi->val_only;
1638 wi->val_only = true;
1639 wi->is_lhs = false;
1640 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1641 wi, NULL);
1642 /* We need to re-fold the MEM_REF as component references as
1643 part of a ADDR_EXPR address are not allowed. But we cannot
1644 fold here, as the chain record type is not yet finalized. */
1645 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1646 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1647 info->mem_refs->add (tp);
1648 wi->val_only = save_val_only;
1649 break;
1651 case VIEW_CONVERT_EXPR:
1652 /* Just request to look at the subtrees, leaving val_only and lhs
1653 untouched. This might actually be for !val_only + lhs, in which
1654 case we don't want to force a replacement by a temporary. */
1655 *walk_subtrees = 1;
1656 break;
1658 default:
1659 if (!IS_TYPE_OR_DECL_P (t))
1661 *walk_subtrees = 1;
1662 wi->val_only = true;
1663 wi->is_lhs = false;
1665 break;
1668 return NULL_TREE;
1671 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1672 struct walk_stmt_info *);
1674 /* Helper for convert_local_reference. Convert all the references in
1675 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1677 static bool
1678 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1680 struct nesting_info *const info = (struct nesting_info *) wi->info;
1681 bool need_frame = false, need_stmts = false;
1682 tree clause, decl;
1683 int dummy;
1684 bitmap new_suppress;
1686 new_suppress = BITMAP_GGC_ALLOC ();
1687 bitmap_copy (new_suppress, info->suppress_expansion);
1689 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1691 switch (OMP_CLAUSE_CODE (clause))
1693 case OMP_CLAUSE_REDUCTION:
1694 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1695 need_stmts = true;
1696 goto do_decl_clause;
1698 case OMP_CLAUSE_LASTPRIVATE:
1699 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1700 need_stmts = true;
1701 goto do_decl_clause;
1703 case OMP_CLAUSE_LINEAR:
1704 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1705 need_stmts = true;
1706 wi->val_only = true;
1707 wi->is_lhs = false;
1708 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1709 wi);
1710 goto do_decl_clause;
1712 case OMP_CLAUSE_PRIVATE:
1713 case OMP_CLAUSE_FIRSTPRIVATE:
1714 case OMP_CLAUSE_COPYPRIVATE:
1715 case OMP_CLAUSE_SHARED:
1716 do_decl_clause:
1717 decl = OMP_CLAUSE_DECL (clause);
1718 if (TREE_CODE (decl) == VAR_DECL
1719 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1720 break;
1721 if (decl_function_context (decl) == info->context
1722 && !use_pointer_in_frame (decl))
1724 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1725 if (field)
1727 bitmap_set_bit (new_suppress, DECL_UID (decl));
1728 OMP_CLAUSE_DECL (clause)
1729 = get_local_debug_decl (info, decl, field);
1730 need_frame = true;
1733 break;
1735 case OMP_CLAUSE_SCHEDULE:
1736 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1737 break;
1738 /* FALLTHRU */
1739 case OMP_CLAUSE_FINAL:
1740 case OMP_CLAUSE_IF:
1741 case OMP_CLAUSE_NUM_THREADS:
1742 case OMP_CLAUSE_DEPEND:
1743 case OMP_CLAUSE_DEVICE:
1744 case OMP_CLAUSE_NUM_TEAMS:
1745 case OMP_CLAUSE_THREAD_LIMIT:
1746 case OMP_CLAUSE_SAFELEN:
1747 wi->val_only = true;
1748 wi->is_lhs = false;
1749 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1750 wi);
1751 break;
1753 case OMP_CLAUSE_DIST_SCHEDULE:
1754 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1756 wi->val_only = true;
1757 wi->is_lhs = false;
1758 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1759 &dummy, wi);
1761 break;
1763 case OMP_CLAUSE_MAP:
1764 case OMP_CLAUSE_TO:
1765 case OMP_CLAUSE_FROM:
1766 if (OMP_CLAUSE_SIZE (clause))
1768 wi->val_only = true;
1769 wi->is_lhs = false;
1770 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1771 &dummy, wi);
1773 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1774 goto do_decl_clause;
1775 wi->val_only = true;
1776 wi->is_lhs = false;
1777 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1778 wi, NULL);
1779 break;
1781 case OMP_CLAUSE_ALIGNED:
1782 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1784 wi->val_only = true;
1785 wi->is_lhs = false;
1786 convert_local_reference_op
1787 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1789 /* Like do_decl_clause, but don't add any suppression. */
1790 decl = OMP_CLAUSE_DECL (clause);
1791 if (TREE_CODE (decl) == VAR_DECL
1792 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1793 break;
1794 if (decl_function_context (decl) == info->context
1795 && !use_pointer_in_frame (decl))
1797 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1798 if (field)
1800 OMP_CLAUSE_DECL (clause)
1801 = get_local_debug_decl (info, decl, field);
1802 need_frame = true;
1805 break;
1807 case OMP_CLAUSE_NOWAIT:
1808 case OMP_CLAUSE_ORDERED:
1809 case OMP_CLAUSE_DEFAULT:
1810 case OMP_CLAUSE_COPYIN:
1811 case OMP_CLAUSE_COLLAPSE:
1812 case OMP_CLAUSE_UNTIED:
1813 case OMP_CLAUSE_MERGEABLE:
1814 case OMP_CLAUSE_PROC_BIND:
1815 break;
1817 default:
1818 gcc_unreachable ();
1822 info->suppress_expansion = new_suppress;
1824 if (need_stmts)
1825 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1826 switch (OMP_CLAUSE_CODE (clause))
1828 case OMP_CLAUSE_REDUCTION:
1829 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1831 tree old_context
1832 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1833 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1834 = info->context;
1835 walk_body (convert_local_reference_stmt,
1836 convert_local_reference_op, info,
1837 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1838 walk_body (convert_local_reference_stmt,
1839 convert_local_reference_op, info,
1840 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1841 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1842 = old_context;
1844 break;
1846 case OMP_CLAUSE_LASTPRIVATE:
1847 walk_body (convert_local_reference_stmt,
1848 convert_local_reference_op, info,
1849 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1850 break;
1852 case OMP_CLAUSE_LINEAR:
1853 walk_body (convert_local_reference_stmt,
1854 convert_local_reference_op, info,
1855 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1856 break;
1858 default:
1859 break;
1862 return need_frame;
1866 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1867 and PARM_DECLs that were referenced by inner nested functions.
1868 The rewrite will be a structure reference to the local frame variable. */
1870 static tree
1871 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1872 struct walk_stmt_info *wi)
1874 struct nesting_info *info = (struct nesting_info *) wi->info;
1875 tree save_local_var_chain;
1876 bitmap save_suppress;
1877 gimple stmt = gsi_stmt (*gsi);
1879 switch (gimple_code (stmt))
1881 case GIMPLE_OMP_PARALLEL:
1882 case GIMPLE_OMP_TASK:
1883 save_suppress = info->suppress_expansion;
1884 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1885 wi))
1887 tree c;
1888 (void) get_frame_type (info);
1889 c = build_omp_clause (gimple_location (stmt),
1890 OMP_CLAUSE_SHARED);
1891 OMP_CLAUSE_DECL (c) = info->frame_decl;
1892 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1893 gimple_omp_taskreg_set_clauses (stmt, c);
1896 save_local_var_chain = info->new_local_var_chain;
1897 info->new_local_var_chain = NULL;
1899 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1900 gimple_omp_body_ptr (stmt));
1902 if (info->new_local_var_chain)
1903 declare_vars (info->new_local_var_chain,
1904 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1905 info->new_local_var_chain = save_local_var_chain;
1906 info->suppress_expansion = save_suppress;
1907 break;
1909 case GIMPLE_OMP_FOR:
1910 save_suppress = info->suppress_expansion;
1911 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1912 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1913 convert_local_reference_op, info);
1914 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1915 info, gimple_omp_body_ptr (stmt));
1916 info->suppress_expansion = save_suppress;
1917 break;
1919 case GIMPLE_OMP_SECTIONS:
1920 save_suppress = info->suppress_expansion;
1921 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1922 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1923 info, gimple_omp_body_ptr (stmt));
1924 info->suppress_expansion = save_suppress;
1925 break;
1927 case GIMPLE_OMP_SINGLE:
1928 save_suppress = info->suppress_expansion;
1929 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1930 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1931 info, gimple_omp_body_ptr (stmt));
1932 info->suppress_expansion = save_suppress;
1933 break;
1935 case GIMPLE_OMP_TARGET:
1936 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1938 save_suppress = info->suppress_expansion;
1939 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1940 info->suppress_expansion = save_suppress;
1941 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1942 info, gimple_omp_body_ptr (stmt));
1943 break;
1945 save_suppress = info->suppress_expansion;
1946 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1948 tree c;
1949 (void) get_frame_type (info);
1950 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1951 OMP_CLAUSE_DECL (c) = info->frame_decl;
1952 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TOFROM;
1953 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1954 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1955 gimple_omp_target_set_clauses (stmt, c);
1958 save_local_var_chain = info->new_local_var_chain;
1959 info->new_local_var_chain = NULL;
1961 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1962 gimple_omp_body_ptr (stmt));
1964 if (info->new_local_var_chain)
1965 declare_vars (info->new_local_var_chain,
1966 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1967 info->new_local_var_chain = save_local_var_chain;
1968 info->suppress_expansion = save_suppress;
1969 break;
1971 case GIMPLE_OMP_TEAMS:
1972 save_suppress = info->suppress_expansion;
1973 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1974 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1975 info, gimple_omp_body_ptr (stmt));
1976 info->suppress_expansion = save_suppress;
1977 break;
1979 case GIMPLE_OMP_SECTION:
1980 case GIMPLE_OMP_MASTER:
1981 case GIMPLE_OMP_TASKGROUP:
1982 case GIMPLE_OMP_ORDERED:
1983 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1984 info, gimple_omp_body_ptr (stmt));
1985 break;
1987 case GIMPLE_COND:
1988 wi->val_only = true;
1989 wi->is_lhs = false;
1990 *handled_ops_p = false;
1991 return NULL_TREE;
1993 case GIMPLE_ASSIGN:
1994 if (gimple_clobber_p (stmt))
1996 tree lhs = gimple_assign_lhs (stmt);
1997 if (!use_pointer_in_frame (lhs)
1998 && lookup_field_for_decl (info, lhs, NO_INSERT))
2000 gsi_replace (gsi, gimple_build_nop (), true);
2001 break;
2004 *handled_ops_p = false;
2005 return NULL_TREE;
2007 case GIMPLE_BIND:
2008 for (tree var = gimple_bind_vars (stmt); var; var = DECL_CHAIN (var))
2009 if (TREE_CODE (var) == NAMELIST_DECL)
2011 /* Adjust decls mentioned in NAMELIST_DECL. */
2012 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2013 tree decl;
2014 unsigned int i;
2016 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2018 if (TREE_CODE (decl) == VAR_DECL
2019 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2020 continue;
2021 if (decl_function_context (decl) == info->context
2022 && !use_pointer_in_frame (decl))
2024 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2025 if (field)
2027 CONSTRUCTOR_ELT (decls, i)->value
2028 = get_local_debug_decl (info, decl, field);
2034 *handled_ops_p = false;
2035 return NULL_TREE;
2037 default:
2038 /* For every other statement that we are not interested in
2039 handling here, let the walker traverse the operands. */
2040 *handled_ops_p = false;
2041 return NULL_TREE;
2044 /* Indicate that we have handled all the operands ourselves. */
2045 *handled_ops_p = true;
2046 return NULL_TREE;
2050 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2051 that reference labels from outer functions. The rewrite will be a
2052 call to __builtin_nonlocal_goto. */
2054 static tree
2055 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2056 struct walk_stmt_info *wi)
2058 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2059 tree label, new_label, target_context, x, field;
2060 gimple call;
2061 gimple stmt = gsi_stmt (*gsi);
2063 if (gimple_code (stmt) != GIMPLE_GOTO)
2065 *handled_ops_p = false;
2066 return NULL_TREE;
2069 label = gimple_goto_dest (stmt);
2070 if (TREE_CODE (label) != LABEL_DECL)
2072 *handled_ops_p = false;
2073 return NULL_TREE;
2076 target_context = decl_function_context (label);
2077 if (target_context == info->context)
2079 *handled_ops_p = false;
2080 return NULL_TREE;
2083 for (i = info->outer; target_context != i->context; i = i->outer)
2084 continue;
2086 /* The original user label may also be use for a normal goto, therefore
2087 we must create a new label that will actually receive the abnormal
2088 control transfer. This new label will be marked LABEL_NONLOCAL; this
2089 mark will trigger proper behavior in the cfg, as well as cause the
2090 (hairy target-specific) non-local goto receiver code to be generated
2091 when we expand rtl. Enter this association into var_map so that we
2092 can insert the new label into the IL during a second pass. */
2093 tree *slot = &i->var_map->get_or_insert (label);
2094 if (*slot == NULL)
2096 new_label = create_artificial_label (UNKNOWN_LOCATION);
2097 DECL_NONLOCAL (new_label) = 1;
2098 *slot = new_label;
2100 else
2101 new_label = *slot;
2103 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2104 field = get_nl_goto_field (i);
2105 x = get_frame_field (info, target_context, field, gsi);
2106 x = build_addr (x, target_context);
2107 x = gsi_gimplify_val (info, x, gsi);
2108 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2109 2, build_addr (new_label, target_context), x);
2110 gsi_replace (gsi, call, false);
2112 /* We have handled all of STMT's operands, no need to keep going. */
2113 *handled_ops_p = true;
2114 return NULL_TREE;
2118 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2119 are referenced via nonlocal goto from a nested function. The rewrite
2120 will involve installing a newly generated DECL_NONLOCAL label, and
2121 (potentially) a branch around the rtl gunk that is assumed to be
2122 attached to such a label. */
2124 static tree
2125 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2126 struct walk_stmt_info *wi)
2128 struct nesting_info *const info = (struct nesting_info *) wi->info;
2129 tree label, new_label;
2130 gimple_stmt_iterator tmp_gsi;
2131 gimple stmt = gsi_stmt (*gsi);
2133 if (gimple_code (stmt) != GIMPLE_LABEL)
2135 *handled_ops_p = false;
2136 return NULL_TREE;
2139 label = gimple_label_label (stmt);
2141 tree *slot = info->var_map->get (label);
2142 if (!slot)
2144 *handled_ops_p = false;
2145 return NULL_TREE;
2148 /* If there's any possibility that the previous statement falls through,
2149 then we must branch around the new non-local label. */
2150 tmp_gsi = wi->gsi;
2151 gsi_prev (&tmp_gsi);
2152 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2154 gimple stmt = gimple_build_goto (label);
2155 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2158 new_label = (tree) *slot;
2159 stmt = gimple_build_label (new_label);
2160 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2162 *handled_ops_p = true;
2163 return NULL_TREE;
2167 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2168 of nested functions that require the use of trampolines. The rewrite
2169 will involve a reference a trampoline generated for the occasion. */
2171 static tree
2172 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2174 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2175 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2176 tree t = *tp, decl, target_context, x, builtin;
2177 gimple call;
2179 *walk_subtrees = 0;
2180 switch (TREE_CODE (t))
2182 case ADDR_EXPR:
2183 /* Build
2184 T.1 = &CHAIN->tramp;
2185 T.2 = __builtin_adjust_trampoline (T.1);
2186 T.3 = (func_type)T.2;
2189 decl = TREE_OPERAND (t, 0);
2190 if (TREE_CODE (decl) != FUNCTION_DECL)
2191 break;
2193 /* Only need to process nested functions. */
2194 target_context = decl_function_context (decl);
2195 if (!target_context)
2196 break;
2198 /* If the nested function doesn't use a static chain, then
2199 it doesn't need a trampoline. */
2200 if (!DECL_STATIC_CHAIN (decl))
2201 break;
2203 /* If we don't want a trampoline, then don't build one. */
2204 if (TREE_NO_TRAMPOLINE (t))
2205 break;
2207 /* Lookup the immediate parent of the callee, as that's where
2208 we need to insert the trampoline. */
2209 for (i = info; i->context != target_context; i = i->outer)
2210 continue;
2211 x = lookup_tramp_for_decl (i, decl, INSERT);
2213 /* Compute the address of the field holding the trampoline. */
2214 x = get_frame_field (info, target_context, x, &wi->gsi);
2215 x = build_addr (x, target_context);
2216 x = gsi_gimplify_val (info, x, &wi->gsi);
2218 /* Do machine-specific ugliness. Normally this will involve
2219 computing extra alignment, but it can really be anything. */
2220 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2221 call = gimple_build_call (builtin, 1, x);
2222 x = init_tmp_var_with_call (info, &wi->gsi, call);
2224 /* Cast back to the proper function type. */
2225 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2226 x = init_tmp_var (info, x, &wi->gsi);
2228 *tp = x;
2229 break;
2231 default:
2232 if (!IS_TYPE_OR_DECL_P (t))
2233 *walk_subtrees = 1;
2234 break;
2237 return NULL_TREE;
2241 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2242 to addresses of nested functions that require the use of
2243 trampolines. The rewrite will involve a reference a trampoline
2244 generated for the occasion. */
2246 static tree
2247 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2248 struct walk_stmt_info *wi)
2250 struct nesting_info *info = (struct nesting_info *) wi->info;
2251 gimple stmt = gsi_stmt (*gsi);
2253 switch (gimple_code (stmt))
2255 case GIMPLE_CALL:
2257 /* Only walk call arguments, lest we generate trampolines for
2258 direct calls. */
2259 unsigned long i, nargs = gimple_call_num_args (stmt);
2260 for (i = 0; i < nargs; i++)
2261 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2262 wi, NULL);
2263 break;
2266 case GIMPLE_OMP_TARGET:
2267 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2269 *handled_ops_p = false;
2270 return NULL_TREE;
2272 /* FALLTHRU */
2273 case GIMPLE_OMP_PARALLEL:
2274 case GIMPLE_OMP_TASK:
2276 tree save_local_var_chain;
2277 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2278 save_local_var_chain = info->new_local_var_chain;
2279 info->new_local_var_chain = NULL;
2280 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2281 info, gimple_omp_body_ptr (stmt));
2282 if (info->new_local_var_chain)
2283 declare_vars (info->new_local_var_chain,
2284 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2285 false);
2286 info->new_local_var_chain = save_local_var_chain;
2288 break;
2290 default:
2291 *handled_ops_p = false;
2292 return NULL_TREE;
2295 *handled_ops_p = true;
2296 return NULL_TREE;
2301 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2302 that reference nested functions to make sure that the static chain
2303 is set up properly for the call. */
2305 static tree
2306 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2307 struct walk_stmt_info *wi)
2309 struct nesting_info *const info = (struct nesting_info *) wi->info;
2310 tree decl, target_context;
2311 char save_static_chain_added;
2312 int i;
2313 gimple stmt = gsi_stmt (*gsi);
2315 switch (gimple_code (stmt))
2317 case GIMPLE_CALL:
2318 if (gimple_call_chain (stmt))
2319 break;
2320 decl = gimple_call_fndecl (stmt);
2321 if (!decl)
2322 break;
2323 target_context = decl_function_context (decl);
2324 if (target_context && DECL_STATIC_CHAIN (decl))
2326 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
2327 &wi->gsi));
2328 info->static_chain_added |= (1 << (info->context != target_context));
2330 break;
2332 case GIMPLE_OMP_PARALLEL:
2333 case GIMPLE_OMP_TASK:
2334 save_static_chain_added = info->static_chain_added;
2335 info->static_chain_added = 0;
2336 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2337 for (i = 0; i < 2; i++)
2339 tree c, decl;
2340 if ((info->static_chain_added & (1 << i)) == 0)
2341 continue;
2342 decl = i ? get_chain_decl (info) : info->frame_decl;
2343 /* Don't add CHAIN.* or FRAME.* twice. */
2344 for (c = gimple_omp_taskreg_clauses (stmt);
2346 c = OMP_CLAUSE_CHAIN (c))
2347 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2348 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2349 && OMP_CLAUSE_DECL (c) == decl)
2350 break;
2351 if (c == NULL)
2353 c = build_omp_clause (gimple_location (stmt),
2354 i ? OMP_CLAUSE_FIRSTPRIVATE
2355 : OMP_CLAUSE_SHARED);
2356 OMP_CLAUSE_DECL (c) = decl;
2357 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2358 gimple_omp_taskreg_set_clauses (stmt, c);
2361 info->static_chain_added |= save_static_chain_added;
2362 break;
2364 case GIMPLE_OMP_TARGET:
2365 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2367 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2368 break;
2370 save_static_chain_added = info->static_chain_added;
2371 info->static_chain_added = 0;
2372 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2373 for (i = 0; i < 2; i++)
2375 tree c, decl;
2376 if ((info->static_chain_added & (1 << i)) == 0)
2377 continue;
2378 decl = i ? get_chain_decl (info) : info->frame_decl;
2379 /* Don't add CHAIN.* or FRAME.* twice. */
2380 for (c = gimple_omp_target_clauses (stmt);
2382 c = OMP_CLAUSE_CHAIN (c))
2383 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2384 && OMP_CLAUSE_DECL (c) == decl)
2385 break;
2386 if (c == NULL)
2388 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2389 OMP_CLAUSE_DECL (c) = decl;
2390 OMP_CLAUSE_MAP_KIND (c)
2391 = i ? OMP_CLAUSE_MAP_TO : OMP_CLAUSE_MAP_TOFROM;
2392 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2393 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2394 gimple_omp_target_set_clauses (stmt, c);
2397 info->static_chain_added |= save_static_chain_added;
2398 break;
2400 case GIMPLE_OMP_FOR:
2401 walk_body (convert_gimple_call, NULL, info,
2402 gimple_omp_for_pre_body_ptr (stmt));
2403 /* FALLTHRU */
2404 case GIMPLE_OMP_SECTIONS:
2405 case GIMPLE_OMP_SECTION:
2406 case GIMPLE_OMP_SINGLE:
2407 case GIMPLE_OMP_TEAMS:
2408 case GIMPLE_OMP_MASTER:
2409 case GIMPLE_OMP_TASKGROUP:
2410 case GIMPLE_OMP_ORDERED:
2411 case GIMPLE_OMP_CRITICAL:
2412 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2413 break;
2415 default:
2416 /* Keep looking for other operands. */
2417 *handled_ops_p = false;
2418 return NULL_TREE;
2421 *handled_ops_p = true;
2422 return NULL_TREE;
2425 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2426 call expressions. At the same time, determine if a nested function
2427 actually uses its static chain; if not, remember that. */
2429 static void
2430 convert_all_function_calls (struct nesting_info *root)
2432 unsigned int chain_count = 0, old_chain_count, iter_count;
2433 struct nesting_info *n;
2435 /* First, optimistically clear static_chain for all decls that haven't
2436 used the static chain already for variable access. But always create
2437 it if not optimizing. This makes it possible to reconstruct the static
2438 nesting tree at run time and thus to resolve up-level references from
2439 within the debugger. */
2440 FOR_EACH_NEST_INFO (n, root)
2442 tree decl = n->context;
2443 if (!optimize)
2445 if (n->inner)
2446 (void) get_frame_type (n);
2447 if (n->outer)
2448 (void) get_chain_decl (n);
2450 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2452 DECL_STATIC_CHAIN (decl) = 0;
2453 if (dump_file && (dump_flags & TDF_DETAILS))
2454 fprintf (dump_file, "Guessing no static-chain for %s\n",
2455 lang_hooks.decl_printable_name (decl, 2));
2457 else
2458 DECL_STATIC_CHAIN (decl) = 1;
2459 chain_count += DECL_STATIC_CHAIN (decl);
2462 /* Walk the functions and perform transformations. Note that these
2463 transformations can induce new uses of the static chain, which in turn
2464 require re-examining all users of the decl. */
2465 /* ??? It would make sense to try to use the call graph to speed this up,
2466 but the call graph hasn't really been built yet. Even if it did, we
2467 would still need to iterate in this loop since address-of references
2468 wouldn't show up in the callgraph anyway. */
2469 iter_count = 0;
2472 old_chain_count = chain_count;
2473 chain_count = 0;
2474 iter_count++;
2476 if (dump_file && (dump_flags & TDF_DETAILS))
2477 fputc ('\n', dump_file);
2479 FOR_EACH_NEST_INFO (n, root)
2481 tree decl = n->context;
2482 walk_function (convert_tramp_reference_stmt,
2483 convert_tramp_reference_op, n);
2484 walk_function (convert_gimple_call, NULL, n);
2485 chain_count += DECL_STATIC_CHAIN (decl);
2488 while (chain_count != old_chain_count);
2490 if (dump_file && (dump_flags & TDF_DETAILS))
2491 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2492 iter_count);
2495 struct nesting_copy_body_data
2497 copy_body_data cb;
2498 struct nesting_info *root;
2501 /* A helper subroutine for debug_var_chain type remapping. */
2503 static tree
2504 nesting_copy_decl (tree decl, copy_body_data *id)
2506 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2507 tree *slot = nid->root->var_map->get (decl);
2509 if (slot)
2510 return (tree) *slot;
2512 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2514 tree new_decl = copy_decl_no_change (decl, id);
2515 DECL_ORIGINAL_TYPE (new_decl)
2516 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2517 return new_decl;
2520 if (TREE_CODE (decl) == VAR_DECL
2521 || TREE_CODE (decl) == PARM_DECL
2522 || TREE_CODE (decl) == RESULT_DECL)
2523 return decl;
2525 return copy_decl_no_change (decl, id);
2528 /* A helper function for remap_vla_decls. See if *TP contains
2529 some remapped variables. */
2531 static tree
2532 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2534 struct nesting_info *root = (struct nesting_info *) data;
2535 tree t = *tp;
2537 if (DECL_P (t))
2539 *walk_subtrees = 0;
2540 tree *slot = root->var_map->get (t);
2542 if (slot)
2543 return *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 (root->var_map->get (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 = new hash_map<tree, tree>;
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;
2592 val = DECL_VALUE_EXPR (var);
2593 type = TREE_TYPE (var);
2595 if (!(TREE_CODE (val) == INDIRECT_REF
2596 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2597 && variably_modified_type_p (type, NULL)))
2598 continue;
2600 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2601 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2602 continue;
2604 context = decl_function_context (var);
2605 for (i = root; i; i = i->outer)
2606 if (i->context == context)
2607 break;
2609 if (i == NULL)
2610 continue;
2612 /* Fully expand value expressions. This avoids having debug variables
2613 only referenced from them and that can be swept during GC. */
2614 if (slot)
2616 tree t = (tree) *slot;
2617 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2618 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2621 id.cb.src_fn = i->context;
2622 id.cb.dst_fn = i->context;
2623 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2625 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2626 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2628 newt = TREE_TYPE (newt);
2629 type = TREE_TYPE (type);
2631 if (TYPE_NAME (newt)
2632 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2633 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2634 && newt != type
2635 && TYPE_NAME (newt) == TYPE_NAME (type))
2636 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2638 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2639 if (val != DECL_VALUE_EXPR (var))
2640 SET_DECL_VALUE_EXPR (var, val);
2643 delete id.cb.decl_map;
2646 /* Fold the MEM_REF *E. */
2647 bool
2648 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2650 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2651 *ref_p = fold (*ref_p);
2652 return true;
2655 /* Do "everything else" to clean up or complete state collected by the
2656 various walking passes -- lay out the types and decls, generate code
2657 to initialize the frame decl, store critical expressions in the
2658 struct function for rtl to find. */
2660 static void
2661 finalize_nesting_tree_1 (struct nesting_info *root)
2663 gimple_seq stmt_list;
2664 gimple stmt;
2665 tree context = root->context;
2666 struct function *sf;
2668 stmt_list = NULL;
2670 /* If we created a non-local frame type or decl, we need to lay them
2671 out at this time. */
2672 if (root->frame_type)
2674 /* In some cases the frame type will trigger the -Wpadded warning.
2675 This is not helpful; suppress it. */
2676 int save_warn_padded = warn_padded;
2677 tree *adjust;
2679 warn_padded = 0;
2680 layout_type (root->frame_type);
2681 warn_padded = save_warn_padded;
2682 layout_decl (root->frame_decl, 0);
2684 /* Remove root->frame_decl from root->new_local_var_chain, so
2685 that we can declare it also in the lexical blocks, which
2686 helps ensure virtual regs that end up appearing in its RTL
2687 expression get substituted in instantiate_virtual_regs(). */
2688 for (adjust = &root->new_local_var_chain;
2689 *adjust != root->frame_decl;
2690 adjust = &DECL_CHAIN (*adjust))
2691 gcc_assert (DECL_CHAIN (*adjust));
2692 *adjust = DECL_CHAIN (*adjust);
2694 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2695 declare_vars (root->frame_decl,
2696 gimple_seq_first_stmt (gimple_body (context)), true);
2699 /* If any parameters were referenced non-locally, then we need to
2700 insert a copy. Likewise, if any variables were referenced by
2701 pointer, we need to initialize the address. */
2702 if (root->any_parm_remapped)
2704 tree p;
2705 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2707 tree field, x, y;
2709 field = lookup_field_for_decl (root, p, NO_INSERT);
2710 if (!field)
2711 continue;
2713 if (use_pointer_in_frame (p))
2714 x = build_addr (p, context);
2715 else
2716 x = p;
2718 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2719 root->frame_decl, field, NULL_TREE);
2720 stmt = gimple_build_assign (y, x);
2721 gimple_seq_add_stmt (&stmt_list, stmt);
2722 /* If the assignment is from a non-register the stmt is
2723 not valid gimple. Make it so by using a temporary instead. */
2724 if (!is_gimple_reg (x)
2725 && is_gimple_reg_type (TREE_TYPE (x)))
2727 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2728 x = init_tmp_var (root, x, &gsi);
2729 gimple_assign_set_rhs1 (stmt, x);
2734 /* If a chain_field was created, then it needs to be initialized
2735 from chain_decl. */
2736 if (root->chain_field)
2738 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2739 root->frame_decl, root->chain_field, NULL_TREE);
2740 stmt = gimple_build_assign (x, get_chain_decl (root));
2741 gimple_seq_add_stmt (&stmt_list, stmt);
2744 /* If trampolines were created, then we need to initialize them. */
2745 if (root->any_tramp_created)
2747 struct nesting_info *i;
2748 for (i = root->inner; i ; i = i->next)
2750 tree arg1, arg2, arg3, x, field;
2752 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2753 if (!field)
2754 continue;
2756 gcc_assert (DECL_STATIC_CHAIN (i->context));
2757 arg3 = build_addr (root->frame_decl, context);
2759 arg2 = build_addr (i->context, context);
2761 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2762 root->frame_decl, field, NULL_TREE);
2763 arg1 = build_addr (x, context);
2765 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2766 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2767 gimple_seq_add_stmt (&stmt_list, stmt);
2771 /* If we created initialization statements, insert them. */
2772 if (stmt_list)
2774 gimple bind;
2775 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2776 bind = gimple_seq_first_stmt (gimple_body (context));
2777 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2778 gimple_bind_set_body (bind, stmt_list);
2781 /* If a chain_decl was created, then it needs to be registered with
2782 struct function so that it gets initialized from the static chain
2783 register at the beginning of the function. */
2784 sf = DECL_STRUCT_FUNCTION (root->context);
2785 sf->static_chain_decl = root->chain_decl;
2787 /* Similarly for the non-local goto save area. */
2788 if (root->nl_goto_field)
2790 sf->nonlocal_goto_save_area
2791 = get_frame_field (root, context, root->nl_goto_field, NULL);
2792 sf->has_nonlocal_label = 1;
2795 /* Make sure all new local variables get inserted into the
2796 proper BIND_EXPR. */
2797 if (root->new_local_var_chain)
2798 declare_vars (root->new_local_var_chain,
2799 gimple_seq_first_stmt (gimple_body (root->context)),
2800 false);
2802 if (root->debug_var_chain)
2804 tree debug_var;
2805 gimple scope;
2807 remap_vla_decls (DECL_INITIAL (root->context), root);
2809 for (debug_var = root->debug_var_chain; debug_var;
2810 debug_var = DECL_CHAIN (debug_var))
2811 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2812 break;
2814 /* If there are any debug decls with variable length types,
2815 remap those types using other debug_var_chain variables. */
2816 if (debug_var)
2818 struct nesting_copy_body_data id;
2820 memset (&id, 0, sizeof (id));
2821 id.cb.copy_decl = nesting_copy_decl;
2822 id.cb.decl_map = new hash_map<tree, tree>;
2823 id.root = root;
2825 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2826 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2828 tree type = TREE_TYPE (debug_var);
2829 tree newt, t = type;
2830 struct nesting_info *i;
2832 for (i = root; i; i = i->outer)
2833 if (variably_modified_type_p (type, i->context))
2834 break;
2836 if (i == NULL)
2837 continue;
2839 id.cb.src_fn = i->context;
2840 id.cb.dst_fn = i->context;
2841 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2843 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2844 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2846 newt = TREE_TYPE (newt);
2847 t = TREE_TYPE (t);
2849 if (TYPE_NAME (newt)
2850 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2851 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2852 && newt != t
2853 && TYPE_NAME (newt) == TYPE_NAME (t))
2854 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2857 delete id.cb.decl_map;
2860 scope = gimple_seq_first_stmt (gimple_body (root->context));
2861 if (gimple_bind_block (scope))
2862 declare_vars (root->debug_var_chain, scope, true);
2863 else
2864 BLOCK_VARS (DECL_INITIAL (root->context))
2865 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2866 root->debug_var_chain);
2869 /* Fold the rewritten MEM_REF trees. */
2870 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2872 /* Dump the translated tree function. */
2873 if (dump_file)
2875 fputs ("\n\n", dump_file);
2876 dump_function_to_file (root->context, dump_file, dump_flags);
2880 static void
2881 finalize_nesting_tree (struct nesting_info *root)
2883 struct nesting_info *n;
2884 FOR_EACH_NEST_INFO (n, root)
2885 finalize_nesting_tree_1 (n);
2888 /* Unnest the nodes and pass them to cgraph. */
2890 static void
2891 unnest_nesting_tree_1 (struct nesting_info *root)
2893 struct cgraph_node *node = cgraph_node::get (root->context);
2895 /* For nested functions update the cgraph to reflect unnesting.
2896 We also delay finalizing of these functions up to this point. */
2897 if (node->origin)
2899 node->unnest ();
2900 cgraph_finalize_function (root->context, true);
2904 static void
2905 unnest_nesting_tree (struct nesting_info *root)
2907 struct nesting_info *n;
2908 FOR_EACH_NEST_INFO (n, root)
2909 unnest_nesting_tree_1 (n);
2912 /* Free the data structures allocated during this pass. */
2914 static void
2915 free_nesting_tree (struct nesting_info *root)
2917 struct nesting_info *node, *next;
2919 node = iter_nestinfo_start (root);
2922 next = iter_nestinfo_next (node);
2923 delete node->var_map;
2924 delete node->field_map;
2925 delete node->mem_refs;
2926 free (node);
2927 node = next;
2929 while (node);
2932 /* Gimplify a function and all its nested functions. */
2933 static void
2934 gimplify_all_functions (struct cgraph_node *root)
2936 struct cgraph_node *iter;
2937 if (!gimple_body (root->decl))
2938 gimplify_function_tree (root->decl);
2939 for (iter = root->nested; iter; iter = iter->next_nested)
2940 gimplify_all_functions (iter);
2943 /* Main entry point for this pass. Process FNDECL and all of its nested
2944 subroutines and turn them into something less tightly bound. */
2946 void
2947 lower_nested_functions (tree fndecl)
2949 struct cgraph_node *cgn;
2950 struct nesting_info *root;
2952 /* If there are no nested functions, there's nothing to do. */
2953 cgn = cgraph_node::get (fndecl);
2954 if (!cgn->nested)
2955 return;
2957 gimplify_all_functions (cgn);
2959 dump_file = dump_begin (TDI_nested, &dump_flags);
2960 if (dump_file)
2961 fprintf (dump_file, "\n;; Function %s\n\n",
2962 lang_hooks.decl_printable_name (fndecl, 2));
2964 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2965 root = create_nesting_tree (cgn);
2967 walk_all_functions (convert_nonlocal_reference_stmt,
2968 convert_nonlocal_reference_op,
2969 root);
2970 walk_all_functions (convert_local_reference_stmt,
2971 convert_local_reference_op,
2972 root);
2973 walk_all_functions (convert_nl_goto_reference, NULL, root);
2974 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2976 convert_all_function_calls (root);
2977 finalize_nesting_tree (root);
2978 unnest_nesting_tree (root);
2980 free_nesting_tree (root);
2981 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2983 if (dump_file)
2985 dump_end (TDI_nested, dump_file);
2986 dump_file = NULL;
2990 #include "gt-tree-nested.h"