Mark ChangeLog
[official-gcc.git] / gcc / tree-nested.c
blob06fbbc99e6b3e02eccc316654c1f64150dd1f2b2
1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005, 2006 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "function.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "tree-gimple.h"
32 #include "tree-iterator.h"
33 #include "tree-flow.h"
34 #include "cgraph.h"
35 #include "expr.h"
36 #include "langhooks.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 var_map_elt GTY(())
82 tree old;
83 tree new;
86 struct nesting_info GTY ((chain_next ("%h.next")))
88 struct nesting_info *outer;
89 struct nesting_info *inner;
90 struct nesting_info *next;
92 htab_t GTY ((param_is (struct var_map_elt))) field_map;
93 htab_t GTY ((param_is (struct var_map_elt))) var_map;
94 bitmap suppress_expansion;
96 tree context;
97 tree new_local_var_chain;
98 tree debug_var_chain;
99 tree frame_type;
100 tree frame_decl;
101 tree chain_field;
102 tree chain_decl;
103 tree nl_goto_field;
105 bool any_parm_remapped;
106 bool any_tramp_created;
107 char static_chain_added;
111 /* Hashing and equality functions for nesting_info->var_map. */
113 static hashval_t
114 var_map_hash (const void *x)
116 const struct var_map_elt *a = (const struct var_map_elt *) x;
117 return htab_hash_pointer (a->old);
120 static int
121 var_map_eq (const void *x, const void *y)
123 const struct var_map_elt *a = (const struct var_map_elt *) x;
124 const struct var_map_elt *b = (const struct var_map_elt *) y;
125 return a->old == b->old;
128 /* We're working in so many different function contexts simultaneously,
129 that create_tmp_var is dangerous. Prevent mishap. */
130 #define create_tmp_var cant_use_create_tmp_var_here_dummy
132 /* Like create_tmp_var, except record the variable for registration at
133 the given nesting level. */
135 static tree
136 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
138 tree tmp_var;
140 /* If the type is of variable size or a type which must be created by the
141 frontend, something is wrong. Note that we explicitly allow
142 incomplete types here, since we create them ourselves here. */
143 gcc_assert (!TREE_ADDRESSABLE (type));
144 gcc_assert (!TYPE_SIZE_UNIT (type)
145 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
147 tmp_var = create_tmp_var_raw (type, prefix);
148 DECL_CONTEXT (tmp_var) = info->context;
149 TREE_CHAIN (tmp_var) = info->new_local_var_chain;
150 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
151 if (TREE_CODE (type) == COMPLEX_TYPE)
152 DECL_COMPLEX_GIMPLE_REG_P (tmp_var) = 1;
154 info->new_local_var_chain = tmp_var;
156 return tmp_var;
159 /* Take the address of EXP to be used within function CONTEXT.
160 Mark it for addressability as necessary. */
162 tree
163 build_addr (tree exp, tree context)
165 tree base = exp;
166 tree save_context;
167 tree retval;
169 while (handled_component_p (base))
170 base = TREE_OPERAND (base, 0);
172 if (DECL_P (base))
173 TREE_ADDRESSABLE (base) = 1;
175 /* Building the ADDR_EXPR will compute a set of properties for
176 that ADDR_EXPR. Those properties are unfortunately context
177 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
179 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
180 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
181 way the properties are for the ADDR_EXPR are computed properly. */
182 save_context = current_function_decl;
183 current_function_decl = context;
184 retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
185 current_function_decl = save_context;;
186 return retval;
189 /* Insert FIELD into TYPE, sorted by alignment requirements. */
191 void
192 insert_field_into_struct (tree type, tree field)
194 tree *p;
196 DECL_CONTEXT (field) = type;
198 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
199 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
200 break;
202 TREE_CHAIN (field) = *p;
203 *p = field;
206 /* Build or return the RECORD_TYPE that describes the frame state that is
207 shared between INFO->CONTEXT and its nested functions. This record will
208 not be complete until finalize_nesting_tree; up until that point we'll
209 be adding fields as necessary.
211 We also build the DECL that represents this frame in the function. */
213 static tree
214 get_frame_type (struct nesting_info *info)
216 tree type = info->frame_type;
217 if (!type)
219 char *name;
221 type = make_node (RECORD_TYPE);
223 name = concat ("FRAME.",
224 IDENTIFIER_POINTER (DECL_NAME (info->context)),
225 NULL);
226 TYPE_NAME (type) = get_identifier (name);
227 free (name);
229 info->frame_type = type;
230 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
232 /* ??? Always make it addressable for now, since it is meant to
233 be pointed to by the static chain pointer. This pessimizes
234 when it turns out that no static chains are needed because
235 the nested functions referencing non-local variables are not
236 reachable, but the true pessimization is to create the non-
237 local frame structure in the first place. */
238 TREE_ADDRESSABLE (info->frame_decl) = 1;
240 return type;
243 /* Return true if DECL should be referenced by pointer in the non-local
244 frame structure. */
246 static bool
247 use_pointer_in_frame (tree decl)
249 if (TREE_CODE (decl) == PARM_DECL)
251 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
252 sized decls, and inefficient to copy large aggregates. Don't bother
253 moving anything but scalar variables. */
254 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
256 else
258 /* Variable sized types make things "interesting" in the frame. */
259 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
263 /* Given DECL, a non-locally accessed variable, find or create a field
264 in the non-local frame structure for the given nesting context. */
266 static tree
267 lookup_field_for_decl (struct nesting_info *info, tree decl,
268 enum insert_option insert)
270 struct var_map_elt *elt, dummy;
271 void **slot;
272 tree field;
274 dummy.old = decl;
275 slot = htab_find_slot (info->field_map, &dummy, insert);
276 if (!slot)
278 gcc_assert (insert != INSERT);
279 return NULL;
281 elt = (struct var_map_elt *) *slot;
283 if (!elt && insert == INSERT)
285 field = make_node (FIELD_DECL);
286 DECL_NAME (field) = DECL_NAME (decl);
288 if (use_pointer_in_frame (decl))
290 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
291 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
292 DECL_NONADDRESSABLE_P (field) = 1;
294 else
296 TREE_TYPE (field) = TREE_TYPE (decl);
297 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
298 DECL_ALIGN (field) = DECL_ALIGN (decl);
299 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
300 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
301 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
302 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
305 insert_field_into_struct (get_frame_type (info), field);
307 elt = GGC_NEW (struct var_map_elt);
308 elt->old = decl;
309 elt->new = field;
310 *slot = elt;
312 if (TREE_CODE (decl) == PARM_DECL)
313 info->any_parm_remapped = true;
315 else
316 field = elt ? elt->new : NULL;
318 return field;
321 /* Build or return the variable that holds the static chain within
322 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
324 static tree
325 get_chain_decl (struct nesting_info *info)
327 tree decl = info->chain_decl;
328 if (!decl)
330 tree type;
332 type = get_frame_type (info->outer);
333 type = build_pointer_type (type);
335 /* Note that this variable is *not* entered into any BIND_EXPR;
336 the construction of this variable is handled specially in
337 expand_function_start and initialize_inlined_parameters.
338 Note also that it's represented as a parameter. This is more
339 close to the truth, since the initial value does come from
340 the caller. */
341 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
342 DECL_ARTIFICIAL (decl) = 1;
343 DECL_IGNORED_P (decl) = 1;
344 TREE_USED (decl) = 1;
345 DECL_CONTEXT (decl) = info->context;
346 DECL_ARG_TYPE (decl) = type;
348 /* Tell tree-inline.c that we never write to this variable, so
349 it can copy-prop the replacement value immediately. */
350 TREE_READONLY (decl) = 1;
352 info->chain_decl = decl;
354 return decl;
357 /* Build or return the field within the non-local frame state that holds
358 the static chain for INFO->CONTEXT. This is the way to walk back up
359 multiple nesting levels. */
361 static tree
362 get_chain_field (struct nesting_info *info)
364 tree field = info->chain_field;
365 if (!field)
367 tree type = build_pointer_type (get_frame_type (info->outer));
369 field = make_node (FIELD_DECL);
370 DECL_NAME (field) = get_identifier ("__chain");
371 TREE_TYPE (field) = type;
372 DECL_ALIGN (field) = TYPE_ALIGN (type);
373 DECL_NONADDRESSABLE_P (field) = 1;
375 insert_field_into_struct (get_frame_type (info), field);
377 info->chain_field = field;
379 return field;
382 /* Copy EXP into a temporary. Allocate the temporary in the context of
383 INFO and insert the initialization statement before TSI. */
385 static tree
386 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
388 tree t, stmt;
390 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
391 stmt = build2 (MODIFY_EXPR, TREE_TYPE (t), t, exp);
392 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
393 tsi_link_before (tsi, stmt, TSI_SAME_STMT);
395 return t;
398 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
400 static tree
401 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
403 if (is_gimple_val (exp))
404 return exp;
405 else
406 return init_tmp_var (info, exp, tsi);
409 /* Similarly, but copy from the temporary and insert the statement
410 after the iterator. */
412 static tree
413 save_tmp_var (struct nesting_info *info, tree exp,
414 tree_stmt_iterator *tsi)
416 tree t, stmt;
418 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
419 stmt = build2 (MODIFY_EXPR, TREE_TYPE (t), exp, t);
420 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
421 tsi_link_after (tsi, stmt, TSI_SAME_STMT);
423 return t;
426 /* Build or return the type used to represent a nested function trampoline. */
428 static GTY(()) tree trampoline_type;
430 static tree
431 get_trampoline_type (void)
433 tree record, t;
434 unsigned align, size;
436 if (trampoline_type)
437 return trampoline_type;
439 align = TRAMPOLINE_ALIGNMENT;
440 size = TRAMPOLINE_SIZE;
442 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
443 then allocate extra space so that we can do dynamic alignment. */
444 if (align > STACK_BOUNDARY)
446 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
447 align = STACK_BOUNDARY;
450 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
451 t = build_array_type (char_type_node, t);
452 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
453 DECL_ALIGN (t) = align;
454 DECL_USER_ALIGN (t) = 1;
456 record = make_node (RECORD_TYPE);
457 TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
458 TYPE_FIELDS (record) = t;
459 layout_type (record);
461 return record;
464 /* Given DECL, a nested function, find or create a field in the non-local
465 frame structure for a trampoline for this function. */
467 static tree
468 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
469 enum insert_option insert)
471 struct var_map_elt *elt, dummy;
472 void **slot;
473 tree field;
475 dummy.old = decl;
476 slot = htab_find_slot (info->var_map, &dummy, insert);
477 if (!slot)
479 gcc_assert (insert != INSERT);
480 return NULL;
482 elt = (struct var_map_elt *) *slot;
484 if (!elt && insert == INSERT)
486 field = make_node (FIELD_DECL);
487 DECL_NAME (field) = DECL_NAME (decl);
488 TREE_TYPE (field) = get_trampoline_type ();
489 TREE_ADDRESSABLE (field) = 1;
491 insert_field_into_struct (get_frame_type (info), field);
493 elt = GGC_NEW (struct var_map_elt);
494 elt->old = decl;
495 elt->new = field;
496 *slot = elt;
498 info->any_tramp_created = true;
500 else
501 field = elt ? elt->new : NULL;
503 return field;
506 /* Build or return the field within the non-local frame state that holds
507 the non-local goto "jmp_buf". The buffer itself is maintained by the
508 rtl middle-end as dynamic stack space is allocated. */
510 static tree
511 get_nl_goto_field (struct nesting_info *info)
513 tree field = info->nl_goto_field;
514 if (!field)
516 unsigned size;
517 tree type;
519 /* For __builtin_nonlocal_goto, we need N words. The first is the
520 frame pointer, the rest is for the target's stack pointer save
521 area. The number of words is controlled by STACK_SAVEAREA_MODE;
522 not the best interface, but it'll do for now. */
523 if (Pmode == ptr_mode)
524 type = ptr_type_node;
525 else
526 type = lang_hooks.types.type_for_mode (Pmode, 1);
528 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
529 size = size / GET_MODE_SIZE (Pmode);
530 size = size + 1;
532 type = build_array_type
533 (type, build_index_type (build_int_cst (NULL_TREE, size)));
535 field = make_node (FIELD_DECL);
536 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
537 TREE_TYPE (field) = type;
538 DECL_ALIGN (field) = TYPE_ALIGN (type);
539 TREE_ADDRESSABLE (field) = 1;
541 insert_field_into_struct (get_frame_type (info), field);
543 info->nl_goto_field = field;
546 return field;
549 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
551 static void
552 walk_asm_expr (struct walk_stmt_info *wi, tree stmt)
554 int noutputs = list_length (ASM_OUTPUTS (stmt));
555 const char **oconstraints
556 = (const char **) alloca ((noutputs) * sizeof (const char *));
557 int i;
558 tree link;
559 const char *constraint;
560 bool allows_mem, allows_reg, is_inout;
562 wi->is_lhs = true;
563 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
565 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
566 oconstraints[i] = constraint;
567 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
568 &allows_reg, &is_inout);
570 wi->val_only = (allows_reg || !allows_mem);
571 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
574 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
576 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
577 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
578 oconstraints, &allows_mem, &allows_reg);
580 wi->val_only = (allows_reg || !allows_mem);
581 /* Although input "m" is not really a LHS, we need a lvalue. */
582 wi->is_lhs = !wi->val_only;
583 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
586 wi->is_lhs = false;
587 wi->val_only = true;
590 /* Iterate over all sub-statements of *TP calling walk_tree with
591 WI->CALLBACK for every sub-expression in each statement found. */
593 void
594 walk_stmts (struct walk_stmt_info *wi, tree *tp)
596 tree t = *tp;
597 int walk_subtrees;
599 if (!t)
600 return;
602 if (wi->want_locations && EXPR_HAS_LOCATION (t))
603 input_location = EXPR_LOCATION (t);
605 switch (TREE_CODE (t))
607 case STATEMENT_LIST:
609 tree_stmt_iterator i;
610 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
612 wi->tsi = i;
613 walk_stmts (wi, tsi_stmt_ptr (i));
616 break;
618 case COND_EXPR:
619 walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
620 walk_stmts (wi, &COND_EXPR_THEN (t));
621 walk_stmts (wi, &COND_EXPR_ELSE (t));
622 break;
623 case CATCH_EXPR:
624 walk_stmts (wi, &CATCH_BODY (t));
625 break;
626 case EH_FILTER_EXPR:
627 walk_stmts (wi, &EH_FILTER_FAILURE (t));
628 break;
629 case TRY_CATCH_EXPR:
630 case TRY_FINALLY_EXPR:
631 walk_stmts (wi, &TREE_OPERAND (t, 0));
632 walk_stmts (wi, &TREE_OPERAND (t, 1));
633 break;
635 case BIND_EXPR:
636 if (wi->want_bind_expr)
638 walk_subtrees = 1;
639 wi->callback (tp, &walk_subtrees, wi);
640 if (!walk_subtrees)
641 break;
643 walk_stmts (wi, &BIND_EXPR_BODY (t));
644 break;
646 case RETURN_EXPR:
647 if (wi->want_return_expr)
649 walk_subtrees = 1;
650 wi->callback (tp, &walk_subtrees, wi);
651 if (!walk_subtrees)
652 break;
654 walk_stmts (wi, &TREE_OPERAND (t, 0));
655 break;
657 case MODIFY_EXPR:
658 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
659 wi->val_only = !is_gimple_formal_tmp_var (TREE_OPERAND (t, 0));
660 walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
662 /* If the rhs is appropriate for a memory, we may use a
663 COMPONENT_REF on the lhs. */
664 wi->val_only = !is_gimple_mem_rhs (TREE_OPERAND (t, 1));
665 wi->is_lhs = true;
666 walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
668 wi->val_only = true;
669 wi->is_lhs = false;
670 break;
672 case ASM_EXPR:
673 walk_asm_expr (wi, *tp);
674 break;
676 default:
677 wi->val_only = true;
678 walk_tree (tp, wi->callback, wi, NULL);
679 break;
683 /* Invoke CALLBACK on all statements of *STMT_P. */
685 static void
686 walk_body (walk_tree_fn callback, struct nesting_info *info, tree *stmt_p)
688 struct walk_stmt_info wi;
690 memset (&wi, 0, sizeof (wi));
691 wi.callback = callback;
692 wi.info = info;
693 wi.val_only = true;
695 walk_stmts (&wi, stmt_p);
698 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
700 static inline void
701 walk_function (walk_tree_fn callback, struct nesting_info *info)
703 walk_body (callback, info, &DECL_SAVED_TREE (info->context));
706 /* Similarly for ROOT and all functions nested underneath, depth first. */
708 static void
709 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
713 if (root->inner)
714 walk_all_functions (callback, root->inner);
715 walk_function (callback, root);
716 root = root->next;
718 while (root);
721 /* We have to check for a fairly pathological case. The operands of function
722 nested function are to be interpreted in the context of the enclosing
723 function. So if any are variably-sized, they will get remapped when the
724 enclosing function is inlined. But that remapping would also have to be
725 done in the types of the PARM_DECLs of the nested function, meaning the
726 argument types of that function will disagree with the arguments in the
727 calls to that function. So we'd either have to make a copy of the nested
728 function corresponding to each time the enclosing function was inlined or
729 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
730 function. The former is not practical. The latter would still require
731 detecting this case to know when to add the conversions. So, for now at
732 least, we don't inline such an enclosing function.
734 We have to do that check recursively, so here return indicating whether
735 FNDECL has such a nested function. ORIG_FN is the function we were
736 trying to inline to use for checking whether any argument is variably
737 modified by anything in it.
739 It would be better to do this in tree-inline.c so that we could give
740 the appropriate warning for why a function can't be inlined, but that's
741 too late since the nesting structure has already been flattened and
742 adding a flag just to record this fact seems a waste of a flag. */
744 static bool
745 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
747 struct cgraph_node *cgn = cgraph_node (fndecl);
748 tree arg;
750 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
752 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
753 if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
754 return true;
756 if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
757 return true;
760 return false;
763 /* Construct our local datastructure describing the function nesting
764 tree rooted by CGN. */
766 static struct nesting_info *
767 create_nesting_tree (struct cgraph_node *cgn)
769 struct nesting_info *info = GGC_CNEW (struct nesting_info);
770 info->field_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
771 info->var_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
772 info->suppress_expansion = BITMAP_GGC_ALLOC ();
773 info->context = cgn->decl;
775 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
777 struct nesting_info *sub = create_nesting_tree (cgn);
778 sub->outer = info;
779 sub->next = info->inner;
780 info->inner = sub;
783 /* See discussion at check_for_nested_with_variably_modified for a
784 discussion of why this has to be here. */
785 if (check_for_nested_with_variably_modified (info->context, info->context))
786 DECL_UNINLINABLE (info->context) = true;
788 return info;
791 /* Return an expression computing the static chain for TARGET_CONTEXT
792 from INFO->CONTEXT. Insert any necessary computations before TSI. */
794 static tree
795 get_static_chain (struct nesting_info *info, tree target_context,
796 tree_stmt_iterator *tsi)
798 struct nesting_info *i;
799 tree x;
801 if (info->context == target_context)
803 x = build_addr (info->frame_decl, target_context);
805 else
807 x = get_chain_decl (info);
809 for (i = info->outer; i->context != target_context; i = i->outer)
811 tree field = get_chain_field (i);
813 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
814 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
815 x = init_tmp_var (info, x, tsi);
819 return x;
822 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
823 frame as seen from INFO->CONTEXT. Insert any necessary computations
824 before TSI. */
826 static tree
827 get_frame_field (struct nesting_info *info, tree target_context,
828 tree field, tree_stmt_iterator *tsi)
830 struct nesting_info *i;
831 tree x;
833 if (info->context == target_context)
835 /* Make sure frame_decl gets created. */
836 (void) get_frame_type (info);
837 x = info->frame_decl;
839 else
841 x = get_chain_decl (info);
843 for (i = info->outer; i->context != target_context; i = i->outer)
845 tree field = get_chain_field (i);
847 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
848 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
849 x = init_tmp_var (info, x, tsi);
852 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
855 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
856 return x;
859 /* A subroutine of convert_nonlocal_reference. Create a local variable
860 in the nested function with DECL_VALUE_EXPR set to reference the true
861 variable in the parent function. This is used both for debug info
862 and in OpenMP lowering. */
864 static tree
865 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
867 struct var_map_elt *elt, dummy;
868 tree target_context;
869 struct nesting_info *i;
870 tree x, field, new_decl;
871 void **slot;
873 dummy.old = decl;
874 slot = htab_find_slot (info->var_map, &dummy, INSERT);
875 elt = *slot;
877 if (elt)
878 return elt->new;
880 target_context = decl_function_context (decl);
882 /* A copy of the code in get_frame_field, but without the temporaries. */
883 if (info->context == target_context)
885 /* Make sure frame_decl gets created. */
886 (void) get_frame_type (info);
887 x = info->frame_decl;
888 i = info;
890 else
892 x = get_chain_decl (info);
893 for (i = info->outer; i->context != target_context; i = i->outer)
895 field = get_chain_field (i);
896 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
897 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
899 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
902 field = lookup_field_for_decl (i, decl, INSERT);
903 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
904 if (use_pointer_in_frame (decl))
905 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
907 /* ??? We should be remapping types as well, surely. */
908 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
909 DECL_CONTEXT (new_decl) = info->context;
910 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
911 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
912 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
913 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
914 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
915 TREE_READONLY (new_decl) = TREE_READONLY (decl);
916 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
917 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
919 SET_DECL_VALUE_EXPR (new_decl, x);
920 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
922 elt = ggc_alloc (sizeof (*elt));
923 elt->old = decl;
924 elt->new = new_decl;
925 *slot = elt;
927 TREE_CHAIN (new_decl) = info->debug_var_chain;
928 info->debug_var_chain = new_decl;
930 return new_decl;
933 /* Called via walk_function+walk_tree, rewrite all references to VAR
934 and PARM_DECLs that belong to outer functions.
936 The rewrite will involve some number of structure accesses back up
937 the static chain. E.g. for a variable FOO up one nesting level it'll
938 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
939 indirections apply to decls for which use_pointer_in_frame is true. */
941 static bool convert_nonlocal_omp_clauses (tree *, struct walk_stmt_info *);
943 static tree
944 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
946 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
947 struct nesting_info *info = wi->info;
948 tree t = *tp;
949 tree save_local_var_chain;
950 bitmap save_suppress;
952 *walk_subtrees = 0;
953 switch (TREE_CODE (t))
955 case VAR_DECL:
956 /* Non-automatic variables are never processed. */
957 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
958 break;
959 /* FALLTHRU */
961 case PARM_DECL:
962 if (decl_function_context (t) != info->context)
964 tree x;
965 wi->changed = true;
967 x = get_nonlocal_debug_decl (info, t);
968 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
970 tree target_context = decl_function_context (t);
971 struct nesting_info *i;
972 for (i = info->outer; i->context != target_context; i = i->outer)
973 continue;
974 x = lookup_field_for_decl (i, t, INSERT);
975 x = get_frame_field (info, target_context, x, &wi->tsi);
976 if (use_pointer_in_frame (t))
978 x = init_tmp_var (info, x, &wi->tsi);
979 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
983 if (wi->val_only)
985 if (wi->is_lhs)
986 x = save_tmp_var (info, x, &wi->tsi);
987 else
988 x = init_tmp_var (info, x, &wi->tsi);
991 *tp = x;
993 break;
995 case GOTO_EXPR:
996 /* Don't walk non-local gotos for now. */
997 if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
999 *walk_subtrees = 1;
1000 wi->val_only = true;
1001 wi->is_lhs = false;
1003 break;
1005 case LABEL_DECL:
1006 /* We're taking the address of a label from a parent function, but
1007 this is not itself a non-local goto. Mark the label such that it
1008 will not be deleted, much as we would with a label address in
1009 static storage. */
1010 if (decl_function_context (t) != info->context)
1011 FORCED_LABEL (t) = 1;
1012 break;
1014 case ADDR_EXPR:
1016 bool save_val_only = wi->val_only;
1018 wi->val_only = false;
1019 wi->is_lhs = false;
1020 wi->changed = false;
1021 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
1022 wi->val_only = true;
1024 if (wi->changed)
1026 tree save_context;
1028 /* If we changed anything, then TREE_INVARIANT is be wrong,
1029 since we're no longer directly referencing a decl. */
1030 save_context = current_function_decl;
1031 current_function_decl = info->context;
1032 recompute_tree_invariant_for_addr_expr (t);
1033 current_function_decl = save_context;
1035 /* If the callback converted the address argument in a context
1036 where we only accept variables (and min_invariant, presumably),
1037 then compute the address into a temporary. */
1038 if (save_val_only)
1039 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1042 break;
1044 case REALPART_EXPR:
1045 case IMAGPART_EXPR:
1046 case COMPONENT_REF:
1047 case ARRAY_REF:
1048 case ARRAY_RANGE_REF:
1049 case BIT_FIELD_REF:
1050 /* Go down this entire nest and just look at the final prefix and
1051 anything that describes the references. Otherwise, we lose track
1052 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1053 wi->val_only = true;
1054 wi->is_lhs = false;
1055 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1057 if (TREE_CODE (t) == COMPONENT_REF)
1058 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1059 NULL);
1060 else if (TREE_CODE (t) == ARRAY_REF
1061 || TREE_CODE (t) == ARRAY_RANGE_REF)
1063 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1064 NULL);
1065 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1066 NULL);
1067 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
1068 NULL);
1070 else if (TREE_CODE (t) == BIT_FIELD_REF)
1072 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1073 NULL);
1074 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1075 NULL);
1078 wi->val_only = false;
1079 walk_tree (tp, convert_nonlocal_reference, wi, NULL);
1080 break;
1082 case OMP_PARALLEL:
1083 save_suppress = info->suppress_expansion;
1084 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1086 tree c, decl;
1087 decl = get_chain_decl (info);
1088 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1089 OMP_CLAUSE_DECL (c) = decl;
1090 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1091 OMP_PARALLEL_CLAUSES (t) = c;
1094 save_local_var_chain = info->new_local_var_chain;
1095 info->new_local_var_chain = NULL;
1097 walk_body (convert_nonlocal_reference, info, &OMP_PARALLEL_BODY (t));
1099 if (info->new_local_var_chain)
1100 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1101 info->new_local_var_chain = save_local_var_chain;
1102 info->suppress_expansion = save_suppress;
1103 break;
1105 case OMP_FOR:
1106 case OMP_SECTIONS:
1107 case OMP_SINGLE:
1108 save_suppress = info->suppress_expansion;
1109 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t), wi);
1110 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1111 info->suppress_expansion = save_suppress;
1112 break;
1114 case OMP_SECTION:
1115 case OMP_MASTER:
1116 case OMP_ORDERED:
1117 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1118 break;
1120 default:
1121 if (!IS_TYPE_OR_DECL_P (t))
1123 *walk_subtrees = 1;
1124 wi->val_only = true;
1125 wi->is_lhs = false;
1127 break;
1130 return NULL_TREE;
1133 static bool
1134 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1136 struct nesting_info *info = wi->info;
1137 bool need_chain = false;
1138 tree clause, decl;
1139 int dummy;
1140 bitmap new_suppress;
1142 new_suppress = BITMAP_GGC_ALLOC ();
1143 bitmap_copy (new_suppress, info->suppress_expansion);
1145 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1147 switch (OMP_CLAUSE_CODE (clause))
1149 case OMP_CLAUSE_PRIVATE:
1150 case OMP_CLAUSE_FIRSTPRIVATE:
1151 case OMP_CLAUSE_LASTPRIVATE:
1152 case OMP_CLAUSE_REDUCTION:
1153 case OMP_CLAUSE_COPYPRIVATE:
1154 case OMP_CLAUSE_SHARED:
1155 decl = OMP_CLAUSE_DECL (clause);
1156 if (decl_function_context (decl) != info->context)
1158 bitmap_set_bit (new_suppress, DECL_UID (decl));
1159 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1160 need_chain = true;
1162 break;
1164 case OMP_CLAUSE_SCHEDULE:
1165 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1166 break;
1167 /* FALLTHRU */
1168 case OMP_CLAUSE_IF:
1169 case OMP_CLAUSE_NUM_THREADS:
1170 wi->val_only = true;
1171 wi->is_lhs = false;
1172 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1173 wi);
1174 break;
1176 case OMP_CLAUSE_NOWAIT:
1177 case OMP_CLAUSE_ORDERED:
1178 case OMP_CLAUSE_DEFAULT:
1179 case OMP_CLAUSE_COPYIN:
1180 break;
1182 default:
1183 gcc_unreachable ();
1187 info->suppress_expansion = new_suppress;
1189 return need_chain;
1192 /* A subroutine of convert_local_reference. Create a local variable
1193 in the parent function with DECL_VALUE_EXPR set to reference the
1194 field in FRAME. This is used both for debug info and in OpenMP
1195 lowering. */
1197 static tree
1198 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1200 struct var_map_elt *elt, dummy;
1201 tree x, new_decl;
1202 void **slot;
1204 dummy.old = decl;
1205 slot = htab_find_slot (info->var_map, &dummy, INSERT);
1206 elt = *slot;
1208 if (elt)
1209 return elt->new;
1211 /* Make sure frame_decl gets created. */
1212 (void) get_frame_type (info);
1213 x = info->frame_decl;
1214 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1216 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1217 DECL_CONTEXT (new_decl) = info->context;
1218 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1219 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1220 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1221 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1222 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1223 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1224 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1225 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1227 SET_DECL_VALUE_EXPR (new_decl, x);
1228 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1230 elt = ggc_alloc (sizeof (*elt));
1231 elt->old = decl;
1232 elt->new = new_decl;
1233 *slot = elt;
1235 TREE_CHAIN (new_decl) = info->debug_var_chain;
1236 info->debug_var_chain = new_decl;
1238 /* Do not emit debug info twice. */
1239 DECL_IGNORED_P (decl) = 1;
1241 return new_decl;
1244 /* Called via walk_function+walk_tree, rewrite all references to VAR
1245 and PARM_DECLs that were referenced by inner nested functions.
1246 The rewrite will be a structure reference to the local frame variable. */
1248 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1250 static tree
1251 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
1253 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1254 struct nesting_info *info = wi->info;
1255 tree t = *tp, field, x;
1256 bool save_val_only;
1257 tree save_local_var_chain;
1258 bitmap save_suppress;
1260 *walk_subtrees = 0;
1261 switch (TREE_CODE (t))
1263 case VAR_DECL:
1264 /* Non-automatic variables are never processed. */
1265 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1266 break;
1267 /* FALLTHRU */
1269 case PARM_DECL:
1270 if (decl_function_context (t) == info->context)
1272 /* If we copied a pointer to the frame, then the original decl
1273 is used unchanged in the parent function. */
1274 if (use_pointer_in_frame (t))
1275 break;
1277 /* No need to transform anything if no child references the
1278 variable. */
1279 field = lookup_field_for_decl (info, t, NO_INSERT);
1280 if (!field)
1281 break;
1282 wi->changed = true;
1284 x = get_local_debug_decl (info, t, field);
1285 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1286 x = get_frame_field (info, info->context, field, &wi->tsi);
1288 if (wi->val_only)
1290 if (wi->is_lhs)
1291 x = save_tmp_var (info, x, &wi->tsi);
1292 else
1293 x = init_tmp_var (info, x, &wi->tsi);
1296 *tp = x;
1298 break;
1300 case ADDR_EXPR:
1301 save_val_only = wi->val_only;
1302 wi->val_only = false;
1303 wi->is_lhs = false;
1304 wi->changed = false;
1305 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
1306 wi->val_only = save_val_only;
1308 /* If we converted anything ... */
1309 if (wi->changed)
1311 tree save_context;
1313 /* Then the frame decl is now addressable. */
1314 TREE_ADDRESSABLE (info->frame_decl) = 1;
1316 save_context = current_function_decl;
1317 current_function_decl = info->context;
1318 recompute_tree_invariant_for_addr_expr (t);
1319 current_function_decl = save_context;
1321 /* If we are in a context where we only accept values, then
1322 compute the address into a temporary. */
1323 if (save_val_only)
1324 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1326 break;
1328 case REALPART_EXPR:
1329 case IMAGPART_EXPR:
1330 case COMPONENT_REF:
1331 case ARRAY_REF:
1332 case ARRAY_RANGE_REF:
1333 case BIT_FIELD_REF:
1334 /* Go down this entire nest and just look at the final prefix and
1335 anything that describes the references. Otherwise, we lose track
1336 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1337 save_val_only = wi->val_only;
1338 wi->val_only = true;
1339 wi->is_lhs = false;
1340 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1342 if (TREE_CODE (t) == COMPONENT_REF)
1343 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1344 NULL);
1345 else if (TREE_CODE (t) == ARRAY_REF
1346 || TREE_CODE (t) == ARRAY_RANGE_REF)
1348 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1349 NULL);
1350 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1351 NULL);
1352 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1353 NULL);
1355 else if (TREE_CODE (t) == BIT_FIELD_REF)
1357 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1358 NULL);
1359 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1360 NULL);
1363 wi->val_only = false;
1364 walk_tree (tp, convert_local_reference, wi, NULL);
1365 wi->val_only = save_val_only;
1366 break;
1368 case OMP_PARALLEL:
1369 save_suppress = info->suppress_expansion;
1370 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1372 tree c;
1373 (void) get_frame_type (info);
1374 c = build_omp_clause (OMP_CLAUSE_SHARED);
1375 OMP_CLAUSE_DECL (c) = info->frame_decl;
1376 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1377 OMP_PARALLEL_CLAUSES (t) = c;
1380 save_local_var_chain = info->new_local_var_chain;
1381 info->new_local_var_chain = NULL;
1383 walk_body (convert_local_reference, info, &OMP_PARALLEL_BODY (t));
1385 if (info->new_local_var_chain)
1386 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1387 info->new_local_var_chain = save_local_var_chain;
1388 info->suppress_expansion = save_suppress;
1389 break;
1391 case OMP_FOR:
1392 case OMP_SECTIONS:
1393 case OMP_SINGLE:
1394 save_suppress = info->suppress_expansion;
1395 convert_local_omp_clauses (&OMP_CLAUSES (t), wi);
1396 walk_body (convert_local_reference, info, &OMP_BODY (t));
1397 info->suppress_expansion = save_suppress;
1398 break;
1400 case OMP_SECTION:
1401 case OMP_MASTER:
1402 case OMP_ORDERED:
1403 walk_body (convert_local_reference, info, &OMP_BODY (t));
1404 break;
1406 default:
1407 if (!IS_TYPE_OR_DECL_P (t))
1409 *walk_subtrees = 1;
1410 wi->val_only = true;
1411 wi->is_lhs = false;
1413 break;
1416 return NULL_TREE;
1419 static bool
1420 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1422 struct nesting_info *info = wi->info;
1423 bool need_frame = false;
1424 tree clause, decl;
1425 int dummy;
1426 bitmap new_suppress;
1428 new_suppress = BITMAP_GGC_ALLOC ();
1429 bitmap_copy (new_suppress, info->suppress_expansion);
1431 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1433 switch (OMP_CLAUSE_CODE (clause))
1435 case OMP_CLAUSE_PRIVATE:
1436 case OMP_CLAUSE_FIRSTPRIVATE:
1437 case OMP_CLAUSE_LASTPRIVATE:
1438 case OMP_CLAUSE_REDUCTION:
1439 case OMP_CLAUSE_COPYPRIVATE:
1440 case OMP_CLAUSE_SHARED:
1441 decl = OMP_CLAUSE_DECL (clause);
1442 if (decl_function_context (decl) == info->context
1443 && !use_pointer_in_frame (decl))
1445 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1446 if (field)
1448 bitmap_set_bit (new_suppress, DECL_UID (decl));
1449 OMP_CLAUSE_DECL (clause)
1450 = get_local_debug_decl (info, decl, field);
1451 need_frame = true;
1454 break;
1456 case OMP_CLAUSE_SCHEDULE:
1457 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1458 break;
1459 /* FALLTHRU */
1460 case OMP_CLAUSE_IF:
1461 case OMP_CLAUSE_NUM_THREADS:
1462 wi->val_only = true;
1463 wi->is_lhs = false;
1464 convert_local_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy, wi);
1465 break;
1467 case OMP_CLAUSE_NOWAIT:
1468 case OMP_CLAUSE_ORDERED:
1469 case OMP_CLAUSE_DEFAULT:
1470 case OMP_CLAUSE_COPYIN:
1471 break;
1473 default:
1474 gcc_unreachable ();
1478 info->suppress_expansion = new_suppress;
1480 return need_frame;
1483 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1484 reference labels from outer functions. The rewrite will be a
1485 call to __builtin_nonlocal_goto. */
1487 static tree
1488 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1490 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1491 struct nesting_info *info = wi->info, *i;
1492 tree t = *tp, label, new_label, target_context, x, arg, field;
1493 struct var_map_elt *elt, dummy;
1494 void **slot;
1496 *walk_subtrees = 0;
1497 if (TREE_CODE (t) != GOTO_EXPR)
1498 return NULL_TREE;
1499 label = GOTO_DESTINATION (t);
1500 if (TREE_CODE (label) != LABEL_DECL)
1501 return NULL_TREE;
1502 target_context = decl_function_context (label);
1503 if (target_context == info->context)
1504 return NULL_TREE;
1506 for (i = info->outer; target_context != i->context; i = i->outer)
1507 continue;
1509 /* The original user label may also be use for a normal goto, therefore
1510 we must create a new label that will actually receive the abnormal
1511 control transfer. This new label will be marked LABEL_NONLOCAL; this
1512 mark will trigger proper behavior in the cfg, as well as cause the
1513 (hairy target-specific) non-local goto receiver code to be generated
1514 when we expand rtl. Enter this association into var_map so that we
1515 can insert the new label into the IL during a second pass. */
1516 dummy.old = label;
1517 slot = htab_find_slot (i->var_map, &dummy, INSERT);
1518 elt = (struct var_map_elt *) *slot;
1519 if (elt == NULL)
1521 new_label = create_artificial_label ();
1522 DECL_NONLOCAL (new_label) = 1;
1524 elt = GGC_NEW (struct var_map_elt);
1525 elt->old = label;
1526 elt->new = new_label;
1527 *slot = elt;
1529 else
1530 new_label = elt->new;
1532 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1533 field = get_nl_goto_field (i);
1534 x = get_frame_field (info, target_context, field, &wi->tsi);
1535 x = build_addr (x, target_context);
1536 x = tsi_gimplify_val (info, x, &wi->tsi);
1537 arg = tree_cons (NULL, x, NULL);
1538 x = build_addr (new_label, target_context);
1539 arg = tree_cons (NULL, x, arg);
1540 x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1541 x = build_function_call_expr (x, arg);
1543 SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1544 *tsi_stmt_ptr (wi->tsi) = x;
1546 return NULL_TREE;
1549 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1550 are referenced via nonlocal goto from a nested function. The rewrite
1551 will involve installing a newly generated DECL_NONLOCAL label, and
1552 (potentially) a branch around the rtl gunk that is assumed to be
1553 attached to such a label. */
1555 static tree
1556 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1558 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1559 struct nesting_info *info = wi->info;
1560 tree t = *tp, label, new_label, x;
1561 struct var_map_elt *elt, dummy;
1562 tree_stmt_iterator tmp_tsi;
1564 *walk_subtrees = 0;
1565 if (TREE_CODE (t) != LABEL_EXPR)
1566 return NULL_TREE;
1567 label = LABEL_EXPR_LABEL (t);
1569 dummy.old = label;
1570 elt = (struct var_map_elt *) htab_find (info->var_map, &dummy);
1571 if (!elt)
1572 return NULL_TREE;
1573 new_label = elt->new;
1575 /* If there's any possibility that the previous statement falls through,
1576 then we must branch around the new non-local label. */
1577 tmp_tsi = wi->tsi;
1578 tsi_prev (&tmp_tsi);
1579 if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1581 x = build1 (GOTO_EXPR, void_type_node, label);
1582 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1584 x = build1 (LABEL_EXPR, void_type_node, new_label);
1585 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1587 return NULL_TREE;
1590 /* Called via walk_function+walk_tree, rewrite all references to addresses
1591 of nested functions that require the use of trampolines. The rewrite
1592 will involve a reference a trampoline generated for the occasion. */
1594 static tree
1595 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1597 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1598 struct nesting_info *info = wi->info, *i;
1599 tree t = *tp, decl, target_context, x, arg;
1601 *walk_subtrees = 0;
1602 switch (TREE_CODE (t))
1604 case ADDR_EXPR:
1605 /* Build
1606 T.1 = &CHAIN->tramp;
1607 T.2 = __builtin_adjust_trampoline (T.1);
1608 T.3 = (func_type)T.2;
1611 decl = TREE_OPERAND (t, 0);
1612 if (TREE_CODE (decl) != FUNCTION_DECL)
1613 break;
1615 /* Only need to process nested functions. */
1616 target_context = decl_function_context (decl);
1617 if (!target_context)
1618 break;
1620 /* If the nested function doesn't use a static chain, then
1621 it doesn't need a trampoline. */
1622 if (DECL_NO_STATIC_CHAIN (decl))
1623 break;
1625 /* Lookup the immediate parent of the callee, as that's where
1626 we need to insert the trampoline. */
1627 for (i = info; i->context != target_context; i = i->outer)
1628 continue;
1629 x = lookup_tramp_for_decl (i, decl, INSERT);
1631 /* Compute the address of the field holding the trampoline. */
1632 x = get_frame_field (info, target_context, x, &wi->tsi);
1633 x = build_addr (x, target_context);
1634 x = tsi_gimplify_val (info, x, &wi->tsi);
1635 arg = tree_cons (NULL, x, NULL);
1637 /* Do machine-specific ugliness. Normally this will involve
1638 computing extra alignment, but it can really be anything. */
1639 x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1640 x = build_function_call_expr (x, arg);
1641 x = init_tmp_var (info, x, &wi->tsi);
1643 /* Cast back to the proper function type. */
1644 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1645 x = init_tmp_var (info, x, &wi->tsi);
1647 *tp = x;
1648 break;
1650 case CALL_EXPR:
1651 /* Only walk call arguments, lest we generate trampolines for
1652 direct calls. */
1653 walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1654 break;
1656 default:
1657 if (!IS_TYPE_OR_DECL_P (t))
1658 *walk_subtrees = 1;
1659 break;
1662 return NULL_TREE;
1665 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1666 reference nested functions to make sure that the static chain is
1667 set up properly for the call. */
1669 static tree
1670 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1672 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1673 struct nesting_info *info = wi->info;
1674 tree t = *tp, decl, target_context;
1675 char save_static_chain_added;
1676 int i;
1678 *walk_subtrees = 0;
1679 switch (TREE_CODE (t))
1681 case CALL_EXPR:
1682 decl = get_callee_fndecl (t);
1683 if (!decl)
1684 break;
1685 target_context = decl_function_context (decl);
1686 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1688 TREE_OPERAND (t, 2)
1689 = get_static_chain (info, target_context, &wi->tsi);
1690 info->static_chain_added
1691 |= (1 << (info->context != target_context));
1693 break;
1695 case RETURN_EXPR:
1696 case MODIFY_EXPR:
1697 case WITH_SIZE_EXPR:
1698 /* Only return modify and with_size_expr may contain calls. */
1699 *walk_subtrees = 1;
1700 break;
1702 case OMP_PARALLEL:
1703 save_static_chain_added = info->static_chain_added;
1704 info->static_chain_added = 0;
1705 walk_body (convert_call_expr, info, &OMP_PARALLEL_BODY (t));
1706 for (i = 0; i < 2; i++)
1708 tree c, decl;
1709 if ((info->static_chain_added & (1 << i)) == 0)
1710 continue;
1711 decl = i ? get_chain_decl (info) : info->frame_decl;
1712 /* Don't add CHAIN.* or FRAME.* twice. */
1713 for (c = OMP_PARALLEL_CLAUSES (t); c; c = OMP_CLAUSE_CHAIN (c))
1714 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1715 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1716 && OMP_CLAUSE_DECL (c) == decl)
1717 break;
1718 if (c == NULL)
1720 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1721 OMP_CLAUSE_DECL (c) = decl;
1722 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1723 OMP_PARALLEL_CLAUSES (t) = c;
1726 info->static_chain_added |= save_static_chain_added;
1727 break;
1729 case OMP_FOR:
1730 case OMP_SECTIONS:
1731 case OMP_SECTION:
1732 case OMP_SINGLE:
1733 case OMP_MASTER:
1734 case OMP_ORDERED:
1735 case OMP_CRITICAL:
1736 walk_body (convert_call_expr, info, &OMP_BODY (t));
1737 break;
1739 default:
1740 break;
1743 return NULL_TREE;
1746 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1747 trampolines and call expressions. On the way back up, determine if
1748 a nested function actually uses its static chain; if not, remember that. */
1750 static void
1751 convert_all_function_calls (struct nesting_info *root)
1755 if (root->inner)
1756 convert_all_function_calls (root->inner);
1758 walk_function (convert_tramp_reference, root);
1759 walk_function (convert_call_expr, root);
1761 /* If the function does not use a static chain, then remember that. */
1762 if (root->outer && !root->chain_decl && !root->chain_field)
1763 DECL_NO_STATIC_CHAIN (root->context) = 1;
1764 else
1765 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1767 root = root->next;
1769 while (root);
1772 /* Do "everything else" to clean up or complete state collected by the
1773 various walking passes -- lay out the types and decls, generate code
1774 to initialize the frame decl, store critical expressions in the
1775 struct function for rtl to find. */
1777 static void
1778 finalize_nesting_tree_1 (struct nesting_info *root)
1780 tree stmt_list = NULL;
1781 tree context = root->context;
1782 struct function *sf;
1784 /* If we created a non-local frame type or decl, we need to lay them
1785 out at this time. */
1786 if (root->frame_type)
1788 /* In some cases the frame type will trigger the -Wpadded warning.
1789 This is not helpful; suppress it. */
1790 int save_warn_padded = warn_padded;
1791 warn_padded = 0;
1792 layout_type (root->frame_type);
1793 warn_padded = save_warn_padded;
1794 layout_decl (root->frame_decl, 0);
1797 /* If any parameters were referenced non-locally, then we need to
1798 insert a copy. Likewise, if any variables were referenced by
1799 pointer, we need to initialize the address. */
1800 if (root->any_parm_remapped)
1802 tree p;
1803 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1805 tree field, x, y;
1807 field = lookup_field_for_decl (root, p, NO_INSERT);
1808 if (!field)
1809 continue;
1811 if (use_pointer_in_frame (p))
1812 x = build_addr (p, context);
1813 else
1814 x = p;
1816 y = build3 (COMPONENT_REF, TREE_TYPE (field),
1817 root->frame_decl, field, NULL_TREE);
1818 x = build2 (MODIFY_EXPR, TREE_TYPE (field), y, x);
1819 append_to_statement_list (x, &stmt_list);
1823 /* If a chain_field was created, then it needs to be initialized
1824 from chain_decl. */
1825 if (root->chain_field)
1827 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
1828 root->frame_decl, root->chain_field, NULL_TREE);
1829 x = build2 (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1830 append_to_statement_list (x, &stmt_list);
1833 /* If trampolines were created, then we need to initialize them. */
1834 if (root->any_tramp_created)
1836 struct nesting_info *i;
1837 for (i = root->inner; i ; i = i->next)
1839 tree arg, x, field;
1841 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1842 if (!field)
1843 continue;
1845 if (DECL_NO_STATIC_CHAIN (i->context))
1846 x = null_pointer_node;
1847 else
1848 x = build_addr (root->frame_decl, context);
1849 arg = tree_cons (NULL, x, NULL);
1851 x = build_addr (i->context, context);
1852 arg = tree_cons (NULL, x, arg);
1854 x = build3 (COMPONENT_REF, TREE_TYPE (field),
1855 root->frame_decl, field, NULL_TREE);
1856 x = build_addr (x, context);
1857 arg = tree_cons (NULL, x, arg);
1859 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1860 x = build_function_call_expr (x, arg);
1862 append_to_statement_list (x, &stmt_list);
1866 /* If we created initialization statements, insert them. */
1867 if (stmt_list)
1869 annotate_all_with_locus (&stmt_list,
1870 DECL_SOURCE_LOCATION (context));
1871 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1872 &stmt_list);
1873 BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1876 /* If a chain_decl was created, then it needs to be registered with
1877 struct function so that it gets initialized from the static chain
1878 register at the beginning of the function. */
1879 sf = DECL_STRUCT_FUNCTION (root->context);
1880 sf->static_chain_decl = root->chain_decl;
1882 /* Similarly for the non-local goto save area. */
1883 if (root->nl_goto_field)
1885 sf->nonlocal_goto_save_area
1886 = get_frame_field (root, context, root->nl_goto_field, NULL);
1887 sf->has_nonlocal_label = 1;
1890 /* Make sure all new local variables get inserted into the
1891 proper BIND_EXPR. */
1892 if (root->new_local_var_chain)
1893 declare_vars (root->new_local_var_chain, DECL_SAVED_TREE (root->context),
1894 false);
1895 if (root->debug_var_chain)
1896 declare_vars (root->debug_var_chain, DECL_SAVED_TREE (root->context),
1897 true);
1899 /* Dump the translated tree function. */
1900 dump_function (TDI_nested, root->context);
1903 static void
1904 finalize_nesting_tree (struct nesting_info *root)
1908 if (root->inner)
1909 finalize_nesting_tree (root->inner);
1910 finalize_nesting_tree_1 (root);
1911 root = root->next;
1913 while (root);
1916 /* Unnest the nodes and pass them to cgraph. */
1918 static void
1919 unnest_nesting_tree_1 (struct nesting_info *root)
1921 struct cgraph_node *node = cgraph_node (root->context);
1923 /* For nested functions update the cgraph to reflect unnesting.
1924 We also delay finalizing of these functions up to this point. */
1925 if (node->origin)
1927 cgraph_unnest_node (cgraph_node (root->context));
1928 cgraph_finalize_function (root->context, true);
1932 static void
1933 unnest_nesting_tree (struct nesting_info *root)
1937 if (root->inner)
1938 unnest_nesting_tree (root->inner);
1939 unnest_nesting_tree_1 (root);
1940 root = root->next;
1942 while (root);
1945 /* Free the data structures allocated during this pass. */
1947 static void
1948 free_nesting_tree (struct nesting_info *root)
1950 struct nesting_info *next;
1953 if (root->inner)
1954 free_nesting_tree (root->inner);
1955 htab_delete (root->var_map);
1956 next = root->next;
1957 ggc_free (root);
1958 root = next;
1960 while (root);
1963 static GTY(()) struct nesting_info *root;
1965 /* Main entry point for this pass. Process FNDECL and all of its nested
1966 subroutines and turn them into something less tightly bound. */
1968 void
1969 lower_nested_functions (tree fndecl)
1971 struct cgraph_node *cgn;
1973 /* If there are no nested functions, there's nothing to do. */
1974 cgn = cgraph_node (fndecl);
1975 if (!cgn->nested)
1976 return;
1978 root = create_nesting_tree (cgn);
1979 walk_all_functions (convert_nonlocal_reference, root);
1980 walk_all_functions (convert_local_reference, root);
1981 walk_all_functions (convert_nl_goto_reference, root);
1982 walk_all_functions (convert_nl_goto_receiver, root);
1983 convert_all_function_calls (root);
1984 finalize_nesting_tree (root);
1985 unnest_nesting_tree (root);
1986 free_nesting_tree (root);
1987 root = NULL;
1990 #include "gt-tree-nested.h"