* include/bits/alloc_traits.h (__alloctr_rebind): Remove.
[official-gcc.git] / gcc / tree-nested.c
blob6b9d753ee52d8ed374a4c46c6f406b0f81aee738
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 "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stringpool.h"
29 #include "stor-layout.h"
30 #include "tm_p.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "tree-dump.h"
34 #include "tree-inline.h"
35 #include "predict.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "gimplify.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-iterator.h"
45 #include "bitmap.h"
46 #include "cgraph.h"
47 #include "tree-cfg.h"
48 #include "rtl.h"
49 #include "flags.h"
50 #include "insn-config.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "varasm.h"
57 #include "stmt.h"
58 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
59 #include "langhooks.h"
60 #include "gimple-low.h"
61 #include "gomp-constants.h"
64 /* The object of this pass is to lower the representation of a set of nested
65 functions in order to expose all of the gory details of the various
66 nonlocal references. We want to do this sooner rather than later, in
67 order to give us more freedom in emitting all of the functions in question.
69 Back in olden times, when gcc was young, we developed an insanely
70 complicated scheme whereby variables which were referenced nonlocally
71 were forced to live in the stack of the declaring function, and then
72 the nested functions magically discovered where these variables were
73 placed. In order for this scheme to function properly, it required
74 that the outer function be partially expanded, then we switch to
75 compiling the inner function, and once done with those we switch back
76 to compiling the outer function. Such delicate ordering requirements
77 makes it difficult to do whole translation unit optimizations
78 involving such functions.
80 The implementation here is much more direct. Everything that can be
81 referenced by an inner function is a member of an explicitly created
82 structure herein called the "nonlocal frame struct". The incoming
83 static chain for a nested function is a pointer to this struct in
84 the parent. In this way, we settle on known offsets from a known
85 base, and so are decoupled from the logic that places objects in the
86 function's stack frame. More importantly, we don't have to wait for
87 that to happen -- since the compilation of the inner function is no
88 longer tied to a real stack frame, the nonlocal frame struct can be
89 allocated anywhere. Which means that the outer function is now
90 inlinable.
92 Theory of operation here is very simple. Iterate over all the
93 statements in all the functions (depth first) several times,
94 allocating structures and fields on demand. In general we want to
95 examine inner functions first, so that we can avoid making changes
96 to outer functions which are unnecessary.
98 The order of the passes matters a bit, in that later passes will be
99 skipped if it is discovered that the functions don't actually interact
100 at all. That is, they're nested in the lexical sense but could have
101 been written as independent functions without change. */
104 struct nesting_info
106 struct nesting_info *outer;
107 struct nesting_info *inner;
108 struct nesting_info *next;
110 hash_map<tree, tree> *field_map;
111 hash_map<tree, tree> *var_map;
112 hash_set<tree *> *mem_refs;
113 bitmap suppress_expansion;
115 tree context;
116 tree new_local_var_chain;
117 tree debug_var_chain;
118 tree frame_type;
119 tree frame_decl;
120 tree chain_field;
121 tree chain_decl;
122 tree nl_goto_field;
124 bool any_parm_remapped;
125 bool any_tramp_created;
126 char static_chain_added;
130 /* Iterate over the nesting tree, starting with ROOT, depth first. */
132 static inline struct nesting_info *
133 iter_nestinfo_start (struct nesting_info *root)
135 while (root->inner)
136 root = root->inner;
137 return root;
140 static inline struct nesting_info *
141 iter_nestinfo_next (struct nesting_info *node)
143 if (node->next)
144 return iter_nestinfo_start (node->next);
145 return node->outer;
148 #define FOR_EACH_NEST_INFO(I, ROOT) \
149 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
151 /* Obstack used for the bitmaps in the struct above. */
152 static struct bitmap_obstack nesting_info_bitmap_obstack;
155 /* We're working in so many different function contexts simultaneously,
156 that create_tmp_var is dangerous. Prevent mishap. */
157 #define create_tmp_var cant_use_create_tmp_var_here_dummy
159 /* Like create_tmp_var, except record the variable for registration at
160 the given nesting level. */
162 static tree
163 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
165 tree tmp_var;
167 /* If the type is of variable size or a type which must be created by the
168 frontend, something is wrong. Note that we explicitly allow
169 incomplete types here, since we create them ourselves here. */
170 gcc_assert (!TREE_ADDRESSABLE (type));
171 gcc_assert (!TYPE_SIZE_UNIT (type)
172 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
174 tmp_var = create_tmp_var_raw (type, prefix);
175 DECL_CONTEXT (tmp_var) = info->context;
176 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
177 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
178 if (TREE_CODE (type) == COMPLEX_TYPE
179 || TREE_CODE (type) == VECTOR_TYPE)
180 DECL_GIMPLE_REG_P (tmp_var) = 1;
182 info->new_local_var_chain = tmp_var;
184 return tmp_var;
187 /* Take the address of EXP to be used within function CONTEXT.
188 Mark it for addressability as necessary. */
190 tree
191 build_addr (tree exp, tree context)
193 tree base = exp;
194 tree save_context;
195 tree retval;
197 while (handled_component_p (base))
198 base = TREE_OPERAND (base, 0);
200 if (DECL_P (base))
201 TREE_ADDRESSABLE (base) = 1;
203 /* Building the ADDR_EXPR will compute a set of properties for
204 that ADDR_EXPR. Those properties are unfortunately context
205 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
207 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
208 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
209 way the properties are for the ADDR_EXPR are computed properly. */
210 save_context = current_function_decl;
211 current_function_decl = context;
212 retval = build_fold_addr_expr (exp);
213 current_function_decl = save_context;
214 return retval;
217 /* Insert FIELD into TYPE, sorted by alignment requirements. */
219 void
220 insert_field_into_struct (tree type, tree field)
222 tree *p;
224 DECL_CONTEXT (field) = type;
226 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
227 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
228 break;
230 DECL_CHAIN (field) = *p;
231 *p = field;
233 /* Set correct alignment for frame struct type. */
234 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
235 TYPE_ALIGN (type) = DECL_ALIGN (field);
238 /* Build or return the RECORD_TYPE that describes the frame state that is
239 shared between INFO->CONTEXT and its nested functions. This record will
240 not be complete until finalize_nesting_tree; up until that point we'll
241 be adding fields as necessary.
243 We also build the DECL that represents this frame in the function. */
245 static tree
246 get_frame_type (struct nesting_info *info)
248 tree type = info->frame_type;
249 if (!type)
251 char *name;
253 type = make_node (RECORD_TYPE);
255 name = concat ("FRAME.",
256 IDENTIFIER_POINTER (DECL_NAME (info->context)),
257 NULL);
258 TYPE_NAME (type) = get_identifier (name);
259 free (name);
261 info->frame_type = type;
262 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
263 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
265 /* ??? Always make it addressable for now, since it is meant to
266 be pointed to by the static chain pointer. This pessimizes
267 when it turns out that no static chains are needed because
268 the nested functions referencing non-local variables are not
269 reachable, but the true pessimization is to create the non-
270 local frame structure in the first place. */
271 TREE_ADDRESSABLE (info->frame_decl) = 1;
273 return type;
276 /* Return true if DECL should be referenced by pointer in the non-local
277 frame structure. */
279 static bool
280 use_pointer_in_frame (tree decl)
282 if (TREE_CODE (decl) == PARM_DECL)
284 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
285 sized decls, and inefficient to copy large aggregates. Don't bother
286 moving anything but scalar variables. */
287 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
289 else
291 /* Variable sized types make things "interesting" in the frame. */
292 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
296 /* Given DECL, a non-locally accessed variable, find or create a field
297 in the non-local frame structure for the given nesting context. */
299 static tree
300 lookup_field_for_decl (struct nesting_info *info, tree decl,
301 enum insert_option insert)
303 if (insert == NO_INSERT)
305 tree *slot = info->field_map->get (decl);
306 return slot ? *slot : NULL_TREE;
309 tree *slot = &info->field_map->get_or_insert (decl);
310 if (!*slot)
312 tree field = make_node (FIELD_DECL);
313 DECL_NAME (field) = DECL_NAME (decl);
315 if (use_pointer_in_frame (decl))
317 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
318 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
319 DECL_NONADDRESSABLE_P (field) = 1;
321 else
323 TREE_TYPE (field) = TREE_TYPE (decl);
324 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
325 DECL_ALIGN (field) = DECL_ALIGN (decl);
326 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
327 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
328 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
329 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
332 insert_field_into_struct (get_frame_type (info), field);
333 *slot = field;
335 if (TREE_CODE (decl) == PARM_DECL)
336 info->any_parm_remapped = true;
339 return *slot;
342 /* Build or return the variable that holds the static chain within
343 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
345 static tree
346 get_chain_decl (struct nesting_info *info)
348 tree decl = info->chain_decl;
350 if (!decl)
352 tree type;
354 type = get_frame_type (info->outer);
355 type = build_pointer_type (type);
357 /* Note that this variable is *not* entered into any BIND_EXPR;
358 the construction of this variable is handled specially in
359 expand_function_start and initialize_inlined_parameters.
360 Note also that it's represented as a parameter. This is more
361 close to the truth, since the initial value does come from
362 the caller. */
363 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
364 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
365 DECL_ARTIFICIAL (decl) = 1;
366 DECL_IGNORED_P (decl) = 1;
367 TREE_USED (decl) = 1;
368 DECL_CONTEXT (decl) = info->context;
369 DECL_ARG_TYPE (decl) = type;
371 /* Tell tree-inline.c that we never write to this variable, so
372 it can copy-prop the replacement value immediately. */
373 TREE_READONLY (decl) = 1;
375 info->chain_decl = decl;
377 if (dump_file
378 && (dump_flags & TDF_DETAILS)
379 && !DECL_STATIC_CHAIN (info->context))
380 fprintf (dump_file, "Setting static-chain for %s\n",
381 lang_hooks.decl_printable_name (info->context, 2));
383 DECL_STATIC_CHAIN (info->context) = 1;
385 return decl;
388 /* Build or return the field within the non-local frame state that holds
389 the static chain for INFO->CONTEXT. This is the way to walk back up
390 multiple nesting levels. */
392 static tree
393 get_chain_field (struct nesting_info *info)
395 tree field = info->chain_field;
397 if (!field)
399 tree type = build_pointer_type (get_frame_type (info->outer));
401 field = make_node (FIELD_DECL);
402 DECL_NAME (field) = get_identifier ("__chain");
403 TREE_TYPE (field) = type;
404 DECL_ALIGN (field) = TYPE_ALIGN (type);
405 DECL_NONADDRESSABLE_P (field) = 1;
407 insert_field_into_struct (get_frame_type (info), field);
409 info->chain_field = field;
411 if (dump_file
412 && (dump_flags & TDF_DETAILS)
413 && !DECL_STATIC_CHAIN (info->context))
414 fprintf (dump_file, "Setting static-chain for %s\n",
415 lang_hooks.decl_printable_name (info->context, 2));
417 DECL_STATIC_CHAIN (info->context) = 1;
419 return field;
422 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
424 static tree
425 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
426 gcall *call)
428 tree t;
430 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
431 gimple_call_set_lhs (call, t);
432 if (! gsi_end_p (*gsi))
433 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
434 gsi_insert_before (gsi, call, GSI_SAME_STMT);
436 return t;
440 /* Copy EXP into a temporary. Allocate the temporary in the context of
441 INFO and insert the initialization statement before GSI. */
443 static tree
444 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
446 tree t;
447 gimple stmt;
449 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
450 stmt = gimple_build_assign (t, exp);
451 if (! gsi_end_p (*gsi))
452 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
453 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
455 return t;
459 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
461 static tree
462 gsi_gimplify_val (struct nesting_info *info, tree exp,
463 gimple_stmt_iterator *gsi)
465 if (is_gimple_val (exp))
466 return exp;
467 else
468 return init_tmp_var (info, exp, gsi);
471 /* Similarly, but copy from the temporary and insert the statement
472 after the iterator. */
474 static tree
475 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
477 tree t;
478 gimple stmt;
480 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
481 stmt = gimple_build_assign (exp, t);
482 if (! gsi_end_p (*gsi))
483 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
484 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
486 return t;
489 /* Build or return the type used to represent a nested function trampoline. */
491 static GTY(()) tree trampoline_type;
493 static tree
494 get_trampoline_type (struct nesting_info *info)
496 unsigned align, size;
497 tree t;
499 if (trampoline_type)
500 return trampoline_type;
502 align = TRAMPOLINE_ALIGNMENT;
503 size = TRAMPOLINE_SIZE;
505 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
506 then allocate extra space so that we can do dynamic alignment. */
507 if (align > STACK_BOUNDARY)
509 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
510 align = STACK_BOUNDARY;
513 t = build_index_type (size_int (size - 1));
514 t = build_array_type (char_type_node, t);
515 t = build_decl (DECL_SOURCE_LOCATION (info->context),
516 FIELD_DECL, get_identifier ("__data"), t);
517 DECL_ALIGN (t) = align;
518 DECL_USER_ALIGN (t) = 1;
520 trampoline_type = make_node (RECORD_TYPE);
521 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
522 TYPE_FIELDS (trampoline_type) = t;
523 layout_type (trampoline_type);
524 DECL_CONTEXT (t) = trampoline_type;
526 return trampoline_type;
529 /* Given DECL, a nested function, find or create a field in the non-local
530 frame structure for a trampoline for this function. */
532 static tree
533 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
534 enum insert_option insert)
536 if (insert == NO_INSERT)
538 tree *slot = info->var_map->get (decl);
539 return slot ? *slot : NULL_TREE;
542 tree *slot = &info->var_map->get_or_insert (decl);
543 if (!*slot)
545 tree field = make_node (FIELD_DECL);
546 DECL_NAME (field) = DECL_NAME (decl);
547 TREE_TYPE (field) = get_trampoline_type (info);
548 TREE_ADDRESSABLE (field) = 1;
550 insert_field_into_struct (get_frame_type (info), field);
551 *slot = field;
553 info->any_tramp_created = true;
556 return *slot;
559 /* Build or return the field within the non-local frame state that holds
560 the non-local goto "jmp_buf". The buffer itself is maintained by the
561 rtl middle-end as dynamic stack space is allocated. */
563 static tree
564 get_nl_goto_field (struct nesting_info *info)
566 tree field = info->nl_goto_field;
567 if (!field)
569 unsigned size;
570 tree type;
572 /* For __builtin_nonlocal_goto, we need N words. The first is the
573 frame pointer, the rest is for the target's stack pointer save
574 area. The number of words is controlled by STACK_SAVEAREA_MODE;
575 not the best interface, but it'll do for now. */
576 if (Pmode == ptr_mode)
577 type = ptr_type_node;
578 else
579 type = lang_hooks.types.type_for_mode (Pmode, 1);
581 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
582 size = size / GET_MODE_SIZE (Pmode);
583 size = size + 1;
585 type = build_array_type
586 (type, build_index_type (size_int (size)));
588 field = make_node (FIELD_DECL);
589 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
590 TREE_TYPE (field) = type;
591 DECL_ALIGN (field) = TYPE_ALIGN (type);
592 TREE_ADDRESSABLE (field) = 1;
594 insert_field_into_struct (get_frame_type (info), field);
596 info->nl_goto_field = field;
599 return field;
602 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
604 static void
605 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
606 struct nesting_info *info, gimple_seq *pseq)
608 struct walk_stmt_info wi;
610 memset (&wi, 0, sizeof (wi));
611 wi.info = info;
612 wi.val_only = true;
613 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
617 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
619 static inline void
620 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
621 struct nesting_info *info)
623 gimple_seq body = gimple_body (info->context);
624 walk_body (callback_stmt, callback_op, info, &body);
625 gimple_set_body (info->context, body);
628 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
630 static void
631 walk_gimple_omp_for (gomp_for *for_stmt,
632 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
633 struct nesting_info *info)
635 struct walk_stmt_info wi;
636 gimple_seq seq;
637 tree t;
638 size_t i;
640 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
642 seq = NULL;
643 memset (&wi, 0, sizeof (wi));
644 wi.info = info;
645 wi.gsi = gsi_last (seq);
647 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
649 wi.val_only = false;
650 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
651 &wi, NULL);
652 wi.val_only = true;
653 wi.is_lhs = false;
654 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
655 &wi, NULL);
657 wi.val_only = true;
658 wi.is_lhs = false;
659 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
660 &wi, NULL);
662 t = gimple_omp_for_incr (for_stmt, i);
663 gcc_assert (BINARY_CLASS_P (t));
664 wi.val_only = false;
665 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
666 wi.val_only = true;
667 wi.is_lhs = false;
668 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
671 seq = gsi_seq (wi.gsi);
672 if (!gimple_seq_empty_p (seq))
674 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
675 annotate_all_with_location (seq, gimple_location (for_stmt));
676 gimple_seq_add_seq (&pre_body, seq);
677 gimple_omp_for_set_pre_body (for_stmt, pre_body);
681 /* Similarly for ROOT and all functions nested underneath, depth first. */
683 static void
684 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
685 struct nesting_info *root)
687 struct nesting_info *n;
688 FOR_EACH_NEST_INFO (n, root)
689 walk_function (callback_stmt, callback_op, n);
693 /* We have to check for a fairly pathological case. The operands of function
694 nested function are to be interpreted in the context of the enclosing
695 function. So if any are variably-sized, they will get remapped when the
696 enclosing function is inlined. But that remapping would also have to be
697 done in the types of the PARM_DECLs of the nested function, meaning the
698 argument types of that function will disagree with the arguments in the
699 calls to that function. So we'd either have to make a copy of the nested
700 function corresponding to each time the enclosing function was inlined or
701 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
702 function. The former is not practical. The latter would still require
703 detecting this case to know when to add the conversions. So, for now at
704 least, we don't inline such an enclosing function.
706 We have to do that check recursively, so here return indicating whether
707 FNDECL has such a nested function. ORIG_FN is the function we were
708 trying to inline to use for checking whether any argument is variably
709 modified by anything in it.
711 It would be better to do this in tree-inline.c so that we could give
712 the appropriate warning for why a function can't be inlined, but that's
713 too late since the nesting structure has already been flattened and
714 adding a flag just to record this fact seems a waste of a flag. */
716 static bool
717 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
719 struct cgraph_node *cgn = cgraph_node::get (fndecl);
720 tree arg;
722 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
724 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
725 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
726 return true;
728 if (check_for_nested_with_variably_modified (cgn->decl,
729 orig_fndecl))
730 return true;
733 return false;
736 /* Construct our local datastructure describing the function nesting
737 tree rooted by CGN. */
739 static struct nesting_info *
740 create_nesting_tree (struct cgraph_node *cgn)
742 struct nesting_info *info = XCNEW (struct nesting_info);
743 info->field_map = new hash_map<tree, tree>;
744 info->var_map = new hash_map<tree, tree>;
745 info->mem_refs = new hash_set<tree *>;
746 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
747 info->context = cgn->decl;
749 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
751 struct nesting_info *sub = create_nesting_tree (cgn);
752 sub->outer = info;
753 sub->next = info->inner;
754 info->inner = sub;
757 /* See discussion at check_for_nested_with_variably_modified for a
758 discussion of why this has to be here. */
759 if (check_for_nested_with_variably_modified (info->context, info->context))
760 DECL_UNINLINABLE (info->context) = true;
762 return info;
765 /* Return an expression computing the static chain for TARGET_CONTEXT
766 from INFO->CONTEXT. Insert any necessary computations before TSI. */
768 static tree
769 get_static_chain (struct nesting_info *info, tree target_context,
770 gimple_stmt_iterator *gsi)
772 struct nesting_info *i;
773 tree x;
775 if (info->context == target_context)
777 x = build_addr (info->frame_decl, target_context);
779 else
781 x = get_chain_decl (info);
783 for (i = info->outer; i->context != target_context; i = i->outer)
785 tree field = get_chain_field (i);
787 x = build_simple_mem_ref (x);
788 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
789 x = init_tmp_var (info, x, gsi);
793 return x;
797 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
798 frame as seen from INFO->CONTEXT. Insert any necessary computations
799 before GSI. */
801 static tree
802 get_frame_field (struct nesting_info *info, tree target_context,
803 tree field, gimple_stmt_iterator *gsi)
805 struct nesting_info *i;
806 tree x;
808 if (info->context == target_context)
810 /* Make sure frame_decl gets created. */
811 (void) get_frame_type (info);
812 x = info->frame_decl;
814 else
816 x = get_chain_decl (info);
818 for (i = info->outer; i->context != target_context; i = i->outer)
820 tree field = get_chain_field (i);
822 x = build_simple_mem_ref (x);
823 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
824 x = init_tmp_var (info, x, gsi);
827 x = build_simple_mem_ref (x);
830 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
831 return x;
834 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
836 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
837 in the nested function with DECL_VALUE_EXPR set to reference the true
838 variable in the parent function. This is used both for debug info
839 and in OMP lowering. */
841 static tree
842 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
844 tree target_context;
845 struct nesting_info *i;
846 tree x, field, new_decl;
848 tree *slot = &info->var_map->get_or_insert (decl);
850 if (*slot)
851 return *slot;
853 target_context = decl_function_context (decl);
855 /* A copy of the code in get_frame_field, but without the temporaries. */
856 if (info->context == target_context)
858 /* Make sure frame_decl gets created. */
859 (void) get_frame_type (info);
860 x = info->frame_decl;
861 i = info;
863 else
865 x = get_chain_decl (info);
866 for (i = info->outer; i->context != target_context; i = i->outer)
868 field = get_chain_field (i);
869 x = build_simple_mem_ref (x);
870 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
872 x = build_simple_mem_ref (x);
875 field = lookup_field_for_decl (i, decl, INSERT);
876 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
877 if (use_pointer_in_frame (decl))
878 x = build_simple_mem_ref (x);
880 /* ??? We should be remapping types as well, surely. */
881 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
882 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
883 DECL_CONTEXT (new_decl) = info->context;
884 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
885 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
886 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
887 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
888 TREE_READONLY (new_decl) = TREE_READONLY (decl);
889 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
890 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
891 if ((TREE_CODE (decl) == PARM_DECL
892 || TREE_CODE (decl) == RESULT_DECL
893 || TREE_CODE (decl) == VAR_DECL)
894 && DECL_BY_REFERENCE (decl))
895 DECL_BY_REFERENCE (new_decl) = 1;
897 SET_DECL_VALUE_EXPR (new_decl, x);
898 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
900 *slot = new_decl;
901 DECL_CHAIN (new_decl) = info->debug_var_chain;
902 info->debug_var_chain = new_decl;
904 if (!optimize
905 && info->context != target_context
906 && variably_modified_type_p (TREE_TYPE (decl), NULL))
907 note_nonlocal_vla_type (info, TREE_TYPE (decl));
909 return new_decl;
913 /* Callback for walk_gimple_stmt, rewrite all references to VAR
914 and PARM_DECLs that belong to outer functions.
916 The rewrite will involve some number of structure accesses back up
917 the static chain. E.g. for a variable FOO up one nesting level it'll
918 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
919 indirections apply to decls for which use_pointer_in_frame is true. */
921 static tree
922 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
924 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
925 struct nesting_info *const info = (struct nesting_info *) wi->info;
926 tree t = *tp;
928 *walk_subtrees = 0;
929 switch (TREE_CODE (t))
931 case VAR_DECL:
932 /* Non-automatic variables are never processed. */
933 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
934 break;
935 /* FALLTHRU */
937 case PARM_DECL:
938 if (decl_function_context (t) != info->context)
940 tree x;
941 wi->changed = true;
943 x = get_nonlocal_debug_decl (info, t);
944 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
946 tree target_context = decl_function_context (t);
947 struct nesting_info *i;
948 for (i = info->outer; i->context != target_context; i = i->outer)
949 continue;
950 x = lookup_field_for_decl (i, t, INSERT);
951 x = get_frame_field (info, target_context, x, &wi->gsi);
952 if (use_pointer_in_frame (t))
954 x = init_tmp_var (info, x, &wi->gsi);
955 x = build_simple_mem_ref (x);
959 if (wi->val_only)
961 if (wi->is_lhs)
962 x = save_tmp_var (info, x, &wi->gsi);
963 else
964 x = init_tmp_var (info, x, &wi->gsi);
967 *tp = x;
969 break;
971 case LABEL_DECL:
972 /* We're taking the address of a label from a parent function, but
973 this is not itself a non-local goto. Mark the label such that it
974 will not be deleted, much as we would with a label address in
975 static storage. */
976 if (decl_function_context (t) != info->context)
977 FORCED_LABEL (t) = 1;
978 break;
980 case ADDR_EXPR:
982 bool save_val_only = wi->val_only;
984 wi->val_only = false;
985 wi->is_lhs = false;
986 wi->changed = false;
987 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
988 wi->val_only = true;
990 if (wi->changed)
992 tree save_context;
994 /* If we changed anything, we might no longer be directly
995 referencing a decl. */
996 save_context = current_function_decl;
997 current_function_decl = info->context;
998 recompute_tree_invariant_for_addr_expr (t);
999 current_function_decl = save_context;
1001 /* If the callback converted the address argument in a context
1002 where we only accept variables (and min_invariant, presumably),
1003 then compute the address into a temporary. */
1004 if (save_val_only)
1005 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1006 t, &wi->gsi);
1009 break;
1011 case REALPART_EXPR:
1012 case IMAGPART_EXPR:
1013 case COMPONENT_REF:
1014 case ARRAY_REF:
1015 case ARRAY_RANGE_REF:
1016 case BIT_FIELD_REF:
1017 /* Go down this entire nest and just look at the final prefix and
1018 anything that describes the references. Otherwise, we lose track
1019 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1020 wi->val_only = true;
1021 wi->is_lhs = false;
1022 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1024 if (TREE_CODE (t) == COMPONENT_REF)
1025 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1026 NULL);
1027 else if (TREE_CODE (t) == ARRAY_REF
1028 || TREE_CODE (t) == ARRAY_RANGE_REF)
1030 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1031 wi, NULL);
1032 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1033 wi, NULL);
1034 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1035 wi, NULL);
1038 wi->val_only = false;
1039 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1040 break;
1042 case VIEW_CONVERT_EXPR:
1043 /* Just request to look at the subtrees, leaving val_only and lhs
1044 untouched. This might actually be for !val_only + lhs, in which
1045 case we don't want to force a replacement by a temporary. */
1046 *walk_subtrees = 1;
1047 break;
1049 default:
1050 if (!IS_TYPE_OR_DECL_P (t))
1052 *walk_subtrees = 1;
1053 wi->val_only = true;
1054 wi->is_lhs = false;
1056 break;
1059 return NULL_TREE;
1062 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1063 struct walk_stmt_info *);
1065 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1066 and PARM_DECLs that belong to outer functions. */
1068 static bool
1069 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1071 struct nesting_info *const info = (struct nesting_info *) wi->info;
1072 /* If not optimizing, we will force the creation of the CHAIN object in
1073 convert_all_function_calls, so we need to take it into account here. */
1074 bool need_chain = info->outer && !optimize, 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_SET_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 /* If not optimizing, we will force the creation of the FRAME object in
1703 convert_all_function_calls, so we need to take it into account here. */
1704 bool need_frame = info->inner && !optimize, need_stmts = false;
1705 tree clause, decl;
1706 int dummy;
1707 bitmap new_suppress;
1709 new_suppress = BITMAP_GGC_ALLOC ();
1710 bitmap_copy (new_suppress, info->suppress_expansion);
1712 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1714 switch (OMP_CLAUSE_CODE (clause))
1716 case OMP_CLAUSE_REDUCTION:
1717 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1718 need_stmts = true;
1719 goto do_decl_clause;
1721 case OMP_CLAUSE_LASTPRIVATE:
1722 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1723 need_stmts = true;
1724 goto do_decl_clause;
1726 case OMP_CLAUSE_LINEAR:
1727 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1728 need_stmts = true;
1729 wi->val_only = true;
1730 wi->is_lhs = false;
1731 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1732 wi);
1733 goto do_decl_clause;
1735 case OMP_CLAUSE_PRIVATE:
1736 case OMP_CLAUSE_FIRSTPRIVATE:
1737 case OMP_CLAUSE_COPYPRIVATE:
1738 case OMP_CLAUSE_SHARED:
1739 do_decl_clause:
1740 decl = OMP_CLAUSE_DECL (clause);
1741 if (TREE_CODE (decl) == VAR_DECL
1742 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1743 break;
1744 if (decl_function_context (decl) == info->context
1745 && !use_pointer_in_frame (decl))
1747 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1748 if (field)
1750 bitmap_set_bit (new_suppress, DECL_UID (decl));
1751 OMP_CLAUSE_DECL (clause)
1752 = get_local_debug_decl (info, decl, field);
1753 need_frame = true;
1756 break;
1758 case OMP_CLAUSE_SCHEDULE:
1759 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1760 break;
1761 /* FALLTHRU */
1762 case OMP_CLAUSE_FINAL:
1763 case OMP_CLAUSE_IF:
1764 case OMP_CLAUSE_NUM_THREADS:
1765 case OMP_CLAUSE_DEPEND:
1766 case OMP_CLAUSE_DEVICE:
1767 case OMP_CLAUSE_NUM_TEAMS:
1768 case OMP_CLAUSE_THREAD_LIMIT:
1769 case OMP_CLAUSE_SAFELEN:
1770 case OMP_CLAUSE__CILK_FOR_COUNT_:
1771 wi->val_only = true;
1772 wi->is_lhs = false;
1773 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1774 wi);
1775 break;
1777 case OMP_CLAUSE_DIST_SCHEDULE:
1778 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1780 wi->val_only = true;
1781 wi->is_lhs = false;
1782 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1783 &dummy, wi);
1785 break;
1787 case OMP_CLAUSE_MAP:
1788 case OMP_CLAUSE_TO:
1789 case OMP_CLAUSE_FROM:
1790 if (OMP_CLAUSE_SIZE (clause))
1792 wi->val_only = true;
1793 wi->is_lhs = false;
1794 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1795 &dummy, wi);
1797 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1798 goto do_decl_clause;
1799 wi->val_only = true;
1800 wi->is_lhs = false;
1801 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1802 wi, NULL);
1803 break;
1805 case OMP_CLAUSE_ALIGNED:
1806 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1808 wi->val_only = true;
1809 wi->is_lhs = false;
1810 convert_local_reference_op
1811 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1813 /* Like do_decl_clause, but don't add any suppression. */
1814 decl = OMP_CLAUSE_DECL (clause);
1815 if (TREE_CODE (decl) == VAR_DECL
1816 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1817 break;
1818 if (decl_function_context (decl) == info->context
1819 && !use_pointer_in_frame (decl))
1821 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1822 if (field)
1824 OMP_CLAUSE_DECL (clause)
1825 = get_local_debug_decl (info, decl, field);
1826 need_frame = true;
1829 break;
1831 case OMP_CLAUSE_NOWAIT:
1832 case OMP_CLAUSE_ORDERED:
1833 case OMP_CLAUSE_DEFAULT:
1834 case OMP_CLAUSE_COPYIN:
1835 case OMP_CLAUSE_COLLAPSE:
1836 case OMP_CLAUSE_UNTIED:
1837 case OMP_CLAUSE_MERGEABLE:
1838 case OMP_CLAUSE_PROC_BIND:
1839 break;
1841 default:
1842 gcc_unreachable ();
1846 info->suppress_expansion = new_suppress;
1848 if (need_stmts)
1849 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1850 switch (OMP_CLAUSE_CODE (clause))
1852 case OMP_CLAUSE_REDUCTION:
1853 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1855 tree old_context
1856 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1857 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1858 = info->context;
1859 walk_body (convert_local_reference_stmt,
1860 convert_local_reference_op, info,
1861 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1862 walk_body (convert_local_reference_stmt,
1863 convert_local_reference_op, info,
1864 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1865 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1866 = old_context;
1868 break;
1870 case OMP_CLAUSE_LASTPRIVATE:
1871 walk_body (convert_local_reference_stmt,
1872 convert_local_reference_op, info,
1873 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1874 break;
1876 case OMP_CLAUSE_LINEAR:
1877 walk_body (convert_local_reference_stmt,
1878 convert_local_reference_op, info,
1879 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1880 break;
1882 default:
1883 break;
1886 return need_frame;
1890 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1891 and PARM_DECLs that were referenced by inner nested functions.
1892 The rewrite will be a structure reference to the local frame variable. */
1894 static tree
1895 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1896 struct walk_stmt_info *wi)
1898 struct nesting_info *info = (struct nesting_info *) wi->info;
1899 tree save_local_var_chain;
1900 bitmap save_suppress;
1901 gimple stmt = gsi_stmt (*gsi);
1903 switch (gimple_code (stmt))
1905 case GIMPLE_OMP_PARALLEL:
1906 case GIMPLE_OMP_TASK:
1907 save_suppress = info->suppress_expansion;
1908 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1909 wi))
1911 tree c;
1912 (void) get_frame_type (info);
1913 c = build_omp_clause (gimple_location (stmt),
1914 OMP_CLAUSE_SHARED);
1915 OMP_CLAUSE_DECL (c) = info->frame_decl;
1916 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1917 gimple_omp_taskreg_set_clauses (stmt, c);
1920 save_local_var_chain = info->new_local_var_chain;
1921 info->new_local_var_chain = NULL;
1923 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1924 gimple_omp_body_ptr (stmt));
1926 if (info->new_local_var_chain)
1927 declare_vars (info->new_local_var_chain,
1928 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1929 info->new_local_var_chain = save_local_var_chain;
1930 info->suppress_expansion = save_suppress;
1931 break;
1933 case GIMPLE_OMP_FOR:
1934 save_suppress = info->suppress_expansion;
1935 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1936 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1937 convert_local_reference_stmt,
1938 convert_local_reference_op, info);
1939 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1940 info, gimple_omp_body_ptr (stmt));
1941 info->suppress_expansion = save_suppress;
1942 break;
1944 case GIMPLE_OMP_SECTIONS:
1945 save_suppress = info->suppress_expansion;
1946 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1947 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1948 info, gimple_omp_body_ptr (stmt));
1949 info->suppress_expansion = save_suppress;
1950 break;
1952 case GIMPLE_OMP_SINGLE:
1953 save_suppress = info->suppress_expansion;
1954 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1955 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1956 info, gimple_omp_body_ptr (stmt));
1957 info->suppress_expansion = save_suppress;
1958 break;
1960 case GIMPLE_OMP_TARGET:
1961 if (!is_gimple_omp_offloaded (stmt))
1963 save_suppress = info->suppress_expansion;
1964 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1965 info->suppress_expansion = save_suppress;
1966 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1967 info, gimple_omp_body_ptr (stmt));
1968 break;
1970 save_suppress = info->suppress_expansion;
1971 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1973 tree c;
1974 (void) get_frame_type (info);
1975 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1976 OMP_CLAUSE_DECL (c) = info->frame_decl;
1977 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
1978 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1979 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1980 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1983 save_local_var_chain = info->new_local_var_chain;
1984 info->new_local_var_chain = NULL;
1986 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1987 gimple_omp_body_ptr (stmt));
1989 if (info->new_local_var_chain)
1990 declare_vars (info->new_local_var_chain,
1991 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1992 info->new_local_var_chain = save_local_var_chain;
1993 info->suppress_expansion = save_suppress;
1994 break;
1996 case GIMPLE_OMP_TEAMS:
1997 save_suppress = info->suppress_expansion;
1998 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1999 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2000 info, gimple_omp_body_ptr (stmt));
2001 info->suppress_expansion = save_suppress;
2002 break;
2004 case GIMPLE_OMP_SECTION:
2005 case GIMPLE_OMP_MASTER:
2006 case GIMPLE_OMP_TASKGROUP:
2007 case GIMPLE_OMP_ORDERED:
2008 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2009 info, gimple_omp_body_ptr (stmt));
2010 break;
2012 case GIMPLE_COND:
2013 wi->val_only = true;
2014 wi->is_lhs = false;
2015 *handled_ops_p = false;
2016 return NULL_TREE;
2018 case GIMPLE_ASSIGN:
2019 if (gimple_clobber_p (stmt))
2021 tree lhs = gimple_assign_lhs (stmt);
2022 if (!use_pointer_in_frame (lhs)
2023 && lookup_field_for_decl (info, lhs, NO_INSERT))
2025 gsi_replace (gsi, gimple_build_nop (), true);
2026 break;
2029 *handled_ops_p = false;
2030 return NULL_TREE;
2032 case GIMPLE_BIND:
2033 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2034 var;
2035 var = DECL_CHAIN (var))
2036 if (TREE_CODE (var) == NAMELIST_DECL)
2038 /* Adjust decls mentioned in NAMELIST_DECL. */
2039 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2040 tree decl;
2041 unsigned int i;
2043 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2045 if (TREE_CODE (decl) == VAR_DECL
2046 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2047 continue;
2048 if (decl_function_context (decl) == info->context
2049 && !use_pointer_in_frame (decl))
2051 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2052 if (field)
2054 CONSTRUCTOR_ELT (decls, i)->value
2055 = get_local_debug_decl (info, decl, field);
2061 *handled_ops_p = false;
2062 return NULL_TREE;
2064 default:
2065 /* For every other statement that we are not interested in
2066 handling here, let the walker traverse the operands. */
2067 *handled_ops_p = false;
2068 return NULL_TREE;
2071 /* Indicate that we have handled all the operands ourselves. */
2072 *handled_ops_p = true;
2073 return NULL_TREE;
2077 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2078 that reference labels from outer functions. The rewrite will be a
2079 call to __builtin_nonlocal_goto. */
2081 static tree
2082 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2083 struct walk_stmt_info *wi)
2085 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2086 tree label, new_label, target_context, x, field;
2087 gcall *call;
2088 gimple stmt = gsi_stmt (*gsi);
2090 if (gimple_code (stmt) != GIMPLE_GOTO)
2092 *handled_ops_p = false;
2093 return NULL_TREE;
2096 label = gimple_goto_dest (stmt);
2097 if (TREE_CODE (label) != LABEL_DECL)
2099 *handled_ops_p = false;
2100 return NULL_TREE;
2103 target_context = decl_function_context (label);
2104 if (target_context == info->context)
2106 *handled_ops_p = false;
2107 return NULL_TREE;
2110 for (i = info->outer; target_context != i->context; i = i->outer)
2111 continue;
2113 /* The original user label may also be use for a normal goto, therefore
2114 we must create a new label that will actually receive the abnormal
2115 control transfer. This new label will be marked LABEL_NONLOCAL; this
2116 mark will trigger proper behavior in the cfg, as well as cause the
2117 (hairy target-specific) non-local goto receiver code to be generated
2118 when we expand rtl. Enter this association into var_map so that we
2119 can insert the new label into the IL during a second pass. */
2120 tree *slot = &i->var_map->get_or_insert (label);
2121 if (*slot == NULL)
2123 new_label = create_artificial_label (UNKNOWN_LOCATION);
2124 DECL_NONLOCAL (new_label) = 1;
2125 *slot = new_label;
2127 else
2128 new_label = *slot;
2130 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2131 field = get_nl_goto_field (i);
2132 x = get_frame_field (info, target_context, field, gsi);
2133 x = build_addr (x, target_context);
2134 x = gsi_gimplify_val (info, x, gsi);
2135 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2136 2, build_addr (new_label, target_context), x);
2137 gsi_replace (gsi, call, false);
2139 /* We have handled all of STMT's operands, no need to keep going. */
2140 *handled_ops_p = true;
2141 return NULL_TREE;
2145 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2146 are referenced via nonlocal goto from a nested function. The rewrite
2147 will involve installing a newly generated DECL_NONLOCAL label, and
2148 (potentially) a branch around the rtl gunk that is assumed to be
2149 attached to such a label. */
2151 static tree
2152 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2153 struct walk_stmt_info *wi)
2155 struct nesting_info *const info = (struct nesting_info *) wi->info;
2156 tree label, new_label;
2157 gimple_stmt_iterator tmp_gsi;
2158 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2160 if (!stmt)
2162 *handled_ops_p = false;
2163 return NULL_TREE;
2166 label = gimple_label_label (stmt);
2168 tree *slot = info->var_map->get (label);
2169 if (!slot)
2171 *handled_ops_p = false;
2172 return NULL_TREE;
2175 /* If there's any possibility that the previous statement falls through,
2176 then we must branch around the new non-local label. */
2177 tmp_gsi = wi->gsi;
2178 gsi_prev (&tmp_gsi);
2179 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2181 gimple stmt = gimple_build_goto (label);
2182 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2185 new_label = (tree) *slot;
2186 stmt = gimple_build_label (new_label);
2187 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2189 *handled_ops_p = true;
2190 return NULL_TREE;
2194 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2195 of nested functions that require the use of trampolines. The rewrite
2196 will involve a reference a trampoline generated for the occasion. */
2198 static tree
2199 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2201 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2202 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2203 tree t = *tp, decl, target_context, x, builtin;
2204 gcall *call;
2206 *walk_subtrees = 0;
2207 switch (TREE_CODE (t))
2209 case ADDR_EXPR:
2210 /* Build
2211 T.1 = &CHAIN->tramp;
2212 T.2 = __builtin_adjust_trampoline (T.1);
2213 T.3 = (func_type)T.2;
2216 decl = TREE_OPERAND (t, 0);
2217 if (TREE_CODE (decl) != FUNCTION_DECL)
2218 break;
2220 /* Only need to process nested functions. */
2221 target_context = decl_function_context (decl);
2222 if (!target_context)
2223 break;
2225 /* If the nested function doesn't use a static chain, then
2226 it doesn't need a trampoline. */
2227 if (!DECL_STATIC_CHAIN (decl))
2228 break;
2230 /* If we don't want a trampoline, then don't build one. */
2231 if (TREE_NO_TRAMPOLINE (t))
2232 break;
2234 /* Lookup the immediate parent of the callee, as that's where
2235 we need to insert the trampoline. */
2236 for (i = info; i->context != target_context; i = i->outer)
2237 continue;
2238 x = lookup_tramp_for_decl (i, decl, INSERT);
2240 /* Compute the address of the field holding the trampoline. */
2241 x = get_frame_field (info, target_context, x, &wi->gsi);
2242 x = build_addr (x, target_context);
2243 x = gsi_gimplify_val (info, x, &wi->gsi);
2245 /* Do machine-specific ugliness. Normally this will involve
2246 computing extra alignment, but it can really be anything. */
2247 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2248 call = gimple_build_call (builtin, 1, x);
2249 x = init_tmp_var_with_call (info, &wi->gsi, call);
2251 /* Cast back to the proper function type. */
2252 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2253 x = init_tmp_var (info, x, &wi->gsi);
2255 *tp = x;
2256 break;
2258 default:
2259 if (!IS_TYPE_OR_DECL_P (t))
2260 *walk_subtrees = 1;
2261 break;
2264 return NULL_TREE;
2268 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2269 to addresses of nested functions that require the use of
2270 trampolines. The rewrite will involve a reference a trampoline
2271 generated for the occasion. */
2273 static tree
2274 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2275 struct walk_stmt_info *wi)
2277 struct nesting_info *info = (struct nesting_info *) wi->info;
2278 gimple stmt = gsi_stmt (*gsi);
2280 switch (gimple_code (stmt))
2282 case GIMPLE_CALL:
2284 /* Only walk call arguments, lest we generate trampolines for
2285 direct calls. */
2286 unsigned long i, nargs = gimple_call_num_args (stmt);
2287 for (i = 0; i < nargs; i++)
2288 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2289 wi, NULL);
2290 break;
2293 case GIMPLE_OMP_TARGET:
2294 if (!is_gimple_omp_offloaded (stmt))
2296 *handled_ops_p = false;
2297 return NULL_TREE;
2299 /* FALLTHRU */
2300 case GIMPLE_OMP_PARALLEL:
2301 case GIMPLE_OMP_TASK:
2303 tree save_local_var_chain;
2304 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2305 save_local_var_chain = info->new_local_var_chain;
2306 info->new_local_var_chain = NULL;
2307 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2308 info, gimple_omp_body_ptr (stmt));
2309 if (info->new_local_var_chain)
2310 declare_vars (info->new_local_var_chain,
2311 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2312 false);
2313 info->new_local_var_chain = save_local_var_chain;
2315 break;
2317 default:
2318 *handled_ops_p = false;
2319 return NULL_TREE;
2322 *handled_ops_p = true;
2323 return NULL_TREE;
2328 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2329 that reference nested functions to make sure that the static chain
2330 is set up properly for the call. */
2332 static tree
2333 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2334 struct walk_stmt_info *wi)
2336 struct nesting_info *const info = (struct nesting_info *) wi->info;
2337 tree decl, target_context;
2338 char save_static_chain_added;
2339 int i;
2340 gimple stmt = gsi_stmt (*gsi);
2342 switch (gimple_code (stmt))
2344 case GIMPLE_CALL:
2345 if (gimple_call_chain (stmt))
2346 break;
2347 decl = gimple_call_fndecl (stmt);
2348 if (!decl)
2349 break;
2350 target_context = decl_function_context (decl);
2351 if (target_context && DECL_STATIC_CHAIN (decl))
2353 gimple_call_set_chain (as_a <gcall *> (stmt),
2354 get_static_chain (info, target_context,
2355 &wi->gsi));
2356 info->static_chain_added |= (1 << (info->context != target_context));
2358 break;
2360 case GIMPLE_OMP_PARALLEL:
2361 case GIMPLE_OMP_TASK:
2362 save_static_chain_added = info->static_chain_added;
2363 info->static_chain_added = 0;
2364 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2365 for (i = 0; i < 2; i++)
2367 tree c, decl;
2368 if ((info->static_chain_added & (1 << i)) == 0)
2369 continue;
2370 decl = i ? get_chain_decl (info) : info->frame_decl;
2371 /* Don't add CHAIN.* or FRAME.* twice. */
2372 for (c = gimple_omp_taskreg_clauses (stmt);
2374 c = OMP_CLAUSE_CHAIN (c))
2375 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2376 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2377 && OMP_CLAUSE_DECL (c) == decl)
2378 break;
2379 if (c == NULL)
2381 c = build_omp_clause (gimple_location (stmt),
2382 i ? OMP_CLAUSE_FIRSTPRIVATE
2383 : OMP_CLAUSE_SHARED);
2384 OMP_CLAUSE_DECL (c) = decl;
2385 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2386 gimple_omp_taskreg_set_clauses (stmt, c);
2389 info->static_chain_added |= save_static_chain_added;
2390 break;
2392 case GIMPLE_OMP_TARGET:
2393 if (!is_gimple_omp_offloaded (stmt))
2395 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2396 break;
2398 save_static_chain_added = info->static_chain_added;
2399 info->static_chain_added = 0;
2400 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2401 for (i = 0; i < 2; i++)
2403 tree c, decl;
2404 if ((info->static_chain_added & (1 << i)) == 0)
2405 continue;
2406 decl = i ? get_chain_decl (info) : info->frame_decl;
2407 /* Don't add CHAIN.* or FRAME.* twice. */
2408 for (c = gimple_omp_target_clauses (stmt);
2410 c = OMP_CLAUSE_CHAIN (c))
2411 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2412 && OMP_CLAUSE_DECL (c) == decl)
2413 break;
2414 if (c == NULL)
2416 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2417 OMP_CLAUSE_DECL (c) = decl;
2418 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2419 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2420 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2421 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2425 info->static_chain_added |= save_static_chain_added;
2426 break;
2428 case GIMPLE_OMP_FOR:
2429 walk_body (convert_gimple_call, NULL, info,
2430 gimple_omp_for_pre_body_ptr (stmt));
2431 /* FALLTHRU */
2432 case GIMPLE_OMP_SECTIONS:
2433 case GIMPLE_OMP_SECTION:
2434 case GIMPLE_OMP_SINGLE:
2435 case GIMPLE_OMP_TEAMS:
2436 case GIMPLE_OMP_MASTER:
2437 case GIMPLE_OMP_TASKGROUP:
2438 case GIMPLE_OMP_ORDERED:
2439 case GIMPLE_OMP_CRITICAL:
2440 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2441 break;
2443 default:
2444 /* Keep looking for other operands. */
2445 *handled_ops_p = false;
2446 return NULL_TREE;
2449 *handled_ops_p = true;
2450 return NULL_TREE;
2453 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2454 call expressions. At the same time, determine if a nested function
2455 actually uses its static chain; if not, remember that. */
2457 static void
2458 convert_all_function_calls (struct nesting_info *root)
2460 unsigned int chain_count = 0, old_chain_count, iter_count;
2461 struct nesting_info *n;
2463 /* First, optimistically clear static_chain for all decls that haven't
2464 used the static chain already for variable access. But always create
2465 it if not optimizing. This makes it possible to reconstruct the static
2466 nesting tree at run time and thus to resolve up-level references from
2467 within the debugger. */
2468 FOR_EACH_NEST_INFO (n, root)
2470 tree decl = n->context;
2471 if (!optimize)
2473 if (n->inner)
2474 (void) get_frame_type (n);
2475 if (n->outer)
2476 (void) get_chain_decl (n);
2478 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2480 DECL_STATIC_CHAIN (decl) = 0;
2481 if (dump_file && (dump_flags & TDF_DETAILS))
2482 fprintf (dump_file, "Guessing no static-chain for %s\n",
2483 lang_hooks.decl_printable_name (decl, 2));
2485 else
2486 DECL_STATIC_CHAIN (decl) = 1;
2487 chain_count += DECL_STATIC_CHAIN (decl);
2490 /* Walk the functions and perform transformations. Note that these
2491 transformations can induce new uses of the static chain, which in turn
2492 require re-examining all users of the decl. */
2493 /* ??? It would make sense to try to use the call graph to speed this up,
2494 but the call graph hasn't really been built yet. Even if it did, we
2495 would still need to iterate in this loop since address-of references
2496 wouldn't show up in the callgraph anyway. */
2497 iter_count = 0;
2500 old_chain_count = chain_count;
2501 chain_count = 0;
2502 iter_count++;
2504 if (dump_file && (dump_flags & TDF_DETAILS))
2505 fputc ('\n', dump_file);
2507 FOR_EACH_NEST_INFO (n, root)
2509 tree decl = n->context;
2510 walk_function (convert_tramp_reference_stmt,
2511 convert_tramp_reference_op, n);
2512 walk_function (convert_gimple_call, NULL, n);
2513 chain_count += DECL_STATIC_CHAIN (decl);
2516 while (chain_count != old_chain_count);
2518 if (dump_file && (dump_flags & TDF_DETAILS))
2519 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2520 iter_count);
2523 struct nesting_copy_body_data
2525 copy_body_data cb;
2526 struct nesting_info *root;
2529 /* A helper subroutine for debug_var_chain type remapping. */
2531 static tree
2532 nesting_copy_decl (tree decl, copy_body_data *id)
2534 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2535 tree *slot = nid->root->var_map->get (decl);
2537 if (slot)
2538 return (tree) *slot;
2540 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2542 tree new_decl = copy_decl_no_change (decl, id);
2543 DECL_ORIGINAL_TYPE (new_decl)
2544 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2545 return new_decl;
2548 if (TREE_CODE (decl) == VAR_DECL
2549 || TREE_CODE (decl) == PARM_DECL
2550 || TREE_CODE (decl) == RESULT_DECL)
2551 return decl;
2553 return copy_decl_no_change (decl, id);
2556 /* A helper function for remap_vla_decls. See if *TP contains
2557 some remapped variables. */
2559 static tree
2560 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2562 struct nesting_info *root = (struct nesting_info *) data;
2563 tree t = *tp;
2565 if (DECL_P (t))
2567 *walk_subtrees = 0;
2568 tree *slot = root->var_map->get (t);
2570 if (slot)
2571 return *slot;
2573 return NULL;
2576 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2577 involved. */
2579 static void
2580 remap_vla_decls (tree block, struct nesting_info *root)
2582 tree var, subblock, val, type;
2583 struct nesting_copy_body_data id;
2585 for (subblock = BLOCK_SUBBLOCKS (block);
2586 subblock;
2587 subblock = BLOCK_CHAIN (subblock))
2588 remap_vla_decls (subblock, root);
2590 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2591 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2593 val = DECL_VALUE_EXPR (var);
2594 type = TREE_TYPE (var);
2596 if (!(TREE_CODE (val) == INDIRECT_REF
2597 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2598 && variably_modified_type_p (type, NULL)))
2599 continue;
2601 if (root->var_map->get (TREE_OPERAND (val, 0))
2602 || walk_tree (&type, contains_remapped_vars, root, NULL))
2603 break;
2606 if (var == NULL_TREE)
2607 return;
2609 memset (&id, 0, sizeof (id));
2610 id.cb.copy_decl = nesting_copy_decl;
2611 id.cb.decl_map = new hash_map<tree, tree>;
2612 id.root = root;
2614 for (; var; var = DECL_CHAIN (var))
2615 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2617 struct nesting_info *i;
2618 tree newt, context;
2620 val = DECL_VALUE_EXPR (var);
2621 type = TREE_TYPE (var);
2623 if (!(TREE_CODE (val) == INDIRECT_REF
2624 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2625 && variably_modified_type_p (type, NULL)))
2626 continue;
2628 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2629 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2630 continue;
2632 context = decl_function_context (var);
2633 for (i = root; i; i = i->outer)
2634 if (i->context == context)
2635 break;
2637 if (i == NULL)
2638 continue;
2640 /* Fully expand value expressions. This avoids having debug variables
2641 only referenced from them and that can be swept during GC. */
2642 if (slot)
2644 tree t = (tree) *slot;
2645 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2646 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2649 id.cb.src_fn = i->context;
2650 id.cb.dst_fn = i->context;
2651 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2653 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2654 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2656 newt = TREE_TYPE (newt);
2657 type = TREE_TYPE (type);
2659 if (TYPE_NAME (newt)
2660 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2661 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2662 && newt != type
2663 && TYPE_NAME (newt) == TYPE_NAME (type))
2664 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2666 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2667 if (val != DECL_VALUE_EXPR (var))
2668 SET_DECL_VALUE_EXPR (var, val);
2671 delete id.cb.decl_map;
2674 /* Fold the MEM_REF *E. */
2675 bool
2676 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2678 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2679 *ref_p = fold (*ref_p);
2680 return true;
2683 /* Do "everything else" to clean up or complete state collected by the
2684 various walking passes -- lay out the types and decls, generate code
2685 to initialize the frame decl, store critical expressions in the
2686 struct function for rtl to find. */
2688 static void
2689 finalize_nesting_tree_1 (struct nesting_info *root)
2691 gimple_seq stmt_list;
2692 gimple stmt;
2693 tree context = root->context;
2694 struct function *sf;
2696 stmt_list = NULL;
2698 /* If we created a non-local frame type or decl, we need to lay them
2699 out at this time. */
2700 if (root->frame_type)
2702 /* In some cases the frame type will trigger the -Wpadded warning.
2703 This is not helpful; suppress it. */
2704 int save_warn_padded = warn_padded;
2705 tree *adjust;
2707 warn_padded = 0;
2708 layout_type (root->frame_type);
2709 warn_padded = save_warn_padded;
2710 layout_decl (root->frame_decl, 0);
2712 /* Remove root->frame_decl from root->new_local_var_chain, so
2713 that we can declare it also in the lexical blocks, which
2714 helps ensure virtual regs that end up appearing in its RTL
2715 expression get substituted in instantiate_virtual_regs(). */
2716 for (adjust = &root->new_local_var_chain;
2717 *adjust != root->frame_decl;
2718 adjust = &DECL_CHAIN (*adjust))
2719 gcc_assert (DECL_CHAIN (*adjust));
2720 *adjust = DECL_CHAIN (*adjust);
2722 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2723 declare_vars (root->frame_decl,
2724 gimple_seq_first_stmt (gimple_body (context)), true);
2727 /* If any parameters were referenced non-locally, then we need to
2728 insert a copy. Likewise, if any variables were referenced by
2729 pointer, we need to initialize the address. */
2730 if (root->any_parm_remapped)
2732 tree p;
2733 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2735 tree field, x, y;
2737 field = lookup_field_for_decl (root, p, NO_INSERT);
2738 if (!field)
2739 continue;
2741 if (use_pointer_in_frame (p))
2742 x = build_addr (p, context);
2743 else
2744 x = p;
2746 /* If the assignment is from a non-register the stmt is
2747 not valid gimple. Make it so by using a temporary instead. */
2748 if (!is_gimple_reg (x)
2749 && is_gimple_reg_type (TREE_TYPE (x)))
2751 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2752 x = init_tmp_var (root, x, &gsi);
2755 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2756 root->frame_decl, field, NULL_TREE);
2757 stmt = gimple_build_assign (y, x);
2758 gimple_seq_add_stmt (&stmt_list, stmt);
2762 /* If a chain_field was created, then it needs to be initialized
2763 from chain_decl. */
2764 if (root->chain_field)
2766 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2767 root->frame_decl, root->chain_field, NULL_TREE);
2768 stmt = gimple_build_assign (x, get_chain_decl (root));
2769 gimple_seq_add_stmt (&stmt_list, stmt);
2772 /* If trampolines were created, then we need to initialize them. */
2773 if (root->any_tramp_created)
2775 struct nesting_info *i;
2776 for (i = root->inner; i ; i = i->next)
2778 tree arg1, arg2, arg3, x, field;
2780 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2781 if (!field)
2782 continue;
2784 gcc_assert (DECL_STATIC_CHAIN (i->context));
2785 arg3 = build_addr (root->frame_decl, context);
2787 arg2 = build_addr (i->context, context);
2789 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2790 root->frame_decl, field, NULL_TREE);
2791 arg1 = build_addr (x, context);
2793 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2794 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2795 gimple_seq_add_stmt (&stmt_list, stmt);
2799 /* If we created initialization statements, insert them. */
2800 if (stmt_list)
2802 gbind *bind;
2803 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2804 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2805 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2806 gimple_bind_set_body (bind, stmt_list);
2809 /* If a chain_decl was created, then it needs to be registered with
2810 struct function so that it gets initialized from the static chain
2811 register at the beginning of the function. */
2812 sf = DECL_STRUCT_FUNCTION (root->context);
2813 sf->static_chain_decl = root->chain_decl;
2815 /* Similarly for the non-local goto save area. */
2816 if (root->nl_goto_field)
2818 sf->nonlocal_goto_save_area
2819 = get_frame_field (root, context, root->nl_goto_field, NULL);
2820 sf->has_nonlocal_label = 1;
2823 /* Make sure all new local variables get inserted into the
2824 proper BIND_EXPR. */
2825 if (root->new_local_var_chain)
2826 declare_vars (root->new_local_var_chain,
2827 gimple_seq_first_stmt (gimple_body (root->context)),
2828 false);
2830 if (root->debug_var_chain)
2832 tree debug_var;
2833 gbind *scope;
2835 remap_vla_decls (DECL_INITIAL (root->context), root);
2837 for (debug_var = root->debug_var_chain; debug_var;
2838 debug_var = DECL_CHAIN (debug_var))
2839 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2840 break;
2842 /* If there are any debug decls with variable length types,
2843 remap those types using other debug_var_chain variables. */
2844 if (debug_var)
2846 struct nesting_copy_body_data id;
2848 memset (&id, 0, sizeof (id));
2849 id.cb.copy_decl = nesting_copy_decl;
2850 id.cb.decl_map = new hash_map<tree, tree>;
2851 id.root = root;
2853 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2854 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2856 tree type = TREE_TYPE (debug_var);
2857 tree newt, t = type;
2858 struct nesting_info *i;
2860 for (i = root; i; i = i->outer)
2861 if (variably_modified_type_p (type, i->context))
2862 break;
2864 if (i == NULL)
2865 continue;
2867 id.cb.src_fn = i->context;
2868 id.cb.dst_fn = i->context;
2869 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2871 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2872 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2874 newt = TREE_TYPE (newt);
2875 t = TREE_TYPE (t);
2877 if (TYPE_NAME (newt)
2878 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2879 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2880 && newt != t
2881 && TYPE_NAME (newt) == TYPE_NAME (t))
2882 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2885 delete id.cb.decl_map;
2888 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
2889 if (gimple_bind_block (scope))
2890 declare_vars (root->debug_var_chain, scope, true);
2891 else
2892 BLOCK_VARS (DECL_INITIAL (root->context))
2893 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2894 root->debug_var_chain);
2897 /* Fold the rewritten MEM_REF trees. */
2898 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2900 /* Dump the translated tree function. */
2901 if (dump_file)
2903 fputs ("\n\n", dump_file);
2904 dump_function_to_file (root->context, dump_file, dump_flags);
2908 static void
2909 finalize_nesting_tree (struct nesting_info *root)
2911 struct nesting_info *n;
2912 FOR_EACH_NEST_INFO (n, root)
2913 finalize_nesting_tree_1 (n);
2916 /* Unnest the nodes and pass them to cgraph. */
2918 static void
2919 unnest_nesting_tree_1 (struct nesting_info *root)
2921 struct cgraph_node *node = cgraph_node::get (root->context);
2923 /* For nested functions update the cgraph to reflect unnesting.
2924 We also delay finalizing of these functions up to this point. */
2925 if (node->origin)
2927 node->unnest ();
2928 cgraph_node::finalize_function (root->context, true);
2932 static void
2933 unnest_nesting_tree (struct nesting_info *root)
2935 struct nesting_info *n;
2936 FOR_EACH_NEST_INFO (n, root)
2937 unnest_nesting_tree_1 (n);
2940 /* Free the data structures allocated during this pass. */
2942 static void
2943 free_nesting_tree (struct nesting_info *root)
2945 struct nesting_info *node, *next;
2947 node = iter_nestinfo_start (root);
2950 next = iter_nestinfo_next (node);
2951 delete node->var_map;
2952 delete node->field_map;
2953 delete node->mem_refs;
2954 free (node);
2955 node = next;
2957 while (node);
2960 /* Gimplify a function and all its nested functions. */
2961 static void
2962 gimplify_all_functions (struct cgraph_node *root)
2964 struct cgraph_node *iter;
2965 if (!gimple_body (root->decl))
2966 gimplify_function_tree (root->decl);
2967 for (iter = root->nested; iter; iter = iter->next_nested)
2968 gimplify_all_functions (iter);
2971 /* Main entry point for this pass. Process FNDECL and all of its nested
2972 subroutines and turn them into something less tightly bound. */
2974 void
2975 lower_nested_functions (tree fndecl)
2977 struct cgraph_node *cgn;
2978 struct nesting_info *root;
2980 /* If there are no nested functions, there's nothing to do. */
2981 cgn = cgraph_node::get (fndecl);
2982 if (!cgn->nested)
2983 return;
2985 gimplify_all_functions (cgn);
2987 dump_file = dump_begin (TDI_nested, &dump_flags);
2988 if (dump_file)
2989 fprintf (dump_file, "\n;; Function %s\n\n",
2990 lang_hooks.decl_printable_name (fndecl, 2));
2992 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2993 root = create_nesting_tree (cgn);
2995 walk_all_functions (convert_nonlocal_reference_stmt,
2996 convert_nonlocal_reference_op,
2997 root);
2998 walk_all_functions (convert_local_reference_stmt,
2999 convert_local_reference_op,
3000 root);
3001 walk_all_functions (convert_nl_goto_reference, NULL, root);
3002 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3004 convert_all_function_calls (root);
3005 finalize_nesting_tree (root);
3006 unnest_nesting_tree (root);
3008 free_nesting_tree (root);
3009 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3011 if (dump_file)
3013 dump_end (TDI_nested, dump_file);
3014 dump_file = NULL;
3018 #include "gt-tree-nested.h"