Replace enum omp_clause_map_kind with enum gomp_map_kind.
[official-gcc.git] / gcc / tree-nested.c
blob29326ed4852035010c9bcc72c7fc1bb32c10c884
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2015 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 "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "wide-int.h"
32 #include "inchash.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "stringpool.h"
36 #include "stor-layout.h"
37 #include "tm_p.h"
38 #include "hard-reg-set.h"
39 #include "input.h"
40 #include "function.h"
41 #include "tree-dump.h"
42 #include "tree-inline.h"
43 #include "predict.h"
44 #include "basic-block.h"
45 #include "tree-ssa-alias.h"
46 #include "internal-fn.h"
47 #include "gimple-expr.h"
48 #include "is-a.h"
49 #include "gimple.h"
50 #include "gimplify.h"
51 #include "gimple-iterator.h"
52 #include "gimple-walk.h"
53 #include "tree-iterator.h"
54 #include "bitmap.h"
55 #include "hash-map.h"
56 #include "plugin-api.h"
57 #include "ipa-ref.h"
58 #include "cgraph.h"
59 #include "tree-cfg.h"
60 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
61 #include "langhooks.h"
62 #include "gimple-low.h"
63 #include "gomp-constants.h"
66 /* The object of this pass is to lower the representation of a set of nested
67 functions in order to expose all of the gory details of the various
68 nonlocal references. We want to do this sooner rather than later, in
69 order to give us more freedom in emitting all of the functions in question.
71 Back in olden times, when gcc was young, we developed an insanely
72 complicated scheme whereby variables which were referenced nonlocally
73 were forced to live in the stack of the declaring function, and then
74 the nested functions magically discovered where these variables were
75 placed. In order for this scheme to function properly, it required
76 that the outer function be partially expanded, then we switch to
77 compiling the inner function, and once done with those we switch back
78 to compiling the outer function. Such delicate ordering requirements
79 makes it difficult to do whole translation unit optimizations
80 involving such functions.
82 The implementation here is much more direct. Everything that can be
83 referenced by an inner function is a member of an explicitly created
84 structure herein called the "nonlocal frame struct". The incoming
85 static chain for a nested function is a pointer to this struct in
86 the parent. In this way, we settle on known offsets from a known
87 base, and so are decoupled from the logic that places objects in the
88 function's stack frame. More importantly, we don't have to wait for
89 that to happen -- since the compilation of the inner function is no
90 longer tied to a real stack frame, the nonlocal frame struct can be
91 allocated anywhere. Which means that the outer function is now
92 inlinable.
94 Theory of operation here is very simple. Iterate over all the
95 statements in all the functions (depth first) several times,
96 allocating structures and fields on demand. In general we want to
97 examine inner functions first, so that we can avoid making changes
98 to outer functions which are unnecessary.
100 The order of the passes matters a bit, in that later passes will be
101 skipped if it is discovered that the functions don't actually interact
102 at all. That is, they're nested in the lexical sense but could have
103 been written as independent functions without change. */
106 struct nesting_info
108 struct nesting_info *outer;
109 struct nesting_info *inner;
110 struct nesting_info *next;
112 hash_map<tree, tree> *field_map;
113 hash_map<tree, tree> *var_map;
114 hash_set<tree *> *mem_refs;
115 bitmap suppress_expansion;
117 tree context;
118 tree new_local_var_chain;
119 tree debug_var_chain;
120 tree frame_type;
121 tree frame_decl;
122 tree chain_field;
123 tree chain_decl;
124 tree nl_goto_field;
126 bool any_parm_remapped;
127 bool any_tramp_created;
128 char static_chain_added;
132 /* Iterate over the nesting tree, starting with ROOT, depth first. */
134 static inline struct nesting_info *
135 iter_nestinfo_start (struct nesting_info *root)
137 while (root->inner)
138 root = root->inner;
139 return root;
142 static inline struct nesting_info *
143 iter_nestinfo_next (struct nesting_info *node)
145 if (node->next)
146 return iter_nestinfo_start (node->next);
147 return node->outer;
150 #define FOR_EACH_NEST_INFO(I, ROOT) \
151 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
153 /* Obstack used for the bitmaps in the struct above. */
154 static struct bitmap_obstack nesting_info_bitmap_obstack;
157 /* We're working in so many different function contexts simultaneously,
158 that create_tmp_var is dangerous. Prevent mishap. */
159 #define create_tmp_var cant_use_create_tmp_var_here_dummy
161 /* Like create_tmp_var, except record the variable for registration at
162 the given nesting level. */
164 static tree
165 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
167 tree tmp_var;
169 /* If the type is of variable size or a type which must be created by the
170 frontend, something is wrong. Note that we explicitly allow
171 incomplete types here, since we create them ourselves here. */
172 gcc_assert (!TREE_ADDRESSABLE (type));
173 gcc_assert (!TYPE_SIZE_UNIT (type)
174 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
176 tmp_var = create_tmp_var_raw (type, prefix);
177 DECL_CONTEXT (tmp_var) = info->context;
178 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
179 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
180 if (TREE_CODE (type) == COMPLEX_TYPE
181 || TREE_CODE (type) == VECTOR_TYPE)
182 DECL_GIMPLE_REG_P (tmp_var) = 1;
184 info->new_local_var_chain = tmp_var;
186 return tmp_var;
189 /* Take the address of EXP to be used within function CONTEXT.
190 Mark it for addressability as necessary. */
192 tree
193 build_addr (tree exp, tree context)
195 tree base = exp;
196 tree save_context;
197 tree retval;
199 while (handled_component_p (base))
200 base = TREE_OPERAND (base, 0);
202 if (DECL_P (base))
203 TREE_ADDRESSABLE (base) = 1;
205 /* Building the ADDR_EXPR will compute a set of properties for
206 that ADDR_EXPR. Those properties are unfortunately context
207 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
209 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
210 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
211 way the properties are for the ADDR_EXPR are computed properly. */
212 save_context = current_function_decl;
213 current_function_decl = context;
214 retval = build_fold_addr_expr (exp);
215 current_function_decl = save_context;
216 return retval;
219 /* Insert FIELD into TYPE, sorted by alignment requirements. */
221 void
222 insert_field_into_struct (tree type, tree field)
224 tree *p;
226 DECL_CONTEXT (field) = type;
228 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
229 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
230 break;
232 DECL_CHAIN (field) = *p;
233 *p = field;
235 /* Set correct alignment for frame struct type. */
236 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
237 TYPE_ALIGN (type) = DECL_ALIGN (field);
240 /* Build or return the RECORD_TYPE that describes the frame state that is
241 shared between INFO->CONTEXT and its nested functions. This record will
242 not be complete until finalize_nesting_tree; up until that point we'll
243 be adding fields as necessary.
245 We also build the DECL that represents this frame in the function. */
247 static tree
248 get_frame_type (struct nesting_info *info)
250 tree type = info->frame_type;
251 if (!type)
253 char *name;
255 type = make_node (RECORD_TYPE);
257 name = concat ("FRAME.",
258 IDENTIFIER_POINTER (DECL_NAME (info->context)),
259 NULL);
260 TYPE_NAME (type) = get_identifier (name);
261 free (name);
263 info->frame_type = type;
264 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
265 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
267 /* ??? Always make it addressable for now, since it is meant to
268 be pointed to by the static chain pointer. This pessimizes
269 when it turns out that no static chains are needed because
270 the nested functions referencing non-local variables are not
271 reachable, but the true pessimization is to create the non-
272 local frame structure in the first place. */
273 TREE_ADDRESSABLE (info->frame_decl) = 1;
275 return type;
278 /* Return true if DECL should be referenced by pointer in the non-local
279 frame structure. */
281 static bool
282 use_pointer_in_frame (tree decl)
284 if (TREE_CODE (decl) == PARM_DECL)
286 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
287 sized decls, and inefficient to copy large aggregates. Don't bother
288 moving anything but scalar variables. */
289 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
291 else
293 /* Variable sized types make things "interesting" in the frame. */
294 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
298 /* Given DECL, a non-locally accessed variable, find or create a field
299 in the non-local frame structure for the given nesting context. */
301 static tree
302 lookup_field_for_decl (struct nesting_info *info, tree decl,
303 enum insert_option insert)
305 if (insert == NO_INSERT)
307 tree *slot = info->field_map->get (decl);
308 return slot ? *slot : NULL_TREE;
311 tree *slot = &info->field_map->get_or_insert (decl);
312 if (!*slot)
314 tree field = make_node (FIELD_DECL);
315 DECL_NAME (field) = DECL_NAME (decl);
317 if (use_pointer_in_frame (decl))
319 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
320 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
321 DECL_NONADDRESSABLE_P (field) = 1;
323 else
325 TREE_TYPE (field) = TREE_TYPE (decl);
326 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
327 DECL_ALIGN (field) = DECL_ALIGN (decl);
328 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
329 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
330 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
331 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
334 insert_field_into_struct (get_frame_type (info), field);
335 *slot = field;
337 if (TREE_CODE (decl) == PARM_DECL)
338 info->any_parm_remapped = true;
341 return *slot;
344 /* Build or return the variable that holds the static chain within
345 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
347 static tree
348 get_chain_decl (struct nesting_info *info)
350 tree decl = info->chain_decl;
352 if (!decl)
354 tree type;
356 type = get_frame_type (info->outer);
357 type = build_pointer_type (type);
359 /* Note that this variable is *not* entered into any BIND_EXPR;
360 the construction of this variable is handled specially in
361 expand_function_start and initialize_inlined_parameters.
362 Note also that it's represented as a parameter. This is more
363 close to the truth, since the initial value does come from
364 the caller. */
365 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
366 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
367 DECL_ARTIFICIAL (decl) = 1;
368 DECL_IGNORED_P (decl) = 1;
369 TREE_USED (decl) = 1;
370 DECL_CONTEXT (decl) = info->context;
371 DECL_ARG_TYPE (decl) = type;
373 /* Tell tree-inline.c that we never write to this variable, so
374 it can copy-prop the replacement value immediately. */
375 TREE_READONLY (decl) = 1;
377 info->chain_decl = decl;
379 if (dump_file
380 && (dump_flags & TDF_DETAILS)
381 && !DECL_STATIC_CHAIN (info->context))
382 fprintf (dump_file, "Setting static-chain for %s\n",
383 lang_hooks.decl_printable_name (info->context, 2));
385 DECL_STATIC_CHAIN (info->context) = 1;
387 return decl;
390 /* Build or return the field within the non-local frame state that holds
391 the static chain for INFO->CONTEXT. This is the way to walk back up
392 multiple nesting levels. */
394 static tree
395 get_chain_field (struct nesting_info *info)
397 tree field = info->chain_field;
399 if (!field)
401 tree type = build_pointer_type (get_frame_type (info->outer));
403 field = make_node (FIELD_DECL);
404 DECL_NAME (field) = get_identifier ("__chain");
405 TREE_TYPE (field) = type;
406 DECL_ALIGN (field) = TYPE_ALIGN (type);
407 DECL_NONADDRESSABLE_P (field) = 1;
409 insert_field_into_struct (get_frame_type (info), field);
411 info->chain_field = field;
413 if (dump_file
414 && (dump_flags & TDF_DETAILS)
415 && !DECL_STATIC_CHAIN (info->context))
416 fprintf (dump_file, "Setting static-chain for %s\n",
417 lang_hooks.decl_printable_name (info->context, 2));
419 DECL_STATIC_CHAIN (info->context) = 1;
421 return field;
424 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
426 static tree
427 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
428 gcall *call)
430 tree t;
432 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
433 gimple_call_set_lhs (call, t);
434 if (! gsi_end_p (*gsi))
435 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
436 gsi_insert_before (gsi, call, GSI_SAME_STMT);
438 return t;
442 /* Copy EXP into a temporary. Allocate the temporary in the context of
443 INFO and insert the initialization statement before GSI. */
445 static tree
446 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
448 tree t;
449 gimple stmt;
451 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
452 stmt = gimple_build_assign (t, exp);
453 if (! gsi_end_p (*gsi))
454 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
455 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
457 return t;
461 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
463 static tree
464 gsi_gimplify_val (struct nesting_info *info, tree exp,
465 gimple_stmt_iterator *gsi)
467 if (is_gimple_val (exp))
468 return exp;
469 else
470 return init_tmp_var (info, exp, gsi);
473 /* Similarly, but copy from the temporary and insert the statement
474 after the iterator. */
476 static tree
477 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
479 tree t;
480 gimple stmt;
482 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
483 stmt = gimple_build_assign (exp, t);
484 if (! gsi_end_p (*gsi))
485 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
486 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
488 return t;
491 /* Build or return the type used to represent a nested function trampoline. */
493 static GTY(()) tree trampoline_type;
495 static tree
496 get_trampoline_type (struct nesting_info *info)
498 unsigned align, size;
499 tree t;
501 if (trampoline_type)
502 return trampoline_type;
504 align = TRAMPOLINE_ALIGNMENT;
505 size = TRAMPOLINE_SIZE;
507 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
508 then allocate extra space so that we can do dynamic alignment. */
509 if (align > STACK_BOUNDARY)
511 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
512 align = STACK_BOUNDARY;
515 t = build_index_type (size_int (size - 1));
516 t = build_array_type (char_type_node, t);
517 t = build_decl (DECL_SOURCE_LOCATION (info->context),
518 FIELD_DECL, get_identifier ("__data"), t);
519 DECL_ALIGN (t) = align;
520 DECL_USER_ALIGN (t) = 1;
522 trampoline_type = make_node (RECORD_TYPE);
523 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
524 TYPE_FIELDS (trampoline_type) = t;
525 layout_type (trampoline_type);
526 DECL_CONTEXT (t) = trampoline_type;
528 return trampoline_type;
531 /* Given DECL, a nested function, find or create a field in the non-local
532 frame structure for a trampoline for this function. */
534 static tree
535 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
536 enum insert_option insert)
538 if (insert == NO_INSERT)
540 tree *slot = info->var_map->get (decl);
541 return slot ? *slot : NULL_TREE;
544 tree *slot = &info->var_map->get_or_insert (decl);
545 if (!*slot)
547 tree field = make_node (FIELD_DECL);
548 DECL_NAME (field) = DECL_NAME (decl);
549 TREE_TYPE (field) = get_trampoline_type (info);
550 TREE_ADDRESSABLE (field) = 1;
552 insert_field_into_struct (get_frame_type (info), field);
553 *slot = field;
555 info->any_tramp_created = true;
558 return *slot;
561 /* Build or return the field within the non-local frame state that holds
562 the non-local goto "jmp_buf". The buffer itself is maintained by the
563 rtl middle-end as dynamic stack space is allocated. */
565 static tree
566 get_nl_goto_field (struct nesting_info *info)
568 tree field = info->nl_goto_field;
569 if (!field)
571 unsigned size;
572 tree type;
574 /* For __builtin_nonlocal_goto, we need N words. The first is the
575 frame pointer, the rest is for the target's stack pointer save
576 area. The number of words is controlled by STACK_SAVEAREA_MODE;
577 not the best interface, but it'll do for now. */
578 if (Pmode == ptr_mode)
579 type = ptr_type_node;
580 else
581 type = lang_hooks.types.type_for_mode (Pmode, 1);
583 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
584 size = size / GET_MODE_SIZE (Pmode);
585 size = size + 1;
587 type = build_array_type
588 (type, build_index_type (size_int (size)));
590 field = make_node (FIELD_DECL);
591 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
592 TREE_TYPE (field) = type;
593 DECL_ALIGN (field) = TYPE_ALIGN (type);
594 TREE_ADDRESSABLE (field) = 1;
596 insert_field_into_struct (get_frame_type (info), field);
598 info->nl_goto_field = field;
601 return field;
604 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
606 static void
607 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
608 struct nesting_info *info, gimple_seq *pseq)
610 struct walk_stmt_info wi;
612 memset (&wi, 0, sizeof (wi));
613 wi.info = info;
614 wi.val_only = true;
615 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
619 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
621 static inline void
622 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
623 struct nesting_info *info)
625 gimple_seq body = gimple_body (info->context);
626 walk_body (callback_stmt, callback_op, info, &body);
627 gimple_set_body (info->context, body);
630 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
632 static void
633 walk_gimple_omp_for (gomp_for *for_stmt,
634 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
635 struct nesting_info *info)
637 struct walk_stmt_info wi;
638 gimple_seq seq;
639 tree t;
640 size_t i;
642 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
644 seq = NULL;
645 memset (&wi, 0, sizeof (wi));
646 wi.info = info;
647 wi.gsi = gsi_last (seq);
649 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
651 wi.val_only = false;
652 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
653 &wi, NULL);
654 wi.val_only = true;
655 wi.is_lhs = false;
656 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
657 &wi, NULL);
659 wi.val_only = true;
660 wi.is_lhs = false;
661 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
662 &wi, NULL);
664 t = gimple_omp_for_incr (for_stmt, i);
665 gcc_assert (BINARY_CLASS_P (t));
666 wi.val_only = false;
667 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
668 wi.val_only = true;
669 wi.is_lhs = false;
670 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
673 seq = gsi_seq (wi.gsi);
674 if (!gimple_seq_empty_p (seq))
676 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
677 annotate_all_with_location (seq, gimple_location (for_stmt));
678 gimple_seq_add_seq (&pre_body, seq);
679 gimple_omp_for_set_pre_body (for_stmt, pre_body);
683 /* Similarly for ROOT and all functions nested underneath, depth first. */
685 static void
686 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
687 struct nesting_info *root)
689 struct nesting_info *n;
690 FOR_EACH_NEST_INFO (n, root)
691 walk_function (callback_stmt, callback_op, n);
695 /* We have to check for a fairly pathological case. The operands of function
696 nested function are to be interpreted in the context of the enclosing
697 function. So if any are variably-sized, they will get remapped when the
698 enclosing function is inlined. But that remapping would also have to be
699 done in the types of the PARM_DECLs of the nested function, meaning the
700 argument types of that function will disagree with the arguments in the
701 calls to that function. So we'd either have to make a copy of the nested
702 function corresponding to each time the enclosing function was inlined or
703 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
704 function. The former is not practical. The latter would still require
705 detecting this case to know when to add the conversions. So, for now at
706 least, we don't inline such an enclosing function.
708 We have to do that check recursively, so here return indicating whether
709 FNDECL has such a nested function. ORIG_FN is the function we were
710 trying to inline to use for checking whether any argument is variably
711 modified by anything in it.
713 It would be better to do this in tree-inline.c so that we could give
714 the appropriate warning for why a function can't be inlined, but that's
715 too late since the nesting structure has already been flattened and
716 adding a flag just to record this fact seems a waste of a flag. */
718 static bool
719 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
721 struct cgraph_node *cgn = cgraph_node::get (fndecl);
722 tree arg;
724 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
726 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
727 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
728 return true;
730 if (check_for_nested_with_variably_modified (cgn->decl,
731 orig_fndecl))
732 return true;
735 return false;
738 /* Construct our local datastructure describing the function nesting
739 tree rooted by CGN. */
741 static struct nesting_info *
742 create_nesting_tree (struct cgraph_node *cgn)
744 struct nesting_info *info = XCNEW (struct nesting_info);
745 info->field_map = new hash_map<tree, tree>;
746 info->var_map = new hash_map<tree, tree>;
747 info->mem_refs = new hash_set<tree *>;
748 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
749 info->context = cgn->decl;
751 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
753 struct nesting_info *sub = create_nesting_tree (cgn);
754 sub->outer = info;
755 sub->next = info->inner;
756 info->inner = sub;
759 /* See discussion at check_for_nested_with_variably_modified for a
760 discussion of why this has to be here. */
761 if (check_for_nested_with_variably_modified (info->context, info->context))
762 DECL_UNINLINABLE (info->context) = true;
764 return info;
767 /* Return an expression computing the static chain for TARGET_CONTEXT
768 from INFO->CONTEXT. Insert any necessary computations before TSI. */
770 static tree
771 get_static_chain (struct nesting_info *info, tree target_context,
772 gimple_stmt_iterator *gsi)
774 struct nesting_info *i;
775 tree x;
777 if (info->context == target_context)
779 x = build_addr (info->frame_decl, target_context);
781 else
783 x = get_chain_decl (info);
785 for (i = info->outer; i->context != target_context; i = i->outer)
787 tree field = get_chain_field (i);
789 x = build_simple_mem_ref (x);
790 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
791 x = init_tmp_var (info, x, gsi);
795 return x;
799 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
800 frame as seen from INFO->CONTEXT. Insert any necessary computations
801 before GSI. */
803 static tree
804 get_frame_field (struct nesting_info *info, tree target_context,
805 tree field, gimple_stmt_iterator *gsi)
807 struct nesting_info *i;
808 tree x;
810 if (info->context == target_context)
812 /* Make sure frame_decl gets created. */
813 (void) get_frame_type (info);
814 x = info->frame_decl;
816 else
818 x = get_chain_decl (info);
820 for (i = info->outer; i->context != target_context; i = i->outer)
822 tree field = get_chain_field (i);
824 x = build_simple_mem_ref (x);
825 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
826 x = init_tmp_var (info, x, gsi);
829 x = build_simple_mem_ref (x);
832 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
833 return x;
836 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
838 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
839 in the nested function with DECL_VALUE_EXPR set to reference the true
840 variable in the parent function. This is used both for debug info
841 and in OMP lowering. */
843 static tree
844 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
846 tree target_context;
847 struct nesting_info *i;
848 tree x, field, new_decl;
850 tree *slot = &info->var_map->get_or_insert (decl);
852 if (*slot)
853 return *slot;
855 target_context = decl_function_context (decl);
857 /* A copy of the code in get_frame_field, but without the temporaries. */
858 if (info->context == target_context)
860 /* Make sure frame_decl gets created. */
861 (void) get_frame_type (info);
862 x = info->frame_decl;
863 i = info;
865 else
867 x = get_chain_decl (info);
868 for (i = info->outer; i->context != target_context; i = i->outer)
870 field = get_chain_field (i);
871 x = build_simple_mem_ref (x);
872 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
874 x = build_simple_mem_ref (x);
877 field = lookup_field_for_decl (i, decl, INSERT);
878 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
879 if (use_pointer_in_frame (decl))
880 x = build_simple_mem_ref (x);
882 /* ??? We should be remapping types as well, surely. */
883 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
884 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
885 DECL_CONTEXT (new_decl) = info->context;
886 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
887 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
888 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
889 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
890 TREE_READONLY (new_decl) = TREE_READONLY (decl);
891 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
892 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
893 if ((TREE_CODE (decl) == PARM_DECL
894 || TREE_CODE (decl) == RESULT_DECL
895 || TREE_CODE (decl) == VAR_DECL)
896 && DECL_BY_REFERENCE (decl))
897 DECL_BY_REFERENCE (new_decl) = 1;
899 SET_DECL_VALUE_EXPR (new_decl, x);
900 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
902 *slot = new_decl;
903 DECL_CHAIN (new_decl) = info->debug_var_chain;
904 info->debug_var_chain = new_decl;
906 if (!optimize
907 && info->context != target_context
908 && variably_modified_type_p (TREE_TYPE (decl), NULL))
909 note_nonlocal_vla_type (info, TREE_TYPE (decl));
911 return new_decl;
915 /* Callback for walk_gimple_stmt, rewrite all references to VAR
916 and PARM_DECLs that belong to outer functions.
918 The rewrite will involve some number of structure accesses back up
919 the static chain. E.g. for a variable FOO up one nesting level it'll
920 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
921 indirections apply to decls for which use_pointer_in_frame is true. */
923 static tree
924 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
926 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
927 struct nesting_info *const info = (struct nesting_info *) wi->info;
928 tree t = *tp;
930 *walk_subtrees = 0;
931 switch (TREE_CODE (t))
933 case VAR_DECL:
934 /* Non-automatic variables are never processed. */
935 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
936 break;
937 /* FALLTHRU */
939 case PARM_DECL:
940 if (decl_function_context (t) != info->context)
942 tree x;
943 wi->changed = true;
945 x = get_nonlocal_debug_decl (info, t);
946 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
948 tree target_context = decl_function_context (t);
949 struct nesting_info *i;
950 for (i = info->outer; i->context != target_context; i = i->outer)
951 continue;
952 x = lookup_field_for_decl (i, t, INSERT);
953 x = get_frame_field (info, target_context, x, &wi->gsi);
954 if (use_pointer_in_frame (t))
956 x = init_tmp_var (info, x, &wi->gsi);
957 x = build_simple_mem_ref (x);
961 if (wi->val_only)
963 if (wi->is_lhs)
964 x = save_tmp_var (info, x, &wi->gsi);
965 else
966 x = init_tmp_var (info, x, &wi->gsi);
969 *tp = x;
971 break;
973 case LABEL_DECL:
974 /* We're taking the address of a label from a parent function, but
975 this is not itself a non-local goto. Mark the label such that it
976 will not be deleted, much as we would with a label address in
977 static storage. */
978 if (decl_function_context (t) != info->context)
979 FORCED_LABEL (t) = 1;
980 break;
982 case ADDR_EXPR:
984 bool save_val_only = wi->val_only;
986 wi->val_only = false;
987 wi->is_lhs = false;
988 wi->changed = false;
989 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
990 wi->val_only = true;
992 if (wi->changed)
994 tree save_context;
996 /* If we changed anything, we might no longer be directly
997 referencing a decl. */
998 save_context = current_function_decl;
999 current_function_decl = info->context;
1000 recompute_tree_invariant_for_addr_expr (t);
1001 current_function_decl = save_context;
1003 /* If the callback converted the address argument in a context
1004 where we only accept variables (and min_invariant, presumably),
1005 then compute the address into a temporary. */
1006 if (save_val_only)
1007 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1008 t, &wi->gsi);
1011 break;
1013 case REALPART_EXPR:
1014 case IMAGPART_EXPR:
1015 case COMPONENT_REF:
1016 case ARRAY_REF:
1017 case ARRAY_RANGE_REF:
1018 case BIT_FIELD_REF:
1019 /* Go down this entire nest and just look at the final prefix and
1020 anything that describes the references. Otherwise, we lose track
1021 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1022 wi->val_only = true;
1023 wi->is_lhs = false;
1024 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1026 if (TREE_CODE (t) == COMPONENT_REF)
1027 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1028 NULL);
1029 else if (TREE_CODE (t) == ARRAY_REF
1030 || TREE_CODE (t) == ARRAY_RANGE_REF)
1032 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1033 wi, NULL);
1034 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1035 wi, NULL);
1036 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1037 wi, NULL);
1040 wi->val_only = false;
1041 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1042 break;
1044 case VIEW_CONVERT_EXPR:
1045 /* Just request to look at the subtrees, leaving val_only and lhs
1046 untouched. This might actually be for !val_only + lhs, in which
1047 case we don't want to force a replacement by a temporary. */
1048 *walk_subtrees = 1;
1049 break;
1051 default:
1052 if (!IS_TYPE_OR_DECL_P (t))
1054 *walk_subtrees = 1;
1055 wi->val_only = true;
1056 wi->is_lhs = false;
1058 break;
1061 return NULL_TREE;
1064 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1065 struct walk_stmt_info *);
1067 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1068 and PARM_DECLs that belong to outer functions. */
1070 static bool
1071 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1073 struct nesting_info *const info = (struct nesting_info *) wi->info;
1074 bool need_chain = false, need_stmts = false;
1075 tree clause, decl;
1076 int dummy;
1077 bitmap new_suppress;
1079 new_suppress = BITMAP_GGC_ALLOC ();
1080 bitmap_copy (new_suppress, info->suppress_expansion);
1082 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1084 switch (OMP_CLAUSE_CODE (clause))
1086 case OMP_CLAUSE_REDUCTION:
1087 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1088 need_stmts = true;
1089 goto do_decl_clause;
1091 case OMP_CLAUSE_LASTPRIVATE:
1092 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1093 need_stmts = true;
1094 goto do_decl_clause;
1096 case OMP_CLAUSE_LINEAR:
1097 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1098 need_stmts = true;
1099 wi->val_only = true;
1100 wi->is_lhs = false;
1101 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1102 &dummy, wi);
1103 goto do_decl_clause;
1105 case OMP_CLAUSE_PRIVATE:
1106 case OMP_CLAUSE_FIRSTPRIVATE:
1107 case OMP_CLAUSE_COPYPRIVATE:
1108 case OMP_CLAUSE_SHARED:
1109 do_decl_clause:
1110 decl = OMP_CLAUSE_DECL (clause);
1111 if (TREE_CODE (decl) == VAR_DECL
1112 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1113 break;
1114 if (decl_function_context (decl) != info->context)
1116 bitmap_set_bit (new_suppress, DECL_UID (decl));
1117 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1118 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1119 need_chain = true;
1121 break;
1123 case OMP_CLAUSE_SCHEDULE:
1124 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1125 break;
1126 /* FALLTHRU */
1127 case OMP_CLAUSE_FINAL:
1128 case OMP_CLAUSE_IF:
1129 case OMP_CLAUSE_NUM_THREADS:
1130 case OMP_CLAUSE_DEPEND:
1131 case OMP_CLAUSE_DEVICE:
1132 case OMP_CLAUSE_NUM_TEAMS:
1133 case OMP_CLAUSE_THREAD_LIMIT:
1134 case OMP_CLAUSE_SAFELEN:
1135 case OMP_CLAUSE__CILK_FOR_COUNT_:
1136 wi->val_only = true;
1137 wi->is_lhs = false;
1138 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1139 &dummy, wi);
1140 break;
1142 case OMP_CLAUSE_DIST_SCHEDULE:
1143 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1145 wi->val_only = true;
1146 wi->is_lhs = false;
1147 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1148 &dummy, wi);
1150 break;
1152 case OMP_CLAUSE_MAP:
1153 case OMP_CLAUSE_TO:
1154 case OMP_CLAUSE_FROM:
1155 if (OMP_CLAUSE_SIZE (clause))
1157 wi->val_only = true;
1158 wi->is_lhs = false;
1159 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1160 &dummy, wi);
1162 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1163 goto do_decl_clause;
1164 wi->val_only = true;
1165 wi->is_lhs = false;
1166 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1167 wi, NULL);
1168 break;
1170 case OMP_CLAUSE_ALIGNED:
1171 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1173 wi->val_only = true;
1174 wi->is_lhs = false;
1175 convert_nonlocal_reference_op
1176 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1178 /* Like do_decl_clause, but don't add any suppression. */
1179 decl = OMP_CLAUSE_DECL (clause);
1180 if (TREE_CODE (decl) == VAR_DECL
1181 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1182 break;
1183 if (decl_function_context (decl) != info->context)
1185 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1186 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1187 need_chain = true;
1189 break;
1191 case OMP_CLAUSE_NOWAIT:
1192 case OMP_CLAUSE_ORDERED:
1193 case OMP_CLAUSE_DEFAULT:
1194 case OMP_CLAUSE_COPYIN:
1195 case OMP_CLAUSE_COLLAPSE:
1196 case OMP_CLAUSE_UNTIED:
1197 case OMP_CLAUSE_MERGEABLE:
1198 case OMP_CLAUSE_PROC_BIND:
1199 break;
1201 default:
1202 gcc_unreachable ();
1206 info->suppress_expansion = new_suppress;
1208 if (need_stmts)
1209 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1210 switch (OMP_CLAUSE_CODE (clause))
1212 case OMP_CLAUSE_REDUCTION:
1213 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1215 tree old_context
1216 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1217 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1218 = info->context;
1219 walk_body (convert_nonlocal_reference_stmt,
1220 convert_nonlocal_reference_op, info,
1221 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1222 walk_body (convert_nonlocal_reference_stmt,
1223 convert_nonlocal_reference_op, info,
1224 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1225 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1226 = old_context;
1228 break;
1230 case OMP_CLAUSE_LASTPRIVATE:
1231 walk_body (convert_nonlocal_reference_stmt,
1232 convert_nonlocal_reference_op, info,
1233 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1234 break;
1236 case OMP_CLAUSE_LINEAR:
1237 walk_body (convert_nonlocal_reference_stmt,
1238 convert_nonlocal_reference_op, info,
1239 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1240 break;
1242 default:
1243 break;
1246 return need_chain;
1249 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1251 static void
1252 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1254 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1255 type = TREE_TYPE (type);
1257 if (TYPE_NAME (type)
1258 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1259 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1260 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1262 while (POINTER_TYPE_P (type)
1263 || TREE_CODE (type) == VECTOR_TYPE
1264 || TREE_CODE (type) == FUNCTION_TYPE
1265 || TREE_CODE (type) == METHOD_TYPE)
1266 type = TREE_TYPE (type);
1268 if (TREE_CODE (type) == ARRAY_TYPE)
1270 tree domain, t;
1272 note_nonlocal_vla_type (info, TREE_TYPE (type));
1273 domain = TYPE_DOMAIN (type);
1274 if (domain)
1276 t = TYPE_MIN_VALUE (domain);
1277 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1278 && decl_function_context (t) != info->context)
1279 get_nonlocal_debug_decl (info, t);
1280 t = TYPE_MAX_VALUE (domain);
1281 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1282 && decl_function_context (t) != info->context)
1283 get_nonlocal_debug_decl (info, t);
1288 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1289 in BLOCK. */
1291 static void
1292 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1294 tree var;
1296 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1297 if (TREE_CODE (var) == VAR_DECL
1298 && variably_modified_type_p (TREE_TYPE (var), NULL)
1299 && DECL_HAS_VALUE_EXPR_P (var)
1300 && decl_function_context (var) != info->context)
1301 note_nonlocal_vla_type (info, TREE_TYPE (var));
1304 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1305 PARM_DECLs that belong to outer functions. This handles statements
1306 that are not handled via the standard recursion done in
1307 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1308 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1309 operands of STMT have been handled by this function. */
1311 static tree
1312 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1313 struct walk_stmt_info *wi)
1315 struct nesting_info *info = (struct nesting_info *) wi->info;
1316 tree save_local_var_chain;
1317 bitmap save_suppress;
1318 gimple stmt = gsi_stmt (*gsi);
1320 switch (gimple_code (stmt))
1322 case GIMPLE_GOTO:
1323 /* Don't walk non-local gotos for now. */
1324 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1326 wi->val_only = true;
1327 wi->is_lhs = false;
1328 *handled_ops_p = true;
1329 return NULL_TREE;
1331 break;
1333 case GIMPLE_OMP_PARALLEL:
1334 case GIMPLE_OMP_TASK:
1335 save_suppress = info->suppress_expansion;
1336 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1337 wi))
1339 tree c, decl;
1340 decl = get_chain_decl (info);
1341 c = build_omp_clause (gimple_location (stmt),
1342 OMP_CLAUSE_FIRSTPRIVATE);
1343 OMP_CLAUSE_DECL (c) = decl;
1344 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1345 gimple_omp_taskreg_set_clauses (stmt, c);
1348 save_local_var_chain = info->new_local_var_chain;
1349 info->new_local_var_chain = NULL;
1351 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1352 info, gimple_omp_body_ptr (stmt));
1354 if (info->new_local_var_chain)
1355 declare_vars (info->new_local_var_chain,
1356 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1357 false);
1358 info->new_local_var_chain = save_local_var_chain;
1359 info->suppress_expansion = save_suppress;
1360 break;
1362 case GIMPLE_OMP_FOR:
1363 save_suppress = info->suppress_expansion;
1364 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1365 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1366 convert_nonlocal_reference_stmt,
1367 convert_nonlocal_reference_op, info);
1368 walk_body (convert_nonlocal_reference_stmt,
1369 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1370 info->suppress_expansion = save_suppress;
1371 break;
1373 case GIMPLE_OMP_SECTIONS:
1374 save_suppress = info->suppress_expansion;
1375 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1376 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1377 info, gimple_omp_body_ptr (stmt));
1378 info->suppress_expansion = save_suppress;
1379 break;
1381 case GIMPLE_OMP_SINGLE:
1382 save_suppress = info->suppress_expansion;
1383 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1384 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1385 info, gimple_omp_body_ptr (stmt));
1386 info->suppress_expansion = save_suppress;
1387 break;
1389 case GIMPLE_OMP_TARGET:
1390 if (!is_gimple_omp_offloaded (stmt))
1392 save_suppress = info->suppress_expansion;
1393 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1394 wi);
1395 info->suppress_expansion = save_suppress;
1396 walk_body (convert_nonlocal_reference_stmt,
1397 convert_nonlocal_reference_op, info,
1398 gimple_omp_body_ptr (stmt));
1399 break;
1401 save_suppress = info->suppress_expansion;
1402 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1403 wi))
1405 tree c, decl;
1406 decl = get_chain_decl (info);
1407 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1408 OMP_CLAUSE_DECL (c) = decl;
1409 OMP_CLAUSE_MAP_KIND (c) = GOMP_MAP_TO;
1410 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1411 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1412 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1415 save_local_var_chain = info->new_local_var_chain;
1416 info->new_local_var_chain = NULL;
1418 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1419 info, gimple_omp_body_ptr (stmt));
1421 if (info->new_local_var_chain)
1422 declare_vars (info->new_local_var_chain,
1423 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1424 false);
1425 info->new_local_var_chain = save_local_var_chain;
1426 info->suppress_expansion = save_suppress;
1427 break;
1429 case GIMPLE_OMP_TEAMS:
1430 save_suppress = info->suppress_expansion;
1431 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1432 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1433 info, gimple_omp_body_ptr (stmt));
1434 info->suppress_expansion = save_suppress;
1435 break;
1437 case GIMPLE_OMP_SECTION:
1438 case GIMPLE_OMP_MASTER:
1439 case GIMPLE_OMP_TASKGROUP:
1440 case GIMPLE_OMP_ORDERED:
1441 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1442 info, gimple_omp_body_ptr (stmt));
1443 break;
1445 case GIMPLE_BIND:
1447 gbind *bind_stmt = as_a <gbind *> (stmt);
1448 if (!optimize && gimple_bind_block (bind_stmt))
1449 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1451 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1452 if (TREE_CODE (var) == NAMELIST_DECL)
1454 /* Adjust decls mentioned in NAMELIST_DECL. */
1455 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1456 tree decl;
1457 unsigned int i;
1459 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1461 if (TREE_CODE (decl) == VAR_DECL
1462 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1463 continue;
1464 if (decl_function_context (decl) != info->context)
1465 CONSTRUCTOR_ELT (decls, i)->value
1466 = get_nonlocal_debug_decl (info, decl);
1470 *handled_ops_p = false;
1471 return NULL_TREE;
1473 case GIMPLE_COND:
1474 wi->val_only = true;
1475 wi->is_lhs = false;
1476 *handled_ops_p = false;
1477 return NULL_TREE;
1479 default:
1480 /* For every other statement that we are not interested in
1481 handling here, let the walker traverse the operands. */
1482 *handled_ops_p = false;
1483 return NULL_TREE;
1486 /* We have handled all of STMT operands, no need to traverse the operands. */
1487 *handled_ops_p = true;
1488 return NULL_TREE;
1492 /* A subroutine of convert_local_reference. Create a local variable
1493 in the parent function with DECL_VALUE_EXPR set to reference the
1494 field in FRAME. This is used both for debug info and in OMP
1495 lowering. */
1497 static tree
1498 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1500 tree x, new_decl;
1502 tree *slot = &info->var_map->get_or_insert (decl);
1503 if (*slot)
1504 return *slot;
1506 /* Make sure frame_decl gets created. */
1507 (void) get_frame_type (info);
1508 x = info->frame_decl;
1509 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1511 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1512 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1513 DECL_CONTEXT (new_decl) = info->context;
1514 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1515 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1516 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1517 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1518 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1519 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1520 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1521 if ((TREE_CODE (decl) == PARM_DECL
1522 || TREE_CODE (decl) == RESULT_DECL
1523 || TREE_CODE (decl) == VAR_DECL)
1524 && DECL_BY_REFERENCE (decl))
1525 DECL_BY_REFERENCE (new_decl) = 1;
1527 SET_DECL_VALUE_EXPR (new_decl, x);
1528 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1529 *slot = new_decl;
1531 DECL_CHAIN (new_decl) = info->debug_var_chain;
1532 info->debug_var_chain = new_decl;
1534 /* Do not emit debug info twice. */
1535 DECL_IGNORED_P (decl) = 1;
1537 return new_decl;
1541 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1542 and PARM_DECLs that were referenced by inner nested functions.
1543 The rewrite will be a structure reference to the local frame variable. */
1545 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1547 static tree
1548 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1550 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1551 struct nesting_info *const info = (struct nesting_info *) wi->info;
1552 tree t = *tp, field, x;
1553 bool save_val_only;
1555 *walk_subtrees = 0;
1556 switch (TREE_CODE (t))
1558 case VAR_DECL:
1559 /* Non-automatic variables are never processed. */
1560 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1561 break;
1562 /* FALLTHRU */
1564 case PARM_DECL:
1565 if (decl_function_context (t) == info->context)
1567 /* If we copied a pointer to the frame, then the original decl
1568 is used unchanged in the parent function. */
1569 if (use_pointer_in_frame (t))
1570 break;
1572 /* No need to transform anything if no child references the
1573 variable. */
1574 field = lookup_field_for_decl (info, t, NO_INSERT);
1575 if (!field)
1576 break;
1577 wi->changed = true;
1579 x = get_local_debug_decl (info, t, field);
1580 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1581 x = get_frame_field (info, info->context, field, &wi->gsi);
1583 if (wi->val_only)
1585 if (wi->is_lhs)
1586 x = save_tmp_var (info, x, &wi->gsi);
1587 else
1588 x = init_tmp_var (info, x, &wi->gsi);
1591 *tp = x;
1593 break;
1595 case ADDR_EXPR:
1596 save_val_only = wi->val_only;
1597 wi->val_only = false;
1598 wi->is_lhs = false;
1599 wi->changed = false;
1600 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1601 wi->val_only = save_val_only;
1603 /* If we converted anything ... */
1604 if (wi->changed)
1606 tree save_context;
1608 /* Then the frame decl is now addressable. */
1609 TREE_ADDRESSABLE (info->frame_decl) = 1;
1611 save_context = current_function_decl;
1612 current_function_decl = info->context;
1613 recompute_tree_invariant_for_addr_expr (t);
1614 current_function_decl = save_context;
1616 /* If we are in a context where we only accept values, then
1617 compute the address into a temporary. */
1618 if (save_val_only)
1619 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1620 t, &wi->gsi);
1622 break;
1624 case REALPART_EXPR:
1625 case IMAGPART_EXPR:
1626 case COMPONENT_REF:
1627 case ARRAY_REF:
1628 case ARRAY_RANGE_REF:
1629 case BIT_FIELD_REF:
1630 /* Go down this entire nest and just look at the final prefix and
1631 anything that describes the references. Otherwise, we lose track
1632 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1633 save_val_only = wi->val_only;
1634 wi->val_only = true;
1635 wi->is_lhs = false;
1636 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1638 if (TREE_CODE (t) == COMPONENT_REF)
1639 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1640 NULL);
1641 else if (TREE_CODE (t) == ARRAY_REF
1642 || TREE_CODE (t) == ARRAY_RANGE_REF)
1644 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1645 NULL);
1646 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1647 NULL);
1648 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1649 NULL);
1652 wi->val_only = false;
1653 walk_tree (tp, convert_local_reference_op, wi, NULL);
1654 wi->val_only = save_val_only;
1655 break;
1657 case MEM_REF:
1658 save_val_only = wi->val_only;
1659 wi->val_only = true;
1660 wi->is_lhs = false;
1661 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1662 wi, NULL);
1663 /* We need to re-fold the MEM_REF as component references as
1664 part of a ADDR_EXPR address are not allowed. But we cannot
1665 fold here, as the chain record type is not yet finalized. */
1666 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1667 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1668 info->mem_refs->add (tp);
1669 wi->val_only = save_val_only;
1670 break;
1672 case VIEW_CONVERT_EXPR:
1673 /* Just request to look at the subtrees, leaving val_only and lhs
1674 untouched. This might actually be for !val_only + lhs, in which
1675 case we don't want to force a replacement by a temporary. */
1676 *walk_subtrees = 1;
1677 break;
1679 default:
1680 if (!IS_TYPE_OR_DECL_P (t))
1682 *walk_subtrees = 1;
1683 wi->val_only = true;
1684 wi->is_lhs = false;
1686 break;
1689 return NULL_TREE;
1692 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1693 struct walk_stmt_info *);
1695 /* Helper for convert_local_reference. Convert all the references in
1696 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1698 static bool
1699 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1701 struct nesting_info *const info = (struct nesting_info *) wi->info;
1702 bool need_frame = false, need_stmts = false;
1703 tree clause, decl;
1704 int dummy;
1705 bitmap new_suppress;
1707 new_suppress = BITMAP_GGC_ALLOC ();
1708 bitmap_copy (new_suppress, info->suppress_expansion);
1710 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1712 switch (OMP_CLAUSE_CODE (clause))
1714 case OMP_CLAUSE_REDUCTION:
1715 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1716 need_stmts = true;
1717 goto do_decl_clause;
1719 case OMP_CLAUSE_LASTPRIVATE:
1720 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1721 need_stmts = true;
1722 goto do_decl_clause;
1724 case OMP_CLAUSE_LINEAR:
1725 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1726 need_stmts = true;
1727 wi->val_only = true;
1728 wi->is_lhs = false;
1729 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1730 wi);
1731 goto do_decl_clause;
1733 case OMP_CLAUSE_PRIVATE:
1734 case OMP_CLAUSE_FIRSTPRIVATE:
1735 case OMP_CLAUSE_COPYPRIVATE:
1736 case OMP_CLAUSE_SHARED:
1737 do_decl_clause:
1738 decl = OMP_CLAUSE_DECL (clause);
1739 if (TREE_CODE (decl) == VAR_DECL
1740 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1741 break;
1742 if (decl_function_context (decl) == info->context
1743 && !use_pointer_in_frame (decl))
1745 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1746 if (field)
1748 bitmap_set_bit (new_suppress, DECL_UID (decl));
1749 OMP_CLAUSE_DECL (clause)
1750 = get_local_debug_decl (info, decl, field);
1751 need_frame = true;
1754 break;
1756 case OMP_CLAUSE_SCHEDULE:
1757 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1758 break;
1759 /* FALLTHRU */
1760 case OMP_CLAUSE_FINAL:
1761 case OMP_CLAUSE_IF:
1762 case OMP_CLAUSE_NUM_THREADS:
1763 case OMP_CLAUSE_DEPEND:
1764 case OMP_CLAUSE_DEVICE:
1765 case OMP_CLAUSE_NUM_TEAMS:
1766 case OMP_CLAUSE_THREAD_LIMIT:
1767 case OMP_CLAUSE_SAFELEN:
1768 case OMP_CLAUSE__CILK_FOR_COUNT_:
1769 wi->val_only = true;
1770 wi->is_lhs = false;
1771 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1772 wi);
1773 break;
1775 case OMP_CLAUSE_DIST_SCHEDULE:
1776 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1778 wi->val_only = true;
1779 wi->is_lhs = false;
1780 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1781 &dummy, wi);
1783 break;
1785 case OMP_CLAUSE_MAP:
1786 case OMP_CLAUSE_TO:
1787 case OMP_CLAUSE_FROM:
1788 if (OMP_CLAUSE_SIZE (clause))
1790 wi->val_only = true;
1791 wi->is_lhs = false;
1792 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1793 &dummy, wi);
1795 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1796 goto do_decl_clause;
1797 wi->val_only = true;
1798 wi->is_lhs = false;
1799 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1800 wi, NULL);
1801 break;
1803 case OMP_CLAUSE_ALIGNED:
1804 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1806 wi->val_only = true;
1807 wi->is_lhs = false;
1808 convert_local_reference_op
1809 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1811 /* Like do_decl_clause, but don't add any suppression. */
1812 decl = OMP_CLAUSE_DECL (clause);
1813 if (TREE_CODE (decl) == VAR_DECL
1814 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1815 break;
1816 if (decl_function_context (decl) == info->context
1817 && !use_pointer_in_frame (decl))
1819 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1820 if (field)
1822 OMP_CLAUSE_DECL (clause)
1823 = get_local_debug_decl (info, decl, field);
1824 need_frame = true;
1827 break;
1829 case OMP_CLAUSE_NOWAIT:
1830 case OMP_CLAUSE_ORDERED:
1831 case OMP_CLAUSE_DEFAULT:
1832 case OMP_CLAUSE_COPYIN:
1833 case OMP_CLAUSE_COLLAPSE:
1834 case OMP_CLAUSE_UNTIED:
1835 case OMP_CLAUSE_MERGEABLE:
1836 case OMP_CLAUSE_PROC_BIND:
1837 break;
1839 default:
1840 gcc_unreachable ();
1844 info->suppress_expansion = new_suppress;
1846 if (need_stmts)
1847 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1848 switch (OMP_CLAUSE_CODE (clause))
1850 case OMP_CLAUSE_REDUCTION:
1851 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1853 tree old_context
1854 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1855 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1856 = info->context;
1857 walk_body (convert_local_reference_stmt,
1858 convert_local_reference_op, info,
1859 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1860 walk_body (convert_local_reference_stmt,
1861 convert_local_reference_op, info,
1862 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1863 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1864 = old_context;
1866 break;
1868 case OMP_CLAUSE_LASTPRIVATE:
1869 walk_body (convert_local_reference_stmt,
1870 convert_local_reference_op, info,
1871 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1872 break;
1874 case OMP_CLAUSE_LINEAR:
1875 walk_body (convert_local_reference_stmt,
1876 convert_local_reference_op, info,
1877 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1878 break;
1880 default:
1881 break;
1884 return need_frame;
1888 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1889 and PARM_DECLs that were referenced by inner nested functions.
1890 The rewrite will be a structure reference to the local frame variable. */
1892 static tree
1893 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1894 struct walk_stmt_info *wi)
1896 struct nesting_info *info = (struct nesting_info *) wi->info;
1897 tree save_local_var_chain;
1898 bitmap save_suppress;
1899 gimple stmt = gsi_stmt (*gsi);
1901 switch (gimple_code (stmt))
1903 case GIMPLE_OMP_PARALLEL:
1904 case GIMPLE_OMP_TASK:
1905 save_suppress = info->suppress_expansion;
1906 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1907 wi))
1909 tree c;
1910 (void) get_frame_type (info);
1911 c = build_omp_clause (gimple_location (stmt),
1912 OMP_CLAUSE_SHARED);
1913 OMP_CLAUSE_DECL (c) = info->frame_decl;
1914 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1915 gimple_omp_taskreg_set_clauses (stmt, c);
1918 save_local_var_chain = info->new_local_var_chain;
1919 info->new_local_var_chain = NULL;
1921 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1922 gimple_omp_body_ptr (stmt));
1924 if (info->new_local_var_chain)
1925 declare_vars (info->new_local_var_chain,
1926 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1927 info->new_local_var_chain = save_local_var_chain;
1928 info->suppress_expansion = save_suppress;
1929 break;
1931 case GIMPLE_OMP_FOR:
1932 save_suppress = info->suppress_expansion;
1933 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1934 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1935 convert_local_reference_stmt,
1936 convert_local_reference_op, info);
1937 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1938 info, gimple_omp_body_ptr (stmt));
1939 info->suppress_expansion = save_suppress;
1940 break;
1942 case GIMPLE_OMP_SECTIONS:
1943 save_suppress = info->suppress_expansion;
1944 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1945 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1946 info, gimple_omp_body_ptr (stmt));
1947 info->suppress_expansion = save_suppress;
1948 break;
1950 case GIMPLE_OMP_SINGLE:
1951 save_suppress = info->suppress_expansion;
1952 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1953 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1954 info, gimple_omp_body_ptr (stmt));
1955 info->suppress_expansion = save_suppress;
1956 break;
1958 case GIMPLE_OMP_TARGET:
1959 if (!is_gimple_omp_offloaded (stmt))
1961 save_suppress = info->suppress_expansion;
1962 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1963 info->suppress_expansion = save_suppress;
1964 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1965 info, gimple_omp_body_ptr (stmt));
1966 break;
1968 save_suppress = info->suppress_expansion;
1969 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1971 tree c;
1972 (void) get_frame_type (info);
1973 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1974 OMP_CLAUSE_DECL (c) = info->frame_decl;
1975 OMP_CLAUSE_MAP_KIND (c) = GOMP_MAP_TOFROM;
1976 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1977 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1978 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1981 save_local_var_chain = info->new_local_var_chain;
1982 info->new_local_var_chain = NULL;
1984 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1985 gimple_omp_body_ptr (stmt));
1987 if (info->new_local_var_chain)
1988 declare_vars (info->new_local_var_chain,
1989 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1990 info->new_local_var_chain = save_local_var_chain;
1991 info->suppress_expansion = save_suppress;
1992 break;
1994 case GIMPLE_OMP_TEAMS:
1995 save_suppress = info->suppress_expansion;
1996 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1997 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1998 info, gimple_omp_body_ptr (stmt));
1999 info->suppress_expansion = save_suppress;
2000 break;
2002 case GIMPLE_OMP_SECTION:
2003 case GIMPLE_OMP_MASTER:
2004 case GIMPLE_OMP_TASKGROUP:
2005 case GIMPLE_OMP_ORDERED:
2006 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2007 info, gimple_omp_body_ptr (stmt));
2008 break;
2010 case GIMPLE_COND:
2011 wi->val_only = true;
2012 wi->is_lhs = false;
2013 *handled_ops_p = false;
2014 return NULL_TREE;
2016 case GIMPLE_ASSIGN:
2017 if (gimple_clobber_p (stmt))
2019 tree lhs = gimple_assign_lhs (stmt);
2020 if (!use_pointer_in_frame (lhs)
2021 && lookup_field_for_decl (info, lhs, NO_INSERT))
2023 gsi_replace (gsi, gimple_build_nop (), true);
2024 break;
2027 *handled_ops_p = false;
2028 return NULL_TREE;
2030 case GIMPLE_BIND:
2031 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2032 var;
2033 var = DECL_CHAIN (var))
2034 if (TREE_CODE (var) == NAMELIST_DECL)
2036 /* Adjust decls mentioned in NAMELIST_DECL. */
2037 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2038 tree decl;
2039 unsigned int i;
2041 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2043 if (TREE_CODE (decl) == VAR_DECL
2044 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2045 continue;
2046 if (decl_function_context (decl) == info->context
2047 && !use_pointer_in_frame (decl))
2049 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2050 if (field)
2052 CONSTRUCTOR_ELT (decls, i)->value
2053 = get_local_debug_decl (info, decl, field);
2059 *handled_ops_p = false;
2060 return NULL_TREE;
2062 default:
2063 /* For every other statement that we are not interested in
2064 handling here, let the walker traverse the operands. */
2065 *handled_ops_p = false;
2066 return NULL_TREE;
2069 /* Indicate that we have handled all the operands ourselves. */
2070 *handled_ops_p = true;
2071 return NULL_TREE;
2075 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2076 that reference labels from outer functions. The rewrite will be a
2077 call to __builtin_nonlocal_goto. */
2079 static tree
2080 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2081 struct walk_stmt_info *wi)
2083 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2084 tree label, new_label, target_context, x, field;
2085 gcall *call;
2086 gimple stmt = gsi_stmt (*gsi);
2088 if (gimple_code (stmt) != GIMPLE_GOTO)
2090 *handled_ops_p = false;
2091 return NULL_TREE;
2094 label = gimple_goto_dest (stmt);
2095 if (TREE_CODE (label) != LABEL_DECL)
2097 *handled_ops_p = false;
2098 return NULL_TREE;
2101 target_context = decl_function_context (label);
2102 if (target_context == info->context)
2104 *handled_ops_p = false;
2105 return NULL_TREE;
2108 for (i = info->outer; target_context != i->context; i = i->outer)
2109 continue;
2111 /* The original user label may also be use for a normal goto, therefore
2112 we must create a new label that will actually receive the abnormal
2113 control transfer. This new label will be marked LABEL_NONLOCAL; this
2114 mark will trigger proper behavior in the cfg, as well as cause the
2115 (hairy target-specific) non-local goto receiver code to be generated
2116 when we expand rtl. Enter this association into var_map so that we
2117 can insert the new label into the IL during a second pass. */
2118 tree *slot = &i->var_map->get_or_insert (label);
2119 if (*slot == NULL)
2121 new_label = create_artificial_label (UNKNOWN_LOCATION);
2122 DECL_NONLOCAL (new_label) = 1;
2123 *slot = new_label;
2125 else
2126 new_label = *slot;
2128 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2129 field = get_nl_goto_field (i);
2130 x = get_frame_field (info, target_context, field, gsi);
2131 x = build_addr (x, target_context);
2132 x = gsi_gimplify_val (info, x, gsi);
2133 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2134 2, build_addr (new_label, target_context), x);
2135 gsi_replace (gsi, call, false);
2137 /* We have handled all of STMT's operands, no need to keep going. */
2138 *handled_ops_p = true;
2139 return NULL_TREE;
2143 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2144 are referenced via nonlocal goto from a nested function. The rewrite
2145 will involve installing a newly generated DECL_NONLOCAL label, and
2146 (potentially) a branch around the rtl gunk that is assumed to be
2147 attached to such a label. */
2149 static tree
2150 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2151 struct walk_stmt_info *wi)
2153 struct nesting_info *const info = (struct nesting_info *) wi->info;
2154 tree label, new_label;
2155 gimple_stmt_iterator tmp_gsi;
2156 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2158 if (!stmt)
2160 *handled_ops_p = false;
2161 return NULL_TREE;
2164 label = gimple_label_label (stmt);
2166 tree *slot = info->var_map->get (label);
2167 if (!slot)
2169 *handled_ops_p = false;
2170 return NULL_TREE;
2173 /* If there's any possibility that the previous statement falls through,
2174 then we must branch around the new non-local label. */
2175 tmp_gsi = wi->gsi;
2176 gsi_prev (&tmp_gsi);
2177 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2179 gimple stmt = gimple_build_goto (label);
2180 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2183 new_label = (tree) *slot;
2184 stmt = gimple_build_label (new_label);
2185 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2187 *handled_ops_p = true;
2188 return NULL_TREE;
2192 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2193 of nested functions that require the use of trampolines. The rewrite
2194 will involve a reference a trampoline generated for the occasion. */
2196 static tree
2197 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2199 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2200 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2201 tree t = *tp, decl, target_context, x, builtin;
2202 gcall *call;
2204 *walk_subtrees = 0;
2205 switch (TREE_CODE (t))
2207 case ADDR_EXPR:
2208 /* Build
2209 T.1 = &CHAIN->tramp;
2210 T.2 = __builtin_adjust_trampoline (T.1);
2211 T.3 = (func_type)T.2;
2214 decl = TREE_OPERAND (t, 0);
2215 if (TREE_CODE (decl) != FUNCTION_DECL)
2216 break;
2218 /* Only need to process nested functions. */
2219 target_context = decl_function_context (decl);
2220 if (!target_context)
2221 break;
2223 /* If the nested function doesn't use a static chain, then
2224 it doesn't need a trampoline. */
2225 if (!DECL_STATIC_CHAIN (decl))
2226 break;
2228 /* If we don't want a trampoline, then don't build one. */
2229 if (TREE_NO_TRAMPOLINE (t))
2230 break;
2232 /* Lookup the immediate parent of the callee, as that's where
2233 we need to insert the trampoline. */
2234 for (i = info; i->context != target_context; i = i->outer)
2235 continue;
2236 x = lookup_tramp_for_decl (i, decl, INSERT);
2238 /* Compute the address of the field holding the trampoline. */
2239 x = get_frame_field (info, target_context, x, &wi->gsi);
2240 x = build_addr (x, target_context);
2241 x = gsi_gimplify_val (info, x, &wi->gsi);
2243 /* Do machine-specific ugliness. Normally this will involve
2244 computing extra alignment, but it can really be anything. */
2245 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2246 call = gimple_build_call (builtin, 1, x);
2247 x = init_tmp_var_with_call (info, &wi->gsi, call);
2249 /* Cast back to the proper function type. */
2250 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2251 x = init_tmp_var (info, x, &wi->gsi);
2253 *tp = x;
2254 break;
2256 default:
2257 if (!IS_TYPE_OR_DECL_P (t))
2258 *walk_subtrees = 1;
2259 break;
2262 return NULL_TREE;
2266 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2267 to addresses of nested functions that require the use of
2268 trampolines. The rewrite will involve a reference a trampoline
2269 generated for the occasion. */
2271 static tree
2272 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2273 struct walk_stmt_info *wi)
2275 struct nesting_info *info = (struct nesting_info *) wi->info;
2276 gimple stmt = gsi_stmt (*gsi);
2278 switch (gimple_code (stmt))
2280 case GIMPLE_CALL:
2282 /* Only walk call arguments, lest we generate trampolines for
2283 direct calls. */
2284 unsigned long i, nargs = gimple_call_num_args (stmt);
2285 for (i = 0; i < nargs; i++)
2286 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2287 wi, NULL);
2288 break;
2291 case GIMPLE_OMP_TARGET:
2292 if (!is_gimple_omp_offloaded (stmt))
2294 *handled_ops_p = false;
2295 return NULL_TREE;
2297 /* FALLTHRU */
2298 case GIMPLE_OMP_PARALLEL:
2299 case GIMPLE_OMP_TASK:
2301 tree save_local_var_chain;
2302 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2303 save_local_var_chain = info->new_local_var_chain;
2304 info->new_local_var_chain = NULL;
2305 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2306 info, gimple_omp_body_ptr (stmt));
2307 if (info->new_local_var_chain)
2308 declare_vars (info->new_local_var_chain,
2309 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2310 false);
2311 info->new_local_var_chain = save_local_var_chain;
2313 break;
2315 default:
2316 *handled_ops_p = false;
2317 return NULL_TREE;
2320 *handled_ops_p = true;
2321 return NULL_TREE;
2326 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2327 that reference nested functions to make sure that the static chain
2328 is set up properly for the call. */
2330 static tree
2331 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2332 struct walk_stmt_info *wi)
2334 struct nesting_info *const info = (struct nesting_info *) wi->info;
2335 tree decl, target_context;
2336 char save_static_chain_added;
2337 int i;
2338 gimple stmt = gsi_stmt (*gsi);
2340 switch (gimple_code (stmt))
2342 case GIMPLE_CALL:
2343 if (gimple_call_chain (stmt))
2344 break;
2345 decl = gimple_call_fndecl (stmt);
2346 if (!decl)
2347 break;
2348 target_context = decl_function_context (decl);
2349 if (target_context && DECL_STATIC_CHAIN (decl))
2351 gimple_call_set_chain (as_a <gcall *> (stmt),
2352 get_static_chain (info, target_context,
2353 &wi->gsi));
2354 info->static_chain_added |= (1 << (info->context != target_context));
2356 break;
2358 case GIMPLE_OMP_PARALLEL:
2359 case GIMPLE_OMP_TASK:
2360 save_static_chain_added = info->static_chain_added;
2361 info->static_chain_added = 0;
2362 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2363 for (i = 0; i < 2; i++)
2365 tree c, decl;
2366 if ((info->static_chain_added & (1 << i)) == 0)
2367 continue;
2368 decl = i ? get_chain_decl (info) : info->frame_decl;
2369 /* Don't add CHAIN.* or FRAME.* twice. */
2370 for (c = gimple_omp_taskreg_clauses (stmt);
2372 c = OMP_CLAUSE_CHAIN (c))
2373 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2374 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2375 && OMP_CLAUSE_DECL (c) == decl)
2376 break;
2377 if (c == NULL)
2379 c = build_omp_clause (gimple_location (stmt),
2380 i ? OMP_CLAUSE_FIRSTPRIVATE
2381 : OMP_CLAUSE_SHARED);
2382 OMP_CLAUSE_DECL (c) = decl;
2383 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2384 gimple_omp_taskreg_set_clauses (stmt, c);
2387 info->static_chain_added |= save_static_chain_added;
2388 break;
2390 case GIMPLE_OMP_TARGET:
2391 if (!is_gimple_omp_offloaded (stmt))
2393 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2394 break;
2396 save_static_chain_added = info->static_chain_added;
2397 info->static_chain_added = 0;
2398 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2399 for (i = 0; i < 2; i++)
2401 tree c, decl;
2402 if ((info->static_chain_added & (1 << i)) == 0)
2403 continue;
2404 decl = i ? get_chain_decl (info) : info->frame_decl;
2405 /* Don't add CHAIN.* or FRAME.* twice. */
2406 for (c = gimple_omp_target_clauses (stmt);
2408 c = OMP_CLAUSE_CHAIN (c))
2409 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2410 && OMP_CLAUSE_DECL (c) == decl)
2411 break;
2412 if (c == NULL)
2414 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2415 OMP_CLAUSE_DECL (c) = decl;
2416 OMP_CLAUSE_MAP_KIND (c) = i ? GOMP_MAP_TO : GOMP_MAP_TOFROM;
2417 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2418 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2419 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2423 info->static_chain_added |= save_static_chain_added;
2424 break;
2426 case GIMPLE_OMP_FOR:
2427 walk_body (convert_gimple_call, NULL, info,
2428 gimple_omp_for_pre_body_ptr (stmt));
2429 /* FALLTHRU */
2430 case GIMPLE_OMP_SECTIONS:
2431 case GIMPLE_OMP_SECTION:
2432 case GIMPLE_OMP_SINGLE:
2433 case GIMPLE_OMP_TEAMS:
2434 case GIMPLE_OMP_MASTER:
2435 case GIMPLE_OMP_TASKGROUP:
2436 case GIMPLE_OMP_ORDERED:
2437 case GIMPLE_OMP_CRITICAL:
2438 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2439 break;
2441 default:
2442 /* Keep looking for other operands. */
2443 *handled_ops_p = false;
2444 return NULL_TREE;
2447 *handled_ops_p = true;
2448 return NULL_TREE;
2451 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2452 call expressions. At the same time, determine if a nested function
2453 actually uses its static chain; if not, remember that. */
2455 static void
2456 convert_all_function_calls (struct nesting_info *root)
2458 unsigned int chain_count = 0, old_chain_count, iter_count;
2459 struct nesting_info *n;
2461 /* First, optimistically clear static_chain for all decls that haven't
2462 used the static chain already for variable access. But always create
2463 it if not optimizing. This makes it possible to reconstruct the static
2464 nesting tree at run time and thus to resolve up-level references from
2465 within the debugger. */
2466 FOR_EACH_NEST_INFO (n, root)
2468 tree decl = n->context;
2469 if (!optimize)
2471 if (n->inner)
2472 (void) get_frame_type (n);
2473 if (n->outer)
2474 (void) get_chain_decl (n);
2476 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2478 DECL_STATIC_CHAIN (decl) = 0;
2479 if (dump_file && (dump_flags & TDF_DETAILS))
2480 fprintf (dump_file, "Guessing no static-chain for %s\n",
2481 lang_hooks.decl_printable_name (decl, 2));
2483 else
2484 DECL_STATIC_CHAIN (decl) = 1;
2485 chain_count += DECL_STATIC_CHAIN (decl);
2488 /* Walk the functions and perform transformations. Note that these
2489 transformations can induce new uses of the static chain, which in turn
2490 require re-examining all users of the decl. */
2491 /* ??? It would make sense to try to use the call graph to speed this up,
2492 but the call graph hasn't really been built yet. Even if it did, we
2493 would still need to iterate in this loop since address-of references
2494 wouldn't show up in the callgraph anyway. */
2495 iter_count = 0;
2498 old_chain_count = chain_count;
2499 chain_count = 0;
2500 iter_count++;
2502 if (dump_file && (dump_flags & TDF_DETAILS))
2503 fputc ('\n', dump_file);
2505 FOR_EACH_NEST_INFO (n, root)
2507 tree decl = n->context;
2508 walk_function (convert_tramp_reference_stmt,
2509 convert_tramp_reference_op, n);
2510 walk_function (convert_gimple_call, NULL, n);
2511 chain_count += DECL_STATIC_CHAIN (decl);
2514 while (chain_count != old_chain_count);
2516 if (dump_file && (dump_flags & TDF_DETAILS))
2517 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2518 iter_count);
2521 struct nesting_copy_body_data
2523 copy_body_data cb;
2524 struct nesting_info *root;
2527 /* A helper subroutine for debug_var_chain type remapping. */
2529 static tree
2530 nesting_copy_decl (tree decl, copy_body_data *id)
2532 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2533 tree *slot = nid->root->var_map->get (decl);
2535 if (slot)
2536 return (tree) *slot;
2538 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2540 tree new_decl = copy_decl_no_change (decl, id);
2541 DECL_ORIGINAL_TYPE (new_decl)
2542 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2543 return new_decl;
2546 if (TREE_CODE (decl) == VAR_DECL
2547 || TREE_CODE (decl) == PARM_DECL
2548 || TREE_CODE (decl) == RESULT_DECL)
2549 return decl;
2551 return copy_decl_no_change (decl, id);
2554 /* A helper function for remap_vla_decls. See if *TP contains
2555 some remapped variables. */
2557 static tree
2558 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2560 struct nesting_info *root = (struct nesting_info *) data;
2561 tree t = *tp;
2563 if (DECL_P (t))
2565 *walk_subtrees = 0;
2566 tree *slot = root->var_map->get (t);
2568 if (slot)
2569 return *slot;
2571 return NULL;
2574 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2575 involved. */
2577 static void
2578 remap_vla_decls (tree block, struct nesting_info *root)
2580 tree var, subblock, val, type;
2581 struct nesting_copy_body_data id;
2583 for (subblock = BLOCK_SUBBLOCKS (block);
2584 subblock;
2585 subblock = BLOCK_CHAIN (subblock))
2586 remap_vla_decls (subblock, root);
2588 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2589 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2591 val = DECL_VALUE_EXPR (var);
2592 type = TREE_TYPE (var);
2594 if (!(TREE_CODE (val) == INDIRECT_REF
2595 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2596 && variably_modified_type_p (type, NULL)))
2597 continue;
2599 if (root->var_map->get (TREE_OPERAND (val, 0))
2600 || walk_tree (&type, contains_remapped_vars, root, NULL))
2601 break;
2604 if (var == NULL_TREE)
2605 return;
2607 memset (&id, 0, sizeof (id));
2608 id.cb.copy_decl = nesting_copy_decl;
2609 id.cb.decl_map = new hash_map<tree, tree>;
2610 id.root = root;
2612 for (; var; var = DECL_CHAIN (var))
2613 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2615 struct nesting_info *i;
2616 tree newt, context;
2618 val = DECL_VALUE_EXPR (var);
2619 type = TREE_TYPE (var);
2621 if (!(TREE_CODE (val) == INDIRECT_REF
2622 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2623 && variably_modified_type_p (type, NULL)))
2624 continue;
2626 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2627 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2628 continue;
2630 context = decl_function_context (var);
2631 for (i = root; i; i = i->outer)
2632 if (i->context == context)
2633 break;
2635 if (i == NULL)
2636 continue;
2638 /* Fully expand value expressions. This avoids having debug variables
2639 only referenced from them and that can be swept during GC. */
2640 if (slot)
2642 tree t = (tree) *slot;
2643 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2644 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2647 id.cb.src_fn = i->context;
2648 id.cb.dst_fn = i->context;
2649 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2651 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2652 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2654 newt = TREE_TYPE (newt);
2655 type = TREE_TYPE (type);
2657 if (TYPE_NAME (newt)
2658 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2659 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2660 && newt != type
2661 && TYPE_NAME (newt) == TYPE_NAME (type))
2662 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2664 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2665 if (val != DECL_VALUE_EXPR (var))
2666 SET_DECL_VALUE_EXPR (var, val);
2669 delete id.cb.decl_map;
2672 /* Fold the MEM_REF *E. */
2673 bool
2674 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2676 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2677 *ref_p = fold (*ref_p);
2678 return true;
2681 /* Do "everything else" to clean up or complete state collected by the
2682 various walking passes -- lay out the types and decls, generate code
2683 to initialize the frame decl, store critical expressions in the
2684 struct function for rtl to find. */
2686 static void
2687 finalize_nesting_tree_1 (struct nesting_info *root)
2689 gimple_seq stmt_list;
2690 gimple stmt;
2691 tree context = root->context;
2692 struct function *sf;
2694 stmt_list = NULL;
2696 /* If we created a non-local frame type or decl, we need to lay them
2697 out at this time. */
2698 if (root->frame_type)
2700 /* In some cases the frame type will trigger the -Wpadded warning.
2701 This is not helpful; suppress it. */
2702 int save_warn_padded = warn_padded;
2703 tree *adjust;
2705 warn_padded = 0;
2706 layout_type (root->frame_type);
2707 warn_padded = save_warn_padded;
2708 layout_decl (root->frame_decl, 0);
2710 /* Remove root->frame_decl from root->new_local_var_chain, so
2711 that we can declare it also in the lexical blocks, which
2712 helps ensure virtual regs that end up appearing in its RTL
2713 expression get substituted in instantiate_virtual_regs(). */
2714 for (adjust = &root->new_local_var_chain;
2715 *adjust != root->frame_decl;
2716 adjust = &DECL_CHAIN (*adjust))
2717 gcc_assert (DECL_CHAIN (*adjust));
2718 *adjust = DECL_CHAIN (*adjust);
2720 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2721 declare_vars (root->frame_decl,
2722 gimple_seq_first_stmt (gimple_body (context)), true);
2725 /* If any parameters were referenced non-locally, then we need to
2726 insert a copy. Likewise, if any variables were referenced by
2727 pointer, we need to initialize the address. */
2728 if (root->any_parm_remapped)
2730 tree p;
2731 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2733 tree field, x, y;
2735 field = lookup_field_for_decl (root, p, NO_INSERT);
2736 if (!field)
2737 continue;
2739 if (use_pointer_in_frame (p))
2740 x = build_addr (p, context);
2741 else
2742 x = p;
2744 /* If the assignment is from a non-register the stmt is
2745 not valid gimple. Make it so by using a temporary instead. */
2746 if (!is_gimple_reg (x)
2747 && is_gimple_reg_type (TREE_TYPE (x)))
2749 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2750 x = init_tmp_var (root, x, &gsi);
2753 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2754 root->frame_decl, field, NULL_TREE);
2755 stmt = gimple_build_assign (y, x);
2756 gimple_seq_add_stmt (&stmt_list, stmt);
2760 /* If a chain_field was created, then it needs to be initialized
2761 from chain_decl. */
2762 if (root->chain_field)
2764 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2765 root->frame_decl, root->chain_field, NULL_TREE);
2766 stmt = gimple_build_assign (x, get_chain_decl (root));
2767 gimple_seq_add_stmt (&stmt_list, stmt);
2770 /* If trampolines were created, then we need to initialize them. */
2771 if (root->any_tramp_created)
2773 struct nesting_info *i;
2774 for (i = root->inner; i ; i = i->next)
2776 tree arg1, arg2, arg3, x, field;
2778 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2779 if (!field)
2780 continue;
2782 gcc_assert (DECL_STATIC_CHAIN (i->context));
2783 arg3 = build_addr (root->frame_decl, context);
2785 arg2 = build_addr (i->context, context);
2787 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2788 root->frame_decl, field, NULL_TREE);
2789 arg1 = build_addr (x, context);
2791 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2792 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2793 gimple_seq_add_stmt (&stmt_list, stmt);
2797 /* If we created initialization statements, insert them. */
2798 if (stmt_list)
2800 gbind *bind;
2801 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2802 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2803 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2804 gimple_bind_set_body (bind, stmt_list);
2807 /* If a chain_decl was created, then it needs to be registered with
2808 struct function so that it gets initialized from the static chain
2809 register at the beginning of the function. */
2810 sf = DECL_STRUCT_FUNCTION (root->context);
2811 sf->static_chain_decl = root->chain_decl;
2813 /* Similarly for the non-local goto save area. */
2814 if (root->nl_goto_field)
2816 sf->nonlocal_goto_save_area
2817 = get_frame_field (root, context, root->nl_goto_field, NULL);
2818 sf->has_nonlocal_label = 1;
2821 /* Make sure all new local variables get inserted into the
2822 proper BIND_EXPR. */
2823 if (root->new_local_var_chain)
2824 declare_vars (root->new_local_var_chain,
2825 gimple_seq_first_stmt (gimple_body (root->context)),
2826 false);
2828 if (root->debug_var_chain)
2830 tree debug_var;
2831 gbind *scope;
2833 remap_vla_decls (DECL_INITIAL (root->context), root);
2835 for (debug_var = root->debug_var_chain; debug_var;
2836 debug_var = DECL_CHAIN (debug_var))
2837 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2838 break;
2840 /* If there are any debug decls with variable length types,
2841 remap those types using other debug_var_chain variables. */
2842 if (debug_var)
2844 struct nesting_copy_body_data id;
2846 memset (&id, 0, sizeof (id));
2847 id.cb.copy_decl = nesting_copy_decl;
2848 id.cb.decl_map = new hash_map<tree, tree>;
2849 id.root = root;
2851 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2852 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2854 tree type = TREE_TYPE (debug_var);
2855 tree newt, t = type;
2856 struct nesting_info *i;
2858 for (i = root; i; i = i->outer)
2859 if (variably_modified_type_p (type, i->context))
2860 break;
2862 if (i == NULL)
2863 continue;
2865 id.cb.src_fn = i->context;
2866 id.cb.dst_fn = i->context;
2867 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2869 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2870 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2872 newt = TREE_TYPE (newt);
2873 t = TREE_TYPE (t);
2875 if (TYPE_NAME (newt)
2876 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2877 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2878 && newt != t
2879 && TYPE_NAME (newt) == TYPE_NAME (t))
2880 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2883 delete id.cb.decl_map;
2886 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
2887 if (gimple_bind_block (scope))
2888 declare_vars (root->debug_var_chain, scope, true);
2889 else
2890 BLOCK_VARS (DECL_INITIAL (root->context))
2891 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2892 root->debug_var_chain);
2895 /* Fold the rewritten MEM_REF trees. */
2896 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2898 /* Dump the translated tree function. */
2899 if (dump_file)
2901 fputs ("\n\n", dump_file);
2902 dump_function_to_file (root->context, dump_file, dump_flags);
2906 static void
2907 finalize_nesting_tree (struct nesting_info *root)
2909 struct nesting_info *n;
2910 FOR_EACH_NEST_INFO (n, root)
2911 finalize_nesting_tree_1 (n);
2914 /* Unnest the nodes and pass them to cgraph. */
2916 static void
2917 unnest_nesting_tree_1 (struct nesting_info *root)
2919 struct cgraph_node *node = cgraph_node::get (root->context);
2921 /* For nested functions update the cgraph to reflect unnesting.
2922 We also delay finalizing of these functions up to this point. */
2923 if (node->origin)
2925 node->unnest ();
2926 cgraph_node::finalize_function (root->context, true);
2930 static void
2931 unnest_nesting_tree (struct nesting_info *root)
2933 struct nesting_info *n;
2934 FOR_EACH_NEST_INFO (n, root)
2935 unnest_nesting_tree_1 (n);
2938 /* Free the data structures allocated during this pass. */
2940 static void
2941 free_nesting_tree (struct nesting_info *root)
2943 struct nesting_info *node, *next;
2945 node = iter_nestinfo_start (root);
2948 next = iter_nestinfo_next (node);
2949 delete node->var_map;
2950 delete node->field_map;
2951 delete node->mem_refs;
2952 free (node);
2953 node = next;
2955 while (node);
2958 /* Gimplify a function and all its nested functions. */
2959 static void
2960 gimplify_all_functions (struct cgraph_node *root)
2962 struct cgraph_node *iter;
2963 if (!gimple_body (root->decl))
2964 gimplify_function_tree (root->decl);
2965 for (iter = root->nested; iter; iter = iter->next_nested)
2966 gimplify_all_functions (iter);
2969 /* Main entry point for this pass. Process FNDECL and all of its nested
2970 subroutines and turn them into something less tightly bound. */
2972 void
2973 lower_nested_functions (tree fndecl)
2975 struct cgraph_node *cgn;
2976 struct nesting_info *root;
2978 /* If there are no nested functions, there's nothing to do. */
2979 cgn = cgraph_node::get (fndecl);
2980 if (!cgn->nested)
2981 return;
2983 gimplify_all_functions (cgn);
2985 dump_file = dump_begin (TDI_nested, &dump_flags);
2986 if (dump_file)
2987 fprintf (dump_file, "\n;; Function %s\n\n",
2988 lang_hooks.decl_printable_name (fndecl, 2));
2990 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2991 root = create_nesting_tree (cgn);
2993 walk_all_functions (convert_nonlocal_reference_stmt,
2994 convert_nonlocal_reference_op,
2995 root);
2996 walk_all_functions (convert_local_reference_stmt,
2997 convert_local_reference_op,
2998 root);
2999 walk_all_functions (convert_nl_goto_reference, NULL, root);
3000 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3002 convert_all_function_calls (root);
3003 finalize_nesting_tree (root);
3004 unnest_nesting_tree (root);
3006 free_nesting_tree (root);
3007 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3009 if (dump_file)
3011 dump_end (TDI_nested, dump_file);
3012 dump_file = NULL;
3016 #include "gt-tree-nested.h"