diagnostic.c (get_terminal_width): Renamed from
[official-gcc.git] / gcc / tree-nested.c
blob4d318376e58408a7e94571016521f9e8c3b393e7
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 "hash-map.h"
50 #include "plugin-api.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
55 #include "langhooks.h"
56 #include "gimple-low.h"
59 /* The object of this pass is to lower the representation of a set of nested
60 functions in order to expose all of the gory details of the various
61 nonlocal references. We want to do this sooner rather than later, in
62 order to give us more freedom in emitting all of the functions in question.
64 Back in olden times, when gcc was young, we developed an insanely
65 complicated scheme whereby variables which were referenced nonlocally
66 were forced to live in the stack of the declaring function, and then
67 the nested functions magically discovered where these variables were
68 placed. In order for this scheme to function properly, it required
69 that the outer function be partially expanded, then we switch to
70 compiling the inner function, and once done with those we switch back
71 to compiling the outer function. Such delicate ordering requirements
72 makes it difficult to do whole translation unit optimizations
73 involving such functions.
75 The implementation here is much more direct. Everything that can be
76 referenced by an inner function is a member of an explicitly created
77 structure herein called the "nonlocal frame struct". The incoming
78 static chain for a nested function is a pointer to this struct in
79 the parent. In this way, we settle on known offsets from a known
80 base, and so are decoupled from the logic that places objects in the
81 function's stack frame. More importantly, we don't have to wait for
82 that to happen -- since the compilation of the inner function is no
83 longer tied to a real stack frame, the nonlocal frame struct can be
84 allocated anywhere. Which means that the outer function is now
85 inlinable.
87 Theory of operation here is very simple. Iterate over all the
88 statements in all the functions (depth first) several times,
89 allocating structures and fields on demand. In general we want to
90 examine inner functions first, so that we can avoid making changes
91 to outer functions which are unnecessary.
93 The order of the passes matters a bit, in that later passes will be
94 skipped if it is discovered that the functions don't actually interact
95 at all. That is, they're nested in the lexical sense but could have
96 been written as independent functions without change. */
99 struct nesting_info
101 struct nesting_info *outer;
102 struct nesting_info *inner;
103 struct nesting_info *next;
105 hash_map<tree, tree> *field_map;
106 hash_map<tree, tree> *var_map;
107 hash_set<tree *> *mem_refs;
108 bitmap suppress_expansion;
110 tree context;
111 tree new_local_var_chain;
112 tree debug_var_chain;
113 tree frame_type;
114 tree frame_decl;
115 tree chain_field;
116 tree chain_decl;
117 tree nl_goto_field;
119 bool any_parm_remapped;
120 bool any_tramp_created;
121 char static_chain_added;
125 /* Iterate over the nesting tree, starting with ROOT, depth first. */
127 static inline struct nesting_info *
128 iter_nestinfo_start (struct nesting_info *root)
130 while (root->inner)
131 root = root->inner;
132 return root;
135 static inline struct nesting_info *
136 iter_nestinfo_next (struct nesting_info *node)
138 if (node->next)
139 return iter_nestinfo_start (node->next);
140 return node->outer;
143 #define FOR_EACH_NEST_INFO(I, ROOT) \
144 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
146 /* Obstack used for the bitmaps in the struct above. */
147 static struct bitmap_obstack nesting_info_bitmap_obstack;
150 /* We're working in so many different function contexts simultaneously,
151 that create_tmp_var is dangerous. Prevent mishap. */
152 #define create_tmp_var cant_use_create_tmp_var_here_dummy
154 /* Like create_tmp_var, except record the variable for registration at
155 the given nesting level. */
157 static tree
158 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
160 tree tmp_var;
162 /* If the type is of variable size or a type which must be created by the
163 frontend, something is wrong. Note that we explicitly allow
164 incomplete types here, since we create them ourselves here. */
165 gcc_assert (!TREE_ADDRESSABLE (type));
166 gcc_assert (!TYPE_SIZE_UNIT (type)
167 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
169 tmp_var = create_tmp_var_raw (type, prefix);
170 DECL_CONTEXT (tmp_var) = info->context;
171 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
172 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
173 if (TREE_CODE (type) == COMPLEX_TYPE
174 || TREE_CODE (type) == VECTOR_TYPE)
175 DECL_GIMPLE_REG_P (tmp_var) = 1;
177 info->new_local_var_chain = tmp_var;
179 return tmp_var;
182 /* Take the address of EXP to be used within function CONTEXT.
183 Mark it for addressability as necessary. */
185 tree
186 build_addr (tree exp, tree context)
188 tree base = exp;
189 tree save_context;
190 tree retval;
192 while (handled_component_p (base))
193 base = TREE_OPERAND (base, 0);
195 if (DECL_P (base))
196 TREE_ADDRESSABLE (base) = 1;
198 /* Building the ADDR_EXPR will compute a set of properties for
199 that ADDR_EXPR. Those properties are unfortunately context
200 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
202 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
203 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
204 way the properties are for the ADDR_EXPR are computed properly. */
205 save_context = current_function_decl;
206 current_function_decl = context;
207 retval = build_fold_addr_expr (exp);
208 current_function_decl = save_context;
209 return retval;
212 /* Insert FIELD into TYPE, sorted by alignment requirements. */
214 void
215 insert_field_into_struct (tree type, tree field)
217 tree *p;
219 DECL_CONTEXT (field) = type;
221 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
222 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
223 break;
225 DECL_CHAIN (field) = *p;
226 *p = field;
228 /* Set correct alignment for frame struct type. */
229 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
230 TYPE_ALIGN (type) = DECL_ALIGN (field);
233 /* Build or return the RECORD_TYPE that describes the frame state that is
234 shared between INFO->CONTEXT and its nested functions. This record will
235 not be complete until finalize_nesting_tree; up until that point we'll
236 be adding fields as necessary.
238 We also build the DECL that represents this frame in the function. */
240 static tree
241 get_frame_type (struct nesting_info *info)
243 tree type = info->frame_type;
244 if (!type)
246 char *name;
248 type = make_node (RECORD_TYPE);
250 name = concat ("FRAME.",
251 IDENTIFIER_POINTER (DECL_NAME (info->context)),
252 NULL);
253 TYPE_NAME (type) = get_identifier (name);
254 free (name);
256 info->frame_type = type;
257 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
258 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
260 /* ??? Always make it addressable for now, since it is meant to
261 be pointed to by the static chain pointer. This pessimizes
262 when it turns out that no static chains are needed because
263 the nested functions referencing non-local variables are not
264 reachable, but the true pessimization is to create the non-
265 local frame structure in the first place. */
266 TREE_ADDRESSABLE (info->frame_decl) = 1;
268 return type;
271 /* Return true if DECL should be referenced by pointer in the non-local
272 frame structure. */
274 static bool
275 use_pointer_in_frame (tree decl)
277 if (TREE_CODE (decl) == PARM_DECL)
279 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
280 sized decls, and inefficient to copy large aggregates. Don't bother
281 moving anything but scalar variables. */
282 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
284 else
286 /* Variable sized types make things "interesting" in the frame. */
287 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
291 /* Given DECL, a non-locally accessed variable, find or create a field
292 in the non-local frame structure for the given nesting context. */
294 static tree
295 lookup_field_for_decl (struct nesting_info *info, tree decl,
296 enum insert_option insert)
298 if (insert == NO_INSERT)
300 tree *slot = info->field_map->get (decl);
301 return slot ? *slot : NULL_TREE;
304 tree *slot = &info->field_map->get_or_insert (decl);
305 if (!*slot)
307 tree field = make_node (FIELD_DECL);
308 DECL_NAME (field) = DECL_NAME (decl);
310 if (use_pointer_in_frame (decl))
312 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
313 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
314 DECL_NONADDRESSABLE_P (field) = 1;
316 else
318 TREE_TYPE (field) = TREE_TYPE (decl);
319 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
320 DECL_ALIGN (field) = DECL_ALIGN (decl);
321 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
322 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
323 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
324 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
327 insert_field_into_struct (get_frame_type (info), field);
328 *slot = field;
330 if (TREE_CODE (decl) == PARM_DECL)
331 info->any_parm_remapped = true;
334 return *slot;
337 /* Build or return the variable that holds the static chain within
338 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
340 static tree
341 get_chain_decl (struct nesting_info *info)
343 tree decl = info->chain_decl;
345 if (!decl)
347 tree type;
349 type = get_frame_type (info->outer);
350 type = build_pointer_type (type);
352 /* Note that this variable is *not* entered into any BIND_EXPR;
353 the construction of this variable is handled specially in
354 expand_function_start and initialize_inlined_parameters.
355 Note also that it's represented as a parameter. This is more
356 close to the truth, since the initial value does come from
357 the caller. */
358 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
359 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
360 DECL_ARTIFICIAL (decl) = 1;
361 DECL_IGNORED_P (decl) = 1;
362 TREE_USED (decl) = 1;
363 DECL_CONTEXT (decl) = info->context;
364 DECL_ARG_TYPE (decl) = type;
366 /* Tell tree-inline.c that we never write to this variable, so
367 it can copy-prop the replacement value immediately. */
368 TREE_READONLY (decl) = 1;
370 info->chain_decl = decl;
372 if (dump_file
373 && (dump_flags & TDF_DETAILS)
374 && !DECL_STATIC_CHAIN (info->context))
375 fprintf (dump_file, "Setting static-chain for %s\n",
376 lang_hooks.decl_printable_name (info->context, 2));
378 DECL_STATIC_CHAIN (info->context) = 1;
380 return decl;
383 /* Build or return the field within the non-local frame state that holds
384 the static chain for INFO->CONTEXT. This is the way to walk back up
385 multiple nesting levels. */
387 static tree
388 get_chain_field (struct nesting_info *info)
390 tree field = info->chain_field;
392 if (!field)
394 tree type = build_pointer_type (get_frame_type (info->outer));
396 field = make_node (FIELD_DECL);
397 DECL_NAME (field) = get_identifier ("__chain");
398 TREE_TYPE (field) = type;
399 DECL_ALIGN (field) = TYPE_ALIGN (type);
400 DECL_NONADDRESSABLE_P (field) = 1;
402 insert_field_into_struct (get_frame_type (info), field);
404 info->chain_field = field;
406 if (dump_file
407 && (dump_flags & TDF_DETAILS)
408 && !DECL_STATIC_CHAIN (info->context))
409 fprintf (dump_file, "Setting static-chain for %s\n",
410 lang_hooks.decl_printable_name (info->context, 2));
412 DECL_STATIC_CHAIN (info->context) = 1;
414 return field;
417 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
419 static tree
420 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
421 gcall *call)
423 tree t;
425 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
426 gimple_call_set_lhs (call, t);
427 if (! gsi_end_p (*gsi))
428 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
429 gsi_insert_before (gsi, call, GSI_SAME_STMT);
431 return t;
435 /* Copy EXP into a temporary. Allocate the temporary in the context of
436 INFO and insert the initialization statement before GSI. */
438 static tree
439 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
441 tree t;
442 gimple stmt;
444 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
445 stmt = gimple_build_assign (t, exp);
446 if (! gsi_end_p (*gsi))
447 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
448 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
450 return t;
454 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
456 static tree
457 gsi_gimplify_val (struct nesting_info *info, tree exp,
458 gimple_stmt_iterator *gsi)
460 if (is_gimple_val (exp))
461 return exp;
462 else
463 return init_tmp_var (info, exp, gsi);
466 /* Similarly, but copy from the temporary and insert the statement
467 after the iterator. */
469 static tree
470 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
472 tree t;
473 gimple stmt;
475 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
476 stmt = gimple_build_assign (exp, t);
477 if (! gsi_end_p (*gsi))
478 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
479 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
481 return t;
484 /* Build or return the type used to represent a nested function trampoline. */
486 static GTY(()) tree trampoline_type;
488 static tree
489 get_trampoline_type (struct nesting_info *info)
491 unsigned align, size;
492 tree t;
494 if (trampoline_type)
495 return trampoline_type;
497 align = TRAMPOLINE_ALIGNMENT;
498 size = TRAMPOLINE_SIZE;
500 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
501 then allocate extra space so that we can do dynamic alignment. */
502 if (align > STACK_BOUNDARY)
504 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
505 align = STACK_BOUNDARY;
508 t = build_index_type (size_int (size - 1));
509 t = build_array_type (char_type_node, t);
510 t = build_decl (DECL_SOURCE_LOCATION (info->context),
511 FIELD_DECL, get_identifier ("__data"), t);
512 DECL_ALIGN (t) = align;
513 DECL_USER_ALIGN (t) = 1;
515 trampoline_type = make_node (RECORD_TYPE);
516 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
517 TYPE_FIELDS (trampoline_type) = t;
518 layout_type (trampoline_type);
519 DECL_CONTEXT (t) = trampoline_type;
521 return trampoline_type;
524 /* Given DECL, a nested function, find or create a field in the non-local
525 frame structure for a trampoline for this function. */
527 static tree
528 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
529 enum insert_option insert)
531 if (insert == NO_INSERT)
533 tree *slot = info->var_map->get (decl);
534 return slot ? *slot : NULL_TREE;
537 tree *slot = &info->var_map->get_or_insert (decl);
538 if (!*slot)
540 tree field = make_node (FIELD_DECL);
541 DECL_NAME (field) = DECL_NAME (decl);
542 TREE_TYPE (field) = get_trampoline_type (info);
543 TREE_ADDRESSABLE (field) = 1;
545 insert_field_into_struct (get_frame_type (info), field);
546 *slot = field;
548 info->any_tramp_created = true;
551 return *slot;
554 /* Build or return the field within the non-local frame state that holds
555 the non-local goto "jmp_buf". The buffer itself is maintained by the
556 rtl middle-end as dynamic stack space is allocated. */
558 static tree
559 get_nl_goto_field (struct nesting_info *info)
561 tree field = info->nl_goto_field;
562 if (!field)
564 unsigned size;
565 tree type;
567 /* For __builtin_nonlocal_goto, we need N words. The first is the
568 frame pointer, the rest is for the target's stack pointer save
569 area. The number of words is controlled by STACK_SAVEAREA_MODE;
570 not the best interface, but it'll do for now. */
571 if (Pmode == ptr_mode)
572 type = ptr_type_node;
573 else
574 type = lang_hooks.types.type_for_mode (Pmode, 1);
576 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
577 size = size / GET_MODE_SIZE (Pmode);
578 size = size + 1;
580 type = build_array_type
581 (type, build_index_type (size_int (size)));
583 field = make_node (FIELD_DECL);
584 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
585 TREE_TYPE (field) = type;
586 DECL_ALIGN (field) = TYPE_ALIGN (type);
587 TREE_ADDRESSABLE (field) = 1;
589 insert_field_into_struct (get_frame_type (info), field);
591 info->nl_goto_field = field;
594 return field;
597 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
599 static void
600 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
601 struct nesting_info *info, gimple_seq *pseq)
603 struct walk_stmt_info wi;
605 memset (&wi, 0, sizeof (wi));
606 wi.info = info;
607 wi.val_only = true;
608 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
612 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
614 static inline void
615 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
616 struct nesting_info *info)
618 gimple_seq body = gimple_body (info->context);
619 walk_body (callback_stmt, callback_op, info, &body);
620 gimple_set_body (info->context, body);
623 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
625 static void
626 walk_gimple_omp_for (gomp_for *for_stmt,
627 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
628 struct nesting_info *info)
630 struct walk_stmt_info wi;
631 gimple_seq seq;
632 tree t;
633 size_t i;
635 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
637 seq = NULL;
638 memset (&wi, 0, sizeof (wi));
639 wi.info = info;
640 wi.gsi = gsi_last (seq);
642 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
644 wi.val_only = false;
645 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
646 &wi, NULL);
647 wi.val_only = true;
648 wi.is_lhs = false;
649 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
650 &wi, NULL);
652 wi.val_only = true;
653 wi.is_lhs = false;
654 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
655 &wi, NULL);
657 t = gimple_omp_for_incr (for_stmt, i);
658 gcc_assert (BINARY_CLASS_P (t));
659 wi.val_only = false;
660 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
661 wi.val_only = true;
662 wi.is_lhs = false;
663 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
666 seq = gsi_seq (wi.gsi);
667 if (!gimple_seq_empty_p (seq))
669 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
670 annotate_all_with_location (seq, gimple_location (for_stmt));
671 gimple_seq_add_seq (&pre_body, seq);
672 gimple_omp_for_set_pre_body (for_stmt, pre_body);
676 /* Similarly for ROOT and all functions nested underneath, depth first. */
678 static void
679 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
680 struct nesting_info *root)
682 struct nesting_info *n;
683 FOR_EACH_NEST_INFO (n, root)
684 walk_function (callback_stmt, callback_op, n);
688 /* We have to check for a fairly pathological case. The operands of function
689 nested function are to be interpreted in the context of the enclosing
690 function. So if any are variably-sized, they will get remapped when the
691 enclosing function is inlined. But that remapping would also have to be
692 done in the types of the PARM_DECLs of the nested function, meaning the
693 argument types of that function will disagree with the arguments in the
694 calls to that function. So we'd either have to make a copy of the nested
695 function corresponding to each time the enclosing function was inlined or
696 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
697 function. The former is not practical. The latter would still require
698 detecting this case to know when to add the conversions. So, for now at
699 least, we don't inline such an enclosing function.
701 We have to do that check recursively, so here return indicating whether
702 FNDECL has such a nested function. ORIG_FN is the function we were
703 trying to inline to use for checking whether any argument is variably
704 modified by anything in it.
706 It would be better to do this in tree-inline.c so that we could give
707 the appropriate warning for why a function can't be inlined, but that's
708 too late since the nesting structure has already been flattened and
709 adding a flag just to record this fact seems a waste of a flag. */
711 static bool
712 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
714 struct cgraph_node *cgn = cgraph_node::get (fndecl);
715 tree arg;
717 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
719 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
720 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
721 return true;
723 if (check_for_nested_with_variably_modified (cgn->decl,
724 orig_fndecl))
725 return true;
728 return false;
731 /* Construct our local datastructure describing the function nesting
732 tree rooted by CGN. */
734 static struct nesting_info *
735 create_nesting_tree (struct cgraph_node *cgn)
737 struct nesting_info *info = XCNEW (struct nesting_info);
738 info->field_map = new hash_map<tree, tree>;
739 info->var_map = new hash_map<tree, tree>;
740 info->mem_refs = new hash_set<tree *>;
741 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
742 info->context = cgn->decl;
744 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
746 struct nesting_info *sub = create_nesting_tree (cgn);
747 sub->outer = info;
748 sub->next = info->inner;
749 info->inner = sub;
752 /* See discussion at check_for_nested_with_variably_modified for a
753 discussion of why this has to be here. */
754 if (check_for_nested_with_variably_modified (info->context, info->context))
755 DECL_UNINLINABLE (info->context) = true;
757 return info;
760 /* Return an expression computing the static chain for TARGET_CONTEXT
761 from INFO->CONTEXT. Insert any necessary computations before TSI. */
763 static tree
764 get_static_chain (struct nesting_info *info, tree target_context,
765 gimple_stmt_iterator *gsi)
767 struct nesting_info *i;
768 tree x;
770 if (info->context == target_context)
772 x = build_addr (info->frame_decl, target_context);
774 else
776 x = get_chain_decl (info);
778 for (i = info->outer; i->context != target_context; i = i->outer)
780 tree field = get_chain_field (i);
782 x = build_simple_mem_ref (x);
783 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
784 x = init_tmp_var (info, x, gsi);
788 return x;
792 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
793 frame as seen from INFO->CONTEXT. Insert any necessary computations
794 before GSI. */
796 static tree
797 get_frame_field (struct nesting_info *info, tree target_context,
798 tree field, gimple_stmt_iterator *gsi)
800 struct nesting_info *i;
801 tree x;
803 if (info->context == target_context)
805 /* Make sure frame_decl gets created. */
806 (void) get_frame_type (info);
807 x = info->frame_decl;
809 else
811 x = get_chain_decl (info);
813 for (i = info->outer; i->context != target_context; i = i->outer)
815 tree field = get_chain_field (i);
817 x = build_simple_mem_ref (x);
818 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
819 x = init_tmp_var (info, x, gsi);
822 x = build_simple_mem_ref (x);
825 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
826 return x;
829 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
831 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
832 in the nested function with DECL_VALUE_EXPR set to reference the true
833 variable in the parent function. This is used both for debug info
834 and in OpenMP lowering. */
836 static tree
837 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
839 tree target_context;
840 struct nesting_info *i;
841 tree x, field, new_decl;
843 tree *slot = &info->var_map->get_or_insert (decl);
845 if (*slot)
846 return *slot;
848 target_context = decl_function_context (decl);
850 /* A copy of the code in get_frame_field, but without the temporaries. */
851 if (info->context == target_context)
853 /* Make sure frame_decl gets created. */
854 (void) get_frame_type (info);
855 x = info->frame_decl;
856 i = info;
858 else
860 x = get_chain_decl (info);
861 for (i = info->outer; i->context != target_context; i = i->outer)
863 field = get_chain_field (i);
864 x = build_simple_mem_ref (x);
865 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
867 x = build_simple_mem_ref (x);
870 field = lookup_field_for_decl (i, decl, INSERT);
871 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
872 if (use_pointer_in_frame (decl))
873 x = build_simple_mem_ref (x);
875 /* ??? We should be remapping types as well, surely. */
876 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
877 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
878 DECL_CONTEXT (new_decl) = info->context;
879 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
880 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
881 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
882 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
883 TREE_READONLY (new_decl) = TREE_READONLY (decl);
884 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
885 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
886 if ((TREE_CODE (decl) == PARM_DECL
887 || TREE_CODE (decl) == RESULT_DECL
888 || TREE_CODE (decl) == VAR_DECL)
889 && DECL_BY_REFERENCE (decl))
890 DECL_BY_REFERENCE (new_decl) = 1;
892 SET_DECL_VALUE_EXPR (new_decl, x);
893 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
895 *slot = new_decl;
896 DECL_CHAIN (new_decl) = info->debug_var_chain;
897 info->debug_var_chain = new_decl;
899 if (!optimize
900 && info->context != target_context
901 && variably_modified_type_p (TREE_TYPE (decl), NULL))
902 note_nonlocal_vla_type (info, TREE_TYPE (decl));
904 return new_decl;
908 /* Callback for walk_gimple_stmt, rewrite all references to VAR
909 and PARM_DECLs that belong to outer functions.
911 The rewrite will involve some number of structure accesses back up
912 the static chain. E.g. for a variable FOO up one nesting level it'll
913 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
914 indirections apply to decls for which use_pointer_in_frame is true. */
916 static tree
917 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
919 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
920 struct nesting_info *const info = (struct nesting_info *) wi->info;
921 tree t = *tp;
923 *walk_subtrees = 0;
924 switch (TREE_CODE (t))
926 case VAR_DECL:
927 /* Non-automatic variables are never processed. */
928 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
929 break;
930 /* FALLTHRU */
932 case PARM_DECL:
933 if (decl_function_context (t) != info->context)
935 tree x;
936 wi->changed = true;
938 x = get_nonlocal_debug_decl (info, t);
939 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
941 tree target_context = decl_function_context (t);
942 struct nesting_info *i;
943 for (i = info->outer; i->context != target_context; i = i->outer)
944 continue;
945 x = lookup_field_for_decl (i, t, INSERT);
946 x = get_frame_field (info, target_context, x, &wi->gsi);
947 if (use_pointer_in_frame (t))
949 x = init_tmp_var (info, x, &wi->gsi);
950 x = build_simple_mem_ref (x);
954 if (wi->val_only)
956 if (wi->is_lhs)
957 x = save_tmp_var (info, x, &wi->gsi);
958 else
959 x = init_tmp_var (info, x, &wi->gsi);
962 *tp = x;
964 break;
966 case LABEL_DECL:
967 /* We're taking the address of a label from a parent function, but
968 this is not itself a non-local goto. Mark the label such that it
969 will not be deleted, much as we would with a label address in
970 static storage. */
971 if (decl_function_context (t) != info->context)
972 FORCED_LABEL (t) = 1;
973 break;
975 case ADDR_EXPR:
977 bool save_val_only = wi->val_only;
979 wi->val_only = false;
980 wi->is_lhs = false;
981 wi->changed = false;
982 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
983 wi->val_only = true;
985 if (wi->changed)
987 tree save_context;
989 /* If we changed anything, we might no longer be directly
990 referencing a decl. */
991 save_context = current_function_decl;
992 current_function_decl = info->context;
993 recompute_tree_invariant_for_addr_expr (t);
994 current_function_decl = save_context;
996 /* If the callback converted the address argument in a context
997 where we only accept variables (and min_invariant, presumably),
998 then compute the address into a temporary. */
999 if (save_val_only)
1000 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1001 t, &wi->gsi);
1004 break;
1006 case REALPART_EXPR:
1007 case IMAGPART_EXPR:
1008 case COMPONENT_REF:
1009 case ARRAY_REF:
1010 case ARRAY_RANGE_REF:
1011 case BIT_FIELD_REF:
1012 /* Go down this entire nest and just look at the final prefix and
1013 anything that describes the references. Otherwise, we lose track
1014 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1015 wi->val_only = true;
1016 wi->is_lhs = false;
1017 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1019 if (TREE_CODE (t) == COMPONENT_REF)
1020 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1021 NULL);
1022 else if (TREE_CODE (t) == ARRAY_REF
1023 || TREE_CODE (t) == ARRAY_RANGE_REF)
1025 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1026 wi, NULL);
1027 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1028 wi, NULL);
1029 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1030 wi, NULL);
1033 wi->val_only = false;
1034 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1035 break;
1037 case VIEW_CONVERT_EXPR:
1038 /* Just request to look at the subtrees, leaving val_only and lhs
1039 untouched. This might actually be for !val_only + lhs, in which
1040 case we don't want to force a replacement by a temporary. */
1041 *walk_subtrees = 1;
1042 break;
1044 default:
1045 if (!IS_TYPE_OR_DECL_P (t))
1047 *walk_subtrees = 1;
1048 wi->val_only = true;
1049 wi->is_lhs = false;
1051 break;
1054 return NULL_TREE;
1057 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1058 struct walk_stmt_info *);
1060 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1061 and PARM_DECLs that belong to outer functions. */
1063 static bool
1064 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1066 struct nesting_info *const info = (struct nesting_info *) wi->info;
1067 bool need_chain = false, need_stmts = false;
1068 tree clause, decl;
1069 int dummy;
1070 bitmap new_suppress;
1072 new_suppress = BITMAP_GGC_ALLOC ();
1073 bitmap_copy (new_suppress, info->suppress_expansion);
1075 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1077 switch (OMP_CLAUSE_CODE (clause))
1079 case OMP_CLAUSE_REDUCTION:
1080 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1081 need_stmts = true;
1082 goto do_decl_clause;
1084 case OMP_CLAUSE_LASTPRIVATE:
1085 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1086 need_stmts = true;
1087 goto do_decl_clause;
1089 case OMP_CLAUSE_LINEAR:
1090 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1091 need_stmts = true;
1092 wi->val_only = true;
1093 wi->is_lhs = false;
1094 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1095 &dummy, wi);
1096 goto do_decl_clause;
1098 case OMP_CLAUSE_PRIVATE:
1099 case OMP_CLAUSE_FIRSTPRIVATE:
1100 case OMP_CLAUSE_COPYPRIVATE:
1101 case OMP_CLAUSE_SHARED:
1102 do_decl_clause:
1103 decl = OMP_CLAUSE_DECL (clause);
1104 if (TREE_CODE (decl) == VAR_DECL
1105 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1106 break;
1107 if (decl_function_context (decl) != info->context)
1109 bitmap_set_bit (new_suppress, DECL_UID (decl));
1110 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1111 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1112 need_chain = true;
1114 break;
1116 case OMP_CLAUSE_SCHEDULE:
1117 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1118 break;
1119 /* FALLTHRU */
1120 case OMP_CLAUSE_FINAL:
1121 case OMP_CLAUSE_IF:
1122 case OMP_CLAUSE_NUM_THREADS:
1123 case OMP_CLAUSE_DEPEND:
1124 case OMP_CLAUSE_DEVICE:
1125 case OMP_CLAUSE_NUM_TEAMS:
1126 case OMP_CLAUSE_THREAD_LIMIT:
1127 case OMP_CLAUSE_SAFELEN:
1128 case OMP_CLAUSE__CILK_FOR_COUNT_:
1129 wi->val_only = true;
1130 wi->is_lhs = false;
1131 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1132 &dummy, wi);
1133 break;
1135 case OMP_CLAUSE_DIST_SCHEDULE:
1136 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1138 wi->val_only = true;
1139 wi->is_lhs = false;
1140 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1141 &dummy, wi);
1143 break;
1145 case OMP_CLAUSE_MAP:
1146 case OMP_CLAUSE_TO:
1147 case OMP_CLAUSE_FROM:
1148 if (OMP_CLAUSE_SIZE (clause))
1150 wi->val_only = true;
1151 wi->is_lhs = false;
1152 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1153 &dummy, wi);
1155 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1156 goto do_decl_clause;
1157 wi->val_only = true;
1158 wi->is_lhs = false;
1159 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1160 wi, NULL);
1161 break;
1163 case OMP_CLAUSE_ALIGNED:
1164 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1166 wi->val_only = true;
1167 wi->is_lhs = false;
1168 convert_nonlocal_reference_op
1169 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1171 /* Like do_decl_clause, but don't add any suppression. */
1172 decl = OMP_CLAUSE_DECL (clause);
1173 if (TREE_CODE (decl) == VAR_DECL
1174 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1175 break;
1176 if (decl_function_context (decl) != info->context)
1178 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1179 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1180 need_chain = true;
1182 break;
1184 case OMP_CLAUSE_NOWAIT:
1185 case OMP_CLAUSE_ORDERED:
1186 case OMP_CLAUSE_DEFAULT:
1187 case OMP_CLAUSE_COPYIN:
1188 case OMP_CLAUSE_COLLAPSE:
1189 case OMP_CLAUSE_UNTIED:
1190 case OMP_CLAUSE_MERGEABLE:
1191 case OMP_CLAUSE_PROC_BIND:
1192 break;
1194 default:
1195 gcc_unreachable ();
1199 info->suppress_expansion = new_suppress;
1201 if (need_stmts)
1202 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1203 switch (OMP_CLAUSE_CODE (clause))
1205 case OMP_CLAUSE_REDUCTION:
1206 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1208 tree old_context
1209 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1210 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1211 = info->context;
1212 walk_body (convert_nonlocal_reference_stmt,
1213 convert_nonlocal_reference_op, info,
1214 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1215 walk_body (convert_nonlocal_reference_stmt,
1216 convert_nonlocal_reference_op, info,
1217 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1218 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1219 = old_context;
1221 break;
1223 case OMP_CLAUSE_LASTPRIVATE:
1224 walk_body (convert_nonlocal_reference_stmt,
1225 convert_nonlocal_reference_op, info,
1226 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1227 break;
1229 case OMP_CLAUSE_LINEAR:
1230 walk_body (convert_nonlocal_reference_stmt,
1231 convert_nonlocal_reference_op, info,
1232 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1233 break;
1235 default:
1236 break;
1239 return need_chain;
1242 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1244 static void
1245 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1247 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1248 type = TREE_TYPE (type);
1250 if (TYPE_NAME (type)
1251 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1252 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1253 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1255 while (POINTER_TYPE_P (type)
1256 || TREE_CODE (type) == VECTOR_TYPE
1257 || TREE_CODE (type) == FUNCTION_TYPE
1258 || TREE_CODE (type) == METHOD_TYPE)
1259 type = TREE_TYPE (type);
1261 if (TREE_CODE (type) == ARRAY_TYPE)
1263 tree domain, t;
1265 note_nonlocal_vla_type (info, TREE_TYPE (type));
1266 domain = TYPE_DOMAIN (type);
1267 if (domain)
1269 t = TYPE_MIN_VALUE (domain);
1270 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1271 && decl_function_context (t) != info->context)
1272 get_nonlocal_debug_decl (info, t);
1273 t = TYPE_MAX_VALUE (domain);
1274 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1275 && decl_function_context (t) != info->context)
1276 get_nonlocal_debug_decl (info, t);
1281 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1282 in BLOCK. */
1284 static void
1285 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1287 tree var;
1289 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1290 if (TREE_CODE (var) == VAR_DECL
1291 && variably_modified_type_p (TREE_TYPE (var), NULL)
1292 && DECL_HAS_VALUE_EXPR_P (var)
1293 && decl_function_context (var) != info->context)
1294 note_nonlocal_vla_type (info, TREE_TYPE (var));
1297 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1298 PARM_DECLs that belong to outer functions. This handles statements
1299 that are not handled via the standard recursion done in
1300 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1301 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1302 operands of STMT have been handled by this function. */
1304 static tree
1305 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1306 struct walk_stmt_info *wi)
1308 struct nesting_info *info = (struct nesting_info *) wi->info;
1309 tree save_local_var_chain;
1310 bitmap save_suppress;
1311 gimple stmt = gsi_stmt (*gsi);
1313 switch (gimple_code (stmt))
1315 case GIMPLE_GOTO:
1316 /* Don't walk non-local gotos for now. */
1317 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1319 wi->val_only = true;
1320 wi->is_lhs = false;
1321 *handled_ops_p = true;
1322 return NULL_TREE;
1324 break;
1326 case GIMPLE_OMP_PARALLEL:
1327 case GIMPLE_OMP_TASK:
1328 save_suppress = info->suppress_expansion;
1329 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1330 wi))
1332 tree c, decl;
1333 decl = get_chain_decl (info);
1334 c = build_omp_clause (gimple_location (stmt),
1335 OMP_CLAUSE_FIRSTPRIVATE);
1336 OMP_CLAUSE_DECL (c) = decl;
1337 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1338 gimple_omp_taskreg_set_clauses (stmt, c);
1341 save_local_var_chain = info->new_local_var_chain;
1342 info->new_local_var_chain = NULL;
1344 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1345 info, gimple_omp_body_ptr (stmt));
1347 if (info->new_local_var_chain)
1348 declare_vars (info->new_local_var_chain,
1349 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1350 false);
1351 info->new_local_var_chain = save_local_var_chain;
1352 info->suppress_expansion = save_suppress;
1353 break;
1355 case GIMPLE_OMP_FOR:
1356 save_suppress = info->suppress_expansion;
1357 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1358 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1359 convert_nonlocal_reference_stmt,
1360 convert_nonlocal_reference_op, info);
1361 walk_body (convert_nonlocal_reference_stmt,
1362 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1363 info->suppress_expansion = save_suppress;
1364 break;
1366 case GIMPLE_OMP_SECTIONS:
1367 save_suppress = info->suppress_expansion;
1368 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1369 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1370 info, gimple_omp_body_ptr (stmt));
1371 info->suppress_expansion = save_suppress;
1372 break;
1374 case GIMPLE_OMP_SINGLE:
1375 save_suppress = info->suppress_expansion;
1376 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1377 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1378 info, gimple_omp_body_ptr (stmt));
1379 info->suppress_expansion = save_suppress;
1380 break;
1382 case GIMPLE_OMP_TARGET:
1383 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1385 save_suppress = info->suppress_expansion;
1386 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1387 wi);
1388 info->suppress_expansion = save_suppress;
1389 walk_body (convert_nonlocal_reference_stmt,
1390 convert_nonlocal_reference_op, info,
1391 gimple_omp_body_ptr (stmt));
1392 break;
1394 save_suppress = info->suppress_expansion;
1395 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1396 wi))
1398 tree c, decl;
1399 decl = get_chain_decl (info);
1400 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1401 OMP_CLAUSE_DECL (c) = decl;
1402 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TO;
1403 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1404 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1405 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1408 save_local_var_chain = info->new_local_var_chain;
1409 info->new_local_var_chain = NULL;
1411 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1412 info, gimple_omp_body_ptr (stmt));
1414 if (info->new_local_var_chain)
1415 declare_vars (info->new_local_var_chain,
1416 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1417 false);
1418 info->new_local_var_chain = save_local_var_chain;
1419 info->suppress_expansion = save_suppress;
1420 break;
1422 case GIMPLE_OMP_TEAMS:
1423 save_suppress = info->suppress_expansion;
1424 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1425 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1426 info, gimple_omp_body_ptr (stmt));
1427 info->suppress_expansion = save_suppress;
1428 break;
1430 case GIMPLE_OMP_SECTION:
1431 case GIMPLE_OMP_MASTER:
1432 case GIMPLE_OMP_TASKGROUP:
1433 case GIMPLE_OMP_ORDERED:
1434 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1435 info, gimple_omp_body_ptr (stmt));
1436 break;
1438 case GIMPLE_BIND:
1440 gbind *bind_stmt = as_a <gbind *> (stmt);
1441 if (!optimize && gimple_bind_block (bind_stmt))
1442 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1444 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1445 if (TREE_CODE (var) == NAMELIST_DECL)
1447 /* Adjust decls mentioned in NAMELIST_DECL. */
1448 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1449 tree decl;
1450 unsigned int i;
1452 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1454 if (TREE_CODE (decl) == VAR_DECL
1455 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1456 continue;
1457 if (decl_function_context (decl) != info->context)
1458 CONSTRUCTOR_ELT (decls, i)->value
1459 = get_nonlocal_debug_decl (info, decl);
1463 *handled_ops_p = false;
1464 return NULL_TREE;
1466 case GIMPLE_COND:
1467 wi->val_only = true;
1468 wi->is_lhs = false;
1469 *handled_ops_p = false;
1470 return NULL_TREE;
1472 default:
1473 /* For every other statement that we are not interested in
1474 handling here, let the walker traverse the operands. */
1475 *handled_ops_p = false;
1476 return NULL_TREE;
1479 /* We have handled all of STMT operands, no need to traverse the operands. */
1480 *handled_ops_p = true;
1481 return NULL_TREE;
1485 /* A subroutine of convert_local_reference. Create a local variable
1486 in the parent function with DECL_VALUE_EXPR set to reference the
1487 field in FRAME. This is used both for debug info and in OpenMP
1488 lowering. */
1490 static tree
1491 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1493 tree x, new_decl;
1495 tree *slot = &info->var_map->get_or_insert (decl);
1496 if (*slot)
1497 return *slot;
1499 /* Make sure frame_decl gets created. */
1500 (void) get_frame_type (info);
1501 x = info->frame_decl;
1502 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1504 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1505 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1506 DECL_CONTEXT (new_decl) = info->context;
1507 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1508 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1509 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1510 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1511 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1512 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1513 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1514 if ((TREE_CODE (decl) == PARM_DECL
1515 || TREE_CODE (decl) == RESULT_DECL
1516 || TREE_CODE (decl) == VAR_DECL)
1517 && DECL_BY_REFERENCE (decl))
1518 DECL_BY_REFERENCE (new_decl) = 1;
1520 SET_DECL_VALUE_EXPR (new_decl, x);
1521 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1522 *slot = new_decl;
1524 DECL_CHAIN (new_decl) = info->debug_var_chain;
1525 info->debug_var_chain = new_decl;
1527 /* Do not emit debug info twice. */
1528 DECL_IGNORED_P (decl) = 1;
1530 return new_decl;
1534 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1535 and PARM_DECLs that were referenced by inner nested functions.
1536 The rewrite will be a structure reference to the local frame variable. */
1538 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1540 static tree
1541 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1543 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1544 struct nesting_info *const info = (struct nesting_info *) wi->info;
1545 tree t = *tp, field, x;
1546 bool save_val_only;
1548 *walk_subtrees = 0;
1549 switch (TREE_CODE (t))
1551 case VAR_DECL:
1552 /* Non-automatic variables are never processed. */
1553 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1554 break;
1555 /* FALLTHRU */
1557 case PARM_DECL:
1558 if (decl_function_context (t) == info->context)
1560 /* If we copied a pointer to the frame, then the original decl
1561 is used unchanged in the parent function. */
1562 if (use_pointer_in_frame (t))
1563 break;
1565 /* No need to transform anything if no child references the
1566 variable. */
1567 field = lookup_field_for_decl (info, t, NO_INSERT);
1568 if (!field)
1569 break;
1570 wi->changed = true;
1572 x = get_local_debug_decl (info, t, field);
1573 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1574 x = get_frame_field (info, info->context, field, &wi->gsi);
1576 if (wi->val_only)
1578 if (wi->is_lhs)
1579 x = save_tmp_var (info, x, &wi->gsi);
1580 else
1581 x = init_tmp_var (info, x, &wi->gsi);
1584 *tp = x;
1586 break;
1588 case ADDR_EXPR:
1589 save_val_only = wi->val_only;
1590 wi->val_only = false;
1591 wi->is_lhs = false;
1592 wi->changed = false;
1593 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1594 wi->val_only = save_val_only;
1596 /* If we converted anything ... */
1597 if (wi->changed)
1599 tree save_context;
1601 /* Then the frame decl is now addressable. */
1602 TREE_ADDRESSABLE (info->frame_decl) = 1;
1604 save_context = current_function_decl;
1605 current_function_decl = info->context;
1606 recompute_tree_invariant_for_addr_expr (t);
1607 current_function_decl = save_context;
1609 /* If we are in a context where we only accept values, then
1610 compute the address into a temporary. */
1611 if (save_val_only)
1612 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1613 t, &wi->gsi);
1615 break;
1617 case REALPART_EXPR:
1618 case IMAGPART_EXPR:
1619 case COMPONENT_REF:
1620 case ARRAY_REF:
1621 case ARRAY_RANGE_REF:
1622 case BIT_FIELD_REF:
1623 /* Go down this entire nest and just look at the final prefix and
1624 anything that describes the references. Otherwise, we lose track
1625 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1626 save_val_only = wi->val_only;
1627 wi->val_only = true;
1628 wi->is_lhs = false;
1629 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1631 if (TREE_CODE (t) == COMPONENT_REF)
1632 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1633 NULL);
1634 else if (TREE_CODE (t) == ARRAY_REF
1635 || TREE_CODE (t) == ARRAY_RANGE_REF)
1637 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1638 NULL);
1639 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1640 NULL);
1641 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1642 NULL);
1645 wi->val_only = false;
1646 walk_tree (tp, convert_local_reference_op, wi, NULL);
1647 wi->val_only = save_val_only;
1648 break;
1650 case MEM_REF:
1651 save_val_only = wi->val_only;
1652 wi->val_only = true;
1653 wi->is_lhs = false;
1654 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1655 wi, NULL);
1656 /* We need to re-fold the MEM_REF as component references as
1657 part of a ADDR_EXPR address are not allowed. But we cannot
1658 fold here, as the chain record type is not yet finalized. */
1659 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1660 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1661 info->mem_refs->add (tp);
1662 wi->val_only = save_val_only;
1663 break;
1665 case VIEW_CONVERT_EXPR:
1666 /* Just request to look at the subtrees, leaving val_only and lhs
1667 untouched. This might actually be for !val_only + lhs, in which
1668 case we don't want to force a replacement by a temporary. */
1669 *walk_subtrees = 1;
1670 break;
1672 default:
1673 if (!IS_TYPE_OR_DECL_P (t))
1675 *walk_subtrees = 1;
1676 wi->val_only = true;
1677 wi->is_lhs = false;
1679 break;
1682 return NULL_TREE;
1685 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1686 struct walk_stmt_info *);
1688 /* Helper for convert_local_reference. Convert all the references in
1689 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1691 static bool
1692 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1694 struct nesting_info *const info = (struct nesting_info *) wi->info;
1695 bool need_frame = false, need_stmts = false;
1696 tree clause, decl;
1697 int dummy;
1698 bitmap new_suppress;
1700 new_suppress = BITMAP_GGC_ALLOC ();
1701 bitmap_copy (new_suppress, info->suppress_expansion);
1703 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1705 switch (OMP_CLAUSE_CODE (clause))
1707 case OMP_CLAUSE_REDUCTION:
1708 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1709 need_stmts = true;
1710 goto do_decl_clause;
1712 case OMP_CLAUSE_LASTPRIVATE:
1713 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1714 need_stmts = true;
1715 goto do_decl_clause;
1717 case OMP_CLAUSE_LINEAR:
1718 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1719 need_stmts = true;
1720 wi->val_only = true;
1721 wi->is_lhs = false;
1722 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1723 wi);
1724 goto do_decl_clause;
1726 case OMP_CLAUSE_PRIVATE:
1727 case OMP_CLAUSE_FIRSTPRIVATE:
1728 case OMP_CLAUSE_COPYPRIVATE:
1729 case OMP_CLAUSE_SHARED:
1730 do_decl_clause:
1731 decl = OMP_CLAUSE_DECL (clause);
1732 if (TREE_CODE (decl) == VAR_DECL
1733 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1734 break;
1735 if (decl_function_context (decl) == info->context
1736 && !use_pointer_in_frame (decl))
1738 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1739 if (field)
1741 bitmap_set_bit (new_suppress, DECL_UID (decl));
1742 OMP_CLAUSE_DECL (clause)
1743 = get_local_debug_decl (info, decl, field);
1744 need_frame = true;
1747 break;
1749 case OMP_CLAUSE_SCHEDULE:
1750 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1751 break;
1752 /* FALLTHRU */
1753 case OMP_CLAUSE_FINAL:
1754 case OMP_CLAUSE_IF:
1755 case OMP_CLAUSE_NUM_THREADS:
1756 case OMP_CLAUSE_DEPEND:
1757 case OMP_CLAUSE_DEVICE:
1758 case OMP_CLAUSE_NUM_TEAMS:
1759 case OMP_CLAUSE_THREAD_LIMIT:
1760 case OMP_CLAUSE_SAFELEN:
1761 case OMP_CLAUSE__CILK_FOR_COUNT_:
1762 wi->val_only = true;
1763 wi->is_lhs = false;
1764 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1765 wi);
1766 break;
1768 case OMP_CLAUSE_DIST_SCHEDULE:
1769 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1771 wi->val_only = true;
1772 wi->is_lhs = false;
1773 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1774 &dummy, wi);
1776 break;
1778 case OMP_CLAUSE_MAP:
1779 case OMP_CLAUSE_TO:
1780 case OMP_CLAUSE_FROM:
1781 if (OMP_CLAUSE_SIZE (clause))
1783 wi->val_only = true;
1784 wi->is_lhs = false;
1785 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1786 &dummy, wi);
1788 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1789 goto do_decl_clause;
1790 wi->val_only = true;
1791 wi->is_lhs = false;
1792 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1793 wi, NULL);
1794 break;
1796 case OMP_CLAUSE_ALIGNED:
1797 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1799 wi->val_only = true;
1800 wi->is_lhs = false;
1801 convert_local_reference_op
1802 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1804 /* Like do_decl_clause, but don't add any suppression. */
1805 decl = OMP_CLAUSE_DECL (clause);
1806 if (TREE_CODE (decl) == VAR_DECL
1807 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1808 break;
1809 if (decl_function_context (decl) == info->context
1810 && !use_pointer_in_frame (decl))
1812 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1813 if (field)
1815 OMP_CLAUSE_DECL (clause)
1816 = get_local_debug_decl (info, decl, field);
1817 need_frame = true;
1820 break;
1822 case OMP_CLAUSE_NOWAIT:
1823 case OMP_CLAUSE_ORDERED:
1824 case OMP_CLAUSE_DEFAULT:
1825 case OMP_CLAUSE_COPYIN:
1826 case OMP_CLAUSE_COLLAPSE:
1827 case OMP_CLAUSE_UNTIED:
1828 case OMP_CLAUSE_MERGEABLE:
1829 case OMP_CLAUSE_PROC_BIND:
1830 break;
1832 default:
1833 gcc_unreachable ();
1837 info->suppress_expansion = new_suppress;
1839 if (need_stmts)
1840 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1841 switch (OMP_CLAUSE_CODE (clause))
1843 case OMP_CLAUSE_REDUCTION:
1844 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1846 tree old_context
1847 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1848 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1849 = info->context;
1850 walk_body (convert_local_reference_stmt,
1851 convert_local_reference_op, info,
1852 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1853 walk_body (convert_local_reference_stmt,
1854 convert_local_reference_op, info,
1855 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1856 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1857 = old_context;
1859 break;
1861 case OMP_CLAUSE_LASTPRIVATE:
1862 walk_body (convert_local_reference_stmt,
1863 convert_local_reference_op, info,
1864 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1865 break;
1867 case OMP_CLAUSE_LINEAR:
1868 walk_body (convert_local_reference_stmt,
1869 convert_local_reference_op, info,
1870 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1871 break;
1873 default:
1874 break;
1877 return need_frame;
1881 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1882 and PARM_DECLs that were referenced by inner nested functions.
1883 The rewrite will be a structure reference to the local frame variable. */
1885 static tree
1886 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1887 struct walk_stmt_info *wi)
1889 struct nesting_info *info = (struct nesting_info *) wi->info;
1890 tree save_local_var_chain;
1891 bitmap save_suppress;
1892 gimple stmt = gsi_stmt (*gsi);
1894 switch (gimple_code (stmt))
1896 case GIMPLE_OMP_PARALLEL:
1897 case GIMPLE_OMP_TASK:
1898 save_suppress = info->suppress_expansion;
1899 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1900 wi))
1902 tree c;
1903 (void) get_frame_type (info);
1904 c = build_omp_clause (gimple_location (stmt),
1905 OMP_CLAUSE_SHARED);
1906 OMP_CLAUSE_DECL (c) = info->frame_decl;
1907 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1908 gimple_omp_taskreg_set_clauses (stmt, c);
1911 save_local_var_chain = info->new_local_var_chain;
1912 info->new_local_var_chain = NULL;
1914 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1915 gimple_omp_body_ptr (stmt));
1917 if (info->new_local_var_chain)
1918 declare_vars (info->new_local_var_chain,
1919 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1920 info->new_local_var_chain = save_local_var_chain;
1921 info->suppress_expansion = save_suppress;
1922 break;
1924 case GIMPLE_OMP_FOR:
1925 save_suppress = info->suppress_expansion;
1926 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1927 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1928 convert_local_reference_stmt,
1929 convert_local_reference_op, info);
1930 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1931 info, gimple_omp_body_ptr (stmt));
1932 info->suppress_expansion = save_suppress;
1933 break;
1935 case GIMPLE_OMP_SECTIONS:
1936 save_suppress = info->suppress_expansion;
1937 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1938 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1939 info, gimple_omp_body_ptr (stmt));
1940 info->suppress_expansion = save_suppress;
1941 break;
1943 case GIMPLE_OMP_SINGLE:
1944 save_suppress = info->suppress_expansion;
1945 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1946 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1947 info, gimple_omp_body_ptr (stmt));
1948 info->suppress_expansion = save_suppress;
1949 break;
1951 case GIMPLE_OMP_TARGET:
1952 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
1954 save_suppress = info->suppress_expansion;
1955 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1956 info->suppress_expansion = save_suppress;
1957 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1958 info, gimple_omp_body_ptr (stmt));
1959 break;
1961 save_suppress = info->suppress_expansion;
1962 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1964 tree c;
1965 (void) get_frame_type (info);
1966 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1967 OMP_CLAUSE_DECL (c) = info->frame_decl;
1968 OMP_CLAUSE_MAP_KIND (c) = OMP_CLAUSE_MAP_TOFROM;
1969 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1970 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1971 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1974 save_local_var_chain = info->new_local_var_chain;
1975 info->new_local_var_chain = NULL;
1977 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1978 gimple_omp_body_ptr (stmt));
1980 if (info->new_local_var_chain)
1981 declare_vars (info->new_local_var_chain,
1982 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1983 info->new_local_var_chain = save_local_var_chain;
1984 info->suppress_expansion = save_suppress;
1985 break;
1987 case GIMPLE_OMP_TEAMS:
1988 save_suppress = info->suppress_expansion;
1989 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1990 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1991 info, gimple_omp_body_ptr (stmt));
1992 info->suppress_expansion = save_suppress;
1993 break;
1995 case GIMPLE_OMP_SECTION:
1996 case GIMPLE_OMP_MASTER:
1997 case GIMPLE_OMP_TASKGROUP:
1998 case GIMPLE_OMP_ORDERED:
1999 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2000 info, gimple_omp_body_ptr (stmt));
2001 break;
2003 case GIMPLE_COND:
2004 wi->val_only = true;
2005 wi->is_lhs = false;
2006 *handled_ops_p = false;
2007 return NULL_TREE;
2009 case GIMPLE_ASSIGN:
2010 if (gimple_clobber_p (stmt))
2012 tree lhs = gimple_assign_lhs (stmt);
2013 if (!use_pointer_in_frame (lhs)
2014 && lookup_field_for_decl (info, lhs, NO_INSERT))
2016 gsi_replace (gsi, gimple_build_nop (), true);
2017 break;
2020 *handled_ops_p = false;
2021 return NULL_TREE;
2023 case GIMPLE_BIND:
2024 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2025 var;
2026 var = DECL_CHAIN (var))
2027 if (TREE_CODE (var) == NAMELIST_DECL)
2029 /* Adjust decls mentioned in NAMELIST_DECL. */
2030 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2031 tree decl;
2032 unsigned int i;
2034 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2036 if (TREE_CODE (decl) == VAR_DECL
2037 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2038 continue;
2039 if (decl_function_context (decl) == info->context
2040 && !use_pointer_in_frame (decl))
2042 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2043 if (field)
2045 CONSTRUCTOR_ELT (decls, i)->value
2046 = get_local_debug_decl (info, decl, field);
2052 *handled_ops_p = false;
2053 return NULL_TREE;
2055 default:
2056 /* For every other statement that we are not interested in
2057 handling here, let the walker traverse the operands. */
2058 *handled_ops_p = false;
2059 return NULL_TREE;
2062 /* Indicate that we have handled all the operands ourselves. */
2063 *handled_ops_p = true;
2064 return NULL_TREE;
2068 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2069 that reference labels from outer functions. The rewrite will be a
2070 call to __builtin_nonlocal_goto. */
2072 static tree
2073 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2074 struct walk_stmt_info *wi)
2076 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2077 tree label, new_label, target_context, x, field;
2078 gcall *call;
2079 gimple stmt = gsi_stmt (*gsi);
2081 if (gimple_code (stmt) != GIMPLE_GOTO)
2083 *handled_ops_p = false;
2084 return NULL_TREE;
2087 label = gimple_goto_dest (stmt);
2088 if (TREE_CODE (label) != LABEL_DECL)
2090 *handled_ops_p = false;
2091 return NULL_TREE;
2094 target_context = decl_function_context (label);
2095 if (target_context == info->context)
2097 *handled_ops_p = false;
2098 return NULL_TREE;
2101 for (i = info->outer; target_context != i->context; i = i->outer)
2102 continue;
2104 /* The original user label may also be use for a normal goto, therefore
2105 we must create a new label that will actually receive the abnormal
2106 control transfer. This new label will be marked LABEL_NONLOCAL; this
2107 mark will trigger proper behavior in the cfg, as well as cause the
2108 (hairy target-specific) non-local goto receiver code to be generated
2109 when we expand rtl. Enter this association into var_map so that we
2110 can insert the new label into the IL during a second pass. */
2111 tree *slot = &i->var_map->get_or_insert (label);
2112 if (*slot == NULL)
2114 new_label = create_artificial_label (UNKNOWN_LOCATION);
2115 DECL_NONLOCAL (new_label) = 1;
2116 *slot = new_label;
2118 else
2119 new_label = *slot;
2121 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2122 field = get_nl_goto_field (i);
2123 x = get_frame_field (info, target_context, field, gsi);
2124 x = build_addr (x, target_context);
2125 x = gsi_gimplify_val (info, x, gsi);
2126 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2127 2, build_addr (new_label, target_context), x);
2128 gsi_replace (gsi, call, false);
2130 /* We have handled all of STMT's operands, no need to keep going. */
2131 *handled_ops_p = true;
2132 return NULL_TREE;
2136 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2137 are referenced via nonlocal goto from a nested function. The rewrite
2138 will involve installing a newly generated DECL_NONLOCAL label, and
2139 (potentially) a branch around the rtl gunk that is assumed to be
2140 attached to such a label. */
2142 static tree
2143 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2144 struct walk_stmt_info *wi)
2146 struct nesting_info *const info = (struct nesting_info *) wi->info;
2147 tree label, new_label;
2148 gimple_stmt_iterator tmp_gsi;
2149 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2151 if (!stmt)
2153 *handled_ops_p = false;
2154 return NULL_TREE;
2157 label = gimple_label_label (stmt);
2159 tree *slot = info->var_map->get (label);
2160 if (!slot)
2162 *handled_ops_p = false;
2163 return NULL_TREE;
2166 /* If there's any possibility that the previous statement falls through,
2167 then we must branch around the new non-local label. */
2168 tmp_gsi = wi->gsi;
2169 gsi_prev (&tmp_gsi);
2170 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2172 gimple stmt = gimple_build_goto (label);
2173 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2176 new_label = (tree) *slot;
2177 stmt = gimple_build_label (new_label);
2178 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2180 *handled_ops_p = true;
2181 return NULL_TREE;
2185 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2186 of nested functions that require the use of trampolines. The rewrite
2187 will involve a reference a trampoline generated for the occasion. */
2189 static tree
2190 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2192 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2193 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2194 tree t = *tp, decl, target_context, x, builtin;
2195 gcall *call;
2197 *walk_subtrees = 0;
2198 switch (TREE_CODE (t))
2200 case ADDR_EXPR:
2201 /* Build
2202 T.1 = &CHAIN->tramp;
2203 T.2 = __builtin_adjust_trampoline (T.1);
2204 T.3 = (func_type)T.2;
2207 decl = TREE_OPERAND (t, 0);
2208 if (TREE_CODE (decl) != FUNCTION_DECL)
2209 break;
2211 /* Only need to process nested functions. */
2212 target_context = decl_function_context (decl);
2213 if (!target_context)
2214 break;
2216 /* If the nested function doesn't use a static chain, then
2217 it doesn't need a trampoline. */
2218 if (!DECL_STATIC_CHAIN (decl))
2219 break;
2221 /* If we don't want a trampoline, then don't build one. */
2222 if (TREE_NO_TRAMPOLINE (t))
2223 break;
2225 /* Lookup the immediate parent of the callee, as that's where
2226 we need to insert the trampoline. */
2227 for (i = info; i->context != target_context; i = i->outer)
2228 continue;
2229 x = lookup_tramp_for_decl (i, decl, INSERT);
2231 /* Compute the address of the field holding the trampoline. */
2232 x = get_frame_field (info, target_context, x, &wi->gsi);
2233 x = build_addr (x, target_context);
2234 x = gsi_gimplify_val (info, x, &wi->gsi);
2236 /* Do machine-specific ugliness. Normally this will involve
2237 computing extra alignment, but it can really be anything. */
2238 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2239 call = gimple_build_call (builtin, 1, x);
2240 x = init_tmp_var_with_call (info, &wi->gsi, call);
2242 /* Cast back to the proper function type. */
2243 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2244 x = init_tmp_var (info, x, &wi->gsi);
2246 *tp = x;
2247 break;
2249 default:
2250 if (!IS_TYPE_OR_DECL_P (t))
2251 *walk_subtrees = 1;
2252 break;
2255 return NULL_TREE;
2259 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2260 to addresses of nested functions that require the use of
2261 trampolines. The rewrite will involve a reference a trampoline
2262 generated for the occasion. */
2264 static tree
2265 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2266 struct walk_stmt_info *wi)
2268 struct nesting_info *info = (struct nesting_info *) wi->info;
2269 gimple stmt = gsi_stmt (*gsi);
2271 switch (gimple_code (stmt))
2273 case GIMPLE_CALL:
2275 /* Only walk call arguments, lest we generate trampolines for
2276 direct calls. */
2277 unsigned long i, nargs = gimple_call_num_args (stmt);
2278 for (i = 0; i < nargs; i++)
2279 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2280 wi, NULL);
2281 break;
2284 case GIMPLE_OMP_TARGET:
2285 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2287 *handled_ops_p = false;
2288 return NULL_TREE;
2290 /* FALLTHRU */
2291 case GIMPLE_OMP_PARALLEL:
2292 case GIMPLE_OMP_TASK:
2294 tree save_local_var_chain;
2295 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2296 save_local_var_chain = info->new_local_var_chain;
2297 info->new_local_var_chain = NULL;
2298 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2299 info, gimple_omp_body_ptr (stmt));
2300 if (info->new_local_var_chain)
2301 declare_vars (info->new_local_var_chain,
2302 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2303 false);
2304 info->new_local_var_chain = save_local_var_chain;
2306 break;
2308 default:
2309 *handled_ops_p = false;
2310 return NULL_TREE;
2313 *handled_ops_p = true;
2314 return NULL_TREE;
2319 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2320 that reference nested functions to make sure that the static chain
2321 is set up properly for the call. */
2323 static tree
2324 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2325 struct walk_stmt_info *wi)
2327 struct nesting_info *const info = (struct nesting_info *) wi->info;
2328 tree decl, target_context;
2329 char save_static_chain_added;
2330 int i;
2331 gimple stmt = gsi_stmt (*gsi);
2333 switch (gimple_code (stmt))
2335 case GIMPLE_CALL:
2336 if (gimple_call_chain (stmt))
2337 break;
2338 decl = gimple_call_fndecl (stmt);
2339 if (!decl)
2340 break;
2341 target_context = decl_function_context (decl);
2342 if (target_context && DECL_STATIC_CHAIN (decl))
2344 gimple_call_set_chain (as_a <gcall *> (stmt),
2345 get_static_chain (info, target_context,
2346 &wi->gsi));
2347 info->static_chain_added |= (1 << (info->context != target_context));
2349 break;
2351 case GIMPLE_OMP_PARALLEL:
2352 case GIMPLE_OMP_TASK:
2353 save_static_chain_added = info->static_chain_added;
2354 info->static_chain_added = 0;
2355 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2356 for (i = 0; i < 2; i++)
2358 tree c, decl;
2359 if ((info->static_chain_added & (1 << i)) == 0)
2360 continue;
2361 decl = i ? get_chain_decl (info) : info->frame_decl;
2362 /* Don't add CHAIN.* or FRAME.* twice. */
2363 for (c = gimple_omp_taskreg_clauses (stmt);
2365 c = OMP_CLAUSE_CHAIN (c))
2366 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2367 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2368 && OMP_CLAUSE_DECL (c) == decl)
2369 break;
2370 if (c == NULL)
2372 c = build_omp_clause (gimple_location (stmt),
2373 i ? OMP_CLAUSE_FIRSTPRIVATE
2374 : OMP_CLAUSE_SHARED);
2375 OMP_CLAUSE_DECL (c) = decl;
2376 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2377 gimple_omp_taskreg_set_clauses (stmt, c);
2380 info->static_chain_added |= save_static_chain_added;
2381 break;
2383 case GIMPLE_OMP_TARGET:
2384 if (gimple_omp_target_kind (stmt) != GF_OMP_TARGET_KIND_REGION)
2386 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2387 break;
2389 save_static_chain_added = info->static_chain_added;
2390 info->static_chain_added = 0;
2391 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2392 for (i = 0; i < 2; i++)
2394 tree c, decl;
2395 if ((info->static_chain_added & (1 << i)) == 0)
2396 continue;
2397 decl = i ? get_chain_decl (info) : info->frame_decl;
2398 /* Don't add CHAIN.* or FRAME.* twice. */
2399 for (c = gimple_omp_target_clauses (stmt);
2401 c = OMP_CLAUSE_CHAIN (c))
2402 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2403 && OMP_CLAUSE_DECL (c) == decl)
2404 break;
2405 if (c == NULL)
2407 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2408 OMP_CLAUSE_DECL (c) = decl;
2409 OMP_CLAUSE_MAP_KIND (c)
2410 = i ? OMP_CLAUSE_MAP_TO : OMP_CLAUSE_MAP_TOFROM;
2411 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2412 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2413 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2417 info->static_chain_added |= save_static_chain_added;
2418 break;
2420 case GIMPLE_OMP_FOR:
2421 walk_body (convert_gimple_call, NULL, info,
2422 gimple_omp_for_pre_body_ptr (stmt));
2423 /* FALLTHRU */
2424 case GIMPLE_OMP_SECTIONS:
2425 case GIMPLE_OMP_SECTION:
2426 case GIMPLE_OMP_SINGLE:
2427 case GIMPLE_OMP_TEAMS:
2428 case GIMPLE_OMP_MASTER:
2429 case GIMPLE_OMP_TASKGROUP:
2430 case GIMPLE_OMP_ORDERED:
2431 case GIMPLE_OMP_CRITICAL:
2432 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2433 break;
2435 default:
2436 /* Keep looking for other operands. */
2437 *handled_ops_p = false;
2438 return NULL_TREE;
2441 *handled_ops_p = true;
2442 return NULL_TREE;
2445 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2446 call expressions. At the same time, determine if a nested function
2447 actually uses its static chain; if not, remember that. */
2449 static void
2450 convert_all_function_calls (struct nesting_info *root)
2452 unsigned int chain_count = 0, old_chain_count, iter_count;
2453 struct nesting_info *n;
2455 /* First, optimistically clear static_chain for all decls that haven't
2456 used the static chain already for variable access. But always create
2457 it if not optimizing. This makes it possible to reconstruct the static
2458 nesting tree at run time and thus to resolve up-level references from
2459 within the debugger. */
2460 FOR_EACH_NEST_INFO (n, root)
2462 tree decl = n->context;
2463 if (!optimize)
2465 if (n->inner)
2466 (void) get_frame_type (n);
2467 if (n->outer)
2468 (void) get_chain_decl (n);
2470 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2472 DECL_STATIC_CHAIN (decl) = 0;
2473 if (dump_file && (dump_flags & TDF_DETAILS))
2474 fprintf (dump_file, "Guessing no static-chain for %s\n",
2475 lang_hooks.decl_printable_name (decl, 2));
2477 else
2478 DECL_STATIC_CHAIN (decl) = 1;
2479 chain_count += DECL_STATIC_CHAIN (decl);
2482 /* Walk the functions and perform transformations. Note that these
2483 transformations can induce new uses of the static chain, which in turn
2484 require re-examining all users of the decl. */
2485 /* ??? It would make sense to try to use the call graph to speed this up,
2486 but the call graph hasn't really been built yet. Even if it did, we
2487 would still need to iterate in this loop since address-of references
2488 wouldn't show up in the callgraph anyway. */
2489 iter_count = 0;
2492 old_chain_count = chain_count;
2493 chain_count = 0;
2494 iter_count++;
2496 if (dump_file && (dump_flags & TDF_DETAILS))
2497 fputc ('\n', dump_file);
2499 FOR_EACH_NEST_INFO (n, root)
2501 tree decl = n->context;
2502 walk_function (convert_tramp_reference_stmt,
2503 convert_tramp_reference_op, n);
2504 walk_function (convert_gimple_call, NULL, n);
2505 chain_count += DECL_STATIC_CHAIN (decl);
2508 while (chain_count != old_chain_count);
2510 if (dump_file && (dump_flags & TDF_DETAILS))
2511 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2512 iter_count);
2515 struct nesting_copy_body_data
2517 copy_body_data cb;
2518 struct nesting_info *root;
2521 /* A helper subroutine for debug_var_chain type remapping. */
2523 static tree
2524 nesting_copy_decl (tree decl, copy_body_data *id)
2526 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2527 tree *slot = nid->root->var_map->get (decl);
2529 if (slot)
2530 return (tree) *slot;
2532 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2534 tree new_decl = copy_decl_no_change (decl, id);
2535 DECL_ORIGINAL_TYPE (new_decl)
2536 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2537 return new_decl;
2540 if (TREE_CODE (decl) == VAR_DECL
2541 || TREE_CODE (decl) == PARM_DECL
2542 || TREE_CODE (decl) == RESULT_DECL)
2543 return decl;
2545 return copy_decl_no_change (decl, id);
2548 /* A helper function for remap_vla_decls. See if *TP contains
2549 some remapped variables. */
2551 static tree
2552 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2554 struct nesting_info *root = (struct nesting_info *) data;
2555 tree t = *tp;
2557 if (DECL_P (t))
2559 *walk_subtrees = 0;
2560 tree *slot = root->var_map->get (t);
2562 if (slot)
2563 return *slot;
2565 return NULL;
2568 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2569 involved. */
2571 static void
2572 remap_vla_decls (tree block, struct nesting_info *root)
2574 tree var, subblock, val, type;
2575 struct nesting_copy_body_data id;
2577 for (subblock = BLOCK_SUBBLOCKS (block);
2578 subblock;
2579 subblock = BLOCK_CHAIN (subblock))
2580 remap_vla_decls (subblock, root);
2582 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2583 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2585 val = DECL_VALUE_EXPR (var);
2586 type = TREE_TYPE (var);
2588 if (!(TREE_CODE (val) == INDIRECT_REF
2589 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2590 && variably_modified_type_p (type, NULL)))
2591 continue;
2593 if (root->var_map->get (TREE_OPERAND (val, 0))
2594 || walk_tree (&type, contains_remapped_vars, root, NULL))
2595 break;
2598 if (var == NULL_TREE)
2599 return;
2601 memset (&id, 0, sizeof (id));
2602 id.cb.copy_decl = nesting_copy_decl;
2603 id.cb.decl_map = new hash_map<tree, tree>;
2604 id.root = root;
2606 for (; var; var = DECL_CHAIN (var))
2607 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2609 struct nesting_info *i;
2610 tree newt, context;
2612 val = DECL_VALUE_EXPR (var);
2613 type = TREE_TYPE (var);
2615 if (!(TREE_CODE (val) == INDIRECT_REF
2616 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2617 && variably_modified_type_p (type, NULL)))
2618 continue;
2620 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2621 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2622 continue;
2624 context = decl_function_context (var);
2625 for (i = root; i; i = i->outer)
2626 if (i->context == context)
2627 break;
2629 if (i == NULL)
2630 continue;
2632 /* Fully expand value expressions. This avoids having debug variables
2633 only referenced from them and that can be swept during GC. */
2634 if (slot)
2636 tree t = (tree) *slot;
2637 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2638 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2641 id.cb.src_fn = i->context;
2642 id.cb.dst_fn = i->context;
2643 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2645 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2646 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2648 newt = TREE_TYPE (newt);
2649 type = TREE_TYPE (type);
2651 if (TYPE_NAME (newt)
2652 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2653 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2654 && newt != type
2655 && TYPE_NAME (newt) == TYPE_NAME (type))
2656 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2658 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2659 if (val != DECL_VALUE_EXPR (var))
2660 SET_DECL_VALUE_EXPR (var, val);
2663 delete id.cb.decl_map;
2666 /* Fold the MEM_REF *E. */
2667 bool
2668 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2670 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2671 *ref_p = fold (*ref_p);
2672 return true;
2675 /* Do "everything else" to clean up or complete state collected by the
2676 various walking passes -- lay out the types and decls, generate code
2677 to initialize the frame decl, store critical expressions in the
2678 struct function for rtl to find. */
2680 static void
2681 finalize_nesting_tree_1 (struct nesting_info *root)
2683 gimple_seq stmt_list;
2684 gimple stmt;
2685 tree context = root->context;
2686 struct function *sf;
2688 stmt_list = NULL;
2690 /* If we created a non-local frame type or decl, we need to lay them
2691 out at this time. */
2692 if (root->frame_type)
2694 /* In some cases the frame type will trigger the -Wpadded warning.
2695 This is not helpful; suppress it. */
2696 int save_warn_padded = warn_padded;
2697 tree *adjust;
2699 warn_padded = 0;
2700 layout_type (root->frame_type);
2701 warn_padded = save_warn_padded;
2702 layout_decl (root->frame_decl, 0);
2704 /* Remove root->frame_decl from root->new_local_var_chain, so
2705 that we can declare it also in the lexical blocks, which
2706 helps ensure virtual regs that end up appearing in its RTL
2707 expression get substituted in instantiate_virtual_regs(). */
2708 for (adjust = &root->new_local_var_chain;
2709 *adjust != root->frame_decl;
2710 adjust = &DECL_CHAIN (*adjust))
2711 gcc_assert (DECL_CHAIN (*adjust));
2712 *adjust = DECL_CHAIN (*adjust);
2714 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2715 declare_vars (root->frame_decl,
2716 gimple_seq_first_stmt (gimple_body (context)), true);
2719 /* If any parameters were referenced non-locally, then we need to
2720 insert a copy. Likewise, if any variables were referenced by
2721 pointer, we need to initialize the address. */
2722 if (root->any_parm_remapped)
2724 tree p;
2725 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2727 tree field, x, y;
2729 field = lookup_field_for_decl (root, p, NO_INSERT);
2730 if (!field)
2731 continue;
2733 if (use_pointer_in_frame (p))
2734 x = build_addr (p, context);
2735 else
2736 x = p;
2738 /* If the assignment is from a non-register the stmt is
2739 not valid gimple. Make it so by using a temporary instead. */
2740 if (!is_gimple_reg (x)
2741 && is_gimple_reg_type (TREE_TYPE (x)))
2743 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2744 x = init_tmp_var (root, x, &gsi);
2747 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2748 root->frame_decl, field, NULL_TREE);
2749 stmt = gimple_build_assign (y, x);
2750 gimple_seq_add_stmt (&stmt_list, stmt);
2754 /* If a chain_field was created, then it needs to be initialized
2755 from chain_decl. */
2756 if (root->chain_field)
2758 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2759 root->frame_decl, root->chain_field, NULL_TREE);
2760 stmt = gimple_build_assign (x, get_chain_decl (root));
2761 gimple_seq_add_stmt (&stmt_list, stmt);
2764 /* If trampolines were created, then we need to initialize them. */
2765 if (root->any_tramp_created)
2767 struct nesting_info *i;
2768 for (i = root->inner; i ; i = i->next)
2770 tree arg1, arg2, arg3, x, field;
2772 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2773 if (!field)
2774 continue;
2776 gcc_assert (DECL_STATIC_CHAIN (i->context));
2777 arg3 = build_addr (root->frame_decl, context);
2779 arg2 = build_addr (i->context, context);
2781 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2782 root->frame_decl, field, NULL_TREE);
2783 arg1 = build_addr (x, context);
2785 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2786 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2787 gimple_seq_add_stmt (&stmt_list, stmt);
2791 /* If we created initialization statements, insert them. */
2792 if (stmt_list)
2794 gbind *bind;
2795 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2796 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2797 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2798 gimple_bind_set_body (bind, stmt_list);
2801 /* If a chain_decl was created, then it needs to be registered with
2802 struct function so that it gets initialized from the static chain
2803 register at the beginning of the function. */
2804 sf = DECL_STRUCT_FUNCTION (root->context);
2805 sf->static_chain_decl = root->chain_decl;
2807 /* Similarly for the non-local goto save area. */
2808 if (root->nl_goto_field)
2810 sf->nonlocal_goto_save_area
2811 = get_frame_field (root, context, root->nl_goto_field, NULL);
2812 sf->has_nonlocal_label = 1;
2815 /* Make sure all new local variables get inserted into the
2816 proper BIND_EXPR. */
2817 if (root->new_local_var_chain)
2818 declare_vars (root->new_local_var_chain,
2819 gimple_seq_first_stmt (gimple_body (root->context)),
2820 false);
2822 if (root->debug_var_chain)
2824 tree debug_var;
2825 gbind *scope;
2827 remap_vla_decls (DECL_INITIAL (root->context), root);
2829 for (debug_var = root->debug_var_chain; debug_var;
2830 debug_var = DECL_CHAIN (debug_var))
2831 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2832 break;
2834 /* If there are any debug decls with variable length types,
2835 remap those types using other debug_var_chain variables. */
2836 if (debug_var)
2838 struct nesting_copy_body_data id;
2840 memset (&id, 0, sizeof (id));
2841 id.cb.copy_decl = nesting_copy_decl;
2842 id.cb.decl_map = new hash_map<tree, tree>;
2843 id.root = root;
2845 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2846 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2848 tree type = TREE_TYPE (debug_var);
2849 tree newt, t = type;
2850 struct nesting_info *i;
2852 for (i = root; i; i = i->outer)
2853 if (variably_modified_type_p (type, i->context))
2854 break;
2856 if (i == NULL)
2857 continue;
2859 id.cb.src_fn = i->context;
2860 id.cb.dst_fn = i->context;
2861 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2863 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2864 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2866 newt = TREE_TYPE (newt);
2867 t = TREE_TYPE (t);
2869 if (TYPE_NAME (newt)
2870 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2871 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2872 && newt != t
2873 && TYPE_NAME (newt) == TYPE_NAME (t))
2874 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2877 delete id.cb.decl_map;
2880 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
2881 if (gimple_bind_block (scope))
2882 declare_vars (root->debug_var_chain, scope, true);
2883 else
2884 BLOCK_VARS (DECL_INITIAL (root->context))
2885 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2886 root->debug_var_chain);
2889 /* Fold the rewritten MEM_REF trees. */
2890 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2892 /* Dump the translated tree function. */
2893 if (dump_file)
2895 fputs ("\n\n", dump_file);
2896 dump_function_to_file (root->context, dump_file, dump_flags);
2900 static void
2901 finalize_nesting_tree (struct nesting_info *root)
2903 struct nesting_info *n;
2904 FOR_EACH_NEST_INFO (n, root)
2905 finalize_nesting_tree_1 (n);
2908 /* Unnest the nodes and pass them to cgraph. */
2910 static void
2911 unnest_nesting_tree_1 (struct nesting_info *root)
2913 struct cgraph_node *node = cgraph_node::get (root->context);
2915 /* For nested functions update the cgraph to reflect unnesting.
2916 We also delay finalizing of these functions up to this point. */
2917 if (node->origin)
2919 node->unnest ();
2920 cgraph_node::finalize_function (root->context, true);
2924 static void
2925 unnest_nesting_tree (struct nesting_info *root)
2927 struct nesting_info *n;
2928 FOR_EACH_NEST_INFO (n, root)
2929 unnest_nesting_tree_1 (n);
2932 /* Free the data structures allocated during this pass. */
2934 static void
2935 free_nesting_tree (struct nesting_info *root)
2937 struct nesting_info *node, *next;
2939 node = iter_nestinfo_start (root);
2942 next = iter_nestinfo_next (node);
2943 delete node->var_map;
2944 delete node->field_map;
2945 delete node->mem_refs;
2946 free (node);
2947 node = next;
2949 while (node);
2952 /* Gimplify a function and all its nested functions. */
2953 static void
2954 gimplify_all_functions (struct cgraph_node *root)
2956 struct cgraph_node *iter;
2957 if (!gimple_body (root->decl))
2958 gimplify_function_tree (root->decl);
2959 for (iter = root->nested; iter; iter = iter->next_nested)
2960 gimplify_all_functions (iter);
2963 /* Main entry point for this pass. Process FNDECL and all of its nested
2964 subroutines and turn them into something less tightly bound. */
2966 void
2967 lower_nested_functions (tree fndecl)
2969 struct cgraph_node *cgn;
2970 struct nesting_info *root;
2972 /* If there are no nested functions, there's nothing to do. */
2973 cgn = cgraph_node::get (fndecl);
2974 if (!cgn->nested)
2975 return;
2977 gimplify_all_functions (cgn);
2979 dump_file = dump_begin (TDI_nested, &dump_flags);
2980 if (dump_file)
2981 fprintf (dump_file, "\n;; Function %s\n\n",
2982 lang_hooks.decl_printable_name (fndecl, 2));
2984 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2985 root = create_nesting_tree (cgn);
2987 walk_all_functions (convert_nonlocal_reference_stmt,
2988 convert_nonlocal_reference_op,
2989 root);
2990 walk_all_functions (convert_local_reference_stmt,
2991 convert_local_reference_op,
2992 root);
2993 walk_all_functions (convert_nl_goto_reference, NULL, root);
2994 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2996 convert_all_function_calls (root);
2997 finalize_nesting_tree (root);
2998 unnest_nesting_tree (root);
3000 free_nesting_tree (root);
3001 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3003 if (dump_file)
3005 dump_end (TDI_nested, dump_file);
3006 dump_file = NULL;
3010 #include "gt-tree-nested.h"