c-family/
[official-gcc.git] / gcc / tree-nested.c
blobb4d8688895e76488dace38ef615fec1d3f868fdc
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "function.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
30 #include "gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
33 #include "cgraph.h"
34 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
35 #include "langhooks.h"
36 #include "pointer-set.h"
39 /* The object of this pass is to lower the representation of a set of nested
40 functions in order to expose all of the gory details of the various
41 nonlocal references. We want to do this sooner rather than later, in
42 order to give us more freedom in emitting all of the functions in question.
44 Back in olden times, when gcc was young, we developed an insanely
45 complicated scheme whereby variables which were referenced nonlocally
46 were forced to live in the stack of the declaring function, and then
47 the nested functions magically discovered where these variables were
48 placed. In order for this scheme to function properly, it required
49 that the outer function be partially expanded, then we switch to
50 compiling the inner function, and once done with those we switch back
51 to compiling the outer function. Such delicate ordering requirements
52 makes it difficult to do whole translation unit optimizations
53 involving such functions.
55 The implementation here is much more direct. Everything that can be
56 referenced by an inner function is a member of an explicitly created
57 structure herein called the "nonlocal frame struct". The incoming
58 static chain for a nested function is a pointer to this struct in
59 the parent. In this way, we settle on known offsets from a known
60 base, and so are decoupled from the logic that places objects in the
61 function's stack frame. More importantly, we don't have to wait for
62 that to happen -- since the compilation of the inner function is no
63 longer tied to a real stack frame, the nonlocal frame struct can be
64 allocated anywhere. Which means that the outer function is now
65 inlinable.
67 Theory of operation here is very simple. Iterate over all the
68 statements in all the functions (depth first) several times,
69 allocating structures and fields on demand. In general we want to
70 examine inner functions first, so that we can avoid making changes
71 to outer functions which are unnecessary.
73 The order of the passes matters a bit, in that later passes will be
74 skipped if it is discovered that the functions don't actually interact
75 at all. That is, they're nested in the lexical sense but could have
76 been written as independent functions without change. */
79 struct nesting_info
81 struct nesting_info *outer;
82 struct nesting_info *inner;
83 struct nesting_info *next;
85 struct pointer_map_t *field_map;
86 struct pointer_map_t *var_map;
87 struct pointer_set_t *mem_refs;
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 /* Iterate over the nesting tree, starting with ROOT, depth first. */
107 static inline struct nesting_info *
108 iter_nestinfo_start (struct nesting_info *root)
110 while (root->inner)
111 root = root->inner;
112 return root;
115 static inline struct nesting_info *
116 iter_nestinfo_next (struct nesting_info *node)
118 if (node->next)
119 return iter_nestinfo_start (node->next);
120 return node->outer;
123 #define FOR_EACH_NEST_INFO(I, ROOT) \
124 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
126 /* Obstack used for the bitmaps in the struct above. */
127 static struct bitmap_obstack nesting_info_bitmap_obstack;
130 /* We're working in so many different function contexts simultaneously,
131 that create_tmp_var is dangerous. Prevent mishap. */
132 #define create_tmp_var cant_use_create_tmp_var_here_dummy
134 /* Like create_tmp_var, except record the variable for registration at
135 the given nesting level. */
137 static tree
138 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
140 tree tmp_var;
142 /* If the type is of variable size or a type which must be created by the
143 frontend, something is wrong. Note that we explicitly allow
144 incomplete types here, since we create them ourselves here. */
145 gcc_assert (!TREE_ADDRESSABLE (type));
146 gcc_assert (!TYPE_SIZE_UNIT (type)
147 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
149 tmp_var = create_tmp_var_raw (type, prefix);
150 DECL_CONTEXT (tmp_var) = info->context;
151 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
152 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
153 if (TREE_CODE (type) == COMPLEX_TYPE
154 || TREE_CODE (type) == VECTOR_TYPE)
155 DECL_GIMPLE_REG_P (tmp_var) = 1;
157 info->new_local_var_chain = tmp_var;
159 return tmp_var;
162 /* Take the address of EXP to be used within function CONTEXT.
163 Mark it for addressability as necessary. */
165 tree
166 build_addr (tree exp, tree context)
168 tree base = exp;
169 tree save_context;
170 tree retval;
172 while (handled_component_p (base))
173 base = TREE_OPERAND (base, 0);
175 if (DECL_P (base))
176 TREE_ADDRESSABLE (base) = 1;
178 /* Building the ADDR_EXPR will compute a set of properties for
179 that ADDR_EXPR. Those properties are unfortunately context
180 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
182 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
183 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
184 way the properties are for the ADDR_EXPR are computed properly. */
185 save_context = current_function_decl;
186 current_function_decl = context;
187 retval = build_fold_addr_expr (exp);
188 current_function_decl = save_context;
189 return retval;
192 /* Insert FIELD into TYPE, sorted by alignment requirements. */
194 void
195 insert_field_into_struct (tree type, tree field)
197 tree *p;
199 DECL_CONTEXT (field) = type;
201 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
202 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
203 break;
205 DECL_CHAIN (field) = *p;
206 *p = field;
208 /* Set correct alignment for frame struct type. */
209 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
210 TYPE_ALIGN (type) = DECL_ALIGN (field);
213 /* Build or return the RECORD_TYPE that describes the frame state that is
214 shared between INFO->CONTEXT and its nested functions. This record will
215 not be complete until finalize_nesting_tree; up until that point we'll
216 be adding fields as necessary.
218 We also build the DECL that represents this frame in the function. */
220 static tree
221 get_frame_type (struct nesting_info *info)
223 tree type = info->frame_type;
224 if (!type)
226 char *name;
228 type = make_node (RECORD_TYPE);
230 name = concat ("FRAME.",
231 IDENTIFIER_POINTER (DECL_NAME (info->context)),
232 NULL);
233 TYPE_NAME (type) = get_identifier (name);
234 free (name);
236 info->frame_type = type;
237 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
238 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
240 /* ??? Always make it addressable for now, since it is meant to
241 be pointed to by the static chain pointer. This pessimizes
242 when it turns out that no static chains are needed because
243 the nested functions referencing non-local variables are not
244 reachable, but the true pessimization is to create the non-
245 local frame structure in the first place. */
246 TREE_ADDRESSABLE (info->frame_decl) = 1;
248 return type;
251 /* Return true if DECL should be referenced by pointer in the non-local
252 frame structure. */
254 static bool
255 use_pointer_in_frame (tree decl)
257 if (TREE_CODE (decl) == PARM_DECL)
259 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
260 sized decls, and inefficient to copy large aggregates. Don't bother
261 moving anything but scalar variables. */
262 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
264 else
266 /* Variable sized types make things "interesting" in the frame. */
267 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
271 /* Given DECL, a non-locally accessed variable, find or create a field
272 in the non-local frame structure for the given nesting context. */
274 static tree
275 lookup_field_for_decl (struct nesting_info *info, tree decl,
276 enum insert_option insert)
278 void **slot;
280 if (insert == NO_INSERT)
282 slot = pointer_map_contains (info->field_map, decl);
283 return slot ? (tree) *slot : NULL_TREE;
286 slot = pointer_map_insert (info->field_map, decl);
287 if (!*slot)
289 tree field = make_node (FIELD_DECL);
290 DECL_NAME (field) = DECL_NAME (decl);
292 if (use_pointer_in_frame (decl))
294 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
295 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
296 DECL_NONADDRESSABLE_P (field) = 1;
298 else
300 TREE_TYPE (field) = TREE_TYPE (decl);
301 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
302 DECL_ALIGN (field) = DECL_ALIGN (decl);
303 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
304 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
305 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
306 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
309 insert_field_into_struct (get_frame_type (info), field);
310 *slot = field;
312 if (TREE_CODE (decl) == PARM_DECL)
313 info->any_parm_remapped = true;
316 return (tree) *slot;
319 /* Build or return the variable that holds the static chain within
320 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
322 static tree
323 get_chain_decl (struct nesting_info *info)
325 tree decl = info->chain_decl;
327 if (!decl)
329 tree type;
331 type = get_frame_type (info->outer);
332 type = build_pointer_type (type);
334 /* Note that this variable is *not* entered into any BIND_EXPR;
335 the construction of this variable is handled specially in
336 expand_function_start and initialize_inlined_parameters.
337 Note also that it's represented as a parameter. This is more
338 close to the truth, since the initial value does come from
339 the caller. */
340 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
341 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 if (dump_file
355 && (dump_flags & TDF_DETAILS)
356 && !DECL_STATIC_CHAIN (info->context))
357 fprintf (dump_file, "Setting static-chain for %s\n",
358 lang_hooks.decl_printable_name (info->context, 2));
360 DECL_STATIC_CHAIN (info->context) = 1;
362 return decl;
365 /* Build or return the field within the non-local frame state that holds
366 the static chain for INFO->CONTEXT. This is the way to walk back up
367 multiple nesting levels. */
369 static tree
370 get_chain_field (struct nesting_info *info)
372 tree field = info->chain_field;
374 if (!field)
376 tree type = build_pointer_type (get_frame_type (info->outer));
378 field = make_node (FIELD_DECL);
379 DECL_NAME (field) = get_identifier ("__chain");
380 TREE_TYPE (field) = type;
381 DECL_ALIGN (field) = TYPE_ALIGN (type);
382 DECL_NONADDRESSABLE_P (field) = 1;
384 insert_field_into_struct (get_frame_type (info), field);
386 info->chain_field = field;
388 if (dump_file
389 && (dump_flags & TDF_DETAILS)
390 && !DECL_STATIC_CHAIN (info->context))
391 fprintf (dump_file, "Setting static-chain for %s\n",
392 lang_hooks.decl_printable_name (info->context, 2));
394 DECL_STATIC_CHAIN (info->context) = 1;
396 return field;
399 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
401 static tree
402 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
403 gimple call)
405 tree t;
407 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
408 gimple_call_set_lhs (call, t);
409 if (! gsi_end_p (*gsi))
410 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
411 gsi_insert_before (gsi, call, GSI_SAME_STMT);
413 return t;
417 /* Copy EXP into a temporary. Allocate the temporary in the context of
418 INFO and insert the initialization statement before GSI. */
420 static tree
421 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
423 tree t;
424 gimple stmt;
426 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
427 stmt = gimple_build_assign (t, exp);
428 if (! gsi_end_p (*gsi))
429 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
430 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
432 return t;
436 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
438 static tree
439 gsi_gimplify_val (struct nesting_info *info, tree exp,
440 gimple_stmt_iterator *gsi)
442 if (is_gimple_val (exp))
443 return exp;
444 else
445 return init_tmp_var (info, exp, gsi);
448 /* Similarly, but copy from the temporary and insert the statement
449 after the iterator. */
451 static tree
452 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
454 tree t;
455 gimple stmt;
457 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
458 stmt = gimple_build_assign (exp, t);
459 if (! gsi_end_p (*gsi))
460 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
461 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
463 return t;
466 /* Build or return the type used to represent a nested function trampoline. */
468 static GTY(()) tree trampoline_type;
470 static tree
471 get_trampoline_type (struct nesting_info *info)
473 unsigned align, size;
474 tree t;
476 if (trampoline_type)
477 return trampoline_type;
479 align = TRAMPOLINE_ALIGNMENT;
480 size = TRAMPOLINE_SIZE;
482 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
483 then allocate extra space so that we can do dynamic alignment. */
484 if (align > STACK_BOUNDARY)
486 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
487 align = STACK_BOUNDARY;
490 t = build_index_type (size_int (size - 1));
491 t = build_array_type (char_type_node, t);
492 t = build_decl (DECL_SOURCE_LOCATION (info->context),
493 FIELD_DECL, get_identifier ("__data"), t);
494 DECL_ALIGN (t) = align;
495 DECL_USER_ALIGN (t) = 1;
497 trampoline_type = make_node (RECORD_TYPE);
498 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
499 TYPE_FIELDS (trampoline_type) = t;
500 layout_type (trampoline_type);
501 DECL_CONTEXT (t) = trampoline_type;
503 return trampoline_type;
506 /* Given DECL, a nested function, find or create a field in the non-local
507 frame structure for a trampoline for this function. */
509 static tree
510 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
511 enum insert_option insert)
513 void **slot;
515 if (insert == NO_INSERT)
517 slot = pointer_map_contains (info->var_map, decl);
518 return slot ? (tree) *slot : NULL_TREE;
521 slot = pointer_map_insert (info->var_map, decl);
522 if (!*slot)
524 tree field = make_node (FIELD_DECL);
525 DECL_NAME (field) = DECL_NAME (decl);
526 TREE_TYPE (field) = get_trampoline_type (info);
527 TREE_ADDRESSABLE (field) = 1;
529 insert_field_into_struct (get_frame_type (info), field);
530 *slot = field;
532 info->any_tramp_created = true;
535 return (tree) *slot;
538 /* Build or return the field within the non-local frame state that holds
539 the non-local goto "jmp_buf". The buffer itself is maintained by the
540 rtl middle-end as dynamic stack space is allocated. */
542 static tree
543 get_nl_goto_field (struct nesting_info *info)
545 tree field = info->nl_goto_field;
546 if (!field)
548 unsigned size;
549 tree type;
551 /* For __builtin_nonlocal_goto, we need N words. The first is the
552 frame pointer, the rest is for the target's stack pointer save
553 area. The number of words is controlled by STACK_SAVEAREA_MODE;
554 not the best interface, but it'll do for now. */
555 if (Pmode == ptr_mode)
556 type = ptr_type_node;
557 else
558 type = lang_hooks.types.type_for_mode (Pmode, 1);
560 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
561 size = size / GET_MODE_SIZE (Pmode);
562 size = size + 1;
564 type = build_array_type
565 (type, build_index_type (size_int (size)));
567 field = make_node (FIELD_DECL);
568 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
569 TREE_TYPE (field) = type;
570 DECL_ALIGN (field) = TYPE_ALIGN (type);
571 TREE_ADDRESSABLE (field) = 1;
573 insert_field_into_struct (get_frame_type (info), field);
575 info->nl_goto_field = field;
578 return field;
581 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
583 static void
584 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
585 struct nesting_info *info, gimple_seq *pseq)
587 struct walk_stmt_info wi;
589 memset (&wi, 0, sizeof (wi));
590 wi.info = info;
591 wi.val_only = true;
592 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
596 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
598 static inline void
599 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
600 struct nesting_info *info)
602 gimple_seq body = gimple_body (info->context);
603 walk_body (callback_stmt, callback_op, info, &body);
604 gimple_set_body (info->context, body);
607 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
609 static void
610 walk_gimple_omp_for (gimple for_stmt,
611 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
612 struct nesting_info *info)
614 struct walk_stmt_info wi;
615 gimple_seq seq;
616 tree t;
617 size_t i;
619 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
621 seq = NULL;
622 memset (&wi, 0, sizeof (wi));
623 wi.info = info;
624 wi.gsi = gsi_last (seq);
626 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
628 wi.val_only = false;
629 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
630 &wi, NULL);
631 wi.val_only = true;
632 wi.is_lhs = false;
633 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
634 &wi, NULL);
636 wi.val_only = true;
637 wi.is_lhs = false;
638 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
639 &wi, NULL);
641 t = gimple_omp_for_incr (for_stmt, i);
642 gcc_assert (BINARY_CLASS_P (t));
643 wi.val_only = false;
644 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
645 wi.val_only = true;
646 wi.is_lhs = false;
647 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
650 seq = gsi_seq (wi.gsi);
651 if (!gimple_seq_empty_p (seq))
653 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
654 annotate_all_with_location (seq, gimple_location (for_stmt));
655 gimple_seq_add_seq (&pre_body, seq);
656 gimple_omp_for_set_pre_body (for_stmt, pre_body);
660 /* Similarly for ROOT and all functions nested underneath, depth first. */
662 static void
663 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
664 struct nesting_info *root)
666 struct nesting_info *n;
667 FOR_EACH_NEST_INFO (n, root)
668 walk_function (callback_stmt, callback_op, n);
672 /* We have to check for a fairly pathological case. The operands of function
673 nested function are to be interpreted in the context of the enclosing
674 function. So if any are variably-sized, they will get remapped when the
675 enclosing function is inlined. But that remapping would also have to be
676 done in the types of the PARM_DECLs of the nested function, meaning the
677 argument types of that function will disagree with the arguments in the
678 calls to that function. So we'd either have to make a copy of the nested
679 function corresponding to each time the enclosing function was inlined or
680 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
681 function. The former is not practical. The latter would still require
682 detecting this case to know when to add the conversions. So, for now at
683 least, we don't inline such an enclosing function.
685 We have to do that check recursively, so here return indicating whether
686 FNDECL has such a nested function. ORIG_FN is the function we were
687 trying to inline to use for checking whether any argument is variably
688 modified by anything in it.
690 It would be better to do this in tree-inline.c so that we could give
691 the appropriate warning for why a function can't be inlined, but that's
692 too late since the nesting structure has already been flattened and
693 adding a flag just to record this fact seems a waste of a flag. */
695 static bool
696 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
698 struct cgraph_node *cgn = cgraph_get_node (fndecl);
699 tree arg;
701 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
703 for (arg = DECL_ARGUMENTS (cgn->symbol.decl); arg; arg = DECL_CHAIN (arg))
704 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
705 return true;
707 if (check_for_nested_with_variably_modified (cgn->symbol.decl,
708 orig_fndecl))
709 return true;
712 return false;
715 /* Construct our local datastructure describing the function nesting
716 tree rooted by CGN. */
718 static struct nesting_info *
719 create_nesting_tree (struct cgraph_node *cgn)
721 struct nesting_info *info = XCNEW (struct nesting_info);
722 info->field_map = pointer_map_create ();
723 info->var_map = pointer_map_create ();
724 info->mem_refs = pointer_set_create ();
725 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
726 info->context = cgn->symbol.decl;
728 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
730 struct nesting_info *sub = create_nesting_tree (cgn);
731 sub->outer = info;
732 sub->next = info->inner;
733 info->inner = sub;
736 /* See discussion at check_for_nested_with_variably_modified for a
737 discussion of why this has to be here. */
738 if (check_for_nested_with_variably_modified (info->context, info->context))
739 DECL_UNINLINABLE (info->context) = true;
741 return info;
744 /* Return an expression computing the static chain for TARGET_CONTEXT
745 from INFO->CONTEXT. Insert any necessary computations before TSI. */
747 static tree
748 get_static_chain (struct nesting_info *info, tree target_context,
749 gimple_stmt_iterator *gsi)
751 struct nesting_info *i;
752 tree x;
754 if (info->context == target_context)
756 x = build_addr (info->frame_decl, target_context);
758 else
760 x = get_chain_decl (info);
762 for (i = info->outer; i->context != target_context; i = i->outer)
764 tree field = get_chain_field (i);
766 x = build_simple_mem_ref (x);
767 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
768 x = init_tmp_var (info, x, gsi);
772 return x;
776 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
777 frame as seen from INFO->CONTEXT. Insert any necessary computations
778 before GSI. */
780 static tree
781 get_frame_field (struct nesting_info *info, tree target_context,
782 tree field, gimple_stmt_iterator *gsi)
784 struct nesting_info *i;
785 tree x;
787 if (info->context == target_context)
789 /* Make sure frame_decl gets created. */
790 (void) get_frame_type (info);
791 x = info->frame_decl;
793 else
795 x = get_chain_decl (info);
797 for (i = info->outer; i->context != target_context; i = i->outer)
799 tree field = get_chain_field (i);
801 x = build_simple_mem_ref (x);
802 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
803 x = init_tmp_var (info, x, gsi);
806 x = build_simple_mem_ref (x);
809 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
810 return x;
813 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
815 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
816 in the nested function with DECL_VALUE_EXPR set to reference the true
817 variable in the parent function. This is used both for debug info
818 and in OpenMP lowering. */
820 static tree
821 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
823 tree target_context;
824 struct nesting_info *i;
825 tree x, field, new_decl;
826 void **slot;
828 slot = pointer_map_insert (info->var_map, decl);
830 if (*slot)
831 return (tree) *slot;
833 target_context = decl_function_context (decl);
835 /* A copy of the code in get_frame_field, but without the temporaries. */
836 if (info->context == target_context)
838 /* Make sure frame_decl gets created. */
839 (void) get_frame_type (info);
840 x = info->frame_decl;
841 i = info;
843 else
845 x = get_chain_decl (info);
846 for (i = info->outer; i->context != target_context; i = i->outer)
848 field = get_chain_field (i);
849 x = build_simple_mem_ref (x);
850 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
852 x = build_simple_mem_ref (x);
855 field = lookup_field_for_decl (i, decl, INSERT);
856 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
857 if (use_pointer_in_frame (decl))
858 x = build_simple_mem_ref (x);
860 /* ??? We should be remapping types as well, surely. */
861 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
862 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
863 DECL_CONTEXT (new_decl) = info->context;
864 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
865 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
866 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
867 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
868 TREE_READONLY (new_decl) = TREE_READONLY (decl);
869 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
870 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
871 if ((TREE_CODE (decl) == PARM_DECL
872 || TREE_CODE (decl) == RESULT_DECL
873 || TREE_CODE (decl) == VAR_DECL)
874 && DECL_BY_REFERENCE (decl))
875 DECL_BY_REFERENCE (new_decl) = 1;
877 SET_DECL_VALUE_EXPR (new_decl, x);
878 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
880 *slot = new_decl;
881 DECL_CHAIN (new_decl) = info->debug_var_chain;
882 info->debug_var_chain = new_decl;
884 if (!optimize
885 && info->context != target_context
886 && variably_modified_type_p (TREE_TYPE (decl), NULL))
887 note_nonlocal_vla_type (info, TREE_TYPE (decl));
889 return new_decl;
893 /* Callback for walk_gimple_stmt, rewrite all references to VAR
894 and PARM_DECLs that belong to outer functions.
896 The rewrite will involve some number of structure accesses back up
897 the static chain. E.g. for a variable FOO up one nesting level it'll
898 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
899 indirections apply to decls for which use_pointer_in_frame is true. */
901 static tree
902 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
904 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
905 struct nesting_info *const info = (struct nesting_info *) wi->info;
906 tree t = *tp;
908 *walk_subtrees = 0;
909 switch (TREE_CODE (t))
911 case VAR_DECL:
912 /* Non-automatic variables are never processed. */
913 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
914 break;
915 /* FALLTHRU */
917 case PARM_DECL:
918 if (decl_function_context (t) != info->context)
920 tree x;
921 wi->changed = true;
923 x = get_nonlocal_debug_decl (info, t);
924 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
926 tree target_context = decl_function_context (t);
927 struct nesting_info *i;
928 for (i = info->outer; i->context != target_context; i = i->outer)
929 continue;
930 x = lookup_field_for_decl (i, t, INSERT);
931 x = get_frame_field (info, target_context, x, &wi->gsi);
932 if (use_pointer_in_frame (t))
934 x = init_tmp_var (info, x, &wi->gsi);
935 x = build_simple_mem_ref (x);
939 if (wi->val_only)
941 if (wi->is_lhs)
942 x = save_tmp_var (info, x, &wi->gsi);
943 else
944 x = init_tmp_var (info, x, &wi->gsi);
947 *tp = x;
949 break;
951 case LABEL_DECL:
952 /* We're taking the address of a label from a parent function, but
953 this is not itself a non-local goto. Mark the label such that it
954 will not be deleted, much as we would with a label address in
955 static storage. */
956 if (decl_function_context (t) != info->context)
957 FORCED_LABEL (t) = 1;
958 break;
960 case ADDR_EXPR:
962 bool save_val_only = wi->val_only;
964 wi->val_only = false;
965 wi->is_lhs = false;
966 wi->changed = false;
967 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
968 wi->val_only = true;
970 if (wi->changed)
972 tree save_context;
974 /* If we changed anything, we might no longer be directly
975 referencing a decl. */
976 save_context = current_function_decl;
977 current_function_decl = info->context;
978 recompute_tree_invariant_for_addr_expr (t);
979 current_function_decl = save_context;
981 /* If the callback converted the address argument in a context
982 where we only accept variables (and min_invariant, presumably),
983 then compute the address into a temporary. */
984 if (save_val_only)
985 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
986 t, &wi->gsi);
989 break;
991 case REALPART_EXPR:
992 case IMAGPART_EXPR:
993 case COMPONENT_REF:
994 case ARRAY_REF:
995 case ARRAY_RANGE_REF:
996 case BIT_FIELD_REF:
997 /* Go down this entire nest and just look at the final prefix and
998 anything that describes the references. Otherwise, we lose track
999 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1000 wi->val_only = true;
1001 wi->is_lhs = false;
1002 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1004 if (TREE_CODE (t) == COMPONENT_REF)
1005 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1006 NULL);
1007 else if (TREE_CODE (t) == ARRAY_REF
1008 || TREE_CODE (t) == ARRAY_RANGE_REF)
1010 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1011 wi, NULL);
1012 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1013 wi, NULL);
1014 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1015 wi, NULL);
1018 wi->val_only = false;
1019 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1020 break;
1022 case VIEW_CONVERT_EXPR:
1023 /* Just request to look at the subtrees, leaving val_only and lhs
1024 untouched. This might actually be for !val_only + lhs, in which
1025 case we don't want to force a replacement by a temporary. */
1026 *walk_subtrees = 1;
1027 break;
1029 default:
1030 if (!IS_TYPE_OR_DECL_P (t))
1032 *walk_subtrees = 1;
1033 wi->val_only = true;
1034 wi->is_lhs = false;
1036 break;
1039 return NULL_TREE;
1042 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1043 struct walk_stmt_info *);
1045 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1046 and PARM_DECLs that belong to outer functions. */
1048 static bool
1049 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1051 struct nesting_info *const info = (struct nesting_info *) wi->info;
1052 bool need_chain = false, need_stmts = false;
1053 tree clause, decl;
1054 int dummy;
1055 bitmap new_suppress;
1057 new_suppress = BITMAP_GGC_ALLOC ();
1058 bitmap_copy (new_suppress, info->suppress_expansion);
1060 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1062 switch (OMP_CLAUSE_CODE (clause))
1064 case OMP_CLAUSE_REDUCTION:
1065 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1066 need_stmts = true;
1067 goto do_decl_clause;
1069 case OMP_CLAUSE_LASTPRIVATE:
1070 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1071 need_stmts = true;
1072 goto do_decl_clause;
1074 case OMP_CLAUSE_PRIVATE:
1075 case OMP_CLAUSE_FIRSTPRIVATE:
1076 case OMP_CLAUSE_COPYPRIVATE:
1077 case OMP_CLAUSE_SHARED:
1078 do_decl_clause:
1079 decl = OMP_CLAUSE_DECL (clause);
1080 if (TREE_CODE (decl) == VAR_DECL
1081 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1082 break;
1083 if (decl_function_context (decl) != info->context)
1085 bitmap_set_bit (new_suppress, DECL_UID (decl));
1086 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1087 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1088 need_chain = true;
1090 break;
1092 case OMP_CLAUSE_SCHEDULE:
1093 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1094 break;
1095 /* FALLTHRU */
1096 case OMP_CLAUSE_FINAL:
1097 case OMP_CLAUSE_IF:
1098 case OMP_CLAUSE_NUM_THREADS:
1099 wi->val_only = true;
1100 wi->is_lhs = false;
1101 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1102 &dummy, wi);
1103 break;
1105 case OMP_CLAUSE_NOWAIT:
1106 case OMP_CLAUSE_ORDERED:
1107 case OMP_CLAUSE_DEFAULT:
1108 case OMP_CLAUSE_COPYIN:
1109 case OMP_CLAUSE_COLLAPSE:
1110 case OMP_CLAUSE_UNTIED:
1111 case OMP_CLAUSE_MERGEABLE:
1112 break;
1114 default:
1115 gcc_unreachable ();
1119 info->suppress_expansion = new_suppress;
1121 if (need_stmts)
1122 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1123 switch (OMP_CLAUSE_CODE (clause))
1125 case OMP_CLAUSE_REDUCTION:
1126 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1128 tree old_context
1129 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1130 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1131 = info->context;
1132 walk_body (convert_nonlocal_reference_stmt,
1133 convert_nonlocal_reference_op, info,
1134 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1135 walk_body (convert_nonlocal_reference_stmt,
1136 convert_nonlocal_reference_op, info,
1137 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1138 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1139 = old_context;
1141 break;
1143 case OMP_CLAUSE_LASTPRIVATE:
1144 walk_body (convert_nonlocal_reference_stmt,
1145 convert_nonlocal_reference_op, info,
1146 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1147 break;
1149 default:
1150 break;
1153 return need_chain;
1156 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1158 static void
1159 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1161 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1162 type = TREE_TYPE (type);
1164 if (TYPE_NAME (type)
1165 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1166 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1167 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1169 while (POINTER_TYPE_P (type)
1170 || TREE_CODE (type) == VECTOR_TYPE
1171 || TREE_CODE (type) == FUNCTION_TYPE
1172 || TREE_CODE (type) == METHOD_TYPE)
1173 type = TREE_TYPE (type);
1175 if (TREE_CODE (type) == ARRAY_TYPE)
1177 tree domain, t;
1179 note_nonlocal_vla_type (info, TREE_TYPE (type));
1180 domain = TYPE_DOMAIN (type);
1181 if (domain)
1183 t = TYPE_MIN_VALUE (domain);
1184 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1185 && decl_function_context (t) != info->context)
1186 get_nonlocal_debug_decl (info, t);
1187 t = TYPE_MAX_VALUE (domain);
1188 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1189 && decl_function_context (t) != info->context)
1190 get_nonlocal_debug_decl (info, t);
1195 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1196 in BLOCK. */
1198 static void
1199 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1201 tree var;
1203 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1204 if (TREE_CODE (var) == VAR_DECL
1205 && variably_modified_type_p (TREE_TYPE (var), NULL)
1206 && DECL_HAS_VALUE_EXPR_P (var)
1207 && decl_function_context (var) != info->context)
1208 note_nonlocal_vla_type (info, TREE_TYPE (var));
1211 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1212 PARM_DECLs that belong to outer functions. This handles statements
1213 that are not handled via the standard recursion done in
1214 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1215 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1216 operands of STMT have been handled by this function. */
1218 static tree
1219 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1220 struct walk_stmt_info *wi)
1222 struct nesting_info *info = (struct nesting_info *) wi->info;
1223 tree save_local_var_chain;
1224 bitmap save_suppress;
1225 gimple stmt = gsi_stmt (*gsi);
1227 switch (gimple_code (stmt))
1229 case GIMPLE_GOTO:
1230 /* Don't walk non-local gotos for now. */
1231 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1233 wi->val_only = true;
1234 wi->is_lhs = false;
1235 *handled_ops_p = true;
1236 return NULL_TREE;
1238 break;
1240 case GIMPLE_OMP_PARALLEL:
1241 case GIMPLE_OMP_TASK:
1242 save_suppress = info->suppress_expansion;
1243 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1244 wi))
1246 tree c, decl;
1247 decl = get_chain_decl (info);
1248 c = build_omp_clause (gimple_location (stmt),
1249 OMP_CLAUSE_FIRSTPRIVATE);
1250 OMP_CLAUSE_DECL (c) = decl;
1251 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1252 gimple_omp_taskreg_set_clauses (stmt, c);
1255 save_local_var_chain = info->new_local_var_chain;
1256 info->new_local_var_chain = NULL;
1258 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1259 info, gimple_omp_body_ptr (stmt));
1261 if (info->new_local_var_chain)
1262 declare_vars (info->new_local_var_chain,
1263 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1264 false);
1265 info->new_local_var_chain = save_local_var_chain;
1266 info->suppress_expansion = save_suppress;
1267 break;
1269 case GIMPLE_OMP_FOR:
1270 save_suppress = info->suppress_expansion;
1271 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1272 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1273 convert_nonlocal_reference_op, info);
1274 walk_body (convert_nonlocal_reference_stmt,
1275 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1276 info->suppress_expansion = save_suppress;
1277 break;
1279 case GIMPLE_OMP_SECTIONS:
1280 save_suppress = info->suppress_expansion;
1281 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1282 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1283 info, gimple_omp_body_ptr (stmt));
1284 info->suppress_expansion = save_suppress;
1285 break;
1287 case GIMPLE_OMP_SINGLE:
1288 save_suppress = info->suppress_expansion;
1289 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1290 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1291 info, gimple_omp_body_ptr (stmt));
1292 info->suppress_expansion = save_suppress;
1293 break;
1295 case GIMPLE_OMP_SECTION:
1296 case GIMPLE_OMP_MASTER:
1297 case GIMPLE_OMP_ORDERED:
1298 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1299 info, gimple_omp_body_ptr (stmt));
1300 break;
1302 case GIMPLE_BIND:
1303 if (!optimize && gimple_bind_block (stmt))
1304 note_nonlocal_block_vlas (info, gimple_bind_block (stmt));
1306 *handled_ops_p = false;
1307 return NULL_TREE;
1309 case GIMPLE_COND:
1310 wi->val_only = true;
1311 wi->is_lhs = false;
1312 *handled_ops_p = false;
1313 return NULL_TREE;
1315 default:
1316 /* For every other statement that we are not interested in
1317 handling here, let the walker traverse the operands. */
1318 *handled_ops_p = false;
1319 return NULL_TREE;
1322 /* We have handled all of STMT operands, no need to traverse the operands. */
1323 *handled_ops_p = true;
1324 return NULL_TREE;
1328 /* A subroutine of convert_local_reference. Create a local variable
1329 in the parent function with DECL_VALUE_EXPR set to reference the
1330 field in FRAME. This is used both for debug info and in OpenMP
1331 lowering. */
1333 static tree
1334 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1336 tree x, new_decl;
1337 void **slot;
1339 slot = pointer_map_insert (info->var_map, decl);
1340 if (*slot)
1341 return (tree) *slot;
1343 /* Make sure frame_decl gets created. */
1344 (void) get_frame_type (info);
1345 x = info->frame_decl;
1346 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1348 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1349 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1350 DECL_CONTEXT (new_decl) = info->context;
1351 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1352 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1353 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1354 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1355 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1356 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1357 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1358 if ((TREE_CODE (decl) == PARM_DECL
1359 || TREE_CODE (decl) == RESULT_DECL
1360 || TREE_CODE (decl) == VAR_DECL)
1361 && DECL_BY_REFERENCE (decl))
1362 DECL_BY_REFERENCE (new_decl) = 1;
1364 SET_DECL_VALUE_EXPR (new_decl, x);
1365 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1366 *slot = new_decl;
1368 DECL_CHAIN (new_decl) = info->debug_var_chain;
1369 info->debug_var_chain = new_decl;
1371 /* Do not emit debug info twice. */
1372 DECL_IGNORED_P (decl) = 1;
1374 return new_decl;
1378 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1379 and PARM_DECLs that were referenced by inner nested functions.
1380 The rewrite will be a structure reference to the local frame variable. */
1382 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1384 static tree
1385 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1387 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1388 struct nesting_info *const info = (struct nesting_info *) wi->info;
1389 tree t = *tp, field, x;
1390 bool save_val_only;
1392 *walk_subtrees = 0;
1393 switch (TREE_CODE (t))
1395 case VAR_DECL:
1396 /* Non-automatic variables are never processed. */
1397 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1398 break;
1399 /* FALLTHRU */
1401 case PARM_DECL:
1402 if (decl_function_context (t) == info->context)
1404 /* If we copied a pointer to the frame, then the original decl
1405 is used unchanged in the parent function. */
1406 if (use_pointer_in_frame (t))
1407 break;
1409 /* No need to transform anything if no child references the
1410 variable. */
1411 field = lookup_field_for_decl (info, t, NO_INSERT);
1412 if (!field)
1413 break;
1414 wi->changed = true;
1416 x = get_local_debug_decl (info, t, field);
1417 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1418 x = get_frame_field (info, info->context, field, &wi->gsi);
1420 if (wi->val_only)
1422 if (wi->is_lhs)
1423 x = save_tmp_var (info, x, &wi->gsi);
1424 else
1425 x = init_tmp_var (info, x, &wi->gsi);
1428 *tp = x;
1430 break;
1432 case ADDR_EXPR:
1433 save_val_only = wi->val_only;
1434 wi->val_only = false;
1435 wi->is_lhs = false;
1436 wi->changed = false;
1437 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1438 wi->val_only = save_val_only;
1440 /* If we converted anything ... */
1441 if (wi->changed)
1443 tree save_context;
1445 /* Then the frame decl is now addressable. */
1446 TREE_ADDRESSABLE (info->frame_decl) = 1;
1448 save_context = current_function_decl;
1449 current_function_decl = info->context;
1450 recompute_tree_invariant_for_addr_expr (t);
1451 current_function_decl = save_context;
1453 /* If we are in a context where we only accept values, then
1454 compute the address into a temporary. */
1455 if (save_val_only)
1456 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1457 t, &wi->gsi);
1459 break;
1461 case REALPART_EXPR:
1462 case IMAGPART_EXPR:
1463 case COMPONENT_REF:
1464 case ARRAY_REF:
1465 case ARRAY_RANGE_REF:
1466 case BIT_FIELD_REF:
1467 /* Go down this entire nest and just look at the final prefix and
1468 anything that describes the references. Otherwise, we lose track
1469 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1470 save_val_only = wi->val_only;
1471 wi->val_only = true;
1472 wi->is_lhs = false;
1473 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1475 if (TREE_CODE (t) == COMPONENT_REF)
1476 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1477 NULL);
1478 else if (TREE_CODE (t) == ARRAY_REF
1479 || TREE_CODE (t) == ARRAY_RANGE_REF)
1481 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1482 NULL);
1483 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1484 NULL);
1485 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1486 NULL);
1489 wi->val_only = false;
1490 walk_tree (tp, convert_local_reference_op, wi, NULL);
1491 wi->val_only = save_val_only;
1492 break;
1494 case MEM_REF:
1495 save_val_only = wi->val_only;
1496 wi->val_only = true;
1497 wi->is_lhs = false;
1498 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1499 wi, NULL);
1500 /* We need to re-fold the MEM_REF as component references as
1501 part of a ADDR_EXPR address are not allowed. But we cannot
1502 fold here, as the chain record type is not yet finalized. */
1503 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1504 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1505 pointer_set_insert (info->mem_refs, tp);
1506 wi->val_only = save_val_only;
1507 break;
1509 case VIEW_CONVERT_EXPR:
1510 /* Just request to look at the subtrees, leaving val_only and lhs
1511 untouched. This might actually be for !val_only + lhs, in which
1512 case we don't want to force a replacement by a temporary. */
1513 *walk_subtrees = 1;
1514 break;
1516 default:
1517 if (!IS_TYPE_OR_DECL_P (t))
1519 *walk_subtrees = 1;
1520 wi->val_only = true;
1521 wi->is_lhs = false;
1523 break;
1526 return NULL_TREE;
1529 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1530 struct walk_stmt_info *);
1532 /* Helper for convert_local_reference. Convert all the references in
1533 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1535 static bool
1536 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1538 struct nesting_info *const info = (struct nesting_info *) wi->info;
1539 bool need_frame = false, need_stmts = false;
1540 tree clause, decl;
1541 int dummy;
1542 bitmap new_suppress;
1544 new_suppress = BITMAP_GGC_ALLOC ();
1545 bitmap_copy (new_suppress, info->suppress_expansion);
1547 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1549 switch (OMP_CLAUSE_CODE (clause))
1551 case OMP_CLAUSE_REDUCTION:
1552 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1553 need_stmts = true;
1554 goto do_decl_clause;
1556 case OMP_CLAUSE_LASTPRIVATE:
1557 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1558 need_stmts = true;
1559 goto do_decl_clause;
1561 case OMP_CLAUSE_PRIVATE:
1562 case OMP_CLAUSE_FIRSTPRIVATE:
1563 case OMP_CLAUSE_COPYPRIVATE:
1564 case OMP_CLAUSE_SHARED:
1565 do_decl_clause:
1566 decl = OMP_CLAUSE_DECL (clause);
1567 if (TREE_CODE (decl) == VAR_DECL
1568 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1569 break;
1570 if (decl_function_context (decl) == info->context
1571 && !use_pointer_in_frame (decl))
1573 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1574 if (field)
1576 bitmap_set_bit (new_suppress, DECL_UID (decl));
1577 OMP_CLAUSE_DECL (clause)
1578 = get_local_debug_decl (info, decl, field);
1579 need_frame = true;
1582 break;
1584 case OMP_CLAUSE_SCHEDULE:
1585 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1586 break;
1587 /* FALLTHRU */
1588 case OMP_CLAUSE_FINAL:
1589 case OMP_CLAUSE_IF:
1590 case OMP_CLAUSE_NUM_THREADS:
1591 wi->val_only = true;
1592 wi->is_lhs = false;
1593 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1594 wi);
1595 break;
1597 case OMP_CLAUSE_NOWAIT:
1598 case OMP_CLAUSE_ORDERED:
1599 case OMP_CLAUSE_DEFAULT:
1600 case OMP_CLAUSE_COPYIN:
1601 case OMP_CLAUSE_COLLAPSE:
1602 case OMP_CLAUSE_UNTIED:
1603 case OMP_CLAUSE_MERGEABLE:
1604 break;
1606 default:
1607 gcc_unreachable ();
1611 info->suppress_expansion = new_suppress;
1613 if (need_stmts)
1614 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1615 switch (OMP_CLAUSE_CODE (clause))
1617 case OMP_CLAUSE_REDUCTION:
1618 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1620 tree old_context
1621 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1622 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1623 = info->context;
1624 walk_body (convert_local_reference_stmt,
1625 convert_local_reference_op, info,
1626 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1627 walk_body (convert_local_reference_stmt,
1628 convert_local_reference_op, info,
1629 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1630 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1631 = old_context;
1633 break;
1635 case OMP_CLAUSE_LASTPRIVATE:
1636 walk_body (convert_local_reference_stmt,
1637 convert_local_reference_op, info,
1638 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1639 break;
1641 default:
1642 break;
1645 return need_frame;
1649 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1650 and PARM_DECLs that were referenced by inner nested functions.
1651 The rewrite will be a structure reference to the local frame variable. */
1653 static tree
1654 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1655 struct walk_stmt_info *wi)
1657 struct nesting_info *info = (struct nesting_info *) wi->info;
1658 tree save_local_var_chain;
1659 bitmap save_suppress;
1660 gimple stmt = gsi_stmt (*gsi);
1662 switch (gimple_code (stmt))
1664 case GIMPLE_OMP_PARALLEL:
1665 case GIMPLE_OMP_TASK:
1666 save_suppress = info->suppress_expansion;
1667 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1668 wi))
1670 tree c;
1671 (void) get_frame_type (info);
1672 c = build_omp_clause (gimple_location (stmt),
1673 OMP_CLAUSE_SHARED);
1674 OMP_CLAUSE_DECL (c) = info->frame_decl;
1675 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1676 gimple_omp_taskreg_set_clauses (stmt, c);
1679 save_local_var_chain = info->new_local_var_chain;
1680 info->new_local_var_chain = NULL;
1682 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1683 gimple_omp_body_ptr (stmt));
1685 if (info->new_local_var_chain)
1686 declare_vars (info->new_local_var_chain,
1687 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1688 info->new_local_var_chain = save_local_var_chain;
1689 info->suppress_expansion = save_suppress;
1690 break;
1692 case GIMPLE_OMP_FOR:
1693 save_suppress = info->suppress_expansion;
1694 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1695 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1696 convert_local_reference_op, info);
1697 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1698 info, gimple_omp_body_ptr (stmt));
1699 info->suppress_expansion = save_suppress;
1700 break;
1702 case GIMPLE_OMP_SECTIONS:
1703 save_suppress = info->suppress_expansion;
1704 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1705 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1706 info, gimple_omp_body_ptr (stmt));
1707 info->suppress_expansion = save_suppress;
1708 break;
1710 case GIMPLE_OMP_SINGLE:
1711 save_suppress = info->suppress_expansion;
1712 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1713 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1714 info, gimple_omp_body_ptr (stmt));
1715 info->suppress_expansion = save_suppress;
1716 break;
1718 case GIMPLE_OMP_SECTION:
1719 case GIMPLE_OMP_MASTER:
1720 case GIMPLE_OMP_ORDERED:
1721 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1722 info, gimple_omp_body_ptr (stmt));
1723 break;
1725 case GIMPLE_COND:
1726 wi->val_only = true;
1727 wi->is_lhs = false;
1728 *handled_ops_p = false;
1729 return NULL_TREE;
1731 case GIMPLE_ASSIGN:
1732 if (gimple_clobber_p (stmt))
1734 tree lhs = gimple_assign_lhs (stmt);
1735 if (!use_pointer_in_frame (lhs)
1736 && lookup_field_for_decl (info, lhs, NO_INSERT))
1738 gsi_replace (gsi, gimple_build_nop (), true);
1739 break;
1742 *handled_ops_p = false;
1743 return NULL_TREE;
1745 default:
1746 /* For every other statement that we are not interested in
1747 handling here, let the walker traverse the operands. */
1748 *handled_ops_p = false;
1749 return NULL_TREE;
1752 /* Indicate that we have handled all the operands ourselves. */
1753 *handled_ops_p = true;
1754 return NULL_TREE;
1758 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1759 that reference labels from outer functions. The rewrite will be a
1760 call to __builtin_nonlocal_goto. */
1762 static tree
1763 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1764 struct walk_stmt_info *wi)
1766 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1767 tree label, new_label, target_context, x, field;
1768 void **slot;
1769 gimple call;
1770 gimple stmt = gsi_stmt (*gsi);
1772 if (gimple_code (stmt) != GIMPLE_GOTO)
1774 *handled_ops_p = false;
1775 return NULL_TREE;
1778 label = gimple_goto_dest (stmt);
1779 if (TREE_CODE (label) != LABEL_DECL)
1781 *handled_ops_p = false;
1782 return NULL_TREE;
1785 target_context = decl_function_context (label);
1786 if (target_context == info->context)
1788 *handled_ops_p = false;
1789 return NULL_TREE;
1792 for (i = info->outer; target_context != i->context; i = i->outer)
1793 continue;
1795 /* The original user label may also be use for a normal goto, therefore
1796 we must create a new label that will actually receive the abnormal
1797 control transfer. This new label will be marked LABEL_NONLOCAL; this
1798 mark will trigger proper behavior in the cfg, as well as cause the
1799 (hairy target-specific) non-local goto receiver code to be generated
1800 when we expand rtl. Enter this association into var_map so that we
1801 can insert the new label into the IL during a second pass. */
1802 slot = pointer_map_insert (i->var_map, label);
1803 if (*slot == NULL)
1805 new_label = create_artificial_label (UNKNOWN_LOCATION);
1806 DECL_NONLOCAL (new_label) = 1;
1807 *slot = new_label;
1809 else
1810 new_label = (tree) *slot;
1812 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1813 field = get_nl_goto_field (i);
1814 x = get_frame_field (info, target_context, field, gsi);
1815 x = build_addr (x, target_context);
1816 x = gsi_gimplify_val (info, x, gsi);
1817 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
1818 2, build_addr (new_label, target_context), x);
1819 gsi_replace (gsi, call, false);
1821 /* We have handled all of STMT's operands, no need to keep going. */
1822 *handled_ops_p = true;
1823 return NULL_TREE;
1827 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1828 are referenced via nonlocal goto from a nested function. The rewrite
1829 will involve installing a newly generated DECL_NONLOCAL label, and
1830 (potentially) a branch around the rtl gunk that is assumed to be
1831 attached to such a label. */
1833 static tree
1834 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1835 struct walk_stmt_info *wi)
1837 struct nesting_info *const info = (struct nesting_info *) wi->info;
1838 tree label, new_label;
1839 gimple_stmt_iterator tmp_gsi;
1840 void **slot;
1841 gimple stmt = gsi_stmt (*gsi);
1843 if (gimple_code (stmt) != GIMPLE_LABEL)
1845 *handled_ops_p = false;
1846 return NULL_TREE;
1849 label = gimple_label_label (stmt);
1851 slot = pointer_map_contains (info->var_map, label);
1852 if (!slot)
1854 *handled_ops_p = false;
1855 return NULL_TREE;
1858 /* If there's any possibility that the previous statement falls through,
1859 then we must branch around the new non-local label. */
1860 tmp_gsi = wi->gsi;
1861 gsi_prev (&tmp_gsi);
1862 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
1864 gimple stmt = gimple_build_goto (label);
1865 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1868 new_label = (tree) *slot;
1869 stmt = gimple_build_label (new_label);
1870 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1872 *handled_ops_p = true;
1873 return NULL_TREE;
1877 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1878 of nested functions that require the use of trampolines. The rewrite
1879 will involve a reference a trampoline generated for the occasion. */
1881 static tree
1882 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
1884 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1885 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1886 tree t = *tp, decl, target_context, x, builtin;
1887 gimple call;
1889 *walk_subtrees = 0;
1890 switch (TREE_CODE (t))
1892 case ADDR_EXPR:
1893 /* Build
1894 T.1 = &CHAIN->tramp;
1895 T.2 = __builtin_adjust_trampoline (T.1);
1896 T.3 = (func_type)T.2;
1899 decl = TREE_OPERAND (t, 0);
1900 if (TREE_CODE (decl) != FUNCTION_DECL)
1901 break;
1903 /* Only need to process nested functions. */
1904 target_context = decl_function_context (decl);
1905 if (!target_context)
1906 break;
1908 /* If the nested function doesn't use a static chain, then
1909 it doesn't need a trampoline. */
1910 if (!DECL_STATIC_CHAIN (decl))
1911 break;
1913 /* If we don't want a trampoline, then don't build one. */
1914 if (TREE_NO_TRAMPOLINE (t))
1915 break;
1917 /* Lookup the immediate parent of the callee, as that's where
1918 we need to insert the trampoline. */
1919 for (i = info; i->context != target_context; i = i->outer)
1920 continue;
1921 x = lookup_tramp_for_decl (i, decl, INSERT);
1923 /* Compute the address of the field holding the trampoline. */
1924 x = get_frame_field (info, target_context, x, &wi->gsi);
1925 x = build_addr (x, target_context);
1926 x = gsi_gimplify_val (info, x, &wi->gsi);
1928 /* Do machine-specific ugliness. Normally this will involve
1929 computing extra alignment, but it can really be anything. */
1930 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
1931 call = gimple_build_call (builtin, 1, x);
1932 x = init_tmp_var_with_call (info, &wi->gsi, call);
1934 /* Cast back to the proper function type. */
1935 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1936 x = init_tmp_var (info, x, &wi->gsi);
1938 *tp = x;
1939 break;
1941 default:
1942 if (!IS_TYPE_OR_DECL_P (t))
1943 *walk_subtrees = 1;
1944 break;
1947 return NULL_TREE;
1951 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1952 to addresses of nested functions that require the use of
1953 trampolines. The rewrite will involve a reference a trampoline
1954 generated for the occasion. */
1956 static tree
1957 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1958 struct walk_stmt_info *wi)
1960 struct nesting_info *info = (struct nesting_info *) wi->info;
1961 gimple stmt = gsi_stmt (*gsi);
1963 switch (gimple_code (stmt))
1965 case GIMPLE_CALL:
1967 /* Only walk call arguments, lest we generate trampolines for
1968 direct calls. */
1969 unsigned long i, nargs = gimple_call_num_args (stmt);
1970 for (i = 0; i < nargs; i++)
1971 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
1972 wi, NULL);
1973 break;
1976 case GIMPLE_OMP_PARALLEL:
1977 case GIMPLE_OMP_TASK:
1979 tree save_local_var_chain;
1980 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
1981 save_local_var_chain = info->new_local_var_chain;
1982 info->new_local_var_chain = NULL;
1983 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
1984 info, gimple_omp_body_ptr (stmt));
1985 if (info->new_local_var_chain)
1986 declare_vars (info->new_local_var_chain,
1987 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1988 false);
1989 info->new_local_var_chain = save_local_var_chain;
1991 break;
1993 default:
1994 *handled_ops_p = false;
1995 return NULL_TREE;
1996 break;
1999 *handled_ops_p = true;
2000 return NULL_TREE;
2005 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2006 that reference nested functions to make sure that the static chain
2007 is set up properly for the call. */
2009 static tree
2010 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2011 struct walk_stmt_info *wi)
2013 struct nesting_info *const info = (struct nesting_info *) wi->info;
2014 tree decl, target_context;
2015 char save_static_chain_added;
2016 int i;
2017 gimple stmt = gsi_stmt (*gsi);
2019 switch (gimple_code (stmt))
2021 case GIMPLE_CALL:
2022 if (gimple_call_chain (stmt))
2023 break;
2024 decl = gimple_call_fndecl (stmt);
2025 if (!decl)
2026 break;
2027 target_context = decl_function_context (decl);
2028 if (target_context && DECL_STATIC_CHAIN (decl))
2030 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
2031 &wi->gsi));
2032 info->static_chain_added |= (1 << (info->context != target_context));
2034 break;
2036 case GIMPLE_OMP_PARALLEL:
2037 case GIMPLE_OMP_TASK:
2038 save_static_chain_added = info->static_chain_added;
2039 info->static_chain_added = 0;
2040 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2041 for (i = 0; i < 2; i++)
2043 tree c, decl;
2044 if ((info->static_chain_added & (1 << i)) == 0)
2045 continue;
2046 decl = i ? get_chain_decl (info) : info->frame_decl;
2047 /* Don't add CHAIN.* or FRAME.* twice. */
2048 for (c = gimple_omp_taskreg_clauses (stmt);
2050 c = OMP_CLAUSE_CHAIN (c))
2051 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2052 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2053 && OMP_CLAUSE_DECL (c) == decl)
2054 break;
2055 if (c == NULL)
2057 c = build_omp_clause (gimple_location (stmt),
2058 i ? OMP_CLAUSE_FIRSTPRIVATE
2059 : OMP_CLAUSE_SHARED);
2060 OMP_CLAUSE_DECL (c) = decl;
2061 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2062 gimple_omp_taskreg_set_clauses (stmt, c);
2065 info->static_chain_added |= save_static_chain_added;
2066 break;
2068 case GIMPLE_OMP_FOR:
2069 walk_body (convert_gimple_call, NULL, info,
2070 gimple_omp_for_pre_body_ptr (stmt));
2071 /* FALLTHRU */
2072 case GIMPLE_OMP_SECTIONS:
2073 case GIMPLE_OMP_SECTION:
2074 case GIMPLE_OMP_SINGLE:
2075 case GIMPLE_OMP_MASTER:
2076 case GIMPLE_OMP_ORDERED:
2077 case GIMPLE_OMP_CRITICAL:
2078 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2079 break;
2081 default:
2082 /* Keep looking for other operands. */
2083 *handled_ops_p = false;
2084 return NULL_TREE;
2087 *handled_ops_p = true;
2088 return NULL_TREE;
2091 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2092 call expressions. At the same time, determine if a nested function
2093 actually uses its static chain; if not, remember that. */
2095 static void
2096 convert_all_function_calls (struct nesting_info *root)
2098 unsigned int chain_count = 0, old_chain_count, iter_count;
2099 struct nesting_info *n;
2101 /* First, optimistically clear static_chain for all decls that haven't
2102 used the static chain already for variable access. */
2103 FOR_EACH_NEST_INFO (n, root)
2105 tree decl = n->context;
2106 if (!n->outer || (!n->chain_decl && !n->chain_field))
2108 DECL_STATIC_CHAIN (decl) = 0;
2109 if (dump_file && (dump_flags & TDF_DETAILS))
2110 fprintf (dump_file, "Guessing no static-chain for %s\n",
2111 lang_hooks.decl_printable_name (decl, 2));
2113 else
2114 DECL_STATIC_CHAIN (decl) = 1;
2115 chain_count += DECL_STATIC_CHAIN (decl);
2118 /* Walk the functions and perform transformations. Note that these
2119 transformations can induce new uses of the static chain, which in turn
2120 require re-examining all users of the decl. */
2121 /* ??? It would make sense to try to use the call graph to speed this up,
2122 but the call graph hasn't really been built yet. Even if it did, we
2123 would still need to iterate in this loop since address-of references
2124 wouldn't show up in the callgraph anyway. */
2125 iter_count = 0;
2128 old_chain_count = chain_count;
2129 chain_count = 0;
2130 iter_count++;
2132 if (dump_file && (dump_flags & TDF_DETAILS))
2133 fputc ('\n', dump_file);
2135 FOR_EACH_NEST_INFO (n, root)
2137 tree decl = n->context;
2138 walk_function (convert_tramp_reference_stmt,
2139 convert_tramp_reference_op, n);
2140 walk_function (convert_gimple_call, NULL, n);
2141 chain_count += DECL_STATIC_CHAIN (decl);
2144 while (chain_count != old_chain_count);
2146 if (dump_file && (dump_flags & TDF_DETAILS))
2147 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2148 iter_count);
2151 struct nesting_copy_body_data
2153 copy_body_data cb;
2154 struct nesting_info *root;
2157 /* A helper subroutine for debug_var_chain type remapping. */
2159 static tree
2160 nesting_copy_decl (tree decl, copy_body_data *id)
2162 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2163 void **slot = pointer_map_contains (nid->root->var_map, decl);
2165 if (slot)
2166 return (tree) *slot;
2168 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2170 tree new_decl = copy_decl_no_change (decl, id);
2171 DECL_ORIGINAL_TYPE (new_decl)
2172 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2173 return new_decl;
2176 if (TREE_CODE (decl) == VAR_DECL
2177 || TREE_CODE (decl) == PARM_DECL
2178 || TREE_CODE (decl) == RESULT_DECL)
2179 return decl;
2181 return copy_decl_no_change (decl, id);
2184 /* A helper function for remap_vla_decls. See if *TP contains
2185 some remapped variables. */
2187 static tree
2188 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2190 struct nesting_info *root = (struct nesting_info *) data;
2191 tree t = *tp;
2192 void **slot;
2194 if (DECL_P (t))
2196 *walk_subtrees = 0;
2197 slot = pointer_map_contains (root->var_map, t);
2199 if (slot)
2200 return (tree) *slot;
2202 return NULL;
2205 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2206 involved. */
2208 static void
2209 remap_vla_decls (tree block, struct nesting_info *root)
2211 tree var, subblock, val, type;
2212 struct nesting_copy_body_data id;
2214 for (subblock = BLOCK_SUBBLOCKS (block);
2215 subblock;
2216 subblock = BLOCK_CHAIN (subblock))
2217 remap_vla_decls (subblock, root);
2219 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2220 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2222 val = DECL_VALUE_EXPR (var);
2223 type = TREE_TYPE (var);
2225 if (!(TREE_CODE (val) == INDIRECT_REF
2226 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2227 && variably_modified_type_p (type, NULL)))
2228 continue;
2230 if (pointer_map_contains (root->var_map, TREE_OPERAND (val, 0))
2231 || walk_tree (&type, contains_remapped_vars, root, NULL))
2232 break;
2235 if (var == NULL_TREE)
2236 return;
2238 memset (&id, 0, sizeof (id));
2239 id.cb.copy_decl = nesting_copy_decl;
2240 id.cb.decl_map = pointer_map_create ();
2241 id.root = root;
2243 for (; var; var = DECL_CHAIN (var))
2244 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2246 struct nesting_info *i;
2247 tree newt, context;
2248 void **slot;
2250 val = DECL_VALUE_EXPR (var);
2251 type = TREE_TYPE (var);
2253 if (!(TREE_CODE (val) == INDIRECT_REF
2254 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2255 && variably_modified_type_p (type, NULL)))
2256 continue;
2258 slot = pointer_map_contains (root->var_map, TREE_OPERAND (val, 0));
2259 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2260 continue;
2262 context = decl_function_context (var);
2263 for (i = root; i; i = i->outer)
2264 if (i->context == context)
2265 break;
2267 if (i == NULL)
2268 continue;
2270 /* Fully expand value expressions. This avoids having debug variables
2271 only referenced from them and that can be swept during GC. */
2272 if (slot)
2274 tree t = (tree) *slot;
2275 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2276 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2279 id.cb.src_fn = i->context;
2280 id.cb.dst_fn = i->context;
2281 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2283 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2284 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2286 newt = TREE_TYPE (newt);
2287 type = TREE_TYPE (type);
2289 if (TYPE_NAME (newt)
2290 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2291 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2292 && newt != type
2293 && TYPE_NAME (newt) == TYPE_NAME (type))
2294 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2296 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2297 if (val != DECL_VALUE_EXPR (var))
2298 SET_DECL_VALUE_EXPR (var, val);
2301 pointer_map_destroy (id.cb.decl_map);
2304 /* Fold the MEM_REF *E. */
2305 static bool
2306 fold_mem_refs (const void *e, void *data ATTRIBUTE_UNUSED)
2308 tree *ref_p = CONST_CAST2(tree *, const tree *, (const tree *)e);
2309 *ref_p = fold (*ref_p);
2310 return true;
2313 /* Do "everything else" to clean up or complete state collected by the
2314 various walking passes -- lay out the types and decls, generate code
2315 to initialize the frame decl, store critical expressions in the
2316 struct function for rtl to find. */
2318 static void
2319 finalize_nesting_tree_1 (struct nesting_info *root)
2321 gimple_seq stmt_list;
2322 gimple stmt;
2323 tree context = root->context;
2324 struct function *sf;
2326 stmt_list = NULL;
2328 /* If we created a non-local frame type or decl, we need to lay them
2329 out at this time. */
2330 if (root->frame_type)
2332 /* In some cases the frame type will trigger the -Wpadded warning.
2333 This is not helpful; suppress it. */
2334 int save_warn_padded = warn_padded;
2335 tree *adjust;
2337 warn_padded = 0;
2338 layout_type (root->frame_type);
2339 warn_padded = save_warn_padded;
2340 layout_decl (root->frame_decl, 0);
2342 /* Remove root->frame_decl from root->new_local_var_chain, so
2343 that we can declare it also in the lexical blocks, which
2344 helps ensure virtual regs that end up appearing in its RTL
2345 expression get substituted in instantiate_virtual_regs(). */
2346 for (adjust = &root->new_local_var_chain;
2347 *adjust != root->frame_decl;
2348 adjust = &DECL_CHAIN (*adjust))
2349 gcc_assert (DECL_CHAIN (*adjust));
2350 *adjust = DECL_CHAIN (*adjust);
2352 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2353 declare_vars (root->frame_decl,
2354 gimple_seq_first_stmt (gimple_body (context)), true);
2357 /* If any parameters were referenced non-locally, then we need to
2358 insert a copy. Likewise, if any variables were referenced by
2359 pointer, we need to initialize the address. */
2360 if (root->any_parm_remapped)
2362 tree p;
2363 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2365 tree field, x, y;
2367 field = lookup_field_for_decl (root, p, NO_INSERT);
2368 if (!field)
2369 continue;
2371 if (use_pointer_in_frame (p))
2372 x = build_addr (p, context);
2373 else
2374 x = p;
2376 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2377 root->frame_decl, field, NULL_TREE);
2378 stmt = gimple_build_assign (y, x);
2379 gimple_seq_add_stmt (&stmt_list, stmt);
2380 /* If the assignment is from a non-register the stmt is
2381 not valid gimple. Make it so by using a temporary instead. */
2382 if (!is_gimple_reg (x)
2383 && is_gimple_reg_type (TREE_TYPE (x)))
2385 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2386 x = init_tmp_var (root, x, &gsi);
2387 gimple_assign_set_rhs1 (stmt, x);
2392 /* If a chain_field was created, then it needs to be initialized
2393 from chain_decl. */
2394 if (root->chain_field)
2396 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2397 root->frame_decl, root->chain_field, NULL_TREE);
2398 stmt = gimple_build_assign (x, get_chain_decl (root));
2399 gimple_seq_add_stmt (&stmt_list, stmt);
2402 /* If trampolines were created, then we need to initialize them. */
2403 if (root->any_tramp_created)
2405 struct nesting_info *i;
2406 for (i = root->inner; i ; i = i->next)
2408 tree arg1, arg2, arg3, x, field;
2410 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2411 if (!field)
2412 continue;
2414 gcc_assert (DECL_STATIC_CHAIN (i->context));
2415 arg3 = build_addr (root->frame_decl, context);
2417 arg2 = build_addr (i->context, context);
2419 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2420 root->frame_decl, field, NULL_TREE);
2421 arg1 = build_addr (x, context);
2423 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2424 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2425 gimple_seq_add_stmt (&stmt_list, stmt);
2429 /* If we created initialization statements, insert them. */
2430 if (stmt_list)
2432 gimple bind;
2433 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2434 bind = gimple_seq_first_stmt (gimple_body (context));
2435 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2436 gimple_bind_set_body (bind, stmt_list);
2439 /* If a chain_decl was created, then it needs to be registered with
2440 struct function so that it gets initialized from the static chain
2441 register at the beginning of the function. */
2442 sf = DECL_STRUCT_FUNCTION (root->context);
2443 sf->static_chain_decl = root->chain_decl;
2445 /* Similarly for the non-local goto save area. */
2446 if (root->nl_goto_field)
2448 sf->nonlocal_goto_save_area
2449 = get_frame_field (root, context, root->nl_goto_field, NULL);
2450 sf->has_nonlocal_label = 1;
2453 /* Make sure all new local variables get inserted into the
2454 proper BIND_EXPR. */
2455 if (root->new_local_var_chain)
2456 declare_vars (root->new_local_var_chain,
2457 gimple_seq_first_stmt (gimple_body (root->context)),
2458 false);
2460 if (root->debug_var_chain)
2462 tree debug_var;
2463 gimple scope;
2465 remap_vla_decls (DECL_INITIAL (root->context), root);
2467 for (debug_var = root->debug_var_chain; debug_var;
2468 debug_var = DECL_CHAIN (debug_var))
2469 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2470 break;
2472 /* If there are any debug decls with variable length types,
2473 remap those types using other debug_var_chain variables. */
2474 if (debug_var)
2476 struct nesting_copy_body_data id;
2478 memset (&id, 0, sizeof (id));
2479 id.cb.copy_decl = nesting_copy_decl;
2480 id.cb.decl_map = pointer_map_create ();
2481 id.root = root;
2483 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2484 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2486 tree type = TREE_TYPE (debug_var);
2487 tree newt, t = type;
2488 struct nesting_info *i;
2490 for (i = root; i; i = i->outer)
2491 if (variably_modified_type_p (type, i->context))
2492 break;
2494 if (i == NULL)
2495 continue;
2497 id.cb.src_fn = i->context;
2498 id.cb.dst_fn = i->context;
2499 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2501 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2502 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2504 newt = TREE_TYPE (newt);
2505 t = TREE_TYPE (t);
2507 if (TYPE_NAME (newt)
2508 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2509 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2510 && newt != t
2511 && TYPE_NAME (newt) == TYPE_NAME (t))
2512 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2515 pointer_map_destroy (id.cb.decl_map);
2518 scope = gimple_seq_first_stmt (gimple_body (root->context));
2519 if (gimple_bind_block (scope))
2520 declare_vars (root->debug_var_chain, scope, true);
2521 else
2522 BLOCK_VARS (DECL_INITIAL (root->context))
2523 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2524 root->debug_var_chain);
2527 /* Fold the rewritten MEM_REF trees. */
2528 pointer_set_traverse (root->mem_refs, fold_mem_refs, NULL);
2530 /* Dump the translated tree function. */
2531 if (dump_file)
2533 fputs ("\n\n", dump_file);
2534 dump_function_to_file (root->context, dump_file, dump_flags);
2538 static void
2539 finalize_nesting_tree (struct nesting_info *root)
2541 struct nesting_info *n;
2542 FOR_EACH_NEST_INFO (n, root)
2543 finalize_nesting_tree_1 (n);
2546 /* Unnest the nodes and pass them to cgraph. */
2548 static void
2549 unnest_nesting_tree_1 (struct nesting_info *root)
2551 struct cgraph_node *node = cgraph_get_node (root->context);
2553 /* For nested functions update the cgraph to reflect unnesting.
2554 We also delay finalizing of these functions up to this point. */
2555 if (node->origin)
2557 cgraph_unnest_node (node);
2558 cgraph_finalize_function (root->context, true);
2562 static void
2563 unnest_nesting_tree (struct nesting_info *root)
2565 struct nesting_info *n;
2566 FOR_EACH_NEST_INFO (n, root)
2567 unnest_nesting_tree_1 (n);
2570 /* Free the data structures allocated during this pass. */
2572 static void
2573 free_nesting_tree (struct nesting_info *root)
2575 struct nesting_info *node, *next;
2577 node = iter_nestinfo_start (root);
2580 next = iter_nestinfo_next (node);
2581 pointer_map_destroy (node->var_map);
2582 pointer_map_destroy (node->field_map);
2583 pointer_set_destroy (node->mem_refs);
2584 free (node);
2585 node = next;
2587 while (node);
2590 /* Gimplify a function and all its nested functions. */
2591 static void
2592 gimplify_all_functions (struct cgraph_node *root)
2594 struct cgraph_node *iter;
2595 if (!gimple_body (root->symbol.decl))
2596 gimplify_function_tree (root->symbol.decl);
2597 for (iter = root->nested; iter; iter = iter->next_nested)
2598 gimplify_all_functions (iter);
2601 /* Main entry point for this pass. Process FNDECL and all of its nested
2602 subroutines and turn them into something less tightly bound. */
2604 void
2605 lower_nested_functions (tree fndecl)
2607 struct cgraph_node *cgn;
2608 struct nesting_info *root;
2610 /* If there are no nested functions, there's nothing to do. */
2611 cgn = cgraph_get_node (fndecl);
2612 if (!cgn->nested)
2613 return;
2615 gimplify_all_functions (cgn);
2617 dump_file = dump_begin (TDI_nested, &dump_flags);
2618 if (dump_file)
2619 fprintf (dump_file, "\n;; Function %s\n\n",
2620 lang_hooks.decl_printable_name (fndecl, 2));
2622 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2623 root = create_nesting_tree (cgn);
2625 walk_all_functions (convert_nonlocal_reference_stmt,
2626 convert_nonlocal_reference_op,
2627 root);
2628 walk_all_functions (convert_local_reference_stmt,
2629 convert_local_reference_op,
2630 root);
2631 walk_all_functions (convert_nl_goto_reference, NULL, root);
2632 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2634 convert_all_function_calls (root);
2635 finalize_nesting_tree (root);
2636 unnest_nesting_tree (root);
2638 free_nesting_tree (root);
2639 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2641 if (dump_file)
2643 dump_end (TDI_nested, dump_file);
2644 dump_file = NULL;
2648 #include "gt-tree-nested.h"