2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / tree-nested.c
blob042137f09ad9bc9ca690e2cf75be8b8514c4066f
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");
239 /* ??? Always make it addressable for now, since it is meant to
240 be pointed to by the static chain pointer. This pessimizes
241 when it turns out that no static chains are needed because
242 the nested functions referencing non-local variables are not
243 reachable, but the true pessimization is to create the non-
244 local frame structure in the first place. */
245 TREE_ADDRESSABLE (info->frame_decl) = 1;
247 return type;
250 /* Return true if DECL should be referenced by pointer in the non-local
251 frame structure. */
253 static bool
254 use_pointer_in_frame (tree decl)
256 if (TREE_CODE (decl) == PARM_DECL)
258 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
259 sized decls, and inefficient to copy large aggregates. Don't bother
260 moving anything but scalar variables. */
261 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
263 else
265 /* Variable sized types make things "interesting" in the frame. */
266 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
270 /* Given DECL, a non-locally accessed variable, find or create a field
271 in the non-local frame structure for the given nesting context. */
273 static tree
274 lookup_field_for_decl (struct nesting_info *info, tree decl,
275 enum insert_option insert)
277 void **slot;
279 if (insert == NO_INSERT)
281 slot = pointer_map_contains (info->field_map, decl);
282 return slot ? (tree) *slot : NULL_TREE;
285 slot = pointer_map_insert (info->field_map, decl);
286 if (!*slot)
288 tree field = make_node (FIELD_DECL);
289 DECL_NAME (field) = DECL_NAME (decl);
291 if (use_pointer_in_frame (decl))
293 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
294 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
295 DECL_NONADDRESSABLE_P (field) = 1;
297 else
299 TREE_TYPE (field) = TREE_TYPE (decl);
300 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
301 DECL_ALIGN (field) = DECL_ALIGN (decl);
302 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
303 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
304 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
305 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
308 insert_field_into_struct (get_frame_type (info), field);
309 *slot = field;
311 if (TREE_CODE (decl) == PARM_DECL)
312 info->any_parm_remapped = true;
315 return (tree) *slot;
318 /* Build or return the variable that holds the static chain within
319 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
321 static tree
322 get_chain_decl (struct nesting_info *info)
324 tree decl = info->chain_decl;
326 if (!decl)
328 tree type;
330 type = get_frame_type (info->outer);
331 type = build_pointer_type (type);
333 /* Note that this variable is *not* entered into any BIND_EXPR;
334 the construction of this variable is handled specially in
335 expand_function_start and initialize_inlined_parameters.
336 Note also that it's represented as a parameter. This is more
337 close to the truth, since the initial value does come from
338 the caller. */
339 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
340 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
341 DECL_ARTIFICIAL (decl) = 1;
342 DECL_IGNORED_P (decl) = 1;
343 TREE_USED (decl) = 1;
344 DECL_CONTEXT (decl) = info->context;
345 DECL_ARG_TYPE (decl) = type;
347 /* Tell tree-inline.c that we never write to this variable, so
348 it can copy-prop the replacement value immediately. */
349 TREE_READONLY (decl) = 1;
351 info->chain_decl = decl;
353 if (dump_file
354 && (dump_flags & TDF_DETAILS)
355 && !DECL_STATIC_CHAIN (info->context))
356 fprintf (dump_file, "Setting static-chain for %s\n",
357 lang_hooks.decl_printable_name (info->context, 2));
359 DECL_STATIC_CHAIN (info->context) = 1;
361 return decl;
364 /* Build or return the field within the non-local frame state that holds
365 the static chain for INFO->CONTEXT. This is the way to walk back up
366 multiple nesting levels. */
368 static tree
369 get_chain_field (struct nesting_info *info)
371 tree field = info->chain_field;
373 if (!field)
375 tree type = build_pointer_type (get_frame_type (info->outer));
377 field = make_node (FIELD_DECL);
378 DECL_NAME (field) = get_identifier ("__chain");
379 TREE_TYPE (field) = type;
380 DECL_ALIGN (field) = TYPE_ALIGN (type);
381 DECL_NONADDRESSABLE_P (field) = 1;
383 insert_field_into_struct (get_frame_type (info), field);
385 info->chain_field = field;
387 if (dump_file
388 && (dump_flags & TDF_DETAILS)
389 && !DECL_STATIC_CHAIN (info->context))
390 fprintf (dump_file, "Setting static-chain for %s\n",
391 lang_hooks.decl_printable_name (info->context, 2));
393 DECL_STATIC_CHAIN (info->context) = 1;
395 return field;
398 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
400 static tree
401 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
402 gimple call)
404 tree t;
406 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
407 gimple_call_set_lhs (call, t);
408 if (! gsi_end_p (*gsi))
409 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
410 gsi_insert_before (gsi, call, GSI_SAME_STMT);
412 return t;
416 /* Copy EXP into a temporary. Allocate the temporary in the context of
417 INFO and insert the initialization statement before GSI. */
419 static tree
420 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
422 tree t;
423 gimple stmt;
425 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
426 stmt = gimple_build_assign (t, exp);
427 if (! gsi_end_p (*gsi))
428 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
429 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
431 return t;
435 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
437 static tree
438 gsi_gimplify_val (struct nesting_info *info, tree exp,
439 gimple_stmt_iterator *gsi)
441 if (is_gimple_val (exp))
442 return exp;
443 else
444 return init_tmp_var (info, exp, gsi);
447 /* Similarly, but copy from the temporary and insert the statement
448 after the iterator. */
450 static tree
451 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
453 tree t;
454 gimple stmt;
456 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
457 stmt = gimple_build_assign (exp, t);
458 if (! gsi_end_p (*gsi))
459 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
460 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
462 return t;
465 /* Build or return the type used to represent a nested function trampoline. */
467 static GTY(()) tree trampoline_type;
469 static tree
470 get_trampoline_type (struct nesting_info *info)
472 unsigned align, size;
473 tree t;
475 if (trampoline_type)
476 return trampoline_type;
478 align = TRAMPOLINE_ALIGNMENT;
479 size = TRAMPOLINE_SIZE;
481 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
482 then allocate extra space so that we can do dynamic alignment. */
483 if (align > STACK_BOUNDARY)
485 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
486 align = STACK_BOUNDARY;
489 t = build_index_type (size_int (size - 1));
490 t = build_array_type (char_type_node, t);
491 t = build_decl (DECL_SOURCE_LOCATION (info->context),
492 FIELD_DECL, get_identifier ("__data"), t);
493 DECL_ALIGN (t) = align;
494 DECL_USER_ALIGN (t) = 1;
496 trampoline_type = make_node (RECORD_TYPE);
497 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
498 TYPE_FIELDS (trampoline_type) = t;
499 layout_type (trampoline_type);
500 DECL_CONTEXT (t) = trampoline_type;
502 return trampoline_type;
505 /* Given DECL, a nested function, find or create a field in the non-local
506 frame structure for a trampoline for this function. */
508 static tree
509 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
510 enum insert_option insert)
512 void **slot;
514 if (insert == NO_INSERT)
516 slot = pointer_map_contains (info->var_map, decl);
517 return slot ? (tree) *slot : NULL_TREE;
520 slot = pointer_map_insert (info->var_map, decl);
521 if (!*slot)
523 tree field = make_node (FIELD_DECL);
524 DECL_NAME (field) = DECL_NAME (decl);
525 TREE_TYPE (field) = get_trampoline_type (info);
526 TREE_ADDRESSABLE (field) = 1;
528 insert_field_into_struct (get_frame_type (info), field);
529 *slot = field;
531 info->any_tramp_created = true;
534 return (tree) *slot;
537 /* Build or return the field within the non-local frame state that holds
538 the non-local goto "jmp_buf". The buffer itself is maintained by the
539 rtl middle-end as dynamic stack space is allocated. */
541 static tree
542 get_nl_goto_field (struct nesting_info *info)
544 tree field = info->nl_goto_field;
545 if (!field)
547 unsigned size;
548 tree type;
550 /* For __builtin_nonlocal_goto, we need N words. The first is the
551 frame pointer, the rest is for the target's stack pointer save
552 area. The number of words is controlled by STACK_SAVEAREA_MODE;
553 not the best interface, but it'll do for now. */
554 if (Pmode == ptr_mode)
555 type = ptr_type_node;
556 else
557 type = lang_hooks.types.type_for_mode (Pmode, 1);
559 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
560 size = size / GET_MODE_SIZE (Pmode);
561 size = size + 1;
563 type = build_array_type
564 (type, build_index_type (size_int (size)));
566 field = make_node (FIELD_DECL);
567 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
568 TREE_TYPE (field) = type;
569 DECL_ALIGN (field) = TYPE_ALIGN (type);
570 TREE_ADDRESSABLE (field) = 1;
572 insert_field_into_struct (get_frame_type (info), field);
574 info->nl_goto_field = field;
577 return field;
580 /* Invoke CALLBACK on all statements of GIMPLE sequence SEQ. */
582 static void
583 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
584 struct nesting_info *info, gimple_seq seq)
586 struct walk_stmt_info wi;
588 memset (&wi, 0, sizeof (wi));
589 wi.info = info;
590 wi.val_only = true;
591 walk_gimple_seq (seq, callback_stmt, callback_op, &wi);
595 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
597 static inline void
598 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
599 struct nesting_info *info)
601 walk_body (callback_stmt, callback_op, info, gimple_body (info->context));
604 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
606 static void
607 walk_gimple_omp_for (gimple for_stmt,
608 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
609 struct nesting_info *info)
611 struct walk_stmt_info wi;
612 gimple_seq seq;
613 tree t;
614 size_t i;
616 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body (for_stmt));
618 seq = gimple_seq_alloc ();
619 memset (&wi, 0, sizeof (wi));
620 wi.info = info;
621 wi.gsi = gsi_last (seq);
623 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
625 wi.val_only = false;
626 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
627 &wi, NULL);
628 wi.val_only = true;
629 wi.is_lhs = false;
630 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
631 &wi, NULL);
633 wi.val_only = true;
634 wi.is_lhs = false;
635 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
636 &wi, NULL);
638 t = gimple_omp_for_incr (for_stmt, i);
639 gcc_assert (BINARY_CLASS_P (t));
640 wi.val_only = false;
641 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
642 wi.val_only = true;
643 wi.is_lhs = false;
644 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
647 if (gimple_seq_empty_p (seq))
648 gimple_seq_free (seq);
649 else
651 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
652 annotate_all_with_location (seq, gimple_location (for_stmt));
653 gimple_seq_add_seq (&pre_body, seq);
654 gimple_omp_for_set_pre_body (for_stmt, pre_body);
658 /* Similarly for ROOT and all functions nested underneath, depth first. */
660 static void
661 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
662 struct nesting_info *root)
664 struct nesting_info *n;
665 FOR_EACH_NEST_INFO (n, root)
666 walk_function (callback_stmt, callback_op, n);
670 /* We have to check for a fairly pathological case. The operands of function
671 nested function are to be interpreted in the context of the enclosing
672 function. So if any are variably-sized, they will get remapped when the
673 enclosing function is inlined. But that remapping would also have to be
674 done in the types of the PARM_DECLs of the nested function, meaning the
675 argument types of that function will disagree with the arguments in the
676 calls to that function. So we'd either have to make a copy of the nested
677 function corresponding to each time the enclosing function was inlined or
678 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
679 function. The former is not practical. The latter would still require
680 detecting this case to know when to add the conversions. So, for now at
681 least, we don't inline such an enclosing function.
683 We have to do that check recursively, so here return indicating whether
684 FNDECL has such a nested function. ORIG_FN is the function we were
685 trying to inline to use for checking whether any argument is variably
686 modified by anything in it.
688 It would be better to do this in tree-inline.c so that we could give
689 the appropriate warning for why a function can't be inlined, but that's
690 too late since the nesting structure has already been flattened and
691 adding a flag just to record this fact seems a waste of a flag. */
693 static bool
694 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
696 struct cgraph_node *cgn = cgraph_get_node (fndecl);
697 tree arg;
699 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
701 for (arg = DECL_ARGUMENTS (cgn->symbol.decl); arg; arg = DECL_CHAIN (arg))
702 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
703 return true;
705 if (check_for_nested_with_variably_modified (cgn->symbol.decl,
706 orig_fndecl))
707 return true;
710 return false;
713 /* Construct our local datastructure describing the function nesting
714 tree rooted by CGN. */
716 static struct nesting_info *
717 create_nesting_tree (struct cgraph_node *cgn)
719 struct nesting_info *info = XCNEW (struct nesting_info);
720 info->field_map = pointer_map_create ();
721 info->var_map = pointer_map_create ();
722 info->mem_refs = pointer_set_create ();
723 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
724 info->context = cgn->symbol.decl;
726 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
728 struct nesting_info *sub = create_nesting_tree (cgn);
729 sub->outer = info;
730 sub->next = info->inner;
731 info->inner = sub;
734 /* See discussion at check_for_nested_with_variably_modified for a
735 discussion of why this has to be here. */
736 if (check_for_nested_with_variably_modified (info->context, info->context))
737 DECL_UNINLINABLE (info->context) = true;
739 return info;
742 /* Return an expression computing the static chain for TARGET_CONTEXT
743 from INFO->CONTEXT. Insert any necessary computations before TSI. */
745 static tree
746 get_static_chain (struct nesting_info *info, tree target_context,
747 gimple_stmt_iterator *gsi)
749 struct nesting_info *i;
750 tree x;
752 if (info->context == target_context)
754 x = build_addr (info->frame_decl, target_context);
756 else
758 x = get_chain_decl (info);
760 for (i = info->outer; i->context != target_context; i = i->outer)
762 tree field = get_chain_field (i);
764 x = build_simple_mem_ref (x);
765 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
766 x = init_tmp_var (info, x, gsi);
770 return x;
774 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
775 frame as seen from INFO->CONTEXT. Insert any necessary computations
776 before GSI. */
778 static tree
779 get_frame_field (struct nesting_info *info, tree target_context,
780 tree field, gimple_stmt_iterator *gsi)
782 struct nesting_info *i;
783 tree x;
785 if (info->context == target_context)
787 /* Make sure frame_decl gets created. */
788 (void) get_frame_type (info);
789 x = info->frame_decl;
791 else
793 x = get_chain_decl (info);
795 for (i = info->outer; i->context != target_context; i = i->outer)
797 tree field = get_chain_field (i);
799 x = build_simple_mem_ref (x);
800 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
801 x = init_tmp_var (info, x, gsi);
804 x = build_simple_mem_ref (x);
807 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
808 return x;
811 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
813 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
814 in the nested function with DECL_VALUE_EXPR set to reference the true
815 variable in the parent function. This is used both for debug info
816 and in OpenMP lowering. */
818 static tree
819 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
821 tree target_context;
822 struct nesting_info *i;
823 tree x, field, new_decl;
824 void **slot;
826 slot = pointer_map_insert (info->var_map, decl);
828 if (*slot)
829 return (tree) *slot;
831 target_context = decl_function_context (decl);
833 /* A copy of the code in get_frame_field, but without the temporaries. */
834 if (info->context == target_context)
836 /* Make sure frame_decl gets created. */
837 (void) get_frame_type (info);
838 x = info->frame_decl;
839 i = info;
841 else
843 x = get_chain_decl (info);
844 for (i = info->outer; i->context != target_context; i = i->outer)
846 field = get_chain_field (i);
847 x = build_simple_mem_ref (x);
848 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
850 x = build_simple_mem_ref (x);
853 field = lookup_field_for_decl (i, decl, INSERT);
854 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
855 if (use_pointer_in_frame (decl))
856 x = build_simple_mem_ref (x);
858 /* ??? We should be remapping types as well, surely. */
859 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
860 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
861 DECL_CONTEXT (new_decl) = info->context;
862 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
863 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
864 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
865 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
866 TREE_READONLY (new_decl) = TREE_READONLY (decl);
867 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
868 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
869 if ((TREE_CODE (decl) == PARM_DECL
870 || TREE_CODE (decl) == RESULT_DECL
871 || TREE_CODE (decl) == VAR_DECL)
872 && DECL_BY_REFERENCE (decl))
873 DECL_BY_REFERENCE (new_decl) = 1;
875 SET_DECL_VALUE_EXPR (new_decl, x);
876 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
878 *slot = new_decl;
879 DECL_CHAIN (new_decl) = info->debug_var_chain;
880 info->debug_var_chain = new_decl;
882 if (!optimize
883 && info->context != target_context
884 && variably_modified_type_p (TREE_TYPE (decl), NULL))
885 note_nonlocal_vla_type (info, TREE_TYPE (decl));
887 return new_decl;
891 /* Callback for walk_gimple_stmt, rewrite all references to VAR
892 and PARM_DECLs that belong to outer functions.
894 The rewrite will involve some number of structure accesses back up
895 the static chain. E.g. for a variable FOO up one nesting level it'll
896 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
897 indirections apply to decls for which use_pointer_in_frame is true. */
899 static tree
900 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
902 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
903 struct nesting_info *const info = (struct nesting_info *) wi->info;
904 tree t = *tp;
906 *walk_subtrees = 0;
907 switch (TREE_CODE (t))
909 case VAR_DECL:
910 /* Non-automatic variables are never processed. */
911 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
912 break;
913 /* FALLTHRU */
915 case PARM_DECL:
916 if (decl_function_context (t) != info->context)
918 tree x;
919 wi->changed = true;
921 x = get_nonlocal_debug_decl (info, t);
922 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
924 tree target_context = decl_function_context (t);
925 struct nesting_info *i;
926 for (i = info->outer; i->context != target_context; i = i->outer)
927 continue;
928 x = lookup_field_for_decl (i, t, INSERT);
929 x = get_frame_field (info, target_context, x, &wi->gsi);
930 if (use_pointer_in_frame (t))
932 x = init_tmp_var (info, x, &wi->gsi);
933 x = build_simple_mem_ref (x);
937 if (wi->val_only)
939 if (wi->is_lhs)
940 x = save_tmp_var (info, x, &wi->gsi);
941 else
942 x = init_tmp_var (info, x, &wi->gsi);
945 *tp = x;
947 break;
949 case LABEL_DECL:
950 /* We're taking the address of a label from a parent function, but
951 this is not itself a non-local goto. Mark the label such that it
952 will not be deleted, much as we would with a label address in
953 static storage. */
954 if (decl_function_context (t) != info->context)
955 FORCED_LABEL (t) = 1;
956 break;
958 case ADDR_EXPR:
960 bool save_val_only = wi->val_only;
962 wi->val_only = false;
963 wi->is_lhs = false;
964 wi->changed = false;
965 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
966 wi->val_only = true;
968 if (wi->changed)
970 tree save_context;
972 /* If we changed anything, we might no longer be directly
973 referencing a decl. */
974 save_context = current_function_decl;
975 current_function_decl = info->context;
976 recompute_tree_invariant_for_addr_expr (t);
977 current_function_decl = save_context;
979 /* If the callback converted the address argument in a context
980 where we only accept variables (and min_invariant, presumably),
981 then compute the address into a temporary. */
982 if (save_val_only)
983 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
984 t, &wi->gsi);
987 break;
989 case REALPART_EXPR:
990 case IMAGPART_EXPR:
991 case COMPONENT_REF:
992 case ARRAY_REF:
993 case ARRAY_RANGE_REF:
994 case BIT_FIELD_REF:
995 /* Go down this entire nest and just look at the final prefix and
996 anything that describes the references. Otherwise, we lose track
997 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
998 wi->val_only = true;
999 wi->is_lhs = false;
1000 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1002 if (TREE_CODE (t) == COMPONENT_REF)
1003 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1004 NULL);
1005 else if (TREE_CODE (t) == ARRAY_REF
1006 || TREE_CODE (t) == ARRAY_RANGE_REF)
1008 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1009 wi, NULL);
1010 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1011 wi, NULL);
1012 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1013 wi, NULL);
1015 else if (TREE_CODE (t) == BIT_FIELD_REF)
1017 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1018 wi, NULL);
1019 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1020 wi, NULL);
1023 wi->val_only = false;
1024 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1025 break;
1027 case VIEW_CONVERT_EXPR:
1028 /* Just request to look at the subtrees, leaving val_only and lhs
1029 untouched. This might actually be for !val_only + lhs, in which
1030 case we don't want to force a replacement by a temporary. */
1031 *walk_subtrees = 1;
1032 break;
1034 default:
1035 if (!IS_TYPE_OR_DECL_P (t))
1037 *walk_subtrees = 1;
1038 wi->val_only = true;
1039 wi->is_lhs = false;
1041 break;
1044 return NULL_TREE;
1047 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1048 struct walk_stmt_info *);
1050 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1051 and PARM_DECLs that belong to outer functions. */
1053 static bool
1054 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1056 struct nesting_info *const info = (struct nesting_info *) wi->info;
1057 bool need_chain = false, need_stmts = false;
1058 tree clause, decl;
1059 int dummy;
1060 bitmap new_suppress;
1062 new_suppress = BITMAP_GGC_ALLOC ();
1063 bitmap_copy (new_suppress, info->suppress_expansion);
1065 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1067 switch (OMP_CLAUSE_CODE (clause))
1069 case OMP_CLAUSE_REDUCTION:
1070 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1071 need_stmts = true;
1072 goto do_decl_clause;
1074 case OMP_CLAUSE_LASTPRIVATE:
1075 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1076 need_stmts = true;
1077 goto do_decl_clause;
1079 case OMP_CLAUSE_PRIVATE:
1080 case OMP_CLAUSE_FIRSTPRIVATE:
1081 case OMP_CLAUSE_COPYPRIVATE:
1082 case OMP_CLAUSE_SHARED:
1083 do_decl_clause:
1084 decl = OMP_CLAUSE_DECL (clause);
1085 if (TREE_CODE (decl) == VAR_DECL
1086 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1087 break;
1088 if (decl_function_context (decl) != info->context)
1090 bitmap_set_bit (new_suppress, DECL_UID (decl));
1091 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1092 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1093 need_chain = true;
1095 break;
1097 case OMP_CLAUSE_SCHEDULE:
1098 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1099 break;
1100 /* FALLTHRU */
1101 case OMP_CLAUSE_FINAL:
1102 case OMP_CLAUSE_IF:
1103 case OMP_CLAUSE_NUM_THREADS:
1104 wi->val_only = true;
1105 wi->is_lhs = false;
1106 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1107 &dummy, wi);
1108 break;
1110 case OMP_CLAUSE_NOWAIT:
1111 case OMP_CLAUSE_ORDERED:
1112 case OMP_CLAUSE_DEFAULT:
1113 case OMP_CLAUSE_COPYIN:
1114 case OMP_CLAUSE_COLLAPSE:
1115 case OMP_CLAUSE_UNTIED:
1116 case OMP_CLAUSE_MERGEABLE:
1117 break;
1119 default:
1120 gcc_unreachable ();
1124 info->suppress_expansion = new_suppress;
1126 if (need_stmts)
1127 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1128 switch (OMP_CLAUSE_CODE (clause))
1130 case OMP_CLAUSE_REDUCTION:
1131 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1133 tree old_context
1134 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1135 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1136 = info->context;
1137 walk_body (convert_nonlocal_reference_stmt,
1138 convert_nonlocal_reference_op, info,
1139 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1140 walk_body (convert_nonlocal_reference_stmt,
1141 convert_nonlocal_reference_op, info,
1142 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1143 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1144 = old_context;
1146 break;
1148 case OMP_CLAUSE_LASTPRIVATE:
1149 walk_body (convert_nonlocal_reference_stmt,
1150 convert_nonlocal_reference_op, info,
1151 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1152 break;
1154 default:
1155 break;
1158 return need_chain;
1161 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1163 static void
1164 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1166 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1167 type = TREE_TYPE (type);
1169 if (TYPE_NAME (type)
1170 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1171 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1172 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1174 while (POINTER_TYPE_P (type)
1175 || TREE_CODE (type) == VECTOR_TYPE
1176 || TREE_CODE (type) == FUNCTION_TYPE
1177 || TREE_CODE (type) == METHOD_TYPE)
1178 type = TREE_TYPE (type);
1180 if (TREE_CODE (type) == ARRAY_TYPE)
1182 tree domain, t;
1184 note_nonlocal_vla_type (info, TREE_TYPE (type));
1185 domain = TYPE_DOMAIN (type);
1186 if (domain)
1188 t = TYPE_MIN_VALUE (domain);
1189 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1190 && decl_function_context (t) != info->context)
1191 get_nonlocal_debug_decl (info, t);
1192 t = TYPE_MAX_VALUE (domain);
1193 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1194 && decl_function_context (t) != info->context)
1195 get_nonlocal_debug_decl (info, t);
1200 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1201 in BLOCK. */
1203 static void
1204 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1206 tree var;
1208 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1209 if (TREE_CODE (var) == VAR_DECL
1210 && variably_modified_type_p (TREE_TYPE (var), NULL)
1211 && DECL_HAS_VALUE_EXPR_P (var)
1212 && decl_function_context (var) != info->context)
1213 note_nonlocal_vla_type (info, TREE_TYPE (var));
1216 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1217 PARM_DECLs that belong to outer functions. This handles statements
1218 that are not handled via the standard recursion done in
1219 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1220 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1221 operands of STMT have been handled by this function. */
1223 static tree
1224 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1225 struct walk_stmt_info *wi)
1227 struct nesting_info *info = (struct nesting_info *) wi->info;
1228 tree save_local_var_chain;
1229 bitmap save_suppress;
1230 gimple stmt = gsi_stmt (*gsi);
1232 switch (gimple_code (stmt))
1234 case GIMPLE_GOTO:
1235 /* Don't walk non-local gotos for now. */
1236 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1238 wi->val_only = true;
1239 wi->is_lhs = false;
1240 *handled_ops_p = true;
1241 return NULL_TREE;
1243 break;
1245 case GIMPLE_OMP_PARALLEL:
1246 case GIMPLE_OMP_TASK:
1247 save_suppress = info->suppress_expansion;
1248 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1249 wi))
1251 tree c, decl;
1252 decl = get_chain_decl (info);
1253 c = build_omp_clause (gimple_location (stmt),
1254 OMP_CLAUSE_FIRSTPRIVATE);
1255 OMP_CLAUSE_DECL (c) = decl;
1256 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1257 gimple_omp_taskreg_set_clauses (stmt, c);
1260 save_local_var_chain = info->new_local_var_chain;
1261 info->new_local_var_chain = NULL;
1263 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1264 info, gimple_omp_body (stmt));
1266 if (info->new_local_var_chain)
1267 declare_vars (info->new_local_var_chain,
1268 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1269 false);
1270 info->new_local_var_chain = save_local_var_chain;
1271 info->suppress_expansion = save_suppress;
1272 break;
1274 case GIMPLE_OMP_FOR:
1275 save_suppress = info->suppress_expansion;
1276 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1277 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1278 convert_nonlocal_reference_op, info);
1279 walk_body (convert_nonlocal_reference_stmt,
1280 convert_nonlocal_reference_op, info, gimple_omp_body (stmt));
1281 info->suppress_expansion = save_suppress;
1282 break;
1284 case GIMPLE_OMP_SECTIONS:
1285 save_suppress = info->suppress_expansion;
1286 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1287 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1288 info, gimple_omp_body (stmt));
1289 info->suppress_expansion = save_suppress;
1290 break;
1292 case GIMPLE_OMP_SINGLE:
1293 save_suppress = info->suppress_expansion;
1294 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1295 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1296 info, gimple_omp_body (stmt));
1297 info->suppress_expansion = save_suppress;
1298 break;
1300 case GIMPLE_OMP_SECTION:
1301 case GIMPLE_OMP_MASTER:
1302 case GIMPLE_OMP_ORDERED:
1303 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1304 info, gimple_omp_body (stmt));
1305 break;
1307 case GIMPLE_BIND:
1308 if (!optimize && gimple_bind_block (stmt))
1309 note_nonlocal_block_vlas (info, gimple_bind_block (stmt));
1311 *handled_ops_p = false;
1312 return NULL_TREE;
1314 case GIMPLE_COND:
1315 wi->val_only = true;
1316 wi->is_lhs = false;
1317 *handled_ops_p = false;
1318 return NULL_TREE;
1320 default:
1321 /* For every other statement that we are not interested in
1322 handling here, let the walker traverse the operands. */
1323 *handled_ops_p = false;
1324 return NULL_TREE;
1327 /* We have handled all of STMT operands, no need to traverse the operands. */
1328 *handled_ops_p = true;
1329 return NULL_TREE;
1333 /* A subroutine of convert_local_reference. Create a local variable
1334 in the parent function with DECL_VALUE_EXPR set to reference the
1335 field in FRAME. This is used both for debug info and in OpenMP
1336 lowering. */
1338 static tree
1339 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1341 tree x, new_decl;
1342 void **slot;
1344 slot = pointer_map_insert (info->var_map, decl);
1345 if (*slot)
1346 return (tree) *slot;
1348 /* Make sure frame_decl gets created. */
1349 (void) get_frame_type (info);
1350 x = info->frame_decl;
1351 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1353 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1354 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1355 DECL_CONTEXT (new_decl) = info->context;
1356 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1357 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1358 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1359 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1360 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1361 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1362 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1363 if ((TREE_CODE (decl) == PARM_DECL
1364 || TREE_CODE (decl) == RESULT_DECL
1365 || TREE_CODE (decl) == VAR_DECL)
1366 && DECL_BY_REFERENCE (decl))
1367 DECL_BY_REFERENCE (new_decl) = 1;
1369 SET_DECL_VALUE_EXPR (new_decl, x);
1370 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1371 *slot = new_decl;
1373 DECL_CHAIN (new_decl) = info->debug_var_chain;
1374 info->debug_var_chain = new_decl;
1376 /* Do not emit debug info twice. */
1377 DECL_IGNORED_P (decl) = 1;
1379 return new_decl;
1383 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1384 and PARM_DECLs that were referenced by inner nested functions.
1385 The rewrite will be a structure reference to the local frame variable. */
1387 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1389 static tree
1390 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1392 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1393 struct nesting_info *const info = (struct nesting_info *) wi->info;
1394 tree t = *tp, field, x;
1395 bool save_val_only;
1397 *walk_subtrees = 0;
1398 switch (TREE_CODE (t))
1400 case VAR_DECL:
1401 /* Non-automatic variables are never processed. */
1402 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1403 break;
1404 /* FALLTHRU */
1406 case PARM_DECL:
1407 if (decl_function_context (t) == info->context)
1409 /* If we copied a pointer to the frame, then the original decl
1410 is used unchanged in the parent function. */
1411 if (use_pointer_in_frame (t))
1412 break;
1414 /* No need to transform anything if no child references the
1415 variable. */
1416 field = lookup_field_for_decl (info, t, NO_INSERT);
1417 if (!field)
1418 break;
1419 wi->changed = true;
1421 x = get_local_debug_decl (info, t, field);
1422 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1423 x = get_frame_field (info, info->context, field, &wi->gsi);
1425 if (wi->val_only)
1427 if (wi->is_lhs)
1428 x = save_tmp_var (info, x, &wi->gsi);
1429 else
1430 x = init_tmp_var (info, x, &wi->gsi);
1433 *tp = x;
1435 break;
1437 case ADDR_EXPR:
1438 save_val_only = wi->val_only;
1439 wi->val_only = false;
1440 wi->is_lhs = false;
1441 wi->changed = false;
1442 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1443 wi->val_only = save_val_only;
1445 /* If we converted anything ... */
1446 if (wi->changed)
1448 tree save_context;
1450 /* Then the frame decl is now addressable. */
1451 TREE_ADDRESSABLE (info->frame_decl) = 1;
1453 save_context = current_function_decl;
1454 current_function_decl = info->context;
1455 recompute_tree_invariant_for_addr_expr (t);
1456 current_function_decl = save_context;
1458 /* If we are in a context where we only accept values, then
1459 compute the address into a temporary. */
1460 if (save_val_only)
1461 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1462 t, &wi->gsi);
1464 break;
1466 case REALPART_EXPR:
1467 case IMAGPART_EXPR:
1468 case COMPONENT_REF:
1469 case ARRAY_REF:
1470 case ARRAY_RANGE_REF:
1471 case BIT_FIELD_REF:
1472 /* Go down this entire nest and just look at the final prefix and
1473 anything that describes the references. Otherwise, we lose track
1474 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1475 save_val_only = wi->val_only;
1476 wi->val_only = true;
1477 wi->is_lhs = false;
1478 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1480 if (TREE_CODE (t) == COMPONENT_REF)
1481 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1482 NULL);
1483 else if (TREE_CODE (t) == ARRAY_REF
1484 || TREE_CODE (t) == ARRAY_RANGE_REF)
1486 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1487 NULL);
1488 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1489 NULL);
1490 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1491 NULL);
1493 else if (TREE_CODE (t) == BIT_FIELD_REF)
1495 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1496 NULL);
1497 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1498 NULL);
1501 wi->val_only = false;
1502 walk_tree (tp, convert_local_reference_op, wi, NULL);
1503 wi->val_only = save_val_only;
1504 break;
1506 case MEM_REF:
1507 save_val_only = wi->val_only;
1508 wi->val_only = true;
1509 wi->is_lhs = false;
1510 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1511 wi, NULL);
1512 /* We need to re-fold the MEM_REF as component references as
1513 part of a ADDR_EXPR address are not allowed. But we cannot
1514 fold here, as the chain record type is not yet finalized. */
1515 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1516 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1517 pointer_set_insert (info->mem_refs, tp);
1518 wi->val_only = save_val_only;
1519 break;
1521 case VIEW_CONVERT_EXPR:
1522 /* Just request to look at the subtrees, leaving val_only and lhs
1523 untouched. This might actually be for !val_only + lhs, in which
1524 case we don't want to force a replacement by a temporary. */
1525 *walk_subtrees = 1;
1526 break;
1528 default:
1529 if (!IS_TYPE_OR_DECL_P (t))
1531 *walk_subtrees = 1;
1532 wi->val_only = true;
1533 wi->is_lhs = false;
1535 break;
1538 return NULL_TREE;
1541 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1542 struct walk_stmt_info *);
1544 /* Helper for convert_local_reference. Convert all the references in
1545 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1547 static bool
1548 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1550 struct nesting_info *const info = (struct nesting_info *) wi->info;
1551 bool need_frame = false, need_stmts = false;
1552 tree clause, decl;
1553 int dummy;
1554 bitmap new_suppress;
1556 new_suppress = BITMAP_GGC_ALLOC ();
1557 bitmap_copy (new_suppress, info->suppress_expansion);
1559 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1561 switch (OMP_CLAUSE_CODE (clause))
1563 case OMP_CLAUSE_REDUCTION:
1564 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1565 need_stmts = true;
1566 goto do_decl_clause;
1568 case OMP_CLAUSE_LASTPRIVATE:
1569 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1570 need_stmts = true;
1571 goto do_decl_clause;
1573 case OMP_CLAUSE_PRIVATE:
1574 case OMP_CLAUSE_FIRSTPRIVATE:
1575 case OMP_CLAUSE_COPYPRIVATE:
1576 case OMP_CLAUSE_SHARED:
1577 do_decl_clause:
1578 decl = OMP_CLAUSE_DECL (clause);
1579 if (TREE_CODE (decl) == VAR_DECL
1580 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1581 break;
1582 if (decl_function_context (decl) == info->context
1583 && !use_pointer_in_frame (decl))
1585 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1586 if (field)
1588 bitmap_set_bit (new_suppress, DECL_UID (decl));
1589 OMP_CLAUSE_DECL (clause)
1590 = get_local_debug_decl (info, decl, field);
1591 need_frame = true;
1594 break;
1596 case OMP_CLAUSE_SCHEDULE:
1597 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1598 break;
1599 /* FALLTHRU */
1600 case OMP_CLAUSE_FINAL:
1601 case OMP_CLAUSE_IF:
1602 case OMP_CLAUSE_NUM_THREADS:
1603 wi->val_only = true;
1604 wi->is_lhs = false;
1605 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1606 wi);
1607 break;
1609 case OMP_CLAUSE_NOWAIT:
1610 case OMP_CLAUSE_ORDERED:
1611 case OMP_CLAUSE_DEFAULT:
1612 case OMP_CLAUSE_COPYIN:
1613 case OMP_CLAUSE_COLLAPSE:
1614 case OMP_CLAUSE_UNTIED:
1615 case OMP_CLAUSE_MERGEABLE:
1616 break;
1618 default:
1619 gcc_unreachable ();
1623 info->suppress_expansion = new_suppress;
1625 if (need_stmts)
1626 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1627 switch (OMP_CLAUSE_CODE (clause))
1629 case OMP_CLAUSE_REDUCTION:
1630 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1632 tree old_context
1633 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1634 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1635 = info->context;
1636 walk_body (convert_local_reference_stmt,
1637 convert_local_reference_op, info,
1638 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1639 walk_body (convert_local_reference_stmt,
1640 convert_local_reference_op, info,
1641 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1642 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1643 = old_context;
1645 break;
1647 case OMP_CLAUSE_LASTPRIVATE:
1648 walk_body (convert_local_reference_stmt,
1649 convert_local_reference_op, info,
1650 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1651 break;
1653 default:
1654 break;
1657 return need_frame;
1661 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1662 and PARM_DECLs that were referenced by inner nested functions.
1663 The rewrite will be a structure reference to the local frame variable. */
1665 static tree
1666 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1667 struct walk_stmt_info *wi)
1669 struct nesting_info *info = (struct nesting_info *) wi->info;
1670 tree save_local_var_chain;
1671 bitmap save_suppress;
1672 gimple stmt = gsi_stmt (*gsi);
1674 switch (gimple_code (stmt))
1676 case GIMPLE_OMP_PARALLEL:
1677 case GIMPLE_OMP_TASK:
1678 save_suppress = info->suppress_expansion;
1679 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1680 wi))
1682 tree c;
1683 (void) get_frame_type (info);
1684 c = build_omp_clause (gimple_location (stmt),
1685 OMP_CLAUSE_SHARED);
1686 OMP_CLAUSE_DECL (c) = info->frame_decl;
1687 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1688 gimple_omp_taskreg_set_clauses (stmt, c);
1691 save_local_var_chain = info->new_local_var_chain;
1692 info->new_local_var_chain = NULL;
1694 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1695 gimple_omp_body (stmt));
1697 if (info->new_local_var_chain)
1698 declare_vars (info->new_local_var_chain,
1699 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1700 info->new_local_var_chain = save_local_var_chain;
1701 info->suppress_expansion = save_suppress;
1702 break;
1704 case GIMPLE_OMP_FOR:
1705 save_suppress = info->suppress_expansion;
1706 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1707 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1708 convert_local_reference_op, info);
1709 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1710 info, gimple_omp_body (stmt));
1711 info->suppress_expansion = save_suppress;
1712 break;
1714 case GIMPLE_OMP_SECTIONS:
1715 save_suppress = info->suppress_expansion;
1716 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1717 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1718 info, gimple_omp_body (stmt));
1719 info->suppress_expansion = save_suppress;
1720 break;
1722 case GIMPLE_OMP_SINGLE:
1723 save_suppress = info->suppress_expansion;
1724 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1725 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1726 info, gimple_omp_body (stmt));
1727 info->suppress_expansion = save_suppress;
1728 break;
1730 case GIMPLE_OMP_SECTION:
1731 case GIMPLE_OMP_MASTER:
1732 case GIMPLE_OMP_ORDERED:
1733 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1734 info, gimple_omp_body (stmt));
1735 break;
1737 case GIMPLE_COND:
1738 wi->val_only = true;
1739 wi->is_lhs = false;
1740 *handled_ops_p = false;
1741 return NULL_TREE;
1743 default:
1744 /* For every other statement that we are not interested in
1745 handling here, let the walker traverse the operands. */
1746 *handled_ops_p = false;
1747 return NULL_TREE;
1750 /* Indicate that we have handled all the operands ourselves. */
1751 *handled_ops_p = true;
1752 return NULL_TREE;
1756 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1757 that reference labels from outer functions. The rewrite will be a
1758 call to __builtin_nonlocal_goto. */
1760 static tree
1761 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1762 struct walk_stmt_info *wi)
1764 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1765 tree label, new_label, target_context, x, field;
1766 void **slot;
1767 gimple call;
1768 gimple stmt = gsi_stmt (*gsi);
1770 if (gimple_code (stmt) != GIMPLE_GOTO)
1772 *handled_ops_p = false;
1773 return NULL_TREE;
1776 label = gimple_goto_dest (stmt);
1777 if (TREE_CODE (label) != LABEL_DECL)
1779 *handled_ops_p = false;
1780 return NULL_TREE;
1783 target_context = decl_function_context (label);
1784 if (target_context == info->context)
1786 *handled_ops_p = false;
1787 return NULL_TREE;
1790 for (i = info->outer; target_context != i->context; i = i->outer)
1791 continue;
1793 /* The original user label may also be use for a normal goto, therefore
1794 we must create a new label that will actually receive the abnormal
1795 control transfer. This new label will be marked LABEL_NONLOCAL; this
1796 mark will trigger proper behavior in the cfg, as well as cause the
1797 (hairy target-specific) non-local goto receiver code to be generated
1798 when we expand rtl. Enter this association into var_map so that we
1799 can insert the new label into the IL during a second pass. */
1800 slot = pointer_map_insert (i->var_map, label);
1801 if (*slot == NULL)
1803 new_label = create_artificial_label (UNKNOWN_LOCATION);
1804 DECL_NONLOCAL (new_label) = 1;
1805 *slot = new_label;
1807 else
1808 new_label = (tree) *slot;
1810 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1811 field = get_nl_goto_field (i);
1812 x = get_frame_field (info, target_context, field, &wi->gsi);
1813 x = build_addr (x, target_context);
1814 x = gsi_gimplify_val (info, x, &wi->gsi);
1815 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
1816 2, build_addr (new_label, target_context), x);
1817 gsi_replace (&wi->gsi, call, false);
1819 /* We have handled all of STMT's operands, no need to keep going. */
1820 *handled_ops_p = true;
1821 return NULL_TREE;
1825 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1826 are referenced via nonlocal goto from a nested function. The rewrite
1827 will involve installing a newly generated DECL_NONLOCAL label, and
1828 (potentially) a branch around the rtl gunk that is assumed to be
1829 attached to such a label. */
1831 static tree
1832 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1833 struct walk_stmt_info *wi)
1835 struct nesting_info *const info = (struct nesting_info *) wi->info;
1836 tree label, new_label;
1837 gimple_stmt_iterator tmp_gsi;
1838 void **slot;
1839 gimple stmt = gsi_stmt (*gsi);
1841 if (gimple_code (stmt) != GIMPLE_LABEL)
1843 *handled_ops_p = false;
1844 return NULL_TREE;
1847 label = gimple_label_label (stmt);
1849 slot = pointer_map_contains (info->var_map, label);
1850 if (!slot)
1852 *handled_ops_p = false;
1853 return NULL_TREE;
1856 /* If there's any possibility that the previous statement falls through,
1857 then we must branch around the new non-local label. */
1858 tmp_gsi = wi->gsi;
1859 gsi_prev (&tmp_gsi);
1860 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
1862 gimple stmt = gimple_build_goto (label);
1863 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1866 new_label = (tree) *slot;
1867 stmt = gimple_build_label (new_label);
1868 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1870 *handled_ops_p = true;
1871 return NULL_TREE;
1875 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1876 of nested functions that require the use of trampolines. The rewrite
1877 will involve a reference a trampoline generated for the occasion. */
1879 static tree
1880 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
1882 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1883 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1884 tree t = *tp, decl, target_context, x, builtin;
1885 gimple call;
1887 *walk_subtrees = 0;
1888 switch (TREE_CODE (t))
1890 case ADDR_EXPR:
1891 /* Build
1892 T.1 = &CHAIN->tramp;
1893 T.2 = __builtin_adjust_trampoline (T.1);
1894 T.3 = (func_type)T.2;
1897 decl = TREE_OPERAND (t, 0);
1898 if (TREE_CODE (decl) != FUNCTION_DECL)
1899 break;
1901 /* Only need to process nested functions. */
1902 target_context = decl_function_context (decl);
1903 if (!target_context)
1904 break;
1906 /* If the nested function doesn't use a static chain, then
1907 it doesn't need a trampoline. */
1908 if (!DECL_STATIC_CHAIN (decl))
1909 break;
1911 /* If we don't want a trampoline, then don't build one. */
1912 if (TREE_NO_TRAMPOLINE (t))
1913 break;
1915 /* Lookup the immediate parent of the callee, as that's where
1916 we need to insert the trampoline. */
1917 for (i = info; i->context != target_context; i = i->outer)
1918 continue;
1919 x = lookup_tramp_for_decl (i, decl, INSERT);
1921 /* Compute the address of the field holding the trampoline. */
1922 x = get_frame_field (info, target_context, x, &wi->gsi);
1923 x = build_addr (x, target_context);
1924 x = gsi_gimplify_val (info, x, &wi->gsi);
1926 /* Do machine-specific ugliness. Normally this will involve
1927 computing extra alignment, but it can really be anything. */
1928 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
1929 call = gimple_build_call (builtin, 1, x);
1930 x = init_tmp_var_with_call (info, &wi->gsi, call);
1932 /* Cast back to the proper function type. */
1933 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1934 x = init_tmp_var (info, x, &wi->gsi);
1936 *tp = x;
1937 break;
1939 default:
1940 if (!IS_TYPE_OR_DECL_P (t))
1941 *walk_subtrees = 1;
1942 break;
1945 return NULL_TREE;
1949 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1950 to addresses of nested functions that require the use of
1951 trampolines. The rewrite will involve a reference a trampoline
1952 generated for the occasion. */
1954 static tree
1955 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1956 struct walk_stmt_info *wi)
1958 struct nesting_info *info = (struct nesting_info *) wi->info;
1959 gimple stmt = gsi_stmt (*gsi);
1961 switch (gimple_code (stmt))
1963 case GIMPLE_CALL:
1965 /* Only walk call arguments, lest we generate trampolines for
1966 direct calls. */
1967 unsigned long i, nargs = gimple_call_num_args (stmt);
1968 for (i = 0; i < nargs; i++)
1969 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
1970 wi, NULL);
1971 break;
1974 case GIMPLE_OMP_PARALLEL:
1975 case GIMPLE_OMP_TASK:
1977 tree save_local_var_chain;
1978 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
1979 save_local_var_chain = info->new_local_var_chain;
1980 info->new_local_var_chain = NULL;
1981 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
1982 info, gimple_omp_body (stmt));
1983 if (info->new_local_var_chain)
1984 declare_vars (info->new_local_var_chain,
1985 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1986 false);
1987 info->new_local_var_chain = save_local_var_chain;
1989 break;
1991 default:
1992 *handled_ops_p = false;
1993 return NULL_TREE;
1994 break;
1997 *handled_ops_p = true;
1998 return NULL_TREE;
2003 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2004 that reference nested functions to make sure that the static chain
2005 is set up properly for the call. */
2007 static tree
2008 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2009 struct walk_stmt_info *wi)
2011 struct nesting_info *const info = (struct nesting_info *) wi->info;
2012 tree decl, target_context;
2013 char save_static_chain_added;
2014 int i;
2015 gimple stmt = gsi_stmt (*gsi);
2017 switch (gimple_code (stmt))
2019 case GIMPLE_CALL:
2020 if (gimple_call_chain (stmt))
2021 break;
2022 decl = gimple_call_fndecl (stmt);
2023 if (!decl)
2024 break;
2025 target_context = decl_function_context (decl);
2026 if (target_context && DECL_STATIC_CHAIN (decl))
2028 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
2029 &wi->gsi));
2030 info->static_chain_added |= (1 << (info->context != target_context));
2032 break;
2034 case GIMPLE_OMP_PARALLEL:
2035 case GIMPLE_OMP_TASK:
2036 save_static_chain_added = info->static_chain_added;
2037 info->static_chain_added = 0;
2038 walk_body (convert_gimple_call, NULL, info, gimple_omp_body (stmt));
2039 for (i = 0; i < 2; i++)
2041 tree c, decl;
2042 if ((info->static_chain_added & (1 << i)) == 0)
2043 continue;
2044 decl = i ? get_chain_decl (info) : info->frame_decl;
2045 /* Don't add CHAIN.* or FRAME.* twice. */
2046 for (c = gimple_omp_taskreg_clauses (stmt);
2048 c = OMP_CLAUSE_CHAIN (c))
2049 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2050 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2051 && OMP_CLAUSE_DECL (c) == decl)
2052 break;
2053 if (c == NULL)
2055 c = build_omp_clause (gimple_location (stmt),
2056 i ? OMP_CLAUSE_FIRSTPRIVATE
2057 : OMP_CLAUSE_SHARED);
2058 OMP_CLAUSE_DECL (c) = decl;
2059 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2060 gimple_omp_taskreg_set_clauses (stmt, c);
2063 info->static_chain_added |= save_static_chain_added;
2064 break;
2066 case GIMPLE_OMP_FOR:
2067 walk_body (convert_gimple_call, NULL, info,
2068 gimple_omp_for_pre_body (stmt));
2069 /* FALLTHRU */
2070 case GIMPLE_OMP_SECTIONS:
2071 case GIMPLE_OMP_SECTION:
2072 case GIMPLE_OMP_SINGLE:
2073 case GIMPLE_OMP_MASTER:
2074 case GIMPLE_OMP_ORDERED:
2075 case GIMPLE_OMP_CRITICAL:
2076 walk_body (convert_gimple_call, NULL, info, gimple_omp_body (stmt));
2077 break;
2079 default:
2080 /* Keep looking for other operands. */
2081 *handled_ops_p = false;
2082 return NULL_TREE;
2085 *handled_ops_p = true;
2086 return NULL_TREE;
2089 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2090 call expressions. At the same time, determine if a nested function
2091 actually uses its static chain; if not, remember that. */
2093 static void
2094 convert_all_function_calls (struct nesting_info *root)
2096 unsigned int chain_count = 0, old_chain_count, iter_count;
2097 struct nesting_info *n;
2099 /* First, optimistically clear static_chain for all decls that haven't
2100 used the static chain already for variable access. */
2101 FOR_EACH_NEST_INFO (n, root)
2103 tree decl = n->context;
2104 if (!n->outer || (!n->chain_decl && !n->chain_field))
2106 DECL_STATIC_CHAIN (decl) = 0;
2107 if (dump_file && (dump_flags & TDF_DETAILS))
2108 fprintf (dump_file, "Guessing no static-chain for %s\n",
2109 lang_hooks.decl_printable_name (decl, 2));
2111 else
2112 DECL_STATIC_CHAIN (decl) = 1;
2113 chain_count += DECL_STATIC_CHAIN (decl);
2116 /* Walk the functions and perform transformations. Note that these
2117 transformations can induce new uses of the static chain, which in turn
2118 require re-examining all users of the decl. */
2119 /* ??? It would make sense to try to use the call graph to speed this up,
2120 but the call graph hasn't really been built yet. Even if it did, we
2121 would still need to iterate in this loop since address-of references
2122 wouldn't show up in the callgraph anyway. */
2123 iter_count = 0;
2126 old_chain_count = chain_count;
2127 chain_count = 0;
2128 iter_count++;
2130 if (dump_file && (dump_flags & TDF_DETAILS))
2131 fputc ('\n', dump_file);
2133 FOR_EACH_NEST_INFO (n, root)
2135 tree decl = n->context;
2136 walk_function (convert_tramp_reference_stmt,
2137 convert_tramp_reference_op, n);
2138 walk_function (convert_gimple_call, NULL, n);
2139 chain_count += DECL_STATIC_CHAIN (decl);
2142 while (chain_count != old_chain_count);
2144 if (dump_file && (dump_flags & TDF_DETAILS))
2145 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2146 iter_count);
2149 struct nesting_copy_body_data
2151 copy_body_data cb;
2152 struct nesting_info *root;
2155 /* A helper subroutine for debug_var_chain type remapping. */
2157 static tree
2158 nesting_copy_decl (tree decl, copy_body_data *id)
2160 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2161 void **slot = pointer_map_contains (nid->root->var_map, decl);
2163 if (slot)
2164 return (tree) *slot;
2166 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2168 tree new_decl = copy_decl_no_change (decl, id);
2169 DECL_ORIGINAL_TYPE (new_decl)
2170 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2171 return new_decl;
2174 if (TREE_CODE (decl) == VAR_DECL
2175 || TREE_CODE (decl) == PARM_DECL
2176 || TREE_CODE (decl) == RESULT_DECL)
2177 return decl;
2179 return copy_decl_no_change (decl, id);
2182 /* A helper function for remap_vla_decls. See if *TP contains
2183 some remapped variables. */
2185 static tree
2186 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2188 struct nesting_info *root = (struct nesting_info *) data;
2189 tree t = *tp;
2190 void **slot;
2192 if (DECL_P (t))
2194 *walk_subtrees = 0;
2195 slot = pointer_map_contains (root->var_map, t);
2197 if (slot)
2198 return (tree) *slot;
2200 return NULL;
2203 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2204 involved. */
2206 static void
2207 remap_vla_decls (tree block, struct nesting_info *root)
2209 tree var, subblock, val, type;
2210 struct nesting_copy_body_data id;
2212 for (subblock = BLOCK_SUBBLOCKS (block);
2213 subblock;
2214 subblock = BLOCK_CHAIN (subblock))
2215 remap_vla_decls (subblock, root);
2217 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2218 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2220 val = DECL_VALUE_EXPR (var);
2221 type = TREE_TYPE (var);
2223 if (!(TREE_CODE (val) == INDIRECT_REF
2224 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2225 && variably_modified_type_p (type, NULL)))
2226 continue;
2228 if (pointer_map_contains (root->var_map, TREE_OPERAND (val, 0))
2229 || walk_tree (&type, contains_remapped_vars, root, NULL))
2230 break;
2233 if (var == NULL_TREE)
2234 return;
2236 memset (&id, 0, sizeof (id));
2237 id.cb.copy_decl = nesting_copy_decl;
2238 id.cb.decl_map = pointer_map_create ();
2239 id.root = root;
2241 for (; var; var = DECL_CHAIN (var))
2242 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2244 struct nesting_info *i;
2245 tree newt, context;
2246 void **slot;
2248 val = DECL_VALUE_EXPR (var);
2249 type = TREE_TYPE (var);
2251 if (!(TREE_CODE (val) == INDIRECT_REF
2252 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2253 && variably_modified_type_p (type, NULL)))
2254 continue;
2256 slot = pointer_map_contains (root->var_map, TREE_OPERAND (val, 0));
2257 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2258 continue;
2260 context = decl_function_context (var);
2261 for (i = root; i; i = i->outer)
2262 if (i->context == context)
2263 break;
2265 if (i == NULL)
2266 continue;
2268 /* Fully expand value expressions. This avoids having debug variables
2269 only referenced from them and that can be swept during GC. */
2270 if (slot)
2272 tree t = (tree) *slot;
2273 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2274 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2277 id.cb.src_fn = i->context;
2278 id.cb.dst_fn = i->context;
2279 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2281 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2282 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2284 newt = TREE_TYPE (newt);
2285 type = TREE_TYPE (type);
2287 if (TYPE_NAME (newt)
2288 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2289 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2290 && newt != type
2291 && TYPE_NAME (newt) == TYPE_NAME (type))
2292 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2294 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2295 if (val != DECL_VALUE_EXPR (var))
2296 SET_DECL_VALUE_EXPR (var, val);
2299 pointer_map_destroy (id.cb.decl_map);
2302 /* Fold the MEM_REF *E. */
2303 static bool
2304 fold_mem_refs (const void *e, void *data ATTRIBUTE_UNUSED)
2306 tree *ref_p = CONST_CAST2(tree *, const tree *, (const tree *)e);
2307 *ref_p = fold (*ref_p);
2308 return true;
2311 /* Do "everything else" to clean up or complete state collected by the
2312 various walking passes -- lay out the types and decls, generate code
2313 to initialize the frame decl, store critical expressions in the
2314 struct function for rtl to find. */
2316 static void
2317 finalize_nesting_tree_1 (struct nesting_info *root)
2319 gimple_seq stmt_list;
2320 gimple stmt;
2321 tree context = root->context;
2322 struct function *sf;
2324 stmt_list = NULL;
2326 /* If we created a non-local frame type or decl, we need to lay them
2327 out at this time. */
2328 if (root->frame_type)
2330 /* In some cases the frame type will trigger the -Wpadded warning.
2331 This is not helpful; suppress it. */
2332 int save_warn_padded = warn_padded;
2333 tree *adjust;
2335 warn_padded = 0;
2336 layout_type (root->frame_type);
2337 warn_padded = save_warn_padded;
2338 layout_decl (root->frame_decl, 0);
2340 /* Remove root->frame_decl from root->new_local_var_chain, so
2341 that we can declare it also in the lexical blocks, which
2342 helps ensure virtual regs that end up appearing in its RTL
2343 expression get substituted in instantiate_virtual_regs(). */
2344 for (adjust = &root->new_local_var_chain;
2345 *adjust != root->frame_decl;
2346 adjust = &DECL_CHAIN (*adjust))
2347 gcc_assert (DECL_CHAIN (*adjust));
2348 *adjust = DECL_CHAIN (*adjust);
2350 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2351 declare_vars (root->frame_decl,
2352 gimple_seq_first_stmt (gimple_body (context)), true);
2355 /* If any parameters were referenced non-locally, then we need to
2356 insert a copy. Likewise, if any variables were referenced by
2357 pointer, we need to initialize the address. */
2358 if (root->any_parm_remapped)
2360 tree p;
2361 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2363 tree field, x, y;
2365 field = lookup_field_for_decl (root, p, NO_INSERT);
2366 if (!field)
2367 continue;
2369 if (use_pointer_in_frame (p))
2370 x = build_addr (p, context);
2371 else
2372 x = p;
2374 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2375 root->frame_decl, field, NULL_TREE);
2376 stmt = gimple_build_assign (y, x);
2377 gimple_seq_add_stmt (&stmt_list, stmt);
2378 /* If the assignment is from a non-register the stmt is
2379 not valid gimple. Make it so by using a temporary instead. */
2380 if (!is_gimple_reg (x)
2381 && is_gimple_reg_type (TREE_TYPE (x)))
2383 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2384 x = init_tmp_var (root, x, &gsi);
2385 gimple_assign_set_rhs1 (stmt, x);
2390 /* If a chain_field was created, then it needs to be initialized
2391 from chain_decl. */
2392 if (root->chain_field)
2394 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2395 root->frame_decl, root->chain_field, NULL_TREE);
2396 stmt = gimple_build_assign (x, get_chain_decl (root));
2397 gimple_seq_add_stmt (&stmt_list, stmt);
2400 /* If trampolines were created, then we need to initialize them. */
2401 if (root->any_tramp_created)
2403 struct nesting_info *i;
2404 for (i = root->inner; i ; i = i->next)
2406 tree arg1, arg2, arg3, x, field;
2408 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2409 if (!field)
2410 continue;
2412 gcc_assert (DECL_STATIC_CHAIN (i->context));
2413 arg3 = build_addr (root->frame_decl, context);
2415 arg2 = build_addr (i->context, context);
2417 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2418 root->frame_decl, field, NULL_TREE);
2419 arg1 = build_addr (x, context);
2421 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2422 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2423 gimple_seq_add_stmt (&stmt_list, stmt);
2427 /* If we created initialization statements, insert them. */
2428 if (stmt_list)
2430 gimple bind;
2431 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2432 bind = gimple_seq_first_stmt (gimple_body (context));
2433 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2434 gimple_bind_set_body (bind, stmt_list);
2437 /* If a chain_decl was created, then it needs to be registered with
2438 struct function so that it gets initialized from the static chain
2439 register at the beginning of the function. */
2440 sf = DECL_STRUCT_FUNCTION (root->context);
2441 sf->static_chain_decl = root->chain_decl;
2443 /* Similarly for the non-local goto save area. */
2444 if (root->nl_goto_field)
2446 sf->nonlocal_goto_save_area
2447 = get_frame_field (root, context, root->nl_goto_field, NULL);
2448 sf->has_nonlocal_label = 1;
2451 /* Make sure all new local variables get inserted into the
2452 proper BIND_EXPR. */
2453 if (root->new_local_var_chain)
2454 declare_vars (root->new_local_var_chain,
2455 gimple_seq_first_stmt (gimple_body (root->context)),
2456 false);
2458 if (root->debug_var_chain)
2460 tree debug_var;
2461 gimple scope;
2463 remap_vla_decls (DECL_INITIAL (root->context), root);
2465 for (debug_var = root->debug_var_chain; debug_var;
2466 debug_var = DECL_CHAIN (debug_var))
2467 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2468 break;
2470 /* If there are any debug decls with variable length types,
2471 remap those types using other debug_var_chain variables. */
2472 if (debug_var)
2474 struct nesting_copy_body_data id;
2476 memset (&id, 0, sizeof (id));
2477 id.cb.copy_decl = nesting_copy_decl;
2478 id.cb.decl_map = pointer_map_create ();
2479 id.root = root;
2481 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2482 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2484 tree type = TREE_TYPE (debug_var);
2485 tree newt, t = type;
2486 struct nesting_info *i;
2488 for (i = root; i; i = i->outer)
2489 if (variably_modified_type_p (type, i->context))
2490 break;
2492 if (i == NULL)
2493 continue;
2495 id.cb.src_fn = i->context;
2496 id.cb.dst_fn = i->context;
2497 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2499 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2500 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2502 newt = TREE_TYPE (newt);
2503 t = TREE_TYPE (t);
2505 if (TYPE_NAME (newt)
2506 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2507 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2508 && newt != t
2509 && TYPE_NAME (newt) == TYPE_NAME (t))
2510 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2513 pointer_map_destroy (id.cb.decl_map);
2516 scope = gimple_seq_first_stmt (gimple_body (root->context));
2517 if (gimple_bind_block (scope))
2518 declare_vars (root->debug_var_chain, scope, true);
2519 else
2520 BLOCK_VARS (DECL_INITIAL (root->context))
2521 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2522 root->debug_var_chain);
2525 /* Fold the rewritten MEM_REF trees. */
2526 pointer_set_traverse (root->mem_refs, fold_mem_refs, NULL);
2528 /* Dump the translated tree function. */
2529 if (dump_file)
2531 fputs ("\n\n", dump_file);
2532 dump_function_to_file (root->context, dump_file, dump_flags);
2536 static void
2537 finalize_nesting_tree (struct nesting_info *root)
2539 struct nesting_info *n;
2540 FOR_EACH_NEST_INFO (n, root)
2541 finalize_nesting_tree_1 (n);
2544 /* Unnest the nodes and pass them to cgraph. */
2546 static void
2547 unnest_nesting_tree_1 (struct nesting_info *root)
2549 struct cgraph_node *node = cgraph_get_node (root->context);
2551 /* For nested functions update the cgraph to reflect unnesting.
2552 We also delay finalizing of these functions up to this point. */
2553 if (node->origin)
2555 cgraph_unnest_node (node);
2556 cgraph_finalize_function (root->context, true);
2560 static void
2561 unnest_nesting_tree (struct nesting_info *root)
2563 struct nesting_info *n;
2564 FOR_EACH_NEST_INFO (n, root)
2565 unnest_nesting_tree_1 (n);
2568 /* Free the data structures allocated during this pass. */
2570 static void
2571 free_nesting_tree (struct nesting_info *root)
2573 struct nesting_info *node, *next;
2575 node = iter_nestinfo_start (root);
2578 next = iter_nestinfo_next (node);
2579 pointer_map_destroy (node->var_map);
2580 pointer_map_destroy (node->field_map);
2581 pointer_set_destroy (node->mem_refs);
2582 free (node);
2583 node = next;
2585 while (node);
2588 /* Gimplify a function and all its nested functions. */
2589 static void
2590 gimplify_all_functions (struct cgraph_node *root)
2592 struct cgraph_node *iter;
2593 if (!gimple_body (root->symbol.decl))
2594 gimplify_function_tree (root->symbol.decl);
2595 for (iter = root->nested; iter; iter = iter->next_nested)
2596 gimplify_all_functions (iter);
2599 /* Main entry point for this pass. Process FNDECL and all of its nested
2600 subroutines and turn them into something less tightly bound. */
2602 void
2603 lower_nested_functions (tree fndecl)
2605 struct cgraph_node *cgn;
2606 struct nesting_info *root;
2608 /* If there are no nested functions, there's nothing to do. */
2609 cgn = cgraph_get_node (fndecl);
2610 if (!cgn->nested)
2611 return;
2613 gimplify_all_functions (cgn);
2615 dump_file = dump_begin (TDI_nested, &dump_flags);
2616 if (dump_file)
2617 fprintf (dump_file, "\n;; Function %s\n\n",
2618 lang_hooks.decl_printable_name (fndecl, 2));
2620 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2621 root = create_nesting_tree (cgn);
2623 walk_all_functions (convert_nonlocal_reference_stmt,
2624 convert_nonlocal_reference_op,
2625 root);
2626 walk_all_functions (convert_local_reference_stmt,
2627 convert_local_reference_op,
2628 root);
2629 walk_all_functions (convert_nl_goto_reference, NULL, root);
2630 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2632 convert_all_function_calls (root);
2633 finalize_nesting_tree (root);
2634 unnest_nesting_tree (root);
2636 free_nesting_tree (root);
2637 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2639 if (dump_file)
2641 dump_end (TDI_nested, dump_file);
2642 dump_file = NULL;
2646 #include "gt-tree-nested.h"