Make gimple_goto_dest require a const ggoto *
[official-gcc.git] / gcc / tree-nested.c
blobc95fe819a258815e753f348e6aa422d42c2f28f9
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stringpool.h"
26 #include "stor-layout.h"
27 #include "tm_p.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "vec.h"
31 #include "machmode.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "tree-dump.h"
36 #include "tree-inline.h"
37 #include "predict.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimplify.h"
45 #include "gimple-iterator.h"
46 #include "gimple-walk.h"
47 #include "tree-iterator.h"
48 #include "bitmap.h"
49 #include "cgraph.h"
50 #include "tree-cfg.h"
51 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
52 #include "langhooks.h"
53 #include "gimple-low.h"
56 /* The object of this pass is to lower the representation of a set of nested
57 functions in order to expose all of the gory details of the various
58 nonlocal references. We want to do this sooner rather than later, in
59 order to give us more freedom in emitting all of the functions in question.
61 Back in olden times, when gcc was young, we developed an insanely
62 complicated scheme whereby variables which were referenced nonlocally
63 were forced to live in the stack of the declaring function, and then
64 the nested functions magically discovered where these variables were
65 placed. In order for this scheme to function properly, it required
66 that the outer function be partially expanded, then we switch to
67 compiling the inner function, and once done with those we switch back
68 to compiling the outer function. Such delicate ordering requirements
69 makes it difficult to do whole translation unit optimizations
70 involving such functions.
72 The implementation here is much more direct. Everything that can be
73 referenced by an inner function is a member of an explicitly created
74 structure herein called the "nonlocal frame struct". The incoming
75 static chain for a nested function is a pointer to this struct in
76 the parent. In this way, we settle on known offsets from a known
77 base, and so are decoupled from the logic that places objects in the
78 function's stack frame. More importantly, we don't have to wait for
79 that to happen -- since the compilation of the inner function is no
80 longer tied to a real stack frame, the nonlocal frame struct can be
81 allocated anywhere. Which means that the outer function is now
82 inlinable.
84 Theory of operation here is very simple. Iterate over all the
85 statements in all the functions (depth first) several times,
86 allocating structures and fields on demand. In general we want to
87 examine inner functions first, so that we can avoid making changes
88 to outer functions which are unnecessary.
90 The order of the passes matters a bit, in that later passes will be
91 skipped if it is discovered that the functions don't actually interact
92 at all. That is, they're nested in the lexical sense but could have
93 been written as independent functions without change. */
96 struct nesting_info
98 struct nesting_info *outer;
99 struct nesting_info *inner;
100 struct nesting_info *next;
102 hash_map<tree, tree> *field_map;
103 hash_map<tree, tree> *var_map;
104 hash_set<tree *> *mem_refs;
105 bitmap suppress_expansion;
107 tree context;
108 tree new_local_var_chain;
109 tree debug_var_chain;
110 tree frame_type;
111 tree frame_decl;
112 tree chain_field;
113 tree chain_decl;
114 tree nl_goto_field;
116 bool any_parm_remapped;
117 bool any_tramp_created;
118 char static_chain_added;
122 /* Iterate over the nesting tree, starting with ROOT, depth first. */
124 static inline struct nesting_info *
125 iter_nestinfo_start (struct nesting_info *root)
127 while (root->inner)
128 root = root->inner;
129 return root;
132 static inline struct nesting_info *
133 iter_nestinfo_next (struct nesting_info *node)
135 if (node->next)
136 return iter_nestinfo_start (node->next);
137 return node->outer;
140 #define FOR_EACH_NEST_INFO(I, ROOT) \
141 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
143 /* Obstack used for the bitmaps in the struct above. */
144 static struct bitmap_obstack nesting_info_bitmap_obstack;
147 /* We're working in so many different function contexts simultaneously,
148 that create_tmp_var is dangerous. Prevent mishap. */
149 #define create_tmp_var cant_use_create_tmp_var_here_dummy
151 /* Like create_tmp_var, except record the variable for registration at
152 the given nesting level. */
154 static tree
155 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
157 tree tmp_var;
159 /* If the type is of variable size or a type which must be created by the
160 frontend, something is wrong. Note that we explicitly allow
161 incomplete types here, since we create them ourselves here. */
162 gcc_assert (!TREE_ADDRESSABLE (type));
163 gcc_assert (!TYPE_SIZE_UNIT (type)
164 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
166 tmp_var = create_tmp_var_raw (type, prefix);
167 DECL_CONTEXT (tmp_var) = info->context;
168 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
169 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
170 if (TREE_CODE (type) == COMPLEX_TYPE
171 || TREE_CODE (type) == VECTOR_TYPE)
172 DECL_GIMPLE_REG_P (tmp_var) = 1;
174 info->new_local_var_chain = tmp_var;
176 return tmp_var;
179 /* Take the address of EXP to be used within function CONTEXT.
180 Mark it for addressability as necessary. */
182 tree
183 build_addr (tree exp, tree context)
185 tree base = exp;
186 tree save_context;
187 tree retval;
189 while (handled_component_p (base))
190 base = TREE_OPERAND (base, 0);
192 if (DECL_P (base))
193 TREE_ADDRESSABLE (base) = 1;
195 /* Building the ADDR_EXPR will compute a set of properties for
196 that ADDR_EXPR. Those properties are unfortunately context
197 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
199 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
200 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
201 way the properties are for the ADDR_EXPR are computed properly. */
202 save_context = current_function_decl;
203 current_function_decl = context;
204 retval = build_fold_addr_expr (exp);
205 current_function_decl = save_context;
206 return retval;
209 /* Insert FIELD into TYPE, sorted by alignment requirements. */
211 void
212 insert_field_into_struct (tree type, tree field)
214 tree *p;
216 DECL_CONTEXT (field) = type;
218 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
219 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
220 break;
222 DECL_CHAIN (field) = *p;
223 *p = field;
225 /* Set correct alignment for frame struct type. */
226 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
227 TYPE_ALIGN (type) = DECL_ALIGN (field);
230 /* Build or return the RECORD_TYPE that describes the frame state that is
231 shared between INFO->CONTEXT and its nested functions. This record will
232 not be complete until finalize_nesting_tree; up until that point we'll
233 be adding fields as necessary.
235 We also build the DECL that represents this frame in the function. */
237 static tree
238 get_frame_type (struct nesting_info *info)
240 tree type = info->frame_type;
241 if (!type)
243 char *name;
245 type = make_node (RECORD_TYPE);
247 name = concat ("FRAME.",
248 IDENTIFIER_POINTER (DECL_NAME (info->context)),
249 NULL);
250 TYPE_NAME (type) = get_identifier (name);
251 free (name);
253 info->frame_type = type;
254 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
255 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
257 /* ??? Always make it addressable for now, since it is meant to
258 be pointed to by the static chain pointer. This pessimizes
259 when it turns out that no static chains are needed because
260 the nested functions referencing non-local variables are not
261 reachable, but the true pessimization is to create the non-
262 local frame structure in the first place. */
263 TREE_ADDRESSABLE (info->frame_decl) = 1;
265 return type;
268 /* Return true if DECL should be referenced by pointer in the non-local
269 frame structure. */
271 static bool
272 use_pointer_in_frame (tree decl)
274 if (TREE_CODE (decl) == PARM_DECL)
276 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
277 sized decls, and inefficient to copy large aggregates. Don't bother
278 moving anything but scalar variables. */
279 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
281 else
283 /* Variable sized types make things "interesting" in the frame. */
284 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
288 /* Given DECL, a non-locally accessed variable, find or create a field
289 in the non-local frame structure for the given nesting context. */
291 static tree
292 lookup_field_for_decl (struct nesting_info *info, tree decl,
293 enum insert_option insert)
295 if (insert == NO_INSERT)
297 tree *slot = info->field_map->get (decl);
298 return slot ? *slot : NULL_TREE;
301 tree *slot = &info->field_map->get_or_insert (decl);
302 if (!*slot)
304 tree field = make_node (FIELD_DECL);
305 DECL_NAME (field) = DECL_NAME (decl);
307 if (use_pointer_in_frame (decl))
309 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
310 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
311 DECL_NONADDRESSABLE_P (field) = 1;
313 else
315 TREE_TYPE (field) = TREE_TYPE (decl);
316 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
317 DECL_ALIGN (field) = DECL_ALIGN (decl);
318 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
319 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
320 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
321 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
324 insert_field_into_struct (get_frame_type (info), field);
325 *slot = field;
327 if (TREE_CODE (decl) == PARM_DECL)
328 info->any_parm_remapped = true;
331 return *slot;
334 /* Build or return the variable that holds the static chain within
335 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
337 static tree
338 get_chain_decl (struct nesting_info *info)
340 tree decl = info->chain_decl;
342 if (!decl)
344 tree type;
346 type = get_frame_type (info->outer);
347 type = build_pointer_type (type);
349 /* Note that this variable is *not* entered into any BIND_EXPR;
350 the construction of this variable is handled specially in
351 expand_function_start and initialize_inlined_parameters.
352 Note also that it's represented as a parameter. This is more
353 close to the truth, since the initial value does come from
354 the caller. */
355 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
356 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
357 DECL_ARTIFICIAL (decl) = 1;
358 DECL_IGNORED_P (decl) = 1;
359 TREE_USED (decl) = 1;
360 DECL_CONTEXT (decl) = info->context;
361 DECL_ARG_TYPE (decl) = type;
363 /* Tell tree-inline.c that we never write to this variable, so
364 it can copy-prop the replacement value immediately. */
365 TREE_READONLY (decl) = 1;
367 info->chain_decl = decl;
369 if (dump_file
370 && (dump_flags & TDF_DETAILS)
371 && !DECL_STATIC_CHAIN (info->context))
372 fprintf (dump_file, "Setting static-chain for %s\n",
373 lang_hooks.decl_printable_name (info->context, 2));
375 DECL_STATIC_CHAIN (info->context) = 1;
377 return decl;
380 /* Build or return the field within the non-local frame state that holds
381 the static chain for INFO->CONTEXT. This is the way to walk back up
382 multiple nesting levels. */
384 static tree
385 get_chain_field (struct nesting_info *info)
387 tree field = info->chain_field;
389 if (!field)
391 tree type = build_pointer_type (get_frame_type (info->outer));
393 field = make_node (FIELD_DECL);
394 DECL_NAME (field) = get_identifier ("__chain");
395 TREE_TYPE (field) = type;
396 DECL_ALIGN (field) = TYPE_ALIGN (type);
397 DECL_NONADDRESSABLE_P (field) = 1;
399 insert_field_into_struct (get_frame_type (info), field);
401 info->chain_field = field;
403 if (dump_file
404 && (dump_flags & TDF_DETAILS)
405 && !DECL_STATIC_CHAIN (info->context))
406 fprintf (dump_file, "Setting static-chain for %s\n",
407 lang_hooks.decl_printable_name (info->context, 2));
409 DECL_STATIC_CHAIN (info->context) = 1;
411 return field;
414 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
416 static tree
417 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
418 gcall *call)
420 tree t;
422 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
423 gimple_call_set_lhs (call, t);
424 if (! gsi_end_p (*gsi))
425 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
426 gsi_insert_before (gsi, call, GSI_SAME_STMT);
428 return t;
432 /* Copy EXP into a temporary. Allocate the temporary in the context of
433 INFO and insert the initialization statement before GSI. */
435 static tree
436 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
438 tree t;
439 gimple stmt;
441 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
442 stmt = gimple_build_assign (t, exp);
443 if (! gsi_end_p (*gsi))
444 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
445 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
447 return t;
451 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
453 static tree
454 gsi_gimplify_val (struct nesting_info *info, tree exp,
455 gimple_stmt_iterator *gsi)
457 if (is_gimple_val (exp))
458 return exp;
459 else
460 return init_tmp_var (info, exp, gsi);
463 /* Similarly, but copy from the temporary and insert the statement
464 after the iterator. */
466 static tree
467 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
469 tree t;
470 gimple stmt;
472 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
473 stmt = gimple_build_assign (exp, t);
474 if (! gsi_end_p (*gsi))
475 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
476 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
478 return t;
481 /* Build or return the type used to represent a nested function trampoline. */
483 static GTY(()) tree trampoline_type;
485 static tree
486 get_trampoline_type (struct nesting_info *info)
488 unsigned align, size;
489 tree t;
491 if (trampoline_type)
492 return trampoline_type;
494 align = TRAMPOLINE_ALIGNMENT;
495 size = TRAMPOLINE_SIZE;
497 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
498 then allocate extra space so that we can do dynamic alignment. */
499 if (align > STACK_BOUNDARY)
501 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
502 align = STACK_BOUNDARY;
505 t = build_index_type (size_int (size - 1));
506 t = build_array_type (char_type_node, t);
507 t = build_decl (DECL_SOURCE_LOCATION (info->context),
508 FIELD_DECL, get_identifier ("__data"), t);
509 DECL_ALIGN (t) = align;
510 DECL_USER_ALIGN (t) = 1;
512 trampoline_type = make_node (RECORD_TYPE);
513 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
514 TYPE_FIELDS (trampoline_type) = t;
515 layout_type (trampoline_type);
516 DECL_CONTEXT (t) = trampoline_type;
518 return trampoline_type;
521 /* Given DECL, a nested function, find or create a field in the non-local
522 frame structure for a trampoline for this function. */
524 static tree
525 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
526 enum insert_option insert)
528 if (insert == NO_INSERT)
530 tree *slot = info->var_map->get (decl);
531 return slot ? *slot : NULL_TREE;
534 tree *slot = &info->var_map->get_or_insert (decl);
535 if (!*slot)
537 tree field = make_node (FIELD_DECL);
538 DECL_NAME (field) = DECL_NAME (decl);
539 TREE_TYPE (field) = get_trampoline_type (info);
540 TREE_ADDRESSABLE (field) = 1;
542 insert_field_into_struct (get_frame_type (info), field);
543 *slot = field;
545 info->any_tramp_created = true;
548 return *slot;
551 /* Build or return the field within the non-local frame state that holds
552 the non-local goto "jmp_buf". The buffer itself is maintained by the
553 rtl middle-end as dynamic stack space is allocated. */
555 static tree
556 get_nl_goto_field (struct nesting_info *info)
558 tree field = info->nl_goto_field;
559 if (!field)
561 unsigned size;
562 tree type;
564 /* For __builtin_nonlocal_goto, we need N words. The first is the
565 frame pointer, the rest is for the target's stack pointer save
566 area. The number of words is controlled by STACK_SAVEAREA_MODE;
567 not the best interface, but it'll do for now. */
568 if (Pmode == ptr_mode)
569 type = ptr_type_node;
570 else
571 type = lang_hooks.types.type_for_mode (Pmode, 1);
573 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
574 size = size / GET_MODE_SIZE (Pmode);
575 size = size + 1;
577 type = build_array_type
578 (type, build_index_type (size_int (size)));
580 field = make_node (FIELD_DECL);
581 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
582 TREE_TYPE (field) = type;
583 DECL_ALIGN (field) = TYPE_ALIGN (type);
584 TREE_ADDRESSABLE (field) = 1;
586 insert_field_into_struct (get_frame_type (info), field);
588 info->nl_goto_field = field;
591 return field;
594 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
596 static void
597 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
598 struct nesting_info *info, gimple_seq *pseq)
600 struct walk_stmt_info wi;
602 memset (&wi, 0, sizeof (wi));
603 wi.info = info;
604 wi.val_only = true;
605 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
609 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
611 static inline void
612 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
613 struct nesting_info *info)
615 gimple_seq body = gimple_body (info->context);
616 walk_body (callback_stmt, callback_op, info, &body);
617 gimple_set_body (info->context, body);
620 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
622 static void
623 walk_gimple_omp_for (gomp_for *for_stmt,
624 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
625 struct nesting_info *info)
627 struct walk_stmt_info wi;
628 gimple_seq seq;
629 tree t;
630 size_t i;
632 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
634 seq = NULL;
635 memset (&wi, 0, sizeof (wi));
636 wi.info = info;
637 wi.gsi = gsi_last (seq);
639 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
641 wi.val_only = false;
642 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
643 &wi, NULL);
644 wi.val_only = true;
645 wi.is_lhs = false;
646 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
647 &wi, NULL);
649 wi.val_only = true;
650 wi.is_lhs = false;
651 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
652 &wi, NULL);
654 t = gimple_omp_for_incr (for_stmt, i);
655 gcc_assert (BINARY_CLASS_P (t));
656 wi.val_only = false;
657 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
658 wi.val_only = true;
659 wi.is_lhs = false;
660 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
663 seq = gsi_seq (wi.gsi);
664 if (!gimple_seq_empty_p (seq))
666 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
667 annotate_all_with_location (seq, gimple_location (for_stmt));
668 gimple_seq_add_seq (&pre_body, seq);
669 gimple_omp_for_set_pre_body (for_stmt, pre_body);
673 /* Similarly for ROOT and all functions nested underneath, depth first. */
675 static void
676 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
677 struct nesting_info *root)
679 struct nesting_info *n;
680 FOR_EACH_NEST_INFO (n, root)
681 walk_function (callback_stmt, callback_op, n);
685 /* We have to check for a fairly pathological case. The operands of function
686 nested function are to be interpreted in the context of the enclosing
687 function. So if any are variably-sized, they will get remapped when the
688 enclosing function is inlined. But that remapping would also have to be
689 done in the types of the PARM_DECLs of the nested function, meaning the
690 argument types of that function will disagree with the arguments in the
691 calls to that function. So we'd either have to make a copy of the nested
692 function corresponding to each time the enclosing function was inlined or
693 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
694 function. The former is not practical. The latter would still require
695 detecting this case to know when to add the conversions. So, for now at
696 least, we don't inline such an enclosing function.
698 We have to do that check recursively, so here return indicating whether
699 FNDECL has such a nested function. ORIG_FN is the function we were
700 trying to inline to use for checking whether any argument is variably
701 modified by anything in it.
703 It would be better to do this in tree-inline.c so that we could give
704 the appropriate warning for why a function can't be inlined, but that's
705 too late since the nesting structure has already been flattened and
706 adding a flag just to record this fact seems a waste of a flag. */
708 static bool
709 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
711 struct cgraph_node *cgn = cgraph_node::get (fndecl);
712 tree arg;
714 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
716 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
717 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
718 return true;
720 if (check_for_nested_with_variably_modified (cgn->decl,
721 orig_fndecl))
722 return true;
725 return false;
728 /* Construct our local datastructure describing the function nesting
729 tree rooted by CGN. */
731 static struct nesting_info *
732 create_nesting_tree (struct cgraph_node *cgn)
734 struct nesting_info *info = XCNEW (struct nesting_info);
735 info->field_map = new hash_map<tree, tree>;
736 info->var_map = new hash_map<tree, tree>;
737 info->mem_refs = new hash_set<tree *>;
738 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
739 info->context = cgn->decl;
741 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
743 struct nesting_info *sub = create_nesting_tree (cgn);
744 sub->outer = info;
745 sub->next = info->inner;
746 info->inner = sub;
749 /* See discussion at check_for_nested_with_variably_modified for a
750 discussion of why this has to be here. */
751 if (check_for_nested_with_variably_modified (info->context, info->context))
752 DECL_UNINLINABLE (info->context) = true;
754 return info;
757 /* Return an expression computing the static chain for TARGET_CONTEXT
758 from INFO->CONTEXT. Insert any necessary computations before TSI. */
760 static tree
761 get_static_chain (struct nesting_info *info, tree target_context,
762 gimple_stmt_iterator *gsi)
764 struct nesting_info *i;
765 tree x;
767 if (info->context == target_context)
769 x = build_addr (info->frame_decl, target_context);
771 else
773 x = get_chain_decl (info);
775 for (i = info->outer; i->context != target_context; i = i->outer)
777 tree field = get_chain_field (i);
779 x = build_simple_mem_ref (x);
780 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
781 x = init_tmp_var (info, x, gsi);
785 return x;
789 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
790 frame as seen from INFO->CONTEXT. Insert any necessary computations
791 before GSI. */
793 static tree
794 get_frame_field (struct nesting_info *info, tree target_context,
795 tree field, gimple_stmt_iterator *gsi)
797 struct nesting_info *i;
798 tree x;
800 if (info->context == target_context)
802 /* Make sure frame_decl gets created. */
803 (void) get_frame_type (info);
804 x = info->frame_decl;
806 else
808 x = get_chain_decl (info);
810 for (i = info->outer; i->context != target_context; i = i->outer)
812 tree field = get_chain_field (i);
814 x = build_simple_mem_ref (x);
815 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
816 x = init_tmp_var (info, x, gsi);
819 x = build_simple_mem_ref (x);
822 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
823 return x;
826 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
828 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
829 in the nested function with DECL_VALUE_EXPR set to reference the true
830 variable in the parent function. This is used both for debug info
831 and in OpenMP lowering. */
833 static tree
834 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
836 tree target_context;
837 struct nesting_info *i;
838 tree x, field, new_decl;
840 tree *slot = &info->var_map->get_or_insert (decl);
842 if (*slot)
843 return *slot;
845 target_context = decl_function_context (decl);
847 /* A copy of the code in get_frame_field, but without the temporaries. */
848 if (info->context == target_context)
850 /* Make sure frame_decl gets created. */
851 (void) get_frame_type (info);
852 x = info->frame_decl;
853 i = info;
855 else
857 x = get_chain_decl (info);
858 for (i = info->outer; i->context != target_context; i = i->outer)
860 field = get_chain_field (i);
861 x = build_simple_mem_ref (x);
862 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
864 x = build_simple_mem_ref (x);
867 field = lookup_field_for_decl (i, decl, INSERT);
868 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
869 if (use_pointer_in_frame (decl))
870 x = build_simple_mem_ref (x);
872 /* ??? We should be remapping types as well, surely. */
873 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
874 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
875 DECL_CONTEXT (new_decl) = info->context;
876 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
877 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
878 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
879 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
880 TREE_READONLY (new_decl) = TREE_READONLY (decl);
881 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
882 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
883 if ((TREE_CODE (decl) == PARM_DECL
884 || TREE_CODE (decl) == RESULT_DECL
885 || TREE_CODE (decl) == VAR_DECL)
886 && DECL_BY_REFERENCE (decl))
887 DECL_BY_REFERENCE (new_decl) = 1;
889 SET_DECL_VALUE_EXPR (new_decl, x);
890 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
892 *slot = new_decl;
893 DECL_CHAIN (new_decl) = info->debug_var_chain;
894 info->debug_var_chain = new_decl;
896 if (!optimize
897 && info->context != target_context
898 && variably_modified_type_p (TREE_TYPE (decl), NULL))
899 note_nonlocal_vla_type (info, TREE_TYPE (decl));
901 return new_decl;
905 /* Callback for walk_gimple_stmt, rewrite all references to VAR
906 and PARM_DECLs that belong to outer functions.
908 The rewrite will involve some number of structure accesses back up
909 the static chain. E.g. for a variable FOO up one nesting level it'll
910 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
911 indirections apply to decls for which use_pointer_in_frame is true. */
913 static tree
914 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
916 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
917 struct nesting_info *const info = (struct nesting_info *) wi->info;
918 tree t = *tp;
920 *walk_subtrees = 0;
921 switch (TREE_CODE (t))
923 case VAR_DECL:
924 /* Non-automatic variables are never processed. */
925 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
926 break;
927 /* FALLTHRU */
929 case PARM_DECL:
930 if (decl_function_context (t) != info->context)
932 tree x;
933 wi->changed = true;
935 x = get_nonlocal_debug_decl (info, t);
936 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
938 tree target_context = decl_function_context (t);
939 struct nesting_info *i;
940 for (i = info->outer; i->context != target_context; i = i->outer)
941 continue;
942 x = lookup_field_for_decl (i, t, INSERT);
943 x = get_frame_field (info, target_context, x, &wi->gsi);
944 if (use_pointer_in_frame (t))
946 x = init_tmp_var (info, x, &wi->gsi);
947 x = build_simple_mem_ref (x);
951 if (wi->val_only)
953 if (wi->is_lhs)
954 x = save_tmp_var (info, x, &wi->gsi);
955 else
956 x = init_tmp_var (info, x, &wi->gsi);
959 *tp = x;
961 break;
963 case LABEL_DECL:
964 /* We're taking the address of a label from a parent function, but
965 this is not itself a non-local goto. Mark the label such that it
966 will not be deleted, much as we would with a label address in
967 static storage. */
968 if (decl_function_context (t) != info->context)
969 FORCED_LABEL (t) = 1;
970 break;
972 case ADDR_EXPR:
974 bool save_val_only = wi->val_only;
976 wi->val_only = false;
977 wi->is_lhs = false;
978 wi->changed = false;
979 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
980 wi->val_only = true;
982 if (wi->changed)
984 tree save_context;
986 /* If we changed anything, we might no longer be directly
987 referencing a decl. */
988 save_context = current_function_decl;
989 current_function_decl = info->context;
990 recompute_tree_invariant_for_addr_expr (t);
991 current_function_decl = save_context;
993 /* If the callback converted the address argument in a context
994 where we only accept variables (and min_invariant, presumably),
995 then compute the address into a temporary. */
996 if (save_val_only)
997 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
998 t, &wi->gsi);
1001 break;
1003 case REALPART_EXPR:
1004 case IMAGPART_EXPR:
1005 case COMPONENT_REF:
1006 case ARRAY_REF:
1007 case ARRAY_RANGE_REF:
1008 case BIT_FIELD_REF:
1009 /* Go down this entire nest and just look at the final prefix and
1010 anything that describes the references. Otherwise, we lose track
1011 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1012 wi->val_only = true;
1013 wi->is_lhs = false;
1014 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1016 if (TREE_CODE (t) == COMPONENT_REF)
1017 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1018 NULL);
1019 else if (TREE_CODE (t) == ARRAY_REF
1020 || TREE_CODE (t) == ARRAY_RANGE_REF)
1022 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1023 wi, NULL);
1024 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1025 wi, NULL);
1026 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1027 wi, NULL);
1030 wi->val_only = false;
1031 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1032 break;
1034 case VIEW_CONVERT_EXPR:
1035 /* Just request to look at the subtrees, leaving val_only and lhs
1036 untouched. This might actually be for !val_only + lhs, in which
1037 case we don't want to force a replacement by a temporary. */
1038 *walk_subtrees = 1;
1039 break;
1041 default:
1042 if (!IS_TYPE_OR_DECL_P (t))
1044 *walk_subtrees = 1;
1045 wi->val_only = true;
1046 wi->is_lhs = false;
1048 break;
1051 return NULL_TREE;
1054 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1055 struct walk_stmt_info *);
1057 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1058 and PARM_DECLs that belong to outer functions. */
1060 static bool
1061 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1063 struct nesting_info *const info = (struct nesting_info *) wi->info;
1064 bool need_chain = false, need_stmts = false;
1065 tree clause, decl;
1066 int dummy;
1067 bitmap new_suppress;
1069 new_suppress = BITMAP_GGC_ALLOC ();
1070 bitmap_copy (new_suppress, info->suppress_expansion);
1072 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1074 switch (OMP_CLAUSE_CODE (clause))
1076 case OMP_CLAUSE_REDUCTION:
1077 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1078 need_stmts = true;
1079 goto do_decl_clause;
1081 case OMP_CLAUSE_LASTPRIVATE:
1082 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1083 need_stmts = true;
1084 goto do_decl_clause;
1086 case OMP_CLAUSE_LINEAR:
1087 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1088 need_stmts = true;
1089 wi->val_only = true;
1090 wi->is_lhs = false;
1091 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1092 &dummy, wi);
1093 goto do_decl_clause;
1095 case OMP_CLAUSE_PRIVATE:
1096 case OMP_CLAUSE_FIRSTPRIVATE:
1097 case OMP_CLAUSE_COPYPRIVATE:
1098 case OMP_CLAUSE_SHARED:
1099 do_decl_clause:
1100 decl = OMP_CLAUSE_DECL (clause);
1101 if (TREE_CODE (decl) == VAR_DECL
1102 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1103 break;
1104 if (decl_function_context (decl) != info->context)
1106 bitmap_set_bit (new_suppress, DECL_UID (decl));
1107 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1108 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1109 need_chain = true;
1111 break;
1113 case OMP_CLAUSE_SCHEDULE:
1114 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1115 break;
1116 /* FALLTHRU */
1117 case OMP_CLAUSE_FINAL:
1118 case OMP_CLAUSE_IF:
1119 case OMP_CLAUSE_NUM_THREADS:
1120 case OMP_CLAUSE_DEPEND:
1121 case OMP_CLAUSE_DEVICE:
1122 case OMP_CLAUSE_NUM_TEAMS:
1123 case OMP_CLAUSE_THREAD_LIMIT:
1124 case OMP_CLAUSE_SAFELEN:
1125 case OMP_CLAUSE__CILK_FOR_COUNT_:
1126 wi->val_only = true;
1127 wi->is_lhs = false;
1128 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1129 &dummy, wi);
1130 break;
1132 case OMP_CLAUSE_DIST_SCHEDULE:
1133 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1135 wi->val_only = true;
1136 wi->is_lhs = false;
1137 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1138 &dummy, wi);
1140 break;
1142 case OMP_CLAUSE_MAP:
1143 case OMP_CLAUSE_TO:
1144 case OMP_CLAUSE_FROM:
1145 if (OMP_CLAUSE_SIZE (clause))
1147 wi->val_only = true;
1148 wi->is_lhs = false;
1149 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1150 &dummy, wi);
1152 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1153 goto do_decl_clause;
1154 wi->val_only = true;
1155 wi->is_lhs = false;
1156 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1157 wi, NULL);
1158 break;
1160 case OMP_CLAUSE_ALIGNED:
1161 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1163 wi->val_only = true;
1164 wi->is_lhs = false;
1165 convert_nonlocal_reference_op
1166 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1168 /* Like do_decl_clause, but don't add any suppression. */
1169 decl = OMP_CLAUSE_DECL (clause);
1170 if (TREE_CODE (decl) == VAR_DECL
1171 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1172 break;
1173 if (decl_function_context (decl) != info->context)
1175 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1176 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1177 need_chain = true;
1179 break;
1181 case OMP_CLAUSE_NOWAIT:
1182 case OMP_CLAUSE_ORDERED:
1183 case OMP_CLAUSE_DEFAULT:
1184 case OMP_CLAUSE_COPYIN:
1185 case OMP_CLAUSE_COLLAPSE:
1186 case OMP_CLAUSE_UNTIED:
1187 case OMP_CLAUSE_MERGEABLE:
1188 case OMP_CLAUSE_PROC_BIND:
1189 break;
1191 default:
1192 gcc_unreachable ();
1196 info->suppress_expansion = new_suppress;
1198 if (need_stmts)
1199 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1200 switch (OMP_CLAUSE_CODE (clause))
1202 case OMP_CLAUSE_REDUCTION:
1203 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1205 tree old_context
1206 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1207 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1208 = info->context;
1209 walk_body (convert_nonlocal_reference_stmt,
1210 convert_nonlocal_reference_op, info,
1211 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1212 walk_body (convert_nonlocal_reference_stmt,
1213 convert_nonlocal_reference_op, info,
1214 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1215 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1216 = old_context;
1218 break;
1220 case OMP_CLAUSE_LASTPRIVATE:
1221 walk_body (convert_nonlocal_reference_stmt,
1222 convert_nonlocal_reference_op, info,
1223 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1224 break;
1226 case OMP_CLAUSE_LINEAR:
1227 walk_body (convert_nonlocal_reference_stmt,
1228 convert_nonlocal_reference_op, info,
1229 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1230 break;
1232 default:
1233 break;
1236 return need_chain;
1239 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1241 static void
1242 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1244 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1245 type = TREE_TYPE (type);
1247 if (TYPE_NAME (type)
1248 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1249 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1250 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1252 while (POINTER_TYPE_P (type)
1253 || TREE_CODE (type) == VECTOR_TYPE
1254 || TREE_CODE (type) == FUNCTION_TYPE
1255 || TREE_CODE (type) == METHOD_TYPE)
1256 type = TREE_TYPE (type);
1258 if (TREE_CODE (type) == ARRAY_TYPE)
1260 tree domain, t;
1262 note_nonlocal_vla_type (info, TREE_TYPE (type));
1263 domain = TYPE_DOMAIN (type);
1264 if (domain)
1266 t = TYPE_MIN_VALUE (domain);
1267 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1268 && decl_function_context (t) != info->context)
1269 get_nonlocal_debug_decl (info, t);
1270 t = TYPE_MAX_VALUE (domain);
1271 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1272 && decl_function_context (t) != info->context)
1273 get_nonlocal_debug_decl (info, t);
1278 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1279 in BLOCK. */
1281 static void
1282 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1284 tree var;
1286 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1287 if (TREE_CODE (var) == VAR_DECL
1288 && variably_modified_type_p (TREE_TYPE (var), NULL)
1289 && DECL_HAS_VALUE_EXPR_P (var)
1290 && decl_function_context (var) != info->context)
1291 note_nonlocal_vla_type (info, TREE_TYPE (var));
1294 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1295 PARM_DECLs that belong to outer functions. This handles statements
1296 that are not handled via the standard recursion done in
1297 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1298 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1299 operands of STMT have been handled by this function. */
1301 static tree
1302 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1303 struct walk_stmt_info *wi)
1305 struct nesting_info *info = (struct nesting_info *) wi->info;
1306 tree save_local_var_chain;
1307 bitmap save_suppress;
1308 gimple stmt = gsi_stmt (*gsi);
1310 switch (gimple_code (stmt))
1312 case GIMPLE_GOTO:
1313 /* Don't walk non-local gotos for now. */
1314 if (TREE_CODE (gimple_goto_dest (as_a <ggoto *> (stmt))) != LABEL_DECL)
1316 wi->val_only = true;
1317 wi->is_lhs = false;
1318 *handled_ops_p = true;
1319 return NULL_TREE;
1321 break;
1323 case GIMPLE_OMP_PARALLEL:
1324 case GIMPLE_OMP_TASK:
1325 save_suppress = info->suppress_expansion;
1326 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1327 wi))
1329 tree c, decl;
1330 decl = get_chain_decl (info);
1331 c = build_omp_clause (gimple_location (stmt),
1332 OMP_CLAUSE_FIRSTPRIVATE);
1333 OMP_CLAUSE_DECL (c) = decl;
1334 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1335 gimple_omp_taskreg_set_clauses (stmt, c);
1338 save_local_var_chain = info->new_local_var_chain;
1339 info->new_local_var_chain = NULL;
1341 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1342 info, gimple_omp_body_ptr (stmt));
1344 if (info->new_local_var_chain)
1345 declare_vars (info->new_local_var_chain,
1346 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1347 false);
1348 info->new_local_var_chain = save_local_var_chain;
1349 info->suppress_expansion = save_suppress;
1350 break;
1352 case GIMPLE_OMP_FOR:
1353 save_suppress = info->suppress_expansion;
1354 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1355 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1356 convert_nonlocal_reference_stmt,
1357 convert_nonlocal_reference_op, info);
1358 walk_body (convert_nonlocal_reference_stmt,
1359 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1360 info->suppress_expansion = save_suppress;
1361 break;
1363 case GIMPLE_OMP_SECTIONS:
1364 save_suppress = info->suppress_expansion;
1365 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1366 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1367 info, gimple_omp_body_ptr (stmt));
1368 info->suppress_expansion = save_suppress;
1369 break;
1371 case GIMPLE_OMP_SINGLE:
1372 save_suppress = info->suppress_expansion;
1373 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1374 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1375 info, gimple_omp_body_ptr (stmt));
1376 info->suppress_expansion = save_suppress;
1377 break;
1379 case GIMPLE_OMP_TARGET:
1380 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1382 save_suppress = info->suppress_expansion;
1383 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1384 wi);
1385 info->suppress_expansion = save_suppress;
1386 walk_body (convert_nonlocal_reference_stmt,
1387 convert_nonlocal_reference_op, info,
1388 gimple_omp_body_ptr (stmt));
1389 break;
1391 save_suppress = info->suppress_expansion;
1392 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1393 wi))
1395 tree c, decl;
1396 decl = get_chain_decl (info);
1397 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1398 OMP_CLAUSE_DECL (c) = decl;
1399 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TO;
1400 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1401 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1402 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1405 save_local_var_chain = info->new_local_var_chain;
1406 info->new_local_var_chain = NULL;
1408 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1409 info, gimple_omp_body_ptr (stmt));
1411 if (info->new_local_var_chain)
1412 declare_vars (info->new_local_var_chain,
1413 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1414 false);
1415 info->new_local_var_chain = save_local_var_chain;
1416 info->suppress_expansion = save_suppress;
1417 break;
1419 case GIMPLE_OMP_TEAMS:
1420 save_suppress = info->suppress_expansion;
1421 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1422 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1423 info, gimple_omp_body_ptr (stmt));
1424 info->suppress_expansion = save_suppress;
1425 break;
1427 case GIMPLE_OMP_SECTION:
1428 case GIMPLE_OMP_MASTER:
1429 case GIMPLE_OMP_TASKGROUP:
1430 case GIMPLE_OMP_ORDERED:
1431 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1432 info, gimple_omp_body_ptr (stmt));
1433 break;
1435 case GIMPLE_BIND:
1437 gbind *bind_stmt = as_a <gbind *> (stmt);
1438 if (!optimize && gimple_bind_block (bind_stmt))
1439 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1441 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1442 if (TREE_CODE (var) == NAMELIST_DECL)
1444 /* Adjust decls mentioned in NAMELIST_DECL. */
1445 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1446 tree decl;
1447 unsigned int i;
1449 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1451 if (TREE_CODE (decl) == VAR_DECL
1452 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1453 continue;
1454 if (decl_function_context (decl) != info->context)
1455 CONSTRUCTOR_ELT (decls, i)->value
1456 = get_nonlocal_debug_decl (info, decl);
1460 *handled_ops_p = false;
1461 return NULL_TREE;
1463 case GIMPLE_COND:
1464 wi->val_only = true;
1465 wi->is_lhs = false;
1466 *handled_ops_p = false;
1467 return NULL_TREE;
1469 default:
1470 /* For every other statement that we are not interested in
1471 handling here, let the walker traverse the operands. */
1472 *handled_ops_p = false;
1473 return NULL_TREE;
1476 /* We have handled all of STMT operands, no need to traverse the operands. */
1477 *handled_ops_p = true;
1478 return NULL_TREE;
1482 /* A subroutine of convert_local_reference. Create a local variable
1483 in the parent function with DECL_VALUE_EXPR set to reference the
1484 field in FRAME. This is used both for debug info and in OpenMP
1485 lowering. */
1487 static tree
1488 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1490 tree x, new_decl;
1492 tree *slot = &info->var_map->get_or_insert (decl);
1493 if (*slot)
1494 return *slot;
1496 /* Make sure frame_decl gets created. */
1497 (void) get_frame_type (info);
1498 x = info->frame_decl;
1499 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1501 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1502 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1503 DECL_CONTEXT (new_decl) = info->context;
1504 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1505 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1506 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1507 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1508 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1509 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1510 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1511 if ((TREE_CODE (decl) == PARM_DECL
1512 || TREE_CODE (decl) == RESULT_DECL
1513 || TREE_CODE (decl) == VAR_DECL)
1514 && DECL_BY_REFERENCE (decl))
1515 DECL_BY_REFERENCE (new_decl) = 1;
1517 SET_DECL_VALUE_EXPR (new_decl, x);
1518 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1519 *slot = new_decl;
1521 DECL_CHAIN (new_decl) = info->debug_var_chain;
1522 info->debug_var_chain = new_decl;
1524 /* Do not emit debug info twice. */
1525 DECL_IGNORED_P (decl) = 1;
1527 return new_decl;
1531 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1532 and PARM_DECLs that were referenced by inner nested functions.
1533 The rewrite will be a structure reference to the local frame variable. */
1535 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1537 static tree
1538 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1540 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1541 struct nesting_info *const info = (struct nesting_info *) wi->info;
1542 tree t = *tp, field, x;
1543 bool save_val_only;
1545 *walk_subtrees = 0;
1546 switch (TREE_CODE (t))
1548 case VAR_DECL:
1549 /* Non-automatic variables are never processed. */
1550 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1551 break;
1552 /* FALLTHRU */
1554 case PARM_DECL:
1555 if (decl_function_context (t) == info->context)
1557 /* If we copied a pointer to the frame, then the original decl
1558 is used unchanged in the parent function. */
1559 if (use_pointer_in_frame (t))
1560 break;
1562 /* No need to transform anything if no child references the
1563 variable. */
1564 field = lookup_field_for_decl (info, t, NO_INSERT);
1565 if (!field)
1566 break;
1567 wi->changed = true;
1569 x = get_local_debug_decl (info, t, field);
1570 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1571 x = get_frame_field (info, info->context, field, &wi->gsi);
1573 if (wi->val_only)
1575 if (wi->is_lhs)
1576 x = save_tmp_var (info, x, &wi->gsi);
1577 else
1578 x = init_tmp_var (info, x, &wi->gsi);
1581 *tp = x;
1583 break;
1585 case ADDR_EXPR:
1586 save_val_only = wi->val_only;
1587 wi->val_only = false;
1588 wi->is_lhs = false;
1589 wi->changed = false;
1590 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1591 wi->val_only = save_val_only;
1593 /* If we converted anything ... */
1594 if (wi->changed)
1596 tree save_context;
1598 /* Then the frame decl is now addressable. */
1599 TREE_ADDRESSABLE (info->frame_decl) = 1;
1601 save_context = current_function_decl;
1602 current_function_decl = info->context;
1603 recompute_tree_invariant_for_addr_expr (t);
1604 current_function_decl = save_context;
1606 /* If we are in a context where we only accept values, then
1607 compute the address into a temporary. */
1608 if (save_val_only)
1609 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1610 t, &wi->gsi);
1612 break;
1614 case REALPART_EXPR:
1615 case IMAGPART_EXPR:
1616 case COMPONENT_REF:
1617 case ARRAY_REF:
1618 case ARRAY_RANGE_REF:
1619 case BIT_FIELD_REF:
1620 /* Go down this entire nest and just look at the final prefix and
1621 anything that describes the references. Otherwise, we lose track
1622 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1623 save_val_only = wi->val_only;
1624 wi->val_only = true;
1625 wi->is_lhs = false;
1626 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1628 if (TREE_CODE (t) == COMPONENT_REF)
1629 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1630 NULL);
1631 else if (TREE_CODE (t) == ARRAY_REF
1632 || TREE_CODE (t) == ARRAY_RANGE_REF)
1634 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1635 NULL);
1636 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1637 NULL);
1638 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1639 NULL);
1642 wi->val_only = false;
1643 walk_tree (tp, convert_local_reference_op, wi, NULL);
1644 wi->val_only = save_val_only;
1645 break;
1647 case MEM_REF:
1648 save_val_only = wi->val_only;
1649 wi->val_only = true;
1650 wi->is_lhs = false;
1651 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1652 wi, NULL);
1653 /* We need to re-fold the MEM_REF as component references as
1654 part of a ADDR_EXPR address are not allowed. But we cannot
1655 fold here, as the chain record type is not yet finalized. */
1656 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1657 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1658 info->mem_refs->add (tp);
1659 wi->val_only = save_val_only;
1660 break;
1662 case VIEW_CONVERT_EXPR:
1663 /* Just request to look at the subtrees, leaving val_only and lhs
1664 untouched. This might actually be for !val_only + lhs, in which
1665 case we don't want to force a replacement by a temporary. */
1666 *walk_subtrees = 1;
1667 break;
1669 default:
1670 if (!IS_TYPE_OR_DECL_P (t))
1672 *walk_subtrees = 1;
1673 wi->val_only = true;
1674 wi->is_lhs = false;
1676 break;
1679 return NULL_TREE;
1682 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1683 struct walk_stmt_info *);
1685 /* Helper for convert_local_reference. Convert all the references in
1686 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1688 static bool
1689 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1691 struct nesting_info *const info = (struct nesting_info *) wi->info;
1692 bool need_frame = false, need_stmts = false;
1693 tree clause, decl;
1694 int dummy;
1695 bitmap new_suppress;
1697 new_suppress = BITMAP_GGC_ALLOC ();
1698 bitmap_copy (new_suppress, info->suppress_expansion);
1700 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1702 switch (OMP_CLAUSE_CODE (clause))
1704 case OMP_CLAUSE_REDUCTION:
1705 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1706 need_stmts = true;
1707 goto do_decl_clause;
1709 case OMP_CLAUSE_LASTPRIVATE:
1710 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1711 need_stmts = true;
1712 goto do_decl_clause;
1714 case OMP_CLAUSE_LINEAR:
1715 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1716 need_stmts = true;
1717 wi->val_only = true;
1718 wi->is_lhs = false;
1719 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1720 wi);
1721 goto do_decl_clause;
1723 case OMP_CLAUSE_PRIVATE:
1724 case OMP_CLAUSE_FIRSTPRIVATE:
1725 case OMP_CLAUSE_COPYPRIVATE:
1726 case OMP_CLAUSE_SHARED:
1727 do_decl_clause:
1728 decl = OMP_CLAUSE_DECL (clause);
1729 if (TREE_CODE (decl) == VAR_DECL
1730 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1731 break;
1732 if (decl_function_context (decl) == info->context
1733 && !use_pointer_in_frame (decl))
1735 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1736 if (field)
1738 bitmap_set_bit (new_suppress, DECL_UID (decl));
1739 OMP_CLAUSE_DECL (clause)
1740 = get_local_debug_decl (info, decl, field);
1741 need_frame = true;
1744 break;
1746 case OMP_CLAUSE_SCHEDULE:
1747 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1748 break;
1749 /* FALLTHRU */
1750 case OMP_CLAUSE_FINAL:
1751 case OMP_CLAUSE_IF:
1752 case OMP_CLAUSE_NUM_THREADS:
1753 case OMP_CLAUSE_DEPEND:
1754 case OMP_CLAUSE_DEVICE:
1755 case OMP_CLAUSE_NUM_TEAMS:
1756 case OMP_CLAUSE_THREAD_LIMIT:
1757 case OMP_CLAUSE_SAFELEN:
1758 case OMP_CLAUSE__CILK_FOR_COUNT_:
1759 wi->val_only = true;
1760 wi->is_lhs = false;
1761 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1762 wi);
1763 break;
1765 case OMP_CLAUSE_DIST_SCHEDULE:
1766 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1768 wi->val_only = true;
1769 wi->is_lhs = false;
1770 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1771 &dummy, wi);
1773 break;
1775 case OMP_CLAUSE_MAP:
1776 case OMP_CLAUSE_TO:
1777 case OMP_CLAUSE_FROM:
1778 if (OMP_CLAUSE_SIZE (clause))
1780 wi->val_only = true;
1781 wi->is_lhs = false;
1782 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1783 &dummy, wi);
1785 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1786 goto do_decl_clause;
1787 wi->val_only = true;
1788 wi->is_lhs = false;
1789 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1790 wi, NULL);
1791 break;
1793 case OMP_CLAUSE_ALIGNED:
1794 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1796 wi->val_only = true;
1797 wi->is_lhs = false;
1798 convert_local_reference_op
1799 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1801 /* Like do_decl_clause, but don't add any suppression. */
1802 decl = OMP_CLAUSE_DECL (clause);
1803 if (TREE_CODE (decl) == VAR_DECL
1804 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1805 break;
1806 if (decl_function_context (decl) == info->context
1807 && !use_pointer_in_frame (decl))
1809 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1810 if (field)
1812 OMP_CLAUSE_DECL (clause)
1813 = get_local_debug_decl (info, decl, field);
1814 need_frame = true;
1817 break;
1819 case OMP_CLAUSE_NOWAIT:
1820 case OMP_CLAUSE_ORDERED:
1821 case OMP_CLAUSE_DEFAULT:
1822 case OMP_CLAUSE_COPYIN:
1823 case OMP_CLAUSE_COLLAPSE:
1824 case OMP_CLAUSE_UNTIED:
1825 case OMP_CLAUSE_MERGEABLE:
1826 case OMP_CLAUSE_PROC_BIND:
1827 break;
1829 default:
1830 gcc_unreachable ();
1834 info->suppress_expansion = new_suppress;
1836 if (need_stmts)
1837 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1838 switch (OMP_CLAUSE_CODE (clause))
1840 case OMP_CLAUSE_REDUCTION:
1841 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1843 tree old_context
1844 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1845 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1846 = info->context;
1847 walk_body (convert_local_reference_stmt,
1848 convert_local_reference_op, info,
1849 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1850 walk_body (convert_local_reference_stmt,
1851 convert_local_reference_op, info,
1852 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1853 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1854 = old_context;
1856 break;
1858 case OMP_CLAUSE_LASTPRIVATE:
1859 walk_body (convert_local_reference_stmt,
1860 convert_local_reference_op, info,
1861 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1862 break;
1864 case OMP_CLAUSE_LINEAR:
1865 walk_body (convert_local_reference_stmt,
1866 convert_local_reference_op, info,
1867 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1868 break;
1870 default:
1871 break;
1874 return need_frame;
1878 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1879 and PARM_DECLs that were referenced by inner nested functions.
1880 The rewrite will be a structure reference to the local frame variable. */
1882 static tree
1883 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1884 struct walk_stmt_info *wi)
1886 struct nesting_info *info = (struct nesting_info *) wi->info;
1887 tree save_local_var_chain;
1888 bitmap save_suppress;
1889 gimple stmt = gsi_stmt (*gsi);
1891 switch (gimple_code (stmt))
1893 case GIMPLE_OMP_PARALLEL:
1894 case GIMPLE_OMP_TASK:
1895 save_suppress = info->suppress_expansion;
1896 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1897 wi))
1899 tree c;
1900 (void) get_frame_type (info);
1901 c = build_omp_clause (gimple_location (stmt),
1902 OMP_CLAUSE_SHARED);
1903 OMP_CLAUSE_DECL (c) = info->frame_decl;
1904 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1905 gimple_omp_taskreg_set_clauses (stmt, c);
1908 save_local_var_chain = info->new_local_var_chain;
1909 info->new_local_var_chain = NULL;
1911 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1912 gimple_omp_body_ptr (stmt));
1914 if (info->new_local_var_chain)
1915 declare_vars (info->new_local_var_chain,
1916 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1917 info->new_local_var_chain = save_local_var_chain;
1918 info->suppress_expansion = save_suppress;
1919 break;
1921 case GIMPLE_OMP_FOR:
1922 save_suppress = info->suppress_expansion;
1923 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1924 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1925 convert_local_reference_stmt,
1926 convert_local_reference_op, info);
1927 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1928 info, gimple_omp_body_ptr (stmt));
1929 info->suppress_expansion = save_suppress;
1930 break;
1932 case GIMPLE_OMP_SECTIONS:
1933 save_suppress = info->suppress_expansion;
1934 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1935 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1936 info, gimple_omp_body_ptr (stmt));
1937 info->suppress_expansion = save_suppress;
1938 break;
1940 case GIMPLE_OMP_SINGLE:
1941 save_suppress = info->suppress_expansion;
1942 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1943 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1944 info, gimple_omp_body_ptr (stmt));
1945 info->suppress_expansion = save_suppress;
1946 break;
1948 case GIMPLE_OMP_TARGET:
1949 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1951 save_suppress = info->suppress_expansion;
1952 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1953 info->suppress_expansion = save_suppress;
1954 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1955 info, gimple_omp_body_ptr (stmt));
1956 break;
1958 save_suppress = info->suppress_expansion;
1959 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1961 tree c;
1962 (void) get_frame_type (info);
1963 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1964 OMP_CLAUSE_DECL (c) = info->frame_decl;
1965 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TOFROM;
1966 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1967 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1968 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1971 save_local_var_chain = info->new_local_var_chain;
1972 info->new_local_var_chain = NULL;
1974 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1975 gimple_omp_body_ptr (stmt));
1977 if (info->new_local_var_chain)
1978 declare_vars (info->new_local_var_chain,
1979 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1980 info->new_local_var_chain = save_local_var_chain;
1981 info->suppress_expansion = save_suppress;
1982 break;
1984 case GIMPLE_OMP_TEAMS:
1985 save_suppress = info->suppress_expansion;
1986 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1987 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1988 info, gimple_omp_body_ptr (stmt));
1989 info->suppress_expansion = save_suppress;
1990 break;
1992 case GIMPLE_OMP_SECTION:
1993 case GIMPLE_OMP_MASTER:
1994 case GIMPLE_OMP_TASKGROUP:
1995 case GIMPLE_OMP_ORDERED:
1996 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1997 info, gimple_omp_body_ptr (stmt));
1998 break;
2000 case GIMPLE_COND:
2001 wi->val_only = true;
2002 wi->is_lhs = false;
2003 *handled_ops_p = false;
2004 return NULL_TREE;
2006 case GIMPLE_ASSIGN:
2007 if (gimple_clobber_p (stmt))
2009 tree lhs = gimple_assign_lhs (stmt);
2010 if (!use_pointer_in_frame (lhs)
2011 && lookup_field_for_decl (info, lhs, NO_INSERT))
2013 gsi_replace (gsi, gimple_build_nop (), true);
2014 break;
2017 *handled_ops_p = false;
2018 return NULL_TREE;
2020 case GIMPLE_BIND:
2021 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2022 var;
2023 var = DECL_CHAIN (var))
2024 if (TREE_CODE (var) == NAMELIST_DECL)
2026 /* Adjust decls mentioned in NAMELIST_DECL. */
2027 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2028 tree decl;
2029 unsigned int i;
2031 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2033 if (TREE_CODE (decl) == VAR_DECL
2034 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2035 continue;
2036 if (decl_function_context (decl) == info->context
2037 && !use_pointer_in_frame (decl))
2039 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2040 if (field)
2042 CONSTRUCTOR_ELT (decls, i)->value
2043 = get_local_debug_decl (info, decl, field);
2049 *handled_ops_p = false;
2050 return NULL_TREE;
2052 default:
2053 /* For every other statement that we are not interested in
2054 handling here, let the walker traverse the operands. */
2055 *handled_ops_p = false;
2056 return NULL_TREE;
2059 /* Indicate that we have handled all the operands ourselves. */
2060 *handled_ops_p = true;
2061 return NULL_TREE;
2065 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2066 that reference labels from outer functions. The rewrite will be a
2067 call to __builtin_nonlocal_goto. */
2069 static tree
2070 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2071 struct walk_stmt_info *wi)
2073 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2074 tree label, new_label, target_context, x, field;
2075 gcall *call;
2076 gimple stmt = gsi_stmt (*gsi);
2078 if (gimple_code (stmt) != GIMPLE_GOTO)
2080 *handled_ops_p = false;
2081 return NULL_TREE;
2084 label = gimple_goto_dest (as_a <ggoto *> (stmt));
2085 if (TREE_CODE (label) != LABEL_DECL)
2087 *handled_ops_p = false;
2088 return NULL_TREE;
2091 target_context = decl_function_context (label);
2092 if (target_context == info->context)
2094 *handled_ops_p = false;
2095 return NULL_TREE;
2098 for (i = info->outer; target_context != i->context; i = i->outer)
2099 continue;
2101 /* The original user label may also be use for a normal goto, therefore
2102 we must create a new label that will actually receive the abnormal
2103 control transfer. This new label will be marked LABEL_NONLOCAL; this
2104 mark will trigger proper behavior in the cfg, as well as cause the
2105 (hairy target-specific) non-local goto receiver code to be generated
2106 when we expand rtl. Enter this association into var_map so that we
2107 can insert the new label into the IL during a second pass. */
2108 tree *slot = &i->var_map->get_or_insert (label);
2109 if (*slot == NULL)
2111 new_label = create_artificial_label (UNKNOWN_LOCATION);
2112 DECL_NONLOCAL (new_label) = 1;
2113 *slot = new_label;
2115 else
2116 new_label = *slot;
2118 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2119 field = get_nl_goto_field (i);
2120 x = get_frame_field (info, target_context, field, gsi);
2121 x = build_addr (x, target_context);
2122 x = gsi_gimplify_val (info, x, gsi);
2123 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2124 2, build_addr (new_label, target_context), x);
2125 gsi_replace (gsi, call, false);
2127 /* We have handled all of STMT's operands, no need to keep going. */
2128 *handled_ops_p = true;
2129 return NULL_TREE;
2133 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2134 are referenced via nonlocal goto from a nested function. The rewrite
2135 will involve installing a newly generated DECL_NONLOCAL label, and
2136 (potentially) a branch around the rtl gunk that is assumed to be
2137 attached to such a label. */
2139 static tree
2140 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2141 struct walk_stmt_info *wi)
2143 struct nesting_info *const info = (struct nesting_info *) wi->info;
2144 tree label, new_label;
2145 gimple_stmt_iterator tmp_gsi;
2146 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2148 if (!stmt)
2150 *handled_ops_p = false;
2151 return NULL_TREE;
2154 label = gimple_label_label (stmt);
2156 tree *slot = info->var_map->get (label);
2157 if (!slot)
2159 *handled_ops_p = false;
2160 return NULL_TREE;
2163 /* If there's any possibility that the previous statement falls through,
2164 then we must branch around the new non-local label. */
2165 tmp_gsi = wi->gsi;
2166 gsi_prev (&tmp_gsi);
2167 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2169 gimple stmt = gimple_build_goto (label);
2170 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2173 new_label = (tree) *slot;
2174 stmt = gimple_build_label (new_label);
2175 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2177 *handled_ops_p = true;
2178 return NULL_TREE;
2182 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2183 of nested functions that require the use of trampolines. The rewrite
2184 will involve a reference a trampoline generated for the occasion. */
2186 static tree
2187 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2189 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2190 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2191 tree t = *tp, decl, target_context, x, builtin;
2192 gcall *call;
2194 *walk_subtrees = 0;
2195 switch (TREE_CODE (t))
2197 case ADDR_EXPR:
2198 /* Build
2199 T.1 = &CHAIN->tramp;
2200 T.2 = __builtin_adjust_trampoline (T.1);
2201 T.3 = (func_type)T.2;
2204 decl = TREE_OPERAND (t, 0);
2205 if (TREE_CODE (decl) != FUNCTION_DECL)
2206 break;
2208 /* Only need to process nested functions. */
2209 target_context = decl_function_context (decl);
2210 if (!target_context)
2211 break;
2213 /* If the nested function doesn't use a static chain, then
2214 it doesn't need a trampoline. */
2215 if (!DECL_STATIC_CHAIN (decl))
2216 break;
2218 /* If we don't want a trampoline, then don't build one. */
2219 if (TREE_NO_TRAMPOLINE (t))
2220 break;
2222 /* Lookup the immediate parent of the callee, as that's where
2223 we need to insert the trampoline. */
2224 for (i = info; i->context != target_context; i = i->outer)
2225 continue;
2226 x = lookup_tramp_for_decl (i, decl, INSERT);
2228 /* Compute the address of the field holding the trampoline. */
2229 x = get_frame_field (info, target_context, x, &wi->gsi);
2230 x = build_addr (x, target_context);
2231 x = gsi_gimplify_val (info, x, &wi->gsi);
2233 /* Do machine-specific ugliness. Normally this will involve
2234 computing extra alignment, but it can really be anything. */
2235 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2236 call = gimple_build_call (builtin, 1, x);
2237 x = init_tmp_var_with_call (info, &wi->gsi, call);
2239 /* Cast back to the proper function type. */
2240 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2241 x = init_tmp_var (info, x, &wi->gsi);
2243 *tp = x;
2244 break;
2246 default:
2247 if (!IS_TYPE_OR_DECL_P (t))
2248 *walk_subtrees = 1;
2249 break;
2252 return NULL_TREE;
2256 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2257 to addresses of nested functions that require the use of
2258 trampolines. The rewrite will involve a reference a trampoline
2259 generated for the occasion. */
2261 static tree
2262 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2263 struct walk_stmt_info *wi)
2265 struct nesting_info *info = (struct nesting_info *) wi->info;
2266 gimple stmt = gsi_stmt (*gsi);
2268 switch (gimple_code (stmt))
2270 case GIMPLE_CALL:
2272 /* Only walk call arguments, lest we generate trampolines for
2273 direct calls. */
2274 unsigned long i, nargs = gimple_call_num_args (stmt);
2275 for (i = 0; i < nargs; i++)
2276 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2277 wi, NULL);
2278 break;
2281 case GIMPLE_OMP_TARGET:
2282 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2284 *handled_ops_p = false;
2285 return NULL_TREE;
2287 /* FALLTHRU */
2288 case GIMPLE_OMP_PARALLEL:
2289 case GIMPLE_OMP_TASK:
2291 tree save_local_var_chain;
2292 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2293 save_local_var_chain = info->new_local_var_chain;
2294 info->new_local_var_chain = NULL;
2295 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2296 info, gimple_omp_body_ptr (stmt));
2297 if (info->new_local_var_chain)
2298 declare_vars (info->new_local_var_chain,
2299 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2300 false);
2301 info->new_local_var_chain = save_local_var_chain;
2303 break;
2305 default:
2306 *handled_ops_p = false;
2307 return NULL_TREE;
2310 *handled_ops_p = true;
2311 return NULL_TREE;
2316 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2317 that reference nested functions to make sure that the static chain
2318 is set up properly for the call. */
2320 static tree
2321 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2322 struct walk_stmt_info *wi)
2324 struct nesting_info *const info = (struct nesting_info *) wi->info;
2325 tree decl, target_context;
2326 char save_static_chain_added;
2327 int i;
2328 gimple stmt = gsi_stmt (*gsi);
2330 switch (gimple_code (stmt))
2332 case GIMPLE_CALL:
2333 if (gimple_call_chain (stmt))
2334 break;
2335 decl = gimple_call_fndecl (stmt);
2336 if (!decl)
2337 break;
2338 target_context = decl_function_context (decl);
2339 if (target_context && DECL_STATIC_CHAIN (decl))
2341 gimple_call_set_chain (as_a <gcall *> (stmt),
2342 get_static_chain (info, target_context,
2343 &wi->gsi));
2344 info->static_chain_added |= (1 << (info->context != target_context));
2346 break;
2348 case GIMPLE_OMP_PARALLEL:
2349 case GIMPLE_OMP_TASK:
2350 save_static_chain_added = info->static_chain_added;
2351 info->static_chain_added = 0;
2352 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2353 for (i = 0; i < 2; i++)
2355 tree c, decl;
2356 if ((info->static_chain_added & (1 << i)) == 0)
2357 continue;
2358 decl = i ? get_chain_decl (info) : info->frame_decl;
2359 /* Don't add CHAIN.* or FRAME.* twice. */
2360 for (c = gimple_omp_taskreg_clauses (stmt);
2362 c = OMP_CLAUSE_CHAIN (c))
2363 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2364 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2365 && OMP_CLAUSE_DECL (c) == decl)
2366 break;
2367 if (c == NULL)
2369 c = build_omp_clause (gimple_location (stmt),
2370 i ? OMP_CLAUSE_FIRSTPRIVATE
2371 : OMP_CLAUSE_SHARED);
2372 OMP_CLAUSE_DECL (c) = decl;
2373 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2374 gimple_omp_taskreg_set_clauses (stmt, c);
2377 info->static_chain_added |= save_static_chain_added;
2378 break;
2380 case GIMPLE_OMP_TARGET:
2381 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2383 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2384 break;
2386 save_static_chain_added = info->static_chain_added;
2387 info->static_chain_added = 0;
2388 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2389 for (i = 0; i < 2; i++)
2391 tree c, decl;
2392 if ((info->static_chain_added & (1 << i)) == 0)
2393 continue;
2394 decl = i ? get_chain_decl (info) : info->frame_decl;
2395 /* Don't add CHAIN.* or FRAME.* twice. */
2396 for (c = gimple_omp_target_clauses (stmt);
2398 c = OMP_CLAUSE_CHAIN (c))
2399 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2400 && OMP_CLAUSE_DECL (c) == decl)
2401 break;
2402 if (c == NULL)
2404 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2405 OMP_CLAUSE_DECL (c) = decl;
2406 OMP_CLAUSE_MAP_KIND (c)
2407 = i ? OMP_CLAUSE_MAP_TO : OMP_CLAUSE_MAP_TOFROM;
2408 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2409 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2410 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2414 info->static_chain_added |= save_static_chain_added;
2415 break;
2417 case GIMPLE_OMP_FOR:
2418 walk_body (convert_gimple_call, NULL, info,
2419 gimple_omp_for_pre_body_ptr (stmt));
2420 /* FALLTHRU */
2421 case GIMPLE_OMP_SECTIONS:
2422 case GIMPLE_OMP_SECTION:
2423 case GIMPLE_OMP_SINGLE:
2424 case GIMPLE_OMP_TEAMS:
2425 case GIMPLE_OMP_MASTER:
2426 case GIMPLE_OMP_TASKGROUP:
2427 case GIMPLE_OMP_ORDERED:
2428 case GIMPLE_OMP_CRITICAL:
2429 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2430 break;
2432 default:
2433 /* Keep looking for other operands. */
2434 *handled_ops_p = false;
2435 return NULL_TREE;
2438 *handled_ops_p = true;
2439 return NULL_TREE;
2442 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2443 call expressions. At the same time, determine if a nested function
2444 actually uses its static chain; if not, remember that. */
2446 static void
2447 convert_all_function_calls (struct nesting_info *root)
2449 unsigned int chain_count = 0, old_chain_count, iter_count;
2450 struct nesting_info *n;
2452 /* First, optimistically clear static_chain for all decls that haven't
2453 used the static chain already for variable access. But always create
2454 it if not optimizing. This makes it possible to reconstruct the static
2455 nesting tree at run time and thus to resolve up-level references from
2456 within the debugger. */
2457 FOR_EACH_NEST_INFO (n, root)
2459 tree decl = n->context;
2460 if (!optimize)
2462 if (n->inner)
2463 (void) get_frame_type (n);
2464 if (n->outer)
2465 (void) get_chain_decl (n);
2467 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2469 DECL_STATIC_CHAIN (decl) = 0;
2470 if (dump_file && (dump_flags & TDF_DETAILS))
2471 fprintf (dump_file, "Guessing no static-chain for %s\n",
2472 lang_hooks.decl_printable_name (decl, 2));
2474 else
2475 DECL_STATIC_CHAIN (decl) = 1;
2476 chain_count += DECL_STATIC_CHAIN (decl);
2479 /* Walk the functions and perform transformations. Note that these
2480 transformations can induce new uses of the static chain, which in turn
2481 require re-examining all users of the decl. */
2482 /* ??? It would make sense to try to use the call graph to speed this up,
2483 but the call graph hasn't really been built yet. Even if it did, we
2484 would still need to iterate in this loop since address-of references
2485 wouldn't show up in the callgraph anyway. */
2486 iter_count = 0;
2489 old_chain_count = chain_count;
2490 chain_count = 0;
2491 iter_count++;
2493 if (dump_file && (dump_flags & TDF_DETAILS))
2494 fputc ('\n', dump_file);
2496 FOR_EACH_NEST_INFO (n, root)
2498 tree decl = n->context;
2499 walk_function (convert_tramp_reference_stmt,
2500 convert_tramp_reference_op, n);
2501 walk_function (convert_gimple_call, NULL, n);
2502 chain_count += DECL_STATIC_CHAIN (decl);
2505 while (chain_count != old_chain_count);
2507 if (dump_file && (dump_flags & TDF_DETAILS))
2508 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2509 iter_count);
2512 struct nesting_copy_body_data
2514 copy_body_data cb;
2515 struct nesting_info *root;
2518 /* A helper subroutine for debug_var_chain type remapping. */
2520 static tree
2521 nesting_copy_decl (tree decl, copy_body_data *id)
2523 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2524 tree *slot = nid->root->var_map->get (decl);
2526 if (slot)
2527 return (tree) *slot;
2529 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2531 tree new_decl = copy_decl_no_change (decl, id);
2532 DECL_ORIGINAL_TYPE (new_decl)
2533 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2534 return new_decl;
2537 if (TREE_CODE (decl) == VAR_DECL
2538 || TREE_CODE (decl) == PARM_DECL
2539 || TREE_CODE (decl) == RESULT_DECL)
2540 return decl;
2542 return copy_decl_no_change (decl, id);
2545 /* A helper function for remap_vla_decls. See if *TP contains
2546 some remapped variables. */
2548 static tree
2549 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2551 struct nesting_info *root = (struct nesting_info *) data;
2552 tree t = *tp;
2554 if (DECL_P (t))
2556 *walk_subtrees = 0;
2557 tree *slot = root->var_map->get (t);
2559 if (slot)
2560 return *slot;
2562 return NULL;
2565 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2566 involved. */
2568 static void
2569 remap_vla_decls (tree block, struct nesting_info *root)
2571 tree var, subblock, val, type;
2572 struct nesting_copy_body_data id;
2574 for (subblock = BLOCK_SUBBLOCKS (block);
2575 subblock;
2576 subblock = BLOCK_CHAIN (subblock))
2577 remap_vla_decls (subblock, root);
2579 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2580 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2582 val = DECL_VALUE_EXPR (var);
2583 type = TREE_TYPE (var);
2585 if (!(TREE_CODE (val) == INDIRECT_REF
2586 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2587 && variably_modified_type_p (type, NULL)))
2588 continue;
2590 if (root->var_map->get (TREE_OPERAND (val, 0))
2591 || walk_tree (&type, contains_remapped_vars, root, NULL))
2592 break;
2595 if (var == NULL_TREE)
2596 return;
2598 memset (&id, 0, sizeof (id));
2599 id.cb.copy_decl = nesting_copy_decl;
2600 id.cb.decl_map = new hash_map<tree, tree>;
2601 id.root = root;
2603 for (; var; var = DECL_CHAIN (var))
2604 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2606 struct nesting_info *i;
2607 tree newt, context;
2609 val = DECL_VALUE_EXPR (var);
2610 type = TREE_TYPE (var);
2612 if (!(TREE_CODE (val) == INDIRECT_REF
2613 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2614 && variably_modified_type_p (type, NULL)))
2615 continue;
2617 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2618 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2619 continue;
2621 context = decl_function_context (var);
2622 for (i = root; i; i = i->outer)
2623 if (i->context == context)
2624 break;
2626 if (i == NULL)
2627 continue;
2629 /* Fully expand value expressions. This avoids having debug variables
2630 only referenced from them and that can be swept during GC. */
2631 if (slot)
2633 tree t = (tree) *slot;
2634 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2635 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2638 id.cb.src_fn = i->context;
2639 id.cb.dst_fn = i->context;
2640 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2642 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2643 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2645 newt = TREE_TYPE (newt);
2646 type = TREE_TYPE (type);
2648 if (TYPE_NAME (newt)
2649 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2650 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2651 && newt != type
2652 && TYPE_NAME (newt) == TYPE_NAME (type))
2653 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2655 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2656 if (val != DECL_VALUE_EXPR (var))
2657 SET_DECL_VALUE_EXPR (var, val);
2660 delete id.cb.decl_map;
2663 /* Fold the MEM_REF *E. */
2664 bool
2665 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2667 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2668 *ref_p = fold (*ref_p);
2669 return true;
2672 /* Do "everything else" to clean up or complete state collected by the
2673 various walking passes -- lay out the types and decls, generate code
2674 to initialize the frame decl, store critical expressions in the
2675 struct function for rtl to find. */
2677 static void
2678 finalize_nesting_tree_1 (struct nesting_info *root)
2680 gimple_seq stmt_list;
2681 gimple stmt;
2682 tree context = root->context;
2683 struct function *sf;
2685 stmt_list = NULL;
2687 /* If we created a non-local frame type or decl, we need to lay them
2688 out at this time. */
2689 if (root->frame_type)
2691 /* In some cases the frame type will trigger the -Wpadded warning.
2692 This is not helpful; suppress it. */
2693 int save_warn_padded = warn_padded;
2694 tree *adjust;
2696 warn_padded = 0;
2697 layout_type (root->frame_type);
2698 warn_padded = save_warn_padded;
2699 layout_decl (root->frame_decl, 0);
2701 /* Remove root->frame_decl from root->new_local_var_chain, so
2702 that we can declare it also in the lexical blocks, which
2703 helps ensure virtual regs that end up appearing in its RTL
2704 expression get substituted in instantiate_virtual_regs(). */
2705 for (adjust = &root->new_local_var_chain;
2706 *adjust != root->frame_decl;
2707 adjust = &DECL_CHAIN (*adjust))
2708 gcc_assert (DECL_CHAIN (*adjust));
2709 *adjust = DECL_CHAIN (*adjust);
2711 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2712 declare_vars (root->frame_decl,
2713 gimple_seq_first_stmt (gimple_body (context)), true);
2716 /* If any parameters were referenced non-locally, then we need to
2717 insert a copy. Likewise, if any variables were referenced by
2718 pointer, we need to initialize the address. */
2719 if (root->any_parm_remapped)
2721 tree p;
2722 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2724 tree field, x, y;
2726 field = lookup_field_for_decl (root, p, NO_INSERT);
2727 if (!field)
2728 continue;
2730 if (use_pointer_in_frame (p))
2731 x = build_addr (p, context);
2732 else
2733 x = p;
2735 /* If the assignment is from a non-register the stmt is
2736 not valid gimple. Make it so by using a temporary instead. */
2737 if (!is_gimple_reg (x)
2738 && is_gimple_reg_type (TREE_TYPE (x)))
2740 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2741 x = init_tmp_var (root, x, &gsi);
2744 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2745 root->frame_decl, field, NULL_TREE);
2746 stmt = gimple_build_assign (y, x);
2747 gimple_seq_add_stmt (&stmt_list, stmt);
2751 /* If a chain_field was created, then it needs to be initialized
2752 from chain_decl. */
2753 if (root->chain_field)
2755 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2756 root->frame_decl, root->chain_field, NULL_TREE);
2757 stmt = gimple_build_assign (x, get_chain_decl (root));
2758 gimple_seq_add_stmt (&stmt_list, stmt);
2761 /* If trampolines were created, then we need to initialize them. */
2762 if (root->any_tramp_created)
2764 struct nesting_info *i;
2765 for (i = root->inner; i ; i = i->next)
2767 tree arg1, arg2, arg3, x, field;
2769 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2770 if (!field)
2771 continue;
2773 gcc_assert (DECL_STATIC_CHAIN (i->context));
2774 arg3 = build_addr (root->frame_decl, context);
2776 arg2 = build_addr (i->context, context);
2778 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2779 root->frame_decl, field, NULL_TREE);
2780 arg1 = build_addr (x, context);
2782 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2783 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2784 gimple_seq_add_stmt (&stmt_list, stmt);
2788 /* If we created initialization statements, insert them. */
2789 if (stmt_list)
2791 gbind *bind;
2792 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2793 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2794 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2795 gimple_bind_set_body (bind, stmt_list);
2798 /* If a chain_decl was created, then it needs to be registered with
2799 struct function so that it gets initialized from the static chain
2800 register at the beginning of the function. */
2801 sf = DECL_STRUCT_FUNCTION (root->context);
2802 sf->static_chain_decl = root->chain_decl;
2804 /* Similarly for the non-local goto save area. */
2805 if (root->nl_goto_field)
2807 sf->nonlocal_goto_save_area
2808 = get_frame_field (root, context, root->nl_goto_field, NULL);
2809 sf->has_nonlocal_label = 1;
2812 /* Make sure all new local variables get inserted into the
2813 proper BIND_EXPR. */
2814 if (root->new_local_var_chain)
2815 declare_vars (root->new_local_var_chain,
2816 gimple_seq_first_stmt (gimple_body (root->context)),
2817 false);
2819 if (root->debug_var_chain)
2821 tree debug_var;
2822 gbind *scope;
2824 remap_vla_decls (DECL_INITIAL (root->context), root);
2826 for (debug_var = root->debug_var_chain; debug_var;
2827 debug_var = DECL_CHAIN (debug_var))
2828 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2829 break;
2831 /* If there are any debug decls with variable length types,
2832 remap those types using other debug_var_chain variables. */
2833 if (debug_var)
2835 struct nesting_copy_body_data id;
2837 memset (&id, 0, sizeof (id));
2838 id.cb.copy_decl = nesting_copy_decl;
2839 id.cb.decl_map = new hash_map<tree, tree>;
2840 id.root = root;
2842 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2843 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2845 tree type = TREE_TYPE (debug_var);
2846 tree newt, t = type;
2847 struct nesting_info *i;
2849 for (i = root; i; i = i->outer)
2850 if (variably_modified_type_p (type, i->context))
2851 break;
2853 if (i == NULL)
2854 continue;
2856 id.cb.src_fn = i->context;
2857 id.cb.dst_fn = i->context;
2858 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2860 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2861 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2863 newt = TREE_TYPE (newt);
2864 t = TREE_TYPE (t);
2866 if (TYPE_NAME (newt)
2867 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2868 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2869 && newt != t
2870 && TYPE_NAME (newt) == TYPE_NAME (t))
2871 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2874 delete id.cb.decl_map;
2877 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
2878 if (gimple_bind_block (scope))
2879 declare_vars (root->debug_var_chain, scope, true);
2880 else
2881 BLOCK_VARS (DECL_INITIAL (root->context))
2882 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2883 root->debug_var_chain);
2886 /* Fold the rewritten MEM_REF trees. */
2887 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2889 /* Dump the translated tree function. */
2890 if (dump_file)
2892 fputs ("\n\n", dump_file);
2893 dump_function_to_file (root->context, dump_file, dump_flags);
2897 static void
2898 finalize_nesting_tree (struct nesting_info *root)
2900 struct nesting_info *n;
2901 FOR_EACH_NEST_INFO (n, root)
2902 finalize_nesting_tree_1 (n);
2905 /* Unnest the nodes and pass them to cgraph. */
2907 static void
2908 unnest_nesting_tree_1 (struct nesting_info *root)
2910 struct cgraph_node *node = cgraph_node::get (root->context);
2912 /* For nested functions update the cgraph to reflect unnesting.
2913 We also delay finalizing of these functions up to this point. */
2914 if (node->origin)
2916 node->unnest ();
2917 cgraph_node::finalize_function (root->context, true);
2921 static void
2922 unnest_nesting_tree (struct nesting_info *root)
2924 struct nesting_info *n;
2925 FOR_EACH_NEST_INFO (n, root)
2926 unnest_nesting_tree_1 (n);
2929 /* Free the data structures allocated during this pass. */
2931 static void
2932 free_nesting_tree (struct nesting_info *root)
2934 struct nesting_info *node, *next;
2936 node = iter_nestinfo_start (root);
2939 next = iter_nestinfo_next (node);
2940 delete node->var_map;
2941 delete node->field_map;
2942 delete node->mem_refs;
2943 free (node);
2944 node = next;
2946 while (node);
2949 /* Gimplify a function and all its nested functions. */
2950 static void
2951 gimplify_all_functions (struct cgraph_node *root)
2953 struct cgraph_node *iter;
2954 if (!gimple_body (root->decl))
2955 gimplify_function_tree (root->decl);
2956 for (iter = root->nested; iter; iter = iter->next_nested)
2957 gimplify_all_functions (iter);
2960 /* Main entry point for this pass. Process FNDECL and all of its nested
2961 subroutines and turn them into something less tightly bound. */
2963 void
2964 lower_nested_functions (tree fndecl)
2966 struct cgraph_node *cgn;
2967 struct nesting_info *root;
2969 /* If there are no nested functions, there's nothing to do. */
2970 cgn = cgraph_node::get (fndecl);
2971 if (!cgn->nested)
2972 return;
2974 gimplify_all_functions (cgn);
2976 dump_file = dump_begin (TDI_nested, &dump_flags);
2977 if (dump_file)
2978 fprintf (dump_file, "\n;; Function %s\n\n",
2979 lang_hooks.decl_printable_name (fndecl, 2));
2981 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2982 root = create_nesting_tree (cgn);
2984 walk_all_functions (convert_nonlocal_reference_stmt,
2985 convert_nonlocal_reference_op,
2986 root);
2987 walk_all_functions (convert_local_reference_stmt,
2988 convert_local_reference_op,
2989 root);
2990 walk_all_functions (convert_nl_goto_reference, NULL, root);
2991 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2993 convert_all_function_calls (root);
2994 finalize_nesting_tree (root);
2995 unnest_nesting_tree (root);
2997 free_nesting_tree (root);
2998 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3000 if (dump_file)
3002 dump_end (TDI_nested, dump_file);
3003 dump_file = NULL;
3007 #include "gt-tree-nested.h"