PR c++/30897
[official-gcc.git] / gcc / tree-nested.c
blob118f5fe456916c69f3d7c765a0b9f10bb3fd8a11
1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005, 2006, 2007 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 "rtl.h"
26 #include "tm_p.h"
27 #include "function.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
33 #include "cgraph.h"
34 #include "expr.h"
35 #include "langhooks.h"
36 #include "pointer-set.h"
37 #include "ggc.h"
40 /* The object of this pass is to lower the representation of a set of nested
41 functions in order to expose all of the gory details of the various
42 nonlocal references. We want to do this sooner rather than later, in
43 order to give us more freedom in emitting all of the functions in question.
45 Back in olden times, when gcc was young, we developed an insanely
46 complicated scheme whereby variables which were referenced nonlocally
47 were forced to live in the stack of the declaring function, and then
48 the nested functions magically discovered where these variables were
49 placed. In order for this scheme to function properly, it required
50 that the outer function be partially expanded, then we switch to
51 compiling the inner function, and once done with those we switch back
52 to compiling the outer function. Such delicate ordering requirements
53 makes it difficult to do whole translation unit optimizations
54 involving such functions.
56 The implementation here is much more direct. Everything that can be
57 referenced by an inner function is a member of an explicitly created
58 structure herein called the "nonlocal frame struct". The incoming
59 static chain for a nested function is a pointer to this struct in
60 the parent. In this way, we settle on known offsets from a known
61 base, and so are decoupled from the logic that places objects in the
62 function's stack frame. More importantly, we don't have to wait for
63 that to happen -- since the compilation of the inner function is no
64 longer tied to a real stack frame, the nonlocal frame struct can be
65 allocated anywhere. Which means that the outer function is now
66 inlinable.
68 Theory of operation here is very simple. Iterate over all the
69 statements in all the functions (depth first) several times,
70 allocating structures and fields on demand. In general we want to
71 examine inner functions first, so that we can avoid making changes
72 to outer functions which are unnecessary.
74 The order of the passes matters a bit, in that later passes will be
75 skipped if it is discovered that the functions don't actually interact
76 at all. That is, they're nested in the lexical sense but could have
77 been written as independent functions without change. */
80 struct nesting_info
82 struct nesting_info *outer;
83 struct nesting_info *inner;
84 struct nesting_info *next;
86 struct pointer_map_t *field_map;
87 struct pointer_map_t *var_map;
88 bitmap suppress_expansion;
90 tree context;
91 tree new_local_var_chain;
92 tree debug_var_chain;
93 tree frame_type;
94 tree frame_decl;
95 tree chain_field;
96 tree chain_decl;
97 tree nl_goto_field;
99 bool any_parm_remapped;
100 bool any_tramp_created;
101 char static_chain_added;
105 /* Obstack used for the bitmaps in the struct above. */
106 static struct bitmap_obstack nesting_info_bitmap_obstack;
109 /* We're working in so many different function contexts simultaneously,
110 that create_tmp_var is dangerous. Prevent mishap. */
111 #define create_tmp_var cant_use_create_tmp_var_here_dummy
113 /* Like create_tmp_var, except record the variable for registration at
114 the given nesting level. */
116 static tree
117 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
119 tree tmp_var;
121 /* If the type is of variable size or a type which must be created by the
122 frontend, something is wrong. Note that we explicitly allow
123 incomplete types here, since we create them ourselves here. */
124 gcc_assert (!TREE_ADDRESSABLE (type));
125 gcc_assert (!TYPE_SIZE_UNIT (type)
126 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
128 tmp_var = create_tmp_var_raw (type, prefix);
129 DECL_CONTEXT (tmp_var) = info->context;
130 TREE_CHAIN (tmp_var) = info->new_local_var_chain;
131 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
132 if (TREE_CODE (type) == COMPLEX_TYPE
133 || TREE_CODE (type) == VECTOR_TYPE)
134 DECL_GIMPLE_REG_P (tmp_var) = 1;
136 info->new_local_var_chain = tmp_var;
138 return tmp_var;
141 /* Take the address of EXP to be used within function CONTEXT.
142 Mark it for addressability as necessary. */
144 tree
145 build_addr (tree exp, tree context)
147 tree base = exp;
148 tree save_context;
149 tree retval;
151 while (handled_component_p (base))
152 base = TREE_OPERAND (base, 0);
154 if (DECL_P (base))
155 TREE_ADDRESSABLE (base) = 1;
157 /* Building the ADDR_EXPR will compute a set of properties for
158 that ADDR_EXPR. Those properties are unfortunately context
159 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
161 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
162 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
163 way the properties are for the ADDR_EXPR are computed properly. */
164 save_context = current_function_decl;
165 current_function_decl = context;
166 retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
167 current_function_decl = save_context;
168 return retval;
171 /* Insert FIELD into TYPE, sorted by alignment requirements. */
173 void
174 insert_field_into_struct (tree type, tree field)
176 tree *p;
178 DECL_CONTEXT (field) = type;
180 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
181 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
182 break;
184 TREE_CHAIN (field) = *p;
185 *p = field;
188 /* Build or return the RECORD_TYPE that describes the frame state that is
189 shared between INFO->CONTEXT and its nested functions. This record will
190 not be complete until finalize_nesting_tree; up until that point we'll
191 be adding fields as necessary.
193 We also build the DECL that represents this frame in the function. */
195 static tree
196 get_frame_type (struct nesting_info *info)
198 tree type = info->frame_type;
199 if (!type)
201 char *name;
203 type = make_node (RECORD_TYPE);
205 name = concat ("FRAME.",
206 IDENTIFIER_POINTER (DECL_NAME (info->context)),
207 NULL);
208 TYPE_NAME (type) = get_identifier (name);
209 free (name);
211 info->frame_type = type;
212 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
214 /* ??? Always make it addressable for now, since it is meant to
215 be pointed to by the static chain pointer. This pessimizes
216 when it turns out that no static chains are needed because
217 the nested functions referencing non-local variables are not
218 reachable, but the true pessimization is to create the non-
219 local frame structure in the first place. */
220 TREE_ADDRESSABLE (info->frame_decl) = 1;
222 return type;
225 /* Return true if DECL should be referenced by pointer in the non-local
226 frame structure. */
228 static bool
229 use_pointer_in_frame (tree decl)
231 if (TREE_CODE (decl) == PARM_DECL)
233 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
234 sized decls, and inefficient to copy large aggregates. Don't bother
235 moving anything but scalar variables. */
236 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
238 else
240 /* Variable sized types make things "interesting" in the frame. */
241 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
245 /* Given DECL, a non-locally accessed variable, find or create a field
246 in the non-local frame structure for the given nesting context. */
248 static tree
249 lookup_field_for_decl (struct nesting_info *info, tree decl,
250 enum insert_option insert)
252 void **slot;
254 if (insert == NO_INSERT)
256 slot = pointer_map_contains (info->field_map, decl);
257 return slot ? *slot : NULL;
260 slot = pointer_map_insert (info->field_map, decl);
261 if (!*slot)
263 tree field = make_node (FIELD_DECL);
264 DECL_NAME (field) = DECL_NAME (decl);
266 if (use_pointer_in_frame (decl))
268 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
269 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
270 DECL_NONADDRESSABLE_P (field) = 1;
272 else
274 TREE_TYPE (field) = TREE_TYPE (decl);
275 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
276 DECL_ALIGN (field) = DECL_ALIGN (decl);
277 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
278 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
279 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
280 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
283 insert_field_into_struct (get_frame_type (info), field);
284 *slot = field;
286 if (TREE_CODE (decl) == PARM_DECL)
287 info->any_parm_remapped = true;
290 return *slot;
293 /* Build or return the variable that holds the static chain within
294 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
296 static tree
297 get_chain_decl (struct nesting_info *info)
299 tree decl = info->chain_decl;
300 if (!decl)
302 tree type;
304 type = get_frame_type (info->outer);
305 type = build_pointer_type (type);
307 /* Note that this variable is *not* entered into any BIND_EXPR;
308 the construction of this variable is handled specially in
309 expand_function_start and initialize_inlined_parameters.
310 Note also that it's represented as a parameter. This is more
311 close to the truth, since the initial value does come from
312 the caller. */
313 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
314 DECL_ARTIFICIAL (decl) = 1;
315 DECL_IGNORED_P (decl) = 1;
316 TREE_USED (decl) = 1;
317 DECL_CONTEXT (decl) = info->context;
318 DECL_ARG_TYPE (decl) = type;
320 /* Tell tree-inline.c that we never write to this variable, so
321 it can copy-prop the replacement value immediately. */
322 TREE_READONLY (decl) = 1;
324 info->chain_decl = decl;
326 return decl;
329 /* Build or return the field within the non-local frame state that holds
330 the static chain for INFO->CONTEXT. This is the way to walk back up
331 multiple nesting levels. */
333 static tree
334 get_chain_field (struct nesting_info *info)
336 tree field = info->chain_field;
337 if (!field)
339 tree type = build_pointer_type (get_frame_type (info->outer));
341 field = make_node (FIELD_DECL);
342 DECL_NAME (field) = get_identifier ("__chain");
343 TREE_TYPE (field) = type;
344 DECL_ALIGN (field) = TYPE_ALIGN (type);
345 DECL_NONADDRESSABLE_P (field) = 1;
347 insert_field_into_struct (get_frame_type (info), field);
349 info->chain_field = field;
351 return field;
354 /* Copy EXP into a temporary. Allocate the temporary in the context of
355 INFO and insert the initialization statement before TSI. */
357 static tree
358 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
360 tree t, stmt;
362 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
363 stmt = build_gimple_modify_stmt (t, exp);
364 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
365 tsi_link_before (tsi, stmt, TSI_SAME_STMT);
367 return t;
370 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
372 static tree
373 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
375 if (is_gimple_val (exp))
376 return exp;
377 else
378 return init_tmp_var (info, exp, tsi);
381 /* Similarly, but copy from the temporary and insert the statement
382 after the iterator. */
384 static tree
385 save_tmp_var (struct nesting_info *info, tree exp,
386 tree_stmt_iterator *tsi)
388 tree t, stmt;
390 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
391 stmt = build_gimple_modify_stmt (exp, t);
392 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
393 tsi_link_after (tsi, stmt, TSI_SAME_STMT);
395 return t;
398 /* Build or return the type used to represent a nested function trampoline. */
400 static GTY(()) tree trampoline_type;
402 static tree
403 get_trampoline_type (void)
405 unsigned align, size;
406 tree t;
408 if (trampoline_type)
409 return trampoline_type;
411 align = TRAMPOLINE_ALIGNMENT;
412 size = TRAMPOLINE_SIZE;
414 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
415 then allocate extra space so that we can do dynamic alignment. */
416 if (align > STACK_BOUNDARY)
418 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
419 align = STACK_BOUNDARY;
422 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
423 t = build_array_type (char_type_node, t);
424 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
425 DECL_ALIGN (t) = align;
426 DECL_USER_ALIGN (t) = 1;
428 trampoline_type = make_node (RECORD_TYPE);
429 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
430 TYPE_FIELDS (trampoline_type) = t;
431 layout_type (trampoline_type);
433 return trampoline_type;
436 /* Given DECL, a nested function, find or create a field in the non-local
437 frame structure for a trampoline for this function. */
439 static tree
440 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
441 enum insert_option insert)
443 void **slot;
445 if (insert == NO_INSERT)
447 slot = pointer_map_contains (info->var_map, decl);
448 return slot ? *slot : NULL;
451 slot = pointer_map_insert (info->var_map, decl);
452 if (!*slot)
454 tree field = make_node (FIELD_DECL);
455 DECL_NAME (field) = DECL_NAME (decl);
456 TREE_TYPE (field) = get_trampoline_type ();
457 TREE_ADDRESSABLE (field) = 1;
459 insert_field_into_struct (get_frame_type (info), field);
460 *slot = field;
462 info->any_tramp_created = true;
465 return *slot;
468 /* Build or return the field within the non-local frame state that holds
469 the non-local goto "jmp_buf". The buffer itself is maintained by the
470 rtl middle-end as dynamic stack space is allocated. */
472 static tree
473 get_nl_goto_field (struct nesting_info *info)
475 tree field = info->nl_goto_field;
476 if (!field)
478 unsigned size;
479 tree type;
481 /* For __builtin_nonlocal_goto, we need N words. The first is the
482 frame pointer, the rest is for the target's stack pointer save
483 area. The number of words is controlled by STACK_SAVEAREA_MODE;
484 not the best interface, but it'll do for now. */
485 if (Pmode == ptr_mode)
486 type = ptr_type_node;
487 else
488 type = lang_hooks.types.type_for_mode (Pmode, 1);
490 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
491 size = size / GET_MODE_SIZE (Pmode);
492 size = size + 1;
494 type = build_array_type
495 (type, build_index_type (build_int_cst (NULL_TREE, size)));
497 field = make_node (FIELD_DECL);
498 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
499 TREE_TYPE (field) = type;
500 DECL_ALIGN (field) = TYPE_ALIGN (type);
501 TREE_ADDRESSABLE (field) = 1;
503 insert_field_into_struct (get_frame_type (info), field);
505 info->nl_goto_field = field;
508 return field;
511 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
513 static void
514 walk_asm_expr (struct walk_stmt_info *wi, tree stmt)
516 int noutputs = list_length (ASM_OUTPUTS (stmt));
517 const char **oconstraints
518 = (const char **) alloca ((noutputs) * sizeof (const char *));
519 int i;
520 tree link;
521 const char *constraint;
522 bool allows_mem, allows_reg, is_inout;
524 wi->is_lhs = true;
525 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
527 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
528 oconstraints[i] = constraint;
529 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
530 &allows_reg, &is_inout);
532 wi->val_only = (allows_reg || !allows_mem);
533 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
536 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
538 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
539 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
540 oconstraints, &allows_mem, &allows_reg);
542 wi->val_only = (allows_reg || !allows_mem);
543 /* Although input "m" is not really a LHS, we need a lvalue. */
544 wi->is_lhs = !wi->val_only;
545 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
548 wi->is_lhs = false;
549 wi->val_only = true;
552 /* Iterate over all sub-statements of *TP calling walk_tree with
553 WI->CALLBACK for every sub-expression in each statement found. */
555 void
556 walk_stmts (struct walk_stmt_info *wi, tree *tp)
558 tree t = *tp;
559 int walk_subtrees;
561 if (!t)
562 return;
564 if (wi->want_locations && EXPR_HAS_LOCATION (t))
565 input_location = EXPR_LOCATION (t);
567 switch (TREE_CODE (t))
569 case STATEMENT_LIST:
571 tree_stmt_iterator i;
572 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
574 wi->tsi = i;
575 walk_stmts (wi, tsi_stmt_ptr (i));
578 break;
580 case COND_EXPR:
581 walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
582 walk_stmts (wi, &COND_EXPR_THEN (t));
583 walk_stmts (wi, &COND_EXPR_ELSE (t));
584 break;
585 case CATCH_EXPR:
586 walk_stmts (wi, &CATCH_BODY (t));
587 break;
588 case EH_FILTER_EXPR:
589 walk_stmts (wi, &EH_FILTER_FAILURE (t));
590 break;
591 case TRY_CATCH_EXPR:
592 case TRY_FINALLY_EXPR:
593 walk_stmts (wi, &TREE_OPERAND (t, 0));
594 walk_stmts (wi, &TREE_OPERAND (t, 1));
595 break;
597 case BIND_EXPR:
598 if (wi->want_bind_expr)
600 walk_subtrees = 1;
601 wi->callback (tp, &walk_subtrees, wi);
602 if (!walk_subtrees)
603 break;
605 walk_stmts (wi, &BIND_EXPR_BODY (t));
606 break;
608 case RETURN_EXPR:
609 if (wi->want_return_expr)
611 walk_subtrees = 1;
612 wi->callback (tp, &walk_subtrees, wi);
613 if (!walk_subtrees)
614 break;
616 walk_stmts (wi, &TREE_OPERAND (t, 0));
617 break;
619 case GIMPLE_MODIFY_STMT:
620 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
621 wi->val_only = !is_gimple_formal_tmp_var (GIMPLE_STMT_OPERAND (t, 0));
622 walk_tree (&GIMPLE_STMT_OPERAND (t, 1), wi->callback, wi, NULL);
624 /* If the rhs is appropriate for a memory, we may use a
625 COMPONENT_REF on the lhs. */
626 wi->val_only = !is_gimple_mem_rhs (GIMPLE_STMT_OPERAND (t, 1));
627 wi->is_lhs = true;
628 walk_tree (&GIMPLE_STMT_OPERAND (t, 0), wi->callback, wi, NULL);
630 wi->val_only = true;
631 wi->is_lhs = false;
632 break;
634 case ASM_EXPR:
635 walk_asm_expr (wi, *tp);
636 break;
638 default:
639 wi->val_only = true;
640 walk_tree (tp, wi->callback, wi, NULL);
641 break;
645 /* Invoke CALLBACK on all statements of *STMT_P. */
647 static void
648 walk_body (walk_tree_fn callback, struct nesting_info *info, tree *stmt_p)
650 struct walk_stmt_info wi;
652 memset (&wi, 0, sizeof (wi));
653 wi.callback = callback;
654 wi.info = info;
655 wi.val_only = true;
657 walk_stmts (&wi, stmt_p);
660 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
662 static inline void
663 walk_function (walk_tree_fn callback, struct nesting_info *info)
665 walk_body (callback, info, &DECL_SAVED_TREE (info->context));
668 /* Similarly for ROOT and all functions nested underneath, depth first. */
670 static void
671 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
675 if (root->inner)
676 walk_all_functions (callback, root->inner);
677 walk_function (callback, root);
678 root = root->next;
680 while (root);
683 /* We have to check for a fairly pathological case. The operands of function
684 nested function are to be interpreted in the context of the enclosing
685 function. So if any are variably-sized, they will get remapped when the
686 enclosing function is inlined. But that remapping would also have to be
687 done in the types of the PARM_DECLs of the nested function, meaning the
688 argument types of that function will disagree with the arguments in the
689 calls to that function. So we'd either have to make a copy of the nested
690 function corresponding to each time the enclosing function was inlined or
691 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
692 function. The former is not practical. The latter would still require
693 detecting this case to know when to add the conversions. So, for now at
694 least, we don't inline such an enclosing function.
696 We have to do that check recursively, so here return indicating whether
697 FNDECL has such a nested function. ORIG_FN is the function we were
698 trying to inline to use for checking whether any argument is variably
699 modified by anything in it.
701 It would be better to do this in tree-inline.c so that we could give
702 the appropriate warning for why a function can't be inlined, but that's
703 too late since the nesting structure has already been flattened and
704 adding a flag just to record this fact seems a waste of a flag. */
706 static bool
707 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
709 struct cgraph_node *cgn = cgraph_node (fndecl);
710 tree arg;
712 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
714 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
715 if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
716 return true;
718 if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
719 return true;
722 return false;
725 /* Construct our local datastructure describing the function nesting
726 tree rooted by CGN. */
728 static struct nesting_info *
729 create_nesting_tree (struct cgraph_node *cgn)
731 struct nesting_info *info = XCNEW (struct nesting_info);
732 info->field_map = pointer_map_create ();
733 info->var_map = pointer_map_create ();
734 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
735 info->context = cgn->decl;
737 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
739 struct nesting_info *sub = create_nesting_tree (cgn);
740 sub->outer = info;
741 sub->next = info->inner;
742 info->inner = sub;
745 /* See discussion at check_for_nested_with_variably_modified for a
746 discussion of why this has to be here. */
747 if (check_for_nested_with_variably_modified (info->context, info->context))
748 DECL_UNINLINABLE (info->context) = true;
750 return info;
753 /* Return an expression computing the static chain for TARGET_CONTEXT
754 from INFO->CONTEXT. Insert any necessary computations before TSI. */
756 static tree
757 get_static_chain (struct nesting_info *info, tree target_context,
758 tree_stmt_iterator *tsi)
760 struct nesting_info *i;
761 tree x;
763 if (info->context == target_context)
765 x = build_addr (info->frame_decl, target_context);
767 else
769 x = get_chain_decl (info);
771 for (i = info->outer; i->context != target_context; i = i->outer)
773 tree field = get_chain_field (i);
775 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
776 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
777 x = init_tmp_var (info, x, tsi);
781 return x;
784 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
785 frame as seen from INFO->CONTEXT. Insert any necessary computations
786 before TSI. */
788 static tree
789 get_frame_field (struct nesting_info *info, tree target_context,
790 tree field, tree_stmt_iterator *tsi)
792 struct nesting_info *i;
793 tree x;
795 if (info->context == target_context)
797 /* Make sure frame_decl gets created. */
798 (void) get_frame_type (info);
799 x = info->frame_decl;
801 else
803 x = get_chain_decl (info);
805 for (i = info->outer; i->context != target_context; i = i->outer)
807 tree field = get_chain_field (i);
809 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
810 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
811 x = init_tmp_var (info, x, tsi);
814 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
817 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
818 return x;
821 /* A subroutine of convert_nonlocal_reference. 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;
832 void **slot;
834 slot = pointer_map_insert (info->var_map, decl);
836 if (*slot)
837 return *slot;
839 target_context = decl_function_context (decl);
841 /* A copy of the code in get_frame_field, but without the temporaries. */
842 if (info->context == target_context)
844 /* Make sure frame_decl gets created. */
845 (void) get_frame_type (info);
846 x = info->frame_decl;
847 i = info;
849 else
851 x = get_chain_decl (info);
852 for (i = info->outer; i->context != target_context; i = i->outer)
854 field = get_chain_field (i);
855 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
856 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
858 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
861 field = lookup_field_for_decl (i, decl, INSERT);
862 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
863 if (use_pointer_in_frame (decl))
864 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
866 /* ??? We should be remapping types as well, surely. */
867 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
868 DECL_CONTEXT (new_decl) = info->context;
869 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
870 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
871 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
872 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
873 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
874 TREE_READONLY (new_decl) = TREE_READONLY (decl);
875 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
876 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
878 SET_DECL_VALUE_EXPR (new_decl, x);
879 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
881 *slot = new_decl;
882 TREE_CHAIN (new_decl) = info->debug_var_chain;
883 info->debug_var_chain = new_decl;
885 return new_decl;
888 /* Called via walk_function+walk_tree, rewrite all references to VAR
889 and PARM_DECLs that belong to outer functions.
891 The rewrite will involve some number of structure accesses back up
892 the static chain. E.g. for a variable FOO up one nesting level it'll
893 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
894 indirections apply to decls for which use_pointer_in_frame is true. */
896 static bool convert_nonlocal_omp_clauses (tree *, struct walk_stmt_info *);
898 static tree
899 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
901 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
902 struct nesting_info *info = wi->info;
903 tree t = *tp;
904 tree save_local_var_chain;
905 bitmap save_suppress;
907 *walk_subtrees = 0;
908 switch (TREE_CODE (t))
910 case VAR_DECL:
911 /* Non-automatic variables are never processed. */
912 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
913 break;
914 /* FALLTHRU */
916 case PARM_DECL:
917 if (decl_function_context (t) != info->context)
919 tree x;
920 wi->changed = true;
922 x = get_nonlocal_debug_decl (info, t);
923 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
925 tree target_context = decl_function_context (t);
926 struct nesting_info *i;
927 for (i = info->outer; i->context != target_context; i = i->outer)
928 continue;
929 x = lookup_field_for_decl (i, t, INSERT);
930 x = get_frame_field (info, target_context, x, &wi->tsi);
931 if (use_pointer_in_frame (t))
933 x = init_tmp_var (info, x, &wi->tsi);
934 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
938 if (wi->val_only)
940 if (wi->is_lhs)
941 x = save_tmp_var (info, x, &wi->tsi);
942 else
943 x = init_tmp_var (info, x, &wi->tsi);
946 *tp = x;
948 break;
950 case GOTO_EXPR:
951 /* Don't walk non-local gotos for now. */
952 if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
954 *walk_subtrees = 1;
955 wi->val_only = true;
956 wi->is_lhs = false;
958 break;
960 case LABEL_DECL:
961 /* We're taking the address of a label from a parent function, but
962 this is not itself a non-local goto. Mark the label such that it
963 will not be deleted, much as we would with a label address in
964 static storage. */
965 if (decl_function_context (t) != info->context)
966 FORCED_LABEL (t) = 1;
967 break;
969 case ADDR_EXPR:
971 bool save_val_only = wi->val_only;
973 wi->val_only = false;
974 wi->is_lhs = false;
975 wi->changed = false;
976 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
977 wi->val_only = true;
979 if (wi->changed)
981 tree save_context;
983 /* If we changed anything, then TREE_INVARIANT is be wrong,
984 since we're no longer directly referencing a decl. */
985 save_context = current_function_decl;
986 current_function_decl = info->context;
987 recompute_tree_invariant_for_addr_expr (t);
988 current_function_decl = save_context;
990 /* If the callback converted the address argument in a context
991 where we only accept variables (and min_invariant, presumably),
992 then compute the address into a temporary. */
993 if (save_val_only)
994 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
997 break;
999 case REALPART_EXPR:
1000 case IMAGPART_EXPR:
1001 case COMPONENT_REF:
1002 case ARRAY_REF:
1003 case ARRAY_RANGE_REF:
1004 case BIT_FIELD_REF:
1005 /* Go down this entire nest and just look at the final prefix and
1006 anything that describes the references. Otherwise, we lose track
1007 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1008 wi->val_only = true;
1009 wi->is_lhs = false;
1010 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1012 if (TREE_CODE (t) == COMPONENT_REF)
1013 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1014 NULL);
1015 else if (TREE_CODE (t) == ARRAY_REF
1016 || TREE_CODE (t) == ARRAY_RANGE_REF)
1018 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1019 NULL);
1020 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1021 NULL);
1022 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
1023 NULL);
1025 else if (TREE_CODE (t) == BIT_FIELD_REF)
1027 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1028 NULL);
1029 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1030 NULL);
1033 wi->val_only = false;
1034 walk_tree (tp, convert_nonlocal_reference, wi, NULL);
1035 break;
1037 case VIEW_CONVERT_EXPR:
1038 /* Just request to look at the subtrees, leaving val_only and lhs
1039 untouched. This might actually be for !val_only + lhs, in which
1040 case we don't want to force a replacement by a temporary. */
1041 *walk_subtrees = 1;
1042 break;
1044 case OMP_PARALLEL:
1045 save_suppress = info->suppress_expansion;
1046 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1048 tree c, decl;
1049 decl = get_chain_decl (info);
1050 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1051 OMP_CLAUSE_DECL (c) = decl;
1052 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1053 OMP_PARALLEL_CLAUSES (t) = c;
1056 save_local_var_chain = info->new_local_var_chain;
1057 info->new_local_var_chain = NULL;
1059 walk_body (convert_nonlocal_reference, info, &OMP_PARALLEL_BODY (t));
1061 if (info->new_local_var_chain)
1062 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1063 info->new_local_var_chain = save_local_var_chain;
1064 info->suppress_expansion = save_suppress;
1065 break;
1067 case OMP_FOR:
1068 case OMP_SECTIONS:
1069 case OMP_SINGLE:
1070 save_suppress = info->suppress_expansion;
1071 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t), wi);
1072 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1073 info->suppress_expansion = save_suppress;
1074 break;
1076 case OMP_SECTION:
1077 case OMP_MASTER:
1078 case OMP_ORDERED:
1079 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1080 break;
1082 default:
1083 if (!IS_TYPE_OR_DECL_P (t))
1085 *walk_subtrees = 1;
1086 wi->val_only = true;
1087 wi->is_lhs = false;
1089 break;
1092 return NULL_TREE;
1095 static bool
1096 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1098 struct nesting_info *info = wi->info;
1099 bool need_chain = false;
1100 tree clause, decl;
1101 int dummy;
1102 bitmap new_suppress;
1104 new_suppress = BITMAP_GGC_ALLOC ();
1105 bitmap_copy (new_suppress, info->suppress_expansion);
1107 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1109 switch (OMP_CLAUSE_CODE (clause))
1111 case OMP_CLAUSE_PRIVATE:
1112 case OMP_CLAUSE_FIRSTPRIVATE:
1113 case OMP_CLAUSE_LASTPRIVATE:
1114 case OMP_CLAUSE_REDUCTION:
1115 case OMP_CLAUSE_COPYPRIVATE:
1116 case OMP_CLAUSE_SHARED:
1117 decl = OMP_CLAUSE_DECL (clause);
1118 if (decl_function_context (decl) != info->context)
1120 bitmap_set_bit (new_suppress, DECL_UID (decl));
1121 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1122 need_chain = true;
1124 break;
1126 case OMP_CLAUSE_SCHEDULE:
1127 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1128 break;
1129 /* FALLTHRU */
1130 case OMP_CLAUSE_IF:
1131 case OMP_CLAUSE_NUM_THREADS:
1132 wi->val_only = true;
1133 wi->is_lhs = false;
1134 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1135 wi);
1136 break;
1138 case OMP_CLAUSE_NOWAIT:
1139 case OMP_CLAUSE_ORDERED:
1140 case OMP_CLAUSE_DEFAULT:
1141 case OMP_CLAUSE_COPYIN:
1142 break;
1144 default:
1145 gcc_unreachable ();
1149 info->suppress_expansion = new_suppress;
1151 return need_chain;
1154 /* A subroutine of convert_local_reference. Create a local variable
1155 in the parent function with DECL_VALUE_EXPR set to reference the
1156 field in FRAME. This is used both for debug info and in OpenMP
1157 lowering. */
1159 static tree
1160 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1162 tree x, new_decl;
1163 void **slot;
1165 slot = pointer_map_insert (info->var_map, decl);
1166 if (*slot)
1167 return *slot;
1169 /* Make sure frame_decl gets created. */
1170 (void) get_frame_type (info);
1171 x = info->frame_decl;
1172 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1174 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1175 DECL_CONTEXT (new_decl) = info->context;
1176 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1177 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1178 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1179 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1180 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1181 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1182 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1183 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1185 SET_DECL_VALUE_EXPR (new_decl, x);
1186 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1187 *slot = new_decl;
1189 TREE_CHAIN (new_decl) = info->debug_var_chain;
1190 info->debug_var_chain = new_decl;
1192 /* Do not emit debug info twice. */
1193 DECL_IGNORED_P (decl) = 1;
1195 return new_decl;
1198 /* Called via walk_function+walk_tree, rewrite all references to VAR
1199 and PARM_DECLs that were referenced by inner nested functions.
1200 The rewrite will be a structure reference to the local frame variable. */
1202 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1204 static tree
1205 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
1207 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1208 struct nesting_info *info = wi->info;
1209 tree t = *tp, field, x;
1210 bool save_val_only;
1211 tree save_local_var_chain;
1212 bitmap save_suppress;
1214 *walk_subtrees = 0;
1215 switch (TREE_CODE (t))
1217 case VAR_DECL:
1218 /* Non-automatic variables are never processed. */
1219 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1220 break;
1221 /* FALLTHRU */
1223 case PARM_DECL:
1224 if (decl_function_context (t) == info->context)
1226 /* If we copied a pointer to the frame, then the original decl
1227 is used unchanged in the parent function. */
1228 if (use_pointer_in_frame (t))
1229 break;
1231 /* No need to transform anything if no child references the
1232 variable. */
1233 field = lookup_field_for_decl (info, t, NO_INSERT);
1234 if (!field)
1235 break;
1236 wi->changed = true;
1238 x = get_local_debug_decl (info, t, field);
1239 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1240 x = get_frame_field (info, info->context, field, &wi->tsi);
1242 if (wi->val_only)
1244 if (wi->is_lhs)
1245 x = save_tmp_var (info, x, &wi->tsi);
1246 else
1247 x = init_tmp_var (info, x, &wi->tsi);
1250 *tp = x;
1252 break;
1254 case ADDR_EXPR:
1255 save_val_only = wi->val_only;
1256 wi->val_only = false;
1257 wi->is_lhs = false;
1258 wi->changed = false;
1259 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
1260 wi->val_only = save_val_only;
1262 /* If we converted anything ... */
1263 if (wi->changed)
1265 tree save_context;
1267 /* Then the frame decl is now addressable. */
1268 TREE_ADDRESSABLE (info->frame_decl) = 1;
1270 save_context = current_function_decl;
1271 current_function_decl = info->context;
1272 recompute_tree_invariant_for_addr_expr (t);
1273 current_function_decl = save_context;
1275 /* If we are in a context where we only accept values, then
1276 compute the address into a temporary. */
1277 if (save_val_only)
1278 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1280 break;
1282 case REALPART_EXPR:
1283 case IMAGPART_EXPR:
1284 case COMPONENT_REF:
1285 case ARRAY_REF:
1286 case ARRAY_RANGE_REF:
1287 case BIT_FIELD_REF:
1288 /* Go down this entire nest and just look at the final prefix and
1289 anything that describes the references. Otherwise, we lose track
1290 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1291 save_val_only = wi->val_only;
1292 wi->val_only = true;
1293 wi->is_lhs = false;
1294 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1296 if (TREE_CODE (t) == COMPONENT_REF)
1297 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1298 NULL);
1299 else if (TREE_CODE (t) == ARRAY_REF
1300 || TREE_CODE (t) == ARRAY_RANGE_REF)
1302 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1303 NULL);
1304 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1305 NULL);
1306 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1307 NULL);
1309 else if (TREE_CODE (t) == BIT_FIELD_REF)
1311 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1312 NULL);
1313 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1314 NULL);
1317 wi->val_only = false;
1318 walk_tree (tp, convert_local_reference, wi, NULL);
1319 wi->val_only = save_val_only;
1320 break;
1322 case VIEW_CONVERT_EXPR:
1323 /* Just request to look at the subtrees, leaving val_only and lhs
1324 untouched. This might actually be for !val_only + lhs, in which
1325 case we don't want to force a replacement by a temporary. */
1326 *walk_subtrees = 1;
1327 break;
1329 case OMP_PARALLEL:
1330 save_suppress = info->suppress_expansion;
1331 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1333 tree c;
1334 (void) get_frame_type (info);
1335 c = build_omp_clause (OMP_CLAUSE_SHARED);
1336 OMP_CLAUSE_DECL (c) = info->frame_decl;
1337 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1338 OMP_PARALLEL_CLAUSES (t) = c;
1341 save_local_var_chain = info->new_local_var_chain;
1342 info->new_local_var_chain = NULL;
1344 walk_body (convert_local_reference, info, &OMP_PARALLEL_BODY (t));
1346 if (info->new_local_var_chain)
1347 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1348 info->new_local_var_chain = save_local_var_chain;
1349 info->suppress_expansion = save_suppress;
1350 break;
1352 case OMP_FOR:
1353 case OMP_SECTIONS:
1354 case OMP_SINGLE:
1355 save_suppress = info->suppress_expansion;
1356 convert_local_omp_clauses (&OMP_CLAUSES (t), wi);
1357 walk_body (convert_local_reference, info, &OMP_BODY (t));
1358 info->suppress_expansion = save_suppress;
1359 break;
1361 case OMP_SECTION:
1362 case OMP_MASTER:
1363 case OMP_ORDERED:
1364 walk_body (convert_local_reference, info, &OMP_BODY (t));
1365 break;
1367 default:
1368 if (!IS_TYPE_OR_DECL_P (t))
1370 *walk_subtrees = 1;
1371 wi->val_only = true;
1372 wi->is_lhs = false;
1374 break;
1377 return NULL_TREE;
1380 static bool
1381 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1383 struct nesting_info *info = wi->info;
1384 bool need_frame = false;
1385 tree clause, decl;
1386 int dummy;
1387 bitmap new_suppress;
1389 new_suppress = BITMAP_GGC_ALLOC ();
1390 bitmap_copy (new_suppress, info->suppress_expansion);
1392 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1394 switch (OMP_CLAUSE_CODE (clause))
1396 case OMP_CLAUSE_PRIVATE:
1397 case OMP_CLAUSE_FIRSTPRIVATE:
1398 case OMP_CLAUSE_LASTPRIVATE:
1399 case OMP_CLAUSE_REDUCTION:
1400 case OMP_CLAUSE_COPYPRIVATE:
1401 case OMP_CLAUSE_SHARED:
1402 decl = OMP_CLAUSE_DECL (clause);
1403 if (decl_function_context (decl) == info->context
1404 && !use_pointer_in_frame (decl))
1406 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1407 if (field)
1409 bitmap_set_bit (new_suppress, DECL_UID (decl));
1410 OMP_CLAUSE_DECL (clause)
1411 = get_local_debug_decl (info, decl, field);
1412 need_frame = true;
1415 break;
1417 case OMP_CLAUSE_SCHEDULE:
1418 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1419 break;
1420 /* FALLTHRU */
1421 case OMP_CLAUSE_IF:
1422 case OMP_CLAUSE_NUM_THREADS:
1423 wi->val_only = true;
1424 wi->is_lhs = false;
1425 convert_local_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy, wi);
1426 break;
1428 case OMP_CLAUSE_NOWAIT:
1429 case OMP_CLAUSE_ORDERED:
1430 case OMP_CLAUSE_DEFAULT:
1431 case OMP_CLAUSE_COPYIN:
1432 break;
1434 default:
1435 gcc_unreachable ();
1439 info->suppress_expansion = new_suppress;
1441 return need_frame;
1444 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1445 reference labels from outer functions. The rewrite will be a
1446 call to __builtin_nonlocal_goto. */
1448 static tree
1449 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1451 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1452 struct nesting_info *info = wi->info, *i;
1453 tree t = *tp, label, new_label, target_context, x, field;
1454 void **slot;
1456 *walk_subtrees = 0;
1457 if (TREE_CODE (t) != GOTO_EXPR)
1458 return NULL_TREE;
1459 label = GOTO_DESTINATION (t);
1460 if (TREE_CODE (label) != LABEL_DECL)
1461 return NULL_TREE;
1462 target_context = decl_function_context (label);
1463 if (target_context == info->context)
1464 return NULL_TREE;
1466 for (i = info->outer; target_context != i->context; i = i->outer)
1467 continue;
1469 /* The original user label may also be use for a normal goto, therefore
1470 we must create a new label that will actually receive the abnormal
1471 control transfer. This new label will be marked LABEL_NONLOCAL; this
1472 mark will trigger proper behavior in the cfg, as well as cause the
1473 (hairy target-specific) non-local goto receiver code to be generated
1474 when we expand rtl. Enter this association into var_map so that we
1475 can insert the new label into the IL during a second pass. */
1476 slot = pointer_map_insert (i->var_map, label);
1477 if (*slot == NULL)
1479 new_label = create_artificial_label ();
1480 DECL_NONLOCAL (new_label) = 1;
1481 *slot = new_label;
1483 else
1484 new_label = *slot;
1486 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1487 field = get_nl_goto_field (i);
1488 x = get_frame_field (info, target_context, field, &wi->tsi);
1489 x = build_addr (x, target_context);
1490 x = tsi_gimplify_val (info, x, &wi->tsi);
1491 x = build_call_expr (implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO], 2,
1492 build_addr (new_label, target_context), x);
1494 SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1495 *tsi_stmt_ptr (wi->tsi) = x;
1497 return NULL_TREE;
1500 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1501 are referenced via nonlocal goto from a nested function. The rewrite
1502 will involve installing a newly generated DECL_NONLOCAL label, and
1503 (potentially) a branch around the rtl gunk that is assumed to be
1504 attached to such a label. */
1506 static tree
1507 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1509 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1510 struct nesting_info *info = wi->info;
1511 tree t = *tp, label, new_label, x;
1512 tree_stmt_iterator tmp_tsi;
1513 void **slot;
1515 *walk_subtrees = 0;
1516 if (TREE_CODE (t) != LABEL_EXPR)
1517 return NULL_TREE;
1518 label = LABEL_EXPR_LABEL (t);
1520 slot = pointer_map_contains (info->var_map, label);
1521 if (!slot)
1522 return NULL_TREE;
1524 /* If there's any possibility that the previous statement falls through,
1525 then we must branch around the new non-local label. */
1526 tmp_tsi = wi->tsi;
1527 tsi_prev (&tmp_tsi);
1528 if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1530 x = build1 (GOTO_EXPR, void_type_node, label);
1531 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1534 new_label = (tree) *slot;
1535 x = build1 (LABEL_EXPR, void_type_node, new_label);
1536 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1538 return NULL_TREE;
1541 /* Called via walk_function+walk_tree, rewrite all references to addresses
1542 of nested functions that require the use of trampolines. The rewrite
1543 will involve a reference a trampoline generated for the occasion. */
1545 static tree
1546 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1548 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1549 struct nesting_info *info = wi->info, *i;
1550 tree t = *tp, decl, target_context, x;
1552 *walk_subtrees = 0;
1553 switch (TREE_CODE (t))
1555 case ADDR_EXPR:
1556 /* Build
1557 T.1 = &CHAIN->tramp;
1558 T.2 = __builtin_adjust_trampoline (T.1);
1559 T.3 = (func_type)T.2;
1562 decl = TREE_OPERAND (t, 0);
1563 if (TREE_CODE (decl) != FUNCTION_DECL)
1564 break;
1566 /* Only need to process nested functions. */
1567 target_context = decl_function_context (decl);
1568 if (!target_context)
1569 break;
1571 /* If the nested function doesn't use a static chain, then
1572 it doesn't need a trampoline. */
1573 if (DECL_NO_STATIC_CHAIN (decl))
1574 break;
1576 /* Lookup the immediate parent of the callee, as that's where
1577 we need to insert the trampoline. */
1578 for (i = info; i->context != target_context; i = i->outer)
1579 continue;
1580 x = lookup_tramp_for_decl (i, decl, INSERT);
1582 /* Compute the address of the field holding the trampoline. */
1583 x = get_frame_field (info, target_context, x, &wi->tsi);
1584 x = build_addr (x, target_context);
1585 x = tsi_gimplify_val (info, x, &wi->tsi);
1587 /* Do machine-specific ugliness. Normally this will involve
1588 computing extra alignment, but it can really be anything. */
1589 x = build_call_expr (implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE],
1590 1, x);
1591 x = init_tmp_var (info, x, &wi->tsi);
1593 /* Cast back to the proper function type. */
1594 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1595 x = init_tmp_var (info, x, &wi->tsi);
1597 *tp = x;
1598 break;
1600 case CALL_EXPR:
1601 /* Only walk call arguments, lest we generate trampolines for
1602 direct calls. */
1604 int nargs = call_expr_nargs (t);
1605 int i;
1606 for (i = 0; i < nargs; i++)
1607 walk_tree (&CALL_EXPR_ARG (t, i), convert_tramp_reference, wi, NULL);
1609 break;
1611 default:
1612 if (!IS_TYPE_OR_DECL_P (t))
1613 *walk_subtrees = 1;
1614 break;
1617 return NULL_TREE;
1620 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1621 reference nested functions to make sure that the static chain is
1622 set up properly for the call. */
1624 static tree
1625 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1627 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1628 struct nesting_info *info = wi->info;
1629 tree t = *tp, decl, target_context;
1630 char save_static_chain_added;
1631 int i;
1633 *walk_subtrees = 0;
1634 switch (TREE_CODE (t))
1636 case CALL_EXPR:
1637 decl = get_callee_fndecl (t);
1638 if (!decl)
1639 break;
1640 target_context = decl_function_context (decl);
1641 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1643 CALL_EXPR_STATIC_CHAIN (t)
1644 = get_static_chain (info, target_context, &wi->tsi);
1645 info->static_chain_added
1646 |= (1 << (info->context != target_context));
1648 break;
1650 case RETURN_EXPR:
1651 case GIMPLE_MODIFY_STMT:
1652 case WITH_SIZE_EXPR:
1653 /* Only return modify and with_size_expr may contain calls. */
1654 *walk_subtrees = 1;
1655 break;
1657 case OMP_PARALLEL:
1658 save_static_chain_added = info->static_chain_added;
1659 info->static_chain_added = 0;
1660 walk_body (convert_call_expr, info, &OMP_PARALLEL_BODY (t));
1661 for (i = 0; i < 2; i++)
1663 tree c, decl;
1664 if ((info->static_chain_added & (1 << i)) == 0)
1665 continue;
1666 decl = i ? get_chain_decl (info) : info->frame_decl;
1667 /* Don't add CHAIN.* or FRAME.* twice. */
1668 for (c = OMP_PARALLEL_CLAUSES (t); c; c = OMP_CLAUSE_CHAIN (c))
1669 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1670 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1671 && OMP_CLAUSE_DECL (c) == decl)
1672 break;
1673 if (c == NULL)
1675 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1676 OMP_CLAUSE_DECL (c) = decl;
1677 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1678 OMP_PARALLEL_CLAUSES (t) = c;
1681 info->static_chain_added |= save_static_chain_added;
1682 break;
1684 case OMP_FOR:
1685 case OMP_SECTIONS:
1686 case OMP_SECTION:
1687 case OMP_SINGLE:
1688 case OMP_MASTER:
1689 case OMP_ORDERED:
1690 case OMP_CRITICAL:
1691 walk_body (convert_call_expr, info, &OMP_BODY (t));
1692 break;
1694 default:
1695 break;
1698 return NULL_TREE;
1701 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1702 trampolines and call expressions. On the way back up, determine if
1703 a nested function actually uses its static chain; if not, remember that. */
1705 static void
1706 convert_all_function_calls (struct nesting_info *root)
1710 if (root->inner)
1711 convert_all_function_calls (root->inner);
1713 walk_function (convert_tramp_reference, root);
1714 walk_function (convert_call_expr, root);
1716 /* If the function does not use a static chain, then remember that. */
1717 if (root->outer && !root->chain_decl && !root->chain_field)
1718 DECL_NO_STATIC_CHAIN (root->context) = 1;
1719 else
1720 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1722 root = root->next;
1724 while (root);
1727 /* Do "everything else" to clean up or complete state collected by the
1728 various walking passes -- lay out the types and decls, generate code
1729 to initialize the frame decl, store critical expressions in the
1730 struct function for rtl to find. */
1732 static void
1733 finalize_nesting_tree_1 (struct nesting_info *root)
1735 tree stmt_list = NULL;
1736 tree context = root->context;
1737 struct function *sf;
1739 /* If we created a non-local frame type or decl, we need to lay them
1740 out at this time. */
1741 if (root->frame_type)
1743 /* In some cases the frame type will trigger the -Wpadded warning.
1744 This is not helpful; suppress it. */
1745 int save_warn_padded = warn_padded;
1746 warn_padded = 0;
1747 layout_type (root->frame_type);
1748 warn_padded = save_warn_padded;
1749 layout_decl (root->frame_decl, 0);
1752 /* If any parameters were referenced non-locally, then we need to
1753 insert a copy. Likewise, if any variables were referenced by
1754 pointer, we need to initialize the address. */
1755 if (root->any_parm_remapped)
1757 tree p;
1758 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1760 tree field, x, y;
1762 field = lookup_field_for_decl (root, p, NO_INSERT);
1763 if (!field)
1764 continue;
1766 if (use_pointer_in_frame (p))
1767 x = build_addr (p, context);
1768 else
1769 x = p;
1771 y = build3 (COMPONENT_REF, TREE_TYPE (field),
1772 root->frame_decl, field, NULL_TREE);
1773 x = build_gimple_modify_stmt (y, x);
1774 append_to_statement_list (x, &stmt_list);
1778 /* If a chain_field was created, then it needs to be initialized
1779 from chain_decl. */
1780 if (root->chain_field)
1782 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
1783 root->frame_decl, root->chain_field, NULL_TREE);
1784 x = build_gimple_modify_stmt (x, get_chain_decl (root));
1785 append_to_statement_list (x, &stmt_list);
1788 /* If trampolines were created, then we need to initialize them. */
1789 if (root->any_tramp_created)
1791 struct nesting_info *i;
1792 for (i = root->inner; i ; i = i->next)
1794 tree arg1, arg2, arg3, x, field;
1796 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1797 if (!field)
1798 continue;
1800 if (DECL_NO_STATIC_CHAIN (i->context))
1801 arg3 = null_pointer_node;
1802 else
1803 arg3 = build_addr (root->frame_decl, context);
1805 arg2 = build_addr (i->context, context);
1807 x = build3 (COMPONENT_REF, TREE_TYPE (field),
1808 root->frame_decl, field, NULL_TREE);
1809 arg1 = build_addr (x, context);
1811 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1812 x = build_call_expr (x, 3, arg1, arg2, arg3);
1813 append_to_statement_list (x, &stmt_list);
1817 /* If we created initialization statements, insert them. */
1818 if (stmt_list)
1820 annotate_all_with_locus (&stmt_list,
1821 DECL_SOURCE_LOCATION (context));
1822 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1823 &stmt_list);
1824 BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1827 /* If a chain_decl was created, then it needs to be registered with
1828 struct function so that it gets initialized from the static chain
1829 register at the beginning of the function. */
1830 sf = DECL_STRUCT_FUNCTION (root->context);
1831 sf->static_chain_decl = root->chain_decl;
1833 /* Similarly for the non-local goto save area. */
1834 if (root->nl_goto_field)
1836 sf->nonlocal_goto_save_area
1837 = get_frame_field (root, context, root->nl_goto_field, NULL);
1838 sf->has_nonlocal_label = 1;
1841 /* Make sure all new local variables get inserted into the
1842 proper BIND_EXPR. */
1843 if (root->new_local_var_chain)
1844 declare_vars (root->new_local_var_chain, DECL_SAVED_TREE (root->context),
1845 false);
1846 if (root->debug_var_chain)
1847 declare_vars (root->debug_var_chain, DECL_SAVED_TREE (root->context),
1848 true);
1850 /* Dump the translated tree function. */
1851 dump_function (TDI_nested, root->context);
1854 static void
1855 finalize_nesting_tree (struct nesting_info *root)
1859 if (root->inner)
1860 finalize_nesting_tree (root->inner);
1861 finalize_nesting_tree_1 (root);
1862 root = root->next;
1864 while (root);
1867 /* Unnest the nodes and pass them to cgraph. */
1869 static void
1870 unnest_nesting_tree_1 (struct nesting_info *root)
1872 struct cgraph_node *node = cgraph_node (root->context);
1874 /* For nested functions update the cgraph to reflect unnesting.
1875 We also delay finalizing of these functions up to this point. */
1876 if (node->origin)
1878 cgraph_unnest_node (cgraph_node (root->context));
1879 cgraph_finalize_function (root->context, true);
1883 static void
1884 unnest_nesting_tree (struct nesting_info *root)
1888 if (root->inner)
1889 unnest_nesting_tree (root->inner);
1890 unnest_nesting_tree_1 (root);
1891 root = root->next;
1893 while (root);
1896 /* Free the data structures allocated during this pass. */
1898 static void
1899 free_nesting_tree (struct nesting_info *root)
1901 struct nesting_info *next;
1904 if (root->inner)
1905 free_nesting_tree (root->inner);
1906 pointer_map_destroy (root->var_map);
1907 pointer_map_destroy (root->field_map);
1908 next = root->next;
1909 free (root);
1910 root = next;
1912 while (root);
1915 /* Main entry point for this pass. Process FNDECL and all of its nested
1916 subroutines and turn them into something less tightly bound. */
1918 void
1919 lower_nested_functions (tree fndecl)
1921 struct cgraph_node *cgn;
1922 struct nesting_info *root;
1924 /* If there are no nested functions, there's nothing to do. */
1925 cgn = cgraph_node (fndecl);
1926 if (!cgn->nested)
1927 return;
1929 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
1930 root = create_nesting_tree (cgn);
1931 walk_all_functions (convert_nonlocal_reference, root);
1932 walk_all_functions (convert_local_reference, root);
1933 walk_all_functions (convert_nl_goto_reference, root);
1934 walk_all_functions (convert_nl_goto_receiver, root);
1935 convert_all_function_calls (root);
1936 finalize_nesting_tree (root);
1937 unnest_nesting_tree (root);
1938 free_nesting_tree (root);
1939 bitmap_obstack_release (&nesting_info_bitmap_obstack);
1942 #include "gt-tree-nested.h"