[ARM] PR target/71436: Restrict *load_multiple pattern till after LRA
[official-gcc.git] / gcc / tree-nested.c
blob4a25025ef8f13bc5e52035062fa7e6dbdb7a9695
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2017 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "tree-dump.h"
35 #include "tree-inline.h"
36 #include "gimplify.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "tree-cfg.h"
40 #include "explow.h"
41 #include "langhooks.h"
42 #include "gimple-low.h"
43 #include "gomp-constants.h"
46 /* The object of this pass is to lower the representation of a set of nested
47 functions in order to expose all of the gory details of the various
48 nonlocal references. We want to do this sooner rather than later, in
49 order to give us more freedom in emitting all of the functions in question.
51 Back in olden times, when gcc was young, we developed an insanely
52 complicated scheme whereby variables which were referenced nonlocally
53 were forced to live in the stack of the declaring function, and then
54 the nested functions magically discovered where these variables were
55 placed. In order for this scheme to function properly, it required
56 that the outer function be partially expanded, then we switch to
57 compiling the inner function, and once done with those we switch back
58 to compiling the outer function. Such delicate ordering requirements
59 makes it difficult to do whole translation unit optimizations
60 involving such functions.
62 The implementation here is much more direct. Everything that can be
63 referenced by an inner function is a member of an explicitly created
64 structure herein called the "nonlocal frame struct". The incoming
65 static chain for a nested function is a pointer to this struct in
66 the parent. In this way, we settle on known offsets from a known
67 base, and so are decoupled from the logic that places objects in the
68 function's stack frame. More importantly, we don't have to wait for
69 that to happen -- since the compilation of the inner function is no
70 longer tied to a real stack frame, the nonlocal frame struct can be
71 allocated anywhere. Which means that the outer function is now
72 inlinable.
74 Theory of operation here is very simple. Iterate over all the
75 statements in all the functions (depth first) several times,
76 allocating structures and fields on demand. In general we want to
77 examine inner functions first, so that we can avoid making changes
78 to outer functions which are unnecessary.
80 The order of the passes matters a bit, in that later passes will be
81 skipped if it is discovered that the functions don't actually interact
82 at all. That is, they're nested in the lexical sense but could have
83 been written as independent functions without change. */
86 struct nesting_info
88 struct nesting_info *outer;
89 struct nesting_info *inner;
90 struct nesting_info *next;
92 hash_map<tree, tree> *field_map;
93 hash_map<tree, tree> *var_map;
94 hash_set<tree *> *mem_refs;
95 bitmap suppress_expansion;
97 tree context;
98 tree new_local_var_chain;
99 tree debug_var_chain;
100 tree frame_type;
101 tree frame_decl;
102 tree chain_field;
103 tree chain_decl;
104 tree nl_goto_field;
106 bool any_parm_remapped;
107 bool any_tramp_created;
108 bool any_descr_created;
109 char static_chain_added;
113 /* Iterate over the nesting tree, starting with ROOT, depth first. */
115 static inline struct nesting_info *
116 iter_nestinfo_start (struct nesting_info *root)
118 while (root->inner)
119 root = root->inner;
120 return root;
123 static inline struct nesting_info *
124 iter_nestinfo_next (struct nesting_info *node)
126 if (node->next)
127 return iter_nestinfo_start (node->next);
128 return node->outer;
131 #define FOR_EACH_NEST_INFO(I, ROOT) \
132 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
134 /* Obstack used for the bitmaps in the struct above. */
135 static struct bitmap_obstack nesting_info_bitmap_obstack;
138 /* We're working in so many different function contexts simultaneously,
139 that create_tmp_var is dangerous. Prevent mishap. */
140 #define create_tmp_var cant_use_create_tmp_var_here_dummy
142 /* Like create_tmp_var, except record the variable for registration at
143 the given nesting level. */
145 static tree
146 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
148 tree tmp_var;
150 /* If the type is of variable size or a type which must be created by the
151 frontend, something is wrong. Note that we explicitly allow
152 incomplete types here, since we create them ourselves here. */
153 gcc_assert (!TREE_ADDRESSABLE (type));
154 gcc_assert (!TYPE_SIZE_UNIT (type)
155 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
157 tmp_var = create_tmp_var_raw (type, prefix);
158 DECL_CONTEXT (tmp_var) = info->context;
159 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
160 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
161 if (TREE_CODE (type) == COMPLEX_TYPE
162 || TREE_CODE (type) == VECTOR_TYPE)
163 DECL_GIMPLE_REG_P (tmp_var) = 1;
165 info->new_local_var_chain = tmp_var;
167 return tmp_var;
170 /* Take the address of EXP to be used within function CONTEXT.
171 Mark it for addressability as necessary. */
173 tree
174 build_addr (tree exp)
176 mark_addressable (exp);
177 return build_fold_addr_expr (exp);
180 /* Insert FIELD into TYPE, sorted by alignment requirements. */
182 void
183 insert_field_into_struct (tree type, tree field)
185 tree *p;
187 DECL_CONTEXT (field) = type;
189 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
190 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
191 break;
193 DECL_CHAIN (field) = *p;
194 *p = field;
196 /* Set correct alignment for frame struct type. */
197 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
198 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
201 /* Build or return the RECORD_TYPE that describes the frame state that is
202 shared between INFO->CONTEXT and its nested functions. This record will
203 not be complete until finalize_nesting_tree; up until that point we'll
204 be adding fields as necessary.
206 We also build the DECL that represents this frame in the function. */
208 static tree
209 get_frame_type (struct nesting_info *info)
211 tree type = info->frame_type;
212 if (!type)
214 char *name;
216 type = make_node (RECORD_TYPE);
218 name = concat ("FRAME.",
219 IDENTIFIER_POINTER (DECL_NAME (info->context)),
220 NULL);
221 TYPE_NAME (type) = get_identifier (name);
222 free (name);
224 info->frame_type = type;
225 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
226 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
228 /* ??? Always make it addressable for now, since it is meant to
229 be pointed to by the static chain pointer. This pessimizes
230 when it turns out that no static chains are needed because
231 the nested functions referencing non-local variables are not
232 reachable, but the true pessimization is to create the non-
233 local frame structure in the first place. */
234 TREE_ADDRESSABLE (info->frame_decl) = 1;
236 return type;
239 /* Return true if DECL should be referenced by pointer in the non-local
240 frame structure. */
242 static bool
243 use_pointer_in_frame (tree decl)
245 if (TREE_CODE (decl) == PARM_DECL)
247 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
248 sized decls, and inefficient to copy large aggregates. Don't bother
249 moving anything but scalar variables. */
250 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
252 else
254 /* Variable sized types make things "interesting" in the frame. */
255 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
259 /* Given DECL, a non-locally accessed variable, find or create a field
260 in the non-local frame structure for the given nesting context. */
262 static tree
263 lookup_field_for_decl (struct nesting_info *info, tree decl,
264 enum insert_option insert)
266 if (insert == NO_INSERT)
268 tree *slot = info->field_map->get (decl);
269 return slot ? *slot : NULL_TREE;
272 tree *slot = &info->field_map->get_or_insert (decl);
273 if (!*slot)
275 tree field = make_node (FIELD_DECL);
276 DECL_NAME (field) = DECL_NAME (decl);
278 if (use_pointer_in_frame (decl))
280 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
281 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
282 DECL_NONADDRESSABLE_P (field) = 1;
284 else
286 TREE_TYPE (field) = TREE_TYPE (decl);
287 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
288 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
289 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
290 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
291 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
292 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
295 insert_field_into_struct (get_frame_type (info), field);
296 *slot = field;
298 if (TREE_CODE (decl) == PARM_DECL)
299 info->any_parm_remapped = true;
302 return *slot;
305 /* Build or return the variable that holds the static chain within
306 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
308 static tree
309 get_chain_decl (struct nesting_info *info)
311 tree decl = info->chain_decl;
313 if (!decl)
315 tree type;
317 type = get_frame_type (info->outer);
318 type = build_pointer_type (type);
320 /* Note that this variable is *not* entered into any BIND_EXPR;
321 the construction of this variable is handled specially in
322 expand_function_start and initialize_inlined_parameters.
323 Note also that it's represented as a parameter. This is more
324 close to the truth, since the initial value does come from
325 the caller. */
326 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
327 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
328 DECL_ARTIFICIAL (decl) = 1;
329 DECL_IGNORED_P (decl) = 1;
330 TREE_USED (decl) = 1;
331 DECL_CONTEXT (decl) = info->context;
332 DECL_ARG_TYPE (decl) = type;
334 /* Tell tree-inline.c that we never write to this variable, so
335 it can copy-prop the replacement value immediately. */
336 TREE_READONLY (decl) = 1;
338 info->chain_decl = decl;
340 if (dump_file
341 && (dump_flags & TDF_DETAILS)
342 && !DECL_STATIC_CHAIN (info->context))
343 fprintf (dump_file, "Setting static-chain for %s\n",
344 lang_hooks.decl_printable_name (info->context, 2));
346 DECL_STATIC_CHAIN (info->context) = 1;
348 return decl;
351 /* Build or return the field within the non-local frame state that holds
352 the static chain for INFO->CONTEXT. This is the way to walk back up
353 multiple nesting levels. */
355 static tree
356 get_chain_field (struct nesting_info *info)
358 tree field = info->chain_field;
360 if (!field)
362 tree type = build_pointer_type (get_frame_type (info->outer));
364 field = make_node (FIELD_DECL);
365 DECL_NAME (field) = get_identifier ("__chain");
366 TREE_TYPE (field) = type;
367 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
368 DECL_NONADDRESSABLE_P (field) = 1;
370 insert_field_into_struct (get_frame_type (info), field);
372 info->chain_field = field;
374 if (dump_file
375 && (dump_flags & TDF_DETAILS)
376 && !DECL_STATIC_CHAIN (info->context))
377 fprintf (dump_file, "Setting static-chain for %s\n",
378 lang_hooks.decl_printable_name (info->context, 2));
380 DECL_STATIC_CHAIN (info->context) = 1;
382 return field;
385 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
387 static tree
388 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
389 gcall *call)
391 tree t;
393 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
394 gimple_call_set_lhs (call, t);
395 if (! gsi_end_p (*gsi))
396 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
397 gsi_insert_before (gsi, call, GSI_SAME_STMT);
399 return t;
403 /* Copy EXP into a temporary. Allocate the temporary in the context of
404 INFO and insert the initialization statement before GSI. */
406 static tree
407 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
409 tree t;
410 gimple *stmt;
412 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
413 stmt = gimple_build_assign (t, exp);
414 if (! gsi_end_p (*gsi))
415 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
416 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
418 return t;
422 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
424 static tree
425 gsi_gimplify_val (struct nesting_info *info, tree exp,
426 gimple_stmt_iterator *gsi)
428 if (is_gimple_val (exp))
429 return exp;
430 else
431 return init_tmp_var (info, exp, gsi);
434 /* Similarly, but copy from the temporary and insert the statement
435 after the iterator. */
437 static tree
438 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
440 tree t;
441 gimple *stmt;
443 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
444 stmt = gimple_build_assign (exp, t);
445 if (! gsi_end_p (*gsi))
446 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
447 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
449 return t;
452 /* Build or return the type used to represent a nested function trampoline. */
454 static GTY(()) tree trampoline_type;
456 static tree
457 get_trampoline_type (struct nesting_info *info)
459 unsigned align, size;
460 tree t;
462 if (trampoline_type)
463 return trampoline_type;
465 align = TRAMPOLINE_ALIGNMENT;
466 size = TRAMPOLINE_SIZE;
468 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
469 then allocate extra space so that we can do dynamic alignment. */
470 if (align > STACK_BOUNDARY)
472 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
473 align = STACK_BOUNDARY;
476 t = build_index_type (size_int (size - 1));
477 t = build_array_type (char_type_node, t);
478 t = build_decl (DECL_SOURCE_LOCATION (info->context),
479 FIELD_DECL, get_identifier ("__data"), t);
480 SET_DECL_ALIGN (t, align);
481 DECL_USER_ALIGN (t) = 1;
483 trampoline_type = make_node (RECORD_TYPE);
484 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
485 TYPE_FIELDS (trampoline_type) = t;
486 layout_type (trampoline_type);
487 DECL_CONTEXT (t) = trampoline_type;
489 return trampoline_type;
492 /* Build or return the type used to represent a nested function descriptor. */
494 static GTY(()) tree descriptor_type;
496 static tree
497 get_descriptor_type (struct nesting_info *info)
499 tree t;
501 if (descriptor_type)
502 return descriptor_type;
504 t = build_index_type (integer_one_node);
505 t = build_array_type (ptr_type_node, t);
506 t = build_decl (DECL_SOURCE_LOCATION (info->context),
507 FIELD_DECL, get_identifier ("__data"), t);
509 descriptor_type = make_node (RECORD_TYPE);
510 TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
511 TYPE_FIELDS (descriptor_type) = t;
512 layout_type (descriptor_type);
513 DECL_CONTEXT (t) = descriptor_type;
515 return descriptor_type;
518 /* Given DECL, a nested function, find or create an element in the
519 var map for this function. */
521 static tree
522 lookup_element_for_decl (struct nesting_info *info, tree decl,
523 enum insert_option insert)
525 if (insert == NO_INSERT)
527 tree *slot = info->var_map->get (decl);
528 return slot ? *slot : NULL_TREE;
531 tree *slot = &info->var_map->get_or_insert (decl);
532 if (!*slot)
533 *slot = build_tree_list (NULL_TREE, NULL_TREE);
535 return (tree) *slot;
538 /* Given DECL, a nested function, create a field in the non-local
539 frame structure for this function. */
541 static tree
542 create_field_for_decl (struct nesting_info *info, tree decl, tree type)
544 tree field = make_node (FIELD_DECL);
545 DECL_NAME (field) = DECL_NAME (decl);
546 TREE_TYPE (field) = type;
547 TREE_ADDRESSABLE (field) = 1;
548 insert_field_into_struct (get_frame_type (info), field);
549 return field;
552 /* Given DECL, a nested function, find or create a field in the non-local
553 frame structure for a trampoline for this function. */
555 static tree
556 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
557 enum insert_option insert)
559 tree elt, field;
561 elt = lookup_element_for_decl (info, decl, insert);
562 if (!elt)
563 return NULL_TREE;
565 field = TREE_PURPOSE (elt);
567 if (!field && insert == INSERT)
569 field = create_field_for_decl (info, decl, get_trampoline_type (info));
570 TREE_PURPOSE (elt) = field;
571 info->any_tramp_created = true;
574 return field;
577 /* Given DECL, a nested function, find or create a field in the non-local
578 frame structure for a descriptor for this function. */
580 static tree
581 lookup_descr_for_decl (struct nesting_info *info, tree decl,
582 enum insert_option insert)
584 tree elt, field;
586 elt = lookup_element_for_decl (info, decl, insert);
587 if (!elt)
588 return NULL_TREE;
590 field = TREE_VALUE (elt);
592 if (!field && insert == INSERT)
594 field = create_field_for_decl (info, decl, get_descriptor_type (info));
595 TREE_VALUE (elt) = field;
596 info->any_descr_created = true;
599 return field;
602 /* Build or return the field within the non-local frame state that holds
603 the non-local goto "jmp_buf". The buffer itself is maintained by the
604 rtl middle-end as dynamic stack space is allocated. */
606 static tree
607 get_nl_goto_field (struct nesting_info *info)
609 tree field = info->nl_goto_field;
610 if (!field)
612 unsigned size;
613 tree type;
615 /* For __builtin_nonlocal_goto, we need N words. The first is the
616 frame pointer, the rest is for the target's stack pointer save
617 area. The number of words is controlled by STACK_SAVEAREA_MODE;
618 not the best interface, but it'll do for now. */
619 if (Pmode == ptr_mode)
620 type = ptr_type_node;
621 else
622 type = lang_hooks.types.type_for_mode (Pmode, 1);
624 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
625 size = size / GET_MODE_SIZE (Pmode);
626 size = size + 1;
628 type = build_array_type
629 (type, build_index_type (size_int (size)));
631 field = make_node (FIELD_DECL);
632 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
633 TREE_TYPE (field) = type;
634 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
635 TREE_ADDRESSABLE (field) = 1;
637 insert_field_into_struct (get_frame_type (info), field);
639 info->nl_goto_field = field;
642 return field;
645 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
647 static void
648 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
649 struct nesting_info *info, gimple_seq *pseq)
651 struct walk_stmt_info wi;
653 memset (&wi, 0, sizeof (wi));
654 wi.info = info;
655 wi.val_only = true;
656 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
660 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
662 static inline void
663 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
664 struct nesting_info *info)
666 gimple_seq body = gimple_body (info->context);
667 walk_body (callback_stmt, callback_op, info, &body);
668 gimple_set_body (info->context, body);
671 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
673 static void
674 walk_gimple_omp_for (gomp_for *for_stmt,
675 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
676 struct nesting_info *info)
678 struct walk_stmt_info wi;
679 gimple_seq seq;
680 tree t;
681 size_t i;
683 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
685 seq = NULL;
686 memset (&wi, 0, sizeof (wi));
687 wi.info = info;
688 wi.gsi = gsi_last (seq);
690 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
692 wi.val_only = false;
693 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
694 &wi, NULL);
695 wi.val_only = true;
696 wi.is_lhs = false;
697 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
698 &wi, NULL);
700 wi.val_only = true;
701 wi.is_lhs = false;
702 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
703 &wi, NULL);
705 t = gimple_omp_for_incr (for_stmt, i);
706 gcc_assert (BINARY_CLASS_P (t));
707 wi.val_only = false;
708 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
709 wi.val_only = true;
710 wi.is_lhs = false;
711 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
714 seq = gsi_seq (wi.gsi);
715 if (!gimple_seq_empty_p (seq))
717 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
718 annotate_all_with_location (seq, gimple_location (for_stmt));
719 gimple_seq_add_seq (&pre_body, seq);
720 gimple_omp_for_set_pre_body (for_stmt, pre_body);
724 /* Similarly for ROOT and all functions nested underneath, depth first. */
726 static void
727 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
728 struct nesting_info *root)
730 struct nesting_info *n;
731 FOR_EACH_NEST_INFO (n, root)
732 walk_function (callback_stmt, callback_op, n);
736 /* We have to check for a fairly pathological case. The operands of function
737 nested function are to be interpreted in the context of the enclosing
738 function. So if any are variably-sized, they will get remapped when the
739 enclosing function is inlined. But that remapping would also have to be
740 done in the types of the PARM_DECLs of the nested function, meaning the
741 argument types of that function will disagree with the arguments in the
742 calls to that function. So we'd either have to make a copy of the nested
743 function corresponding to each time the enclosing function was inlined or
744 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
745 function. The former is not practical. The latter would still require
746 detecting this case to know when to add the conversions. So, for now at
747 least, we don't inline such an enclosing function.
749 We have to do that check recursively, so here return indicating whether
750 FNDECL has such a nested function. ORIG_FN is the function we were
751 trying to inline to use for checking whether any argument is variably
752 modified by anything in it.
754 It would be better to do this in tree-inline.c so that we could give
755 the appropriate warning for why a function can't be inlined, but that's
756 too late since the nesting structure has already been flattened and
757 adding a flag just to record this fact seems a waste of a flag. */
759 static bool
760 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
762 struct cgraph_node *cgn = cgraph_node::get (fndecl);
763 tree arg;
765 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
767 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
768 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
769 return true;
771 if (check_for_nested_with_variably_modified (cgn->decl,
772 orig_fndecl))
773 return true;
776 return false;
779 /* Construct our local datastructure describing the function nesting
780 tree rooted by CGN. */
782 static struct nesting_info *
783 create_nesting_tree (struct cgraph_node *cgn)
785 struct nesting_info *info = XCNEW (struct nesting_info);
786 info->field_map = new hash_map<tree, tree>;
787 info->var_map = new hash_map<tree, tree>;
788 info->mem_refs = new hash_set<tree *>;
789 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
790 info->context = cgn->decl;
792 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
794 struct nesting_info *sub = create_nesting_tree (cgn);
795 sub->outer = info;
796 sub->next = info->inner;
797 info->inner = sub;
800 /* See discussion at check_for_nested_with_variably_modified for a
801 discussion of why this has to be here. */
802 if (check_for_nested_with_variably_modified (info->context, info->context))
803 DECL_UNINLINABLE (info->context) = true;
805 return info;
808 /* Return an expression computing the static chain for TARGET_CONTEXT
809 from INFO->CONTEXT. Insert any necessary computations before TSI. */
811 static tree
812 get_static_chain (struct nesting_info *info, tree target_context,
813 gimple_stmt_iterator *gsi)
815 struct nesting_info *i;
816 tree x;
818 if (info->context == target_context)
820 x = build_addr (info->frame_decl);
821 info->static_chain_added |= 1;
823 else
825 x = get_chain_decl (info);
826 info->static_chain_added |= 2;
828 for (i = info->outer; i->context != target_context; i = i->outer)
830 tree field = get_chain_field (i);
832 x = build_simple_mem_ref (x);
833 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
834 x = init_tmp_var (info, x, gsi);
838 return x;
842 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
843 frame as seen from INFO->CONTEXT. Insert any necessary computations
844 before GSI. */
846 static tree
847 get_frame_field (struct nesting_info *info, tree target_context,
848 tree field, gimple_stmt_iterator *gsi)
850 struct nesting_info *i;
851 tree x;
853 if (info->context == target_context)
855 /* Make sure frame_decl gets created. */
856 (void) get_frame_type (info);
857 x = info->frame_decl;
858 info->static_chain_added |= 1;
860 else
862 x = get_chain_decl (info);
863 info->static_chain_added |= 2;
865 for (i = info->outer; i->context != target_context; i = i->outer)
867 tree field = get_chain_field (i);
869 x = build_simple_mem_ref (x);
870 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
871 x = init_tmp_var (info, x, gsi);
874 x = build_simple_mem_ref (x);
877 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
878 return x;
881 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
883 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
884 in the nested function with DECL_VALUE_EXPR set to reference the true
885 variable in the parent function. This is used both for debug info
886 and in OMP lowering. */
888 static tree
889 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
891 tree target_context;
892 struct nesting_info *i;
893 tree x, field, new_decl;
895 tree *slot = &info->var_map->get_or_insert (decl);
897 if (*slot)
898 return *slot;
900 target_context = decl_function_context (decl);
902 /* A copy of the code in get_frame_field, but without the temporaries. */
903 if (info->context == target_context)
905 /* Make sure frame_decl gets created. */
906 (void) get_frame_type (info);
907 x = info->frame_decl;
908 i = info;
909 info->static_chain_added |= 1;
911 else
913 x = get_chain_decl (info);
914 info->static_chain_added |= 2;
915 for (i = info->outer; i->context != target_context; i = i->outer)
917 field = get_chain_field (i);
918 x = build_simple_mem_ref (x);
919 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
921 x = build_simple_mem_ref (x);
924 field = lookup_field_for_decl (i, decl, INSERT);
925 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
926 if (use_pointer_in_frame (decl))
927 x = build_simple_mem_ref (x);
929 /* ??? We should be remapping types as well, surely. */
930 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
931 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
932 DECL_CONTEXT (new_decl) = info->context;
933 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
934 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
935 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
936 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
937 TREE_READONLY (new_decl) = TREE_READONLY (decl);
938 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
939 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
940 if ((TREE_CODE (decl) == PARM_DECL
941 || TREE_CODE (decl) == RESULT_DECL
942 || VAR_P (decl))
943 && DECL_BY_REFERENCE (decl))
944 DECL_BY_REFERENCE (new_decl) = 1;
946 SET_DECL_VALUE_EXPR (new_decl, x);
947 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
949 *slot = new_decl;
950 DECL_CHAIN (new_decl) = info->debug_var_chain;
951 info->debug_var_chain = new_decl;
953 if (!optimize
954 && info->context != target_context
955 && variably_modified_type_p (TREE_TYPE (decl), NULL))
956 note_nonlocal_vla_type (info, TREE_TYPE (decl));
958 return new_decl;
962 /* Callback for walk_gimple_stmt, rewrite all references to VAR
963 and PARM_DECLs that belong to outer functions.
965 The rewrite will involve some number of structure accesses back up
966 the static chain. E.g. for a variable FOO up one nesting level it'll
967 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
968 indirections apply to decls for which use_pointer_in_frame is true. */
970 static tree
971 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
973 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
974 struct nesting_info *const info = (struct nesting_info *) wi->info;
975 tree t = *tp;
977 *walk_subtrees = 0;
978 switch (TREE_CODE (t))
980 case VAR_DECL:
981 /* Non-automatic variables are never processed. */
982 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
983 break;
984 /* FALLTHRU */
986 case PARM_DECL:
987 if (decl_function_context (t) != info->context)
989 tree x;
990 wi->changed = true;
992 x = get_nonlocal_debug_decl (info, t);
993 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
995 tree target_context = decl_function_context (t);
996 struct nesting_info *i;
997 for (i = info->outer; i->context != target_context; i = i->outer)
998 continue;
999 x = lookup_field_for_decl (i, t, INSERT);
1000 x = get_frame_field (info, target_context, x, &wi->gsi);
1001 if (use_pointer_in_frame (t))
1003 x = init_tmp_var (info, x, &wi->gsi);
1004 x = build_simple_mem_ref (x);
1008 if (wi->val_only)
1010 if (wi->is_lhs)
1011 x = save_tmp_var (info, x, &wi->gsi);
1012 else
1013 x = init_tmp_var (info, x, &wi->gsi);
1016 *tp = x;
1018 break;
1020 case LABEL_DECL:
1021 /* We're taking the address of a label from a parent function, but
1022 this is not itself a non-local goto. Mark the label such that it
1023 will not be deleted, much as we would with a label address in
1024 static storage. */
1025 if (decl_function_context (t) != info->context)
1026 FORCED_LABEL (t) = 1;
1027 break;
1029 case ADDR_EXPR:
1031 bool save_val_only = wi->val_only;
1033 wi->val_only = false;
1034 wi->is_lhs = false;
1035 wi->changed = false;
1036 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1037 wi->val_only = true;
1039 if (wi->changed)
1041 tree save_context;
1043 /* If we changed anything, we might no longer be directly
1044 referencing a decl. */
1045 save_context = current_function_decl;
1046 current_function_decl = info->context;
1047 recompute_tree_invariant_for_addr_expr (t);
1048 current_function_decl = save_context;
1050 /* If the callback converted the address argument in a context
1051 where we only accept variables (and min_invariant, presumably),
1052 then compute the address into a temporary. */
1053 if (save_val_only)
1054 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1055 t, &wi->gsi);
1058 break;
1060 case REALPART_EXPR:
1061 case IMAGPART_EXPR:
1062 case COMPONENT_REF:
1063 case ARRAY_REF:
1064 case ARRAY_RANGE_REF:
1065 case BIT_FIELD_REF:
1066 /* Go down this entire nest and just look at the final prefix and
1067 anything that describes the references. Otherwise, we lose track
1068 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1069 wi->val_only = true;
1070 wi->is_lhs = false;
1071 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1073 if (TREE_CODE (t) == COMPONENT_REF)
1074 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1075 NULL);
1076 else if (TREE_CODE (t) == ARRAY_REF
1077 || TREE_CODE (t) == ARRAY_RANGE_REF)
1079 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1080 wi, NULL);
1081 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1082 wi, NULL);
1083 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1084 wi, NULL);
1087 wi->val_only = false;
1088 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1089 break;
1091 case VIEW_CONVERT_EXPR:
1092 /* Just request to look at the subtrees, leaving val_only and lhs
1093 untouched. This might actually be for !val_only + lhs, in which
1094 case we don't want to force a replacement by a temporary. */
1095 *walk_subtrees = 1;
1096 break;
1098 default:
1099 if (!IS_TYPE_OR_DECL_P (t))
1101 *walk_subtrees = 1;
1102 wi->val_only = true;
1103 wi->is_lhs = false;
1105 break;
1108 return NULL_TREE;
1111 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1112 struct walk_stmt_info *);
1114 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1115 and PARM_DECLs that belong to outer functions. */
1117 static bool
1118 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1120 struct nesting_info *const info = (struct nesting_info *) wi->info;
1121 bool need_chain = false, need_stmts = false;
1122 tree clause, decl;
1123 int dummy;
1124 bitmap new_suppress;
1126 new_suppress = BITMAP_GGC_ALLOC ();
1127 bitmap_copy (new_suppress, info->suppress_expansion);
1129 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1131 switch (OMP_CLAUSE_CODE (clause))
1133 case OMP_CLAUSE_REDUCTION:
1134 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1135 need_stmts = true;
1136 goto do_decl_clause;
1138 case OMP_CLAUSE_LASTPRIVATE:
1139 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1140 need_stmts = true;
1141 goto do_decl_clause;
1143 case OMP_CLAUSE_LINEAR:
1144 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1145 need_stmts = true;
1146 wi->val_only = true;
1147 wi->is_lhs = false;
1148 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1149 &dummy, wi);
1150 goto do_decl_clause;
1152 case OMP_CLAUSE_PRIVATE:
1153 case OMP_CLAUSE_FIRSTPRIVATE:
1154 case OMP_CLAUSE_COPYPRIVATE:
1155 case OMP_CLAUSE_SHARED:
1156 case OMP_CLAUSE_TO_DECLARE:
1157 case OMP_CLAUSE_LINK:
1158 case OMP_CLAUSE_USE_DEVICE_PTR:
1159 case OMP_CLAUSE_IS_DEVICE_PTR:
1160 do_decl_clause:
1161 decl = OMP_CLAUSE_DECL (clause);
1162 if (VAR_P (decl)
1163 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1164 break;
1165 if (decl_function_context (decl) != info->context)
1167 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1168 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1169 bitmap_set_bit (new_suppress, DECL_UID (decl));
1170 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1171 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1172 need_chain = true;
1174 break;
1176 case OMP_CLAUSE_SCHEDULE:
1177 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1178 break;
1179 /* FALLTHRU */
1180 case OMP_CLAUSE_FINAL:
1181 case OMP_CLAUSE_IF:
1182 case OMP_CLAUSE_NUM_THREADS:
1183 case OMP_CLAUSE_DEPEND:
1184 case OMP_CLAUSE_DEVICE:
1185 case OMP_CLAUSE_NUM_TEAMS:
1186 case OMP_CLAUSE_THREAD_LIMIT:
1187 case OMP_CLAUSE_SAFELEN:
1188 case OMP_CLAUSE_SIMDLEN:
1189 case OMP_CLAUSE_PRIORITY:
1190 case OMP_CLAUSE_GRAINSIZE:
1191 case OMP_CLAUSE_NUM_TASKS:
1192 case OMP_CLAUSE_HINT:
1193 case OMP_CLAUSE__CILK_FOR_COUNT_:
1194 case OMP_CLAUSE_NUM_GANGS:
1195 case OMP_CLAUSE_NUM_WORKERS:
1196 case OMP_CLAUSE_VECTOR_LENGTH:
1197 case OMP_CLAUSE_GANG:
1198 case OMP_CLAUSE_WORKER:
1199 case OMP_CLAUSE_VECTOR:
1200 case OMP_CLAUSE_ASYNC:
1201 case OMP_CLAUSE_WAIT:
1202 /* Several OpenACC clauses have optional arguments. Check if they
1203 are present. */
1204 if (OMP_CLAUSE_OPERAND (clause, 0))
1206 wi->val_only = true;
1207 wi->is_lhs = false;
1208 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1209 &dummy, wi);
1212 /* The gang clause accepts two arguments. */
1213 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1214 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1216 wi->val_only = true;
1217 wi->is_lhs = false;
1218 convert_nonlocal_reference_op
1219 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1221 break;
1223 case OMP_CLAUSE_DIST_SCHEDULE:
1224 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1226 wi->val_only = true;
1227 wi->is_lhs = false;
1228 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1229 &dummy, wi);
1231 break;
1233 case OMP_CLAUSE_MAP:
1234 case OMP_CLAUSE_TO:
1235 case OMP_CLAUSE_FROM:
1236 if (OMP_CLAUSE_SIZE (clause))
1238 wi->val_only = true;
1239 wi->is_lhs = false;
1240 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1241 &dummy, wi);
1243 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1244 goto do_decl_clause;
1245 wi->val_only = true;
1246 wi->is_lhs = false;
1247 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1248 wi, NULL);
1249 break;
1251 case OMP_CLAUSE_ALIGNED:
1252 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1254 wi->val_only = true;
1255 wi->is_lhs = false;
1256 convert_nonlocal_reference_op
1257 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1259 /* Like do_decl_clause, but don't add any suppression. */
1260 decl = OMP_CLAUSE_DECL (clause);
1261 if (VAR_P (decl)
1262 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1263 break;
1264 if (decl_function_context (decl) != info->context)
1266 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1267 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1268 need_chain = true;
1270 break;
1272 case OMP_CLAUSE_NOWAIT:
1273 case OMP_CLAUSE_ORDERED:
1274 case OMP_CLAUSE_DEFAULT:
1275 case OMP_CLAUSE_COPYIN:
1276 case OMP_CLAUSE_COLLAPSE:
1277 case OMP_CLAUSE_TILE:
1278 case OMP_CLAUSE_UNTIED:
1279 case OMP_CLAUSE_MERGEABLE:
1280 case OMP_CLAUSE_PROC_BIND:
1281 case OMP_CLAUSE_NOGROUP:
1282 case OMP_CLAUSE_THREADS:
1283 case OMP_CLAUSE_SIMD:
1284 case OMP_CLAUSE_DEFAULTMAP:
1285 case OMP_CLAUSE_SEQ:
1286 case OMP_CLAUSE_INDEPENDENT:
1287 case OMP_CLAUSE_AUTO:
1288 break;
1290 /* The following clause belongs to the OpenACC cache directive, which
1291 is discarded during gimplification. */
1292 case OMP_CLAUSE__CACHE_:
1293 /* The following clauses are only allowed in the OpenMP declare simd
1294 directive, so not seen here. */
1295 case OMP_CLAUSE_UNIFORM:
1296 case OMP_CLAUSE_INBRANCH:
1297 case OMP_CLAUSE_NOTINBRANCH:
1298 /* The following clauses are only allowed on OpenMP cancel and
1299 cancellation point directives, which at this point have already
1300 been lowered into a function call. */
1301 case OMP_CLAUSE_FOR:
1302 case OMP_CLAUSE_PARALLEL:
1303 case OMP_CLAUSE_SECTIONS:
1304 case OMP_CLAUSE_TASKGROUP:
1305 /* The following clauses are only added during OMP lowering; nested
1306 function decomposition happens before that. */
1307 case OMP_CLAUSE__LOOPTEMP_:
1308 case OMP_CLAUSE__SIMDUID_:
1309 case OMP_CLAUSE__GRIDDIM_:
1310 /* Anything else. */
1311 default:
1312 gcc_unreachable ();
1316 info->suppress_expansion = new_suppress;
1318 if (need_stmts)
1319 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1320 switch (OMP_CLAUSE_CODE (clause))
1322 case OMP_CLAUSE_REDUCTION:
1323 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1325 tree old_context
1326 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1327 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1328 = info->context;
1329 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1330 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1331 = info->context;
1332 walk_body (convert_nonlocal_reference_stmt,
1333 convert_nonlocal_reference_op, info,
1334 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1335 walk_body (convert_nonlocal_reference_stmt,
1336 convert_nonlocal_reference_op, info,
1337 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1338 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1339 = old_context;
1340 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1341 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1342 = old_context;
1344 break;
1346 case OMP_CLAUSE_LASTPRIVATE:
1347 walk_body (convert_nonlocal_reference_stmt,
1348 convert_nonlocal_reference_op, info,
1349 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1350 break;
1352 case OMP_CLAUSE_LINEAR:
1353 walk_body (convert_nonlocal_reference_stmt,
1354 convert_nonlocal_reference_op, info,
1355 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1356 break;
1358 default:
1359 break;
1362 return need_chain;
1365 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1367 static void
1368 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1370 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1371 type = TREE_TYPE (type);
1373 if (TYPE_NAME (type)
1374 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1375 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1376 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1378 while (POINTER_TYPE_P (type)
1379 || TREE_CODE (type) == VECTOR_TYPE
1380 || TREE_CODE (type) == FUNCTION_TYPE
1381 || TREE_CODE (type) == METHOD_TYPE)
1382 type = TREE_TYPE (type);
1384 if (TREE_CODE (type) == ARRAY_TYPE)
1386 tree domain, t;
1388 note_nonlocal_vla_type (info, TREE_TYPE (type));
1389 domain = TYPE_DOMAIN (type);
1390 if (domain)
1392 t = TYPE_MIN_VALUE (domain);
1393 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1394 && decl_function_context (t) != info->context)
1395 get_nonlocal_debug_decl (info, t);
1396 t = TYPE_MAX_VALUE (domain);
1397 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1398 && decl_function_context (t) != info->context)
1399 get_nonlocal_debug_decl (info, t);
1404 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1405 in BLOCK. */
1407 static void
1408 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1410 tree var;
1412 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1413 if (VAR_P (var)
1414 && variably_modified_type_p (TREE_TYPE (var), NULL)
1415 && DECL_HAS_VALUE_EXPR_P (var)
1416 && decl_function_context (var) != info->context)
1417 note_nonlocal_vla_type (info, TREE_TYPE (var));
1420 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1421 PARM_DECLs that belong to outer functions. This handles statements
1422 that are not handled via the standard recursion done in
1423 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1424 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1425 operands of STMT have been handled by this function. */
1427 static tree
1428 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1429 struct walk_stmt_info *wi)
1431 struct nesting_info *info = (struct nesting_info *) wi->info;
1432 tree save_local_var_chain;
1433 bitmap save_suppress;
1434 gimple *stmt = gsi_stmt (*gsi);
1436 switch (gimple_code (stmt))
1438 case GIMPLE_GOTO:
1439 /* Don't walk non-local gotos for now. */
1440 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1442 wi->val_only = true;
1443 wi->is_lhs = false;
1444 *handled_ops_p = false;
1445 return NULL_TREE;
1447 break;
1449 case GIMPLE_OMP_PARALLEL:
1450 case GIMPLE_OMP_TASK:
1451 save_suppress = info->suppress_expansion;
1452 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1453 wi))
1455 tree c, decl;
1456 decl = get_chain_decl (info);
1457 c = build_omp_clause (gimple_location (stmt),
1458 OMP_CLAUSE_FIRSTPRIVATE);
1459 OMP_CLAUSE_DECL (c) = decl;
1460 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1461 gimple_omp_taskreg_set_clauses (stmt, c);
1464 save_local_var_chain = info->new_local_var_chain;
1465 info->new_local_var_chain = NULL;
1467 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1468 info, gimple_omp_body_ptr (stmt));
1470 if (info->new_local_var_chain)
1471 declare_vars (info->new_local_var_chain,
1472 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1473 false);
1474 info->new_local_var_chain = save_local_var_chain;
1475 info->suppress_expansion = save_suppress;
1476 break;
1478 case GIMPLE_OMP_FOR:
1479 save_suppress = info->suppress_expansion;
1480 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1481 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1482 convert_nonlocal_reference_stmt,
1483 convert_nonlocal_reference_op, info);
1484 walk_body (convert_nonlocal_reference_stmt,
1485 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1486 info->suppress_expansion = save_suppress;
1487 break;
1489 case GIMPLE_OMP_SECTIONS:
1490 save_suppress = info->suppress_expansion;
1491 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1492 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1493 info, gimple_omp_body_ptr (stmt));
1494 info->suppress_expansion = save_suppress;
1495 break;
1497 case GIMPLE_OMP_SINGLE:
1498 save_suppress = info->suppress_expansion;
1499 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1500 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1501 info, gimple_omp_body_ptr (stmt));
1502 info->suppress_expansion = save_suppress;
1503 break;
1505 case GIMPLE_OMP_TARGET:
1506 if (!is_gimple_omp_offloaded (stmt))
1508 save_suppress = info->suppress_expansion;
1509 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1510 wi);
1511 info->suppress_expansion = save_suppress;
1512 walk_body (convert_nonlocal_reference_stmt,
1513 convert_nonlocal_reference_op, info,
1514 gimple_omp_body_ptr (stmt));
1515 break;
1517 save_suppress = info->suppress_expansion;
1518 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1519 wi))
1521 tree c, decl;
1522 decl = get_chain_decl (info);
1523 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1524 OMP_CLAUSE_DECL (c) = decl;
1525 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1526 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1527 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1528 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1531 save_local_var_chain = info->new_local_var_chain;
1532 info->new_local_var_chain = NULL;
1534 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1535 info, gimple_omp_body_ptr (stmt));
1537 if (info->new_local_var_chain)
1538 declare_vars (info->new_local_var_chain,
1539 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1540 false);
1541 info->new_local_var_chain = save_local_var_chain;
1542 info->suppress_expansion = save_suppress;
1543 break;
1545 case GIMPLE_OMP_TEAMS:
1546 save_suppress = info->suppress_expansion;
1547 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1548 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1549 info, gimple_omp_body_ptr (stmt));
1550 info->suppress_expansion = save_suppress;
1551 break;
1553 case GIMPLE_OMP_SECTION:
1554 case GIMPLE_OMP_MASTER:
1555 case GIMPLE_OMP_TASKGROUP:
1556 case GIMPLE_OMP_ORDERED:
1557 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1558 info, gimple_omp_body_ptr (stmt));
1559 break;
1561 case GIMPLE_BIND:
1563 gbind *bind_stmt = as_a <gbind *> (stmt);
1564 if (!optimize && gimple_bind_block (bind_stmt))
1565 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1567 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1568 if (TREE_CODE (var) == NAMELIST_DECL)
1570 /* Adjust decls mentioned in NAMELIST_DECL. */
1571 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1572 tree decl;
1573 unsigned int i;
1575 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1577 if (VAR_P (decl)
1578 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1579 continue;
1580 if (decl_function_context (decl) != info->context)
1581 CONSTRUCTOR_ELT (decls, i)->value
1582 = get_nonlocal_debug_decl (info, decl);
1586 *handled_ops_p = false;
1587 return NULL_TREE;
1589 case GIMPLE_COND:
1590 wi->val_only = true;
1591 wi->is_lhs = false;
1592 *handled_ops_p = false;
1593 return NULL_TREE;
1595 default:
1596 /* For every other statement that we are not interested in
1597 handling here, let the walker traverse the operands. */
1598 *handled_ops_p = false;
1599 return NULL_TREE;
1602 /* We have handled all of STMT operands, no need to traverse the operands. */
1603 *handled_ops_p = true;
1604 return NULL_TREE;
1608 /* A subroutine of convert_local_reference. Create a local variable
1609 in the parent function with DECL_VALUE_EXPR set to reference the
1610 field in FRAME. This is used both for debug info and in OMP
1611 lowering. */
1613 static tree
1614 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1616 tree x, new_decl;
1618 tree *slot = &info->var_map->get_or_insert (decl);
1619 if (*slot)
1620 return *slot;
1622 /* Make sure frame_decl gets created. */
1623 (void) get_frame_type (info);
1624 x = info->frame_decl;
1625 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1627 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1628 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1629 DECL_CONTEXT (new_decl) = info->context;
1630 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1631 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1632 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1633 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1634 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1635 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1636 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1637 if ((TREE_CODE (decl) == PARM_DECL
1638 || TREE_CODE (decl) == RESULT_DECL
1639 || VAR_P (decl))
1640 && DECL_BY_REFERENCE (decl))
1641 DECL_BY_REFERENCE (new_decl) = 1;
1643 SET_DECL_VALUE_EXPR (new_decl, x);
1644 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1645 *slot = new_decl;
1647 DECL_CHAIN (new_decl) = info->debug_var_chain;
1648 info->debug_var_chain = new_decl;
1650 /* Do not emit debug info twice. */
1651 DECL_IGNORED_P (decl) = 1;
1653 return new_decl;
1657 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1658 and PARM_DECLs that were referenced by inner nested functions.
1659 The rewrite will be a structure reference to the local frame variable. */
1661 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1663 static tree
1664 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1666 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1667 struct nesting_info *const info = (struct nesting_info *) wi->info;
1668 tree t = *tp, field, x;
1669 bool save_val_only;
1671 *walk_subtrees = 0;
1672 switch (TREE_CODE (t))
1674 case VAR_DECL:
1675 /* Non-automatic variables are never processed. */
1676 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1677 break;
1678 /* FALLTHRU */
1680 case PARM_DECL:
1681 if (decl_function_context (t) == info->context)
1683 /* If we copied a pointer to the frame, then the original decl
1684 is used unchanged in the parent function. */
1685 if (use_pointer_in_frame (t))
1686 break;
1688 /* No need to transform anything if no child references the
1689 variable. */
1690 field = lookup_field_for_decl (info, t, NO_INSERT);
1691 if (!field)
1692 break;
1693 wi->changed = true;
1695 x = get_local_debug_decl (info, t, field);
1696 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1697 x = get_frame_field (info, info->context, field, &wi->gsi);
1699 if (wi->val_only)
1701 if (wi->is_lhs)
1702 x = save_tmp_var (info, x, &wi->gsi);
1703 else
1704 x = init_tmp_var (info, x, &wi->gsi);
1707 *tp = x;
1709 break;
1711 case ADDR_EXPR:
1712 save_val_only = wi->val_only;
1713 wi->val_only = false;
1714 wi->is_lhs = false;
1715 wi->changed = false;
1716 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1717 wi->val_only = save_val_only;
1719 /* If we converted anything ... */
1720 if (wi->changed)
1722 tree save_context;
1724 /* Then the frame decl is now addressable. */
1725 TREE_ADDRESSABLE (info->frame_decl) = 1;
1727 save_context = current_function_decl;
1728 current_function_decl = info->context;
1729 recompute_tree_invariant_for_addr_expr (t);
1730 current_function_decl = save_context;
1732 /* If we are in a context where we only accept values, then
1733 compute the address into a temporary. */
1734 if (save_val_only)
1735 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1736 t, &wi->gsi);
1738 break;
1740 case REALPART_EXPR:
1741 case IMAGPART_EXPR:
1742 case COMPONENT_REF:
1743 case ARRAY_REF:
1744 case ARRAY_RANGE_REF:
1745 case BIT_FIELD_REF:
1746 /* Go down this entire nest and just look at the final prefix and
1747 anything that describes the references. Otherwise, we lose track
1748 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1749 save_val_only = wi->val_only;
1750 wi->val_only = true;
1751 wi->is_lhs = false;
1752 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1754 if (TREE_CODE (t) == COMPONENT_REF)
1755 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1756 NULL);
1757 else if (TREE_CODE (t) == ARRAY_REF
1758 || TREE_CODE (t) == ARRAY_RANGE_REF)
1760 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1761 NULL);
1762 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1763 NULL);
1764 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1765 NULL);
1768 wi->val_only = false;
1769 walk_tree (tp, convert_local_reference_op, wi, NULL);
1770 wi->val_only = save_val_only;
1771 break;
1773 case MEM_REF:
1774 save_val_only = wi->val_only;
1775 wi->val_only = true;
1776 wi->is_lhs = false;
1777 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1778 wi, NULL);
1779 /* We need to re-fold the MEM_REF as component references as
1780 part of a ADDR_EXPR address are not allowed. But we cannot
1781 fold here, as the chain record type is not yet finalized. */
1782 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1783 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1784 info->mem_refs->add (tp);
1785 wi->val_only = save_val_only;
1786 break;
1788 case VIEW_CONVERT_EXPR:
1789 /* Just request to look at the subtrees, leaving val_only and lhs
1790 untouched. This might actually be for !val_only + lhs, in which
1791 case we don't want to force a replacement by a temporary. */
1792 *walk_subtrees = 1;
1793 break;
1795 default:
1796 if (!IS_TYPE_OR_DECL_P (t))
1798 *walk_subtrees = 1;
1799 wi->val_only = true;
1800 wi->is_lhs = false;
1802 break;
1805 return NULL_TREE;
1808 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1809 struct walk_stmt_info *);
1811 /* Helper for convert_local_reference. Convert all the references in
1812 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1814 static bool
1815 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1817 struct nesting_info *const info = (struct nesting_info *) wi->info;
1818 bool need_frame = false, need_stmts = false;
1819 tree clause, decl;
1820 int dummy;
1821 bitmap new_suppress;
1823 new_suppress = BITMAP_GGC_ALLOC ();
1824 bitmap_copy (new_suppress, info->suppress_expansion);
1826 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1828 switch (OMP_CLAUSE_CODE (clause))
1830 case OMP_CLAUSE_REDUCTION:
1831 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1832 need_stmts = true;
1833 goto do_decl_clause;
1835 case OMP_CLAUSE_LASTPRIVATE:
1836 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1837 need_stmts = true;
1838 goto do_decl_clause;
1840 case OMP_CLAUSE_LINEAR:
1841 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1842 need_stmts = true;
1843 wi->val_only = true;
1844 wi->is_lhs = false;
1845 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1846 wi);
1847 goto do_decl_clause;
1849 case OMP_CLAUSE_PRIVATE:
1850 case OMP_CLAUSE_FIRSTPRIVATE:
1851 case OMP_CLAUSE_COPYPRIVATE:
1852 case OMP_CLAUSE_SHARED:
1853 case OMP_CLAUSE_TO_DECLARE:
1854 case OMP_CLAUSE_LINK:
1855 case OMP_CLAUSE_USE_DEVICE_PTR:
1856 case OMP_CLAUSE_IS_DEVICE_PTR:
1857 do_decl_clause:
1858 decl = OMP_CLAUSE_DECL (clause);
1859 if (VAR_P (decl)
1860 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1861 break;
1862 if (decl_function_context (decl) == info->context
1863 && !use_pointer_in_frame (decl))
1865 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1866 if (field)
1868 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1869 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1870 bitmap_set_bit (new_suppress, DECL_UID (decl));
1871 OMP_CLAUSE_DECL (clause)
1872 = get_local_debug_decl (info, decl, field);
1873 need_frame = true;
1876 break;
1878 case OMP_CLAUSE_SCHEDULE:
1879 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1880 break;
1881 /* FALLTHRU */
1882 case OMP_CLAUSE_FINAL:
1883 case OMP_CLAUSE_IF:
1884 case OMP_CLAUSE_NUM_THREADS:
1885 case OMP_CLAUSE_DEPEND:
1886 case OMP_CLAUSE_DEVICE:
1887 case OMP_CLAUSE_NUM_TEAMS:
1888 case OMP_CLAUSE_THREAD_LIMIT:
1889 case OMP_CLAUSE_SAFELEN:
1890 case OMP_CLAUSE_SIMDLEN:
1891 case OMP_CLAUSE_PRIORITY:
1892 case OMP_CLAUSE_GRAINSIZE:
1893 case OMP_CLAUSE_NUM_TASKS:
1894 case OMP_CLAUSE_HINT:
1895 case OMP_CLAUSE__CILK_FOR_COUNT_:
1896 case OMP_CLAUSE_NUM_GANGS:
1897 case OMP_CLAUSE_NUM_WORKERS:
1898 case OMP_CLAUSE_VECTOR_LENGTH:
1899 case OMP_CLAUSE_GANG:
1900 case OMP_CLAUSE_WORKER:
1901 case OMP_CLAUSE_VECTOR:
1902 case OMP_CLAUSE_ASYNC:
1903 case OMP_CLAUSE_WAIT:
1904 /* Several OpenACC clauses have optional arguments. Check if they
1905 are present. */
1906 if (OMP_CLAUSE_OPERAND (clause, 0))
1908 wi->val_only = true;
1909 wi->is_lhs = false;
1910 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1911 &dummy, wi);
1914 /* The gang clause accepts two arguments. */
1915 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1916 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1918 wi->val_only = true;
1919 wi->is_lhs = false;
1920 convert_nonlocal_reference_op
1921 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1923 break;
1925 case OMP_CLAUSE_DIST_SCHEDULE:
1926 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1928 wi->val_only = true;
1929 wi->is_lhs = false;
1930 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1931 &dummy, wi);
1933 break;
1935 case OMP_CLAUSE_MAP:
1936 case OMP_CLAUSE_TO:
1937 case OMP_CLAUSE_FROM:
1938 if (OMP_CLAUSE_SIZE (clause))
1940 wi->val_only = true;
1941 wi->is_lhs = false;
1942 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1943 &dummy, wi);
1945 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1946 goto do_decl_clause;
1947 wi->val_only = true;
1948 wi->is_lhs = false;
1949 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1950 wi, NULL);
1951 break;
1953 case OMP_CLAUSE_ALIGNED:
1954 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1956 wi->val_only = true;
1957 wi->is_lhs = false;
1958 convert_local_reference_op
1959 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1961 /* Like do_decl_clause, but don't add any suppression. */
1962 decl = OMP_CLAUSE_DECL (clause);
1963 if (VAR_P (decl)
1964 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1965 break;
1966 if (decl_function_context (decl) == info->context
1967 && !use_pointer_in_frame (decl))
1969 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1970 if (field)
1972 OMP_CLAUSE_DECL (clause)
1973 = get_local_debug_decl (info, decl, field);
1974 need_frame = true;
1977 break;
1979 case OMP_CLAUSE_NOWAIT:
1980 case OMP_CLAUSE_ORDERED:
1981 case OMP_CLAUSE_DEFAULT:
1982 case OMP_CLAUSE_COPYIN:
1983 case OMP_CLAUSE_COLLAPSE:
1984 case OMP_CLAUSE_TILE:
1985 case OMP_CLAUSE_UNTIED:
1986 case OMP_CLAUSE_MERGEABLE:
1987 case OMP_CLAUSE_PROC_BIND:
1988 case OMP_CLAUSE_NOGROUP:
1989 case OMP_CLAUSE_THREADS:
1990 case OMP_CLAUSE_SIMD:
1991 case OMP_CLAUSE_DEFAULTMAP:
1992 case OMP_CLAUSE_SEQ:
1993 case OMP_CLAUSE_INDEPENDENT:
1994 case OMP_CLAUSE_AUTO:
1995 break;
1997 /* The following clause belongs to the OpenACC cache directive, which
1998 is discarded during gimplification. */
1999 case OMP_CLAUSE__CACHE_:
2000 /* The following clauses are only allowed in the OpenMP declare simd
2001 directive, so not seen here. */
2002 case OMP_CLAUSE_UNIFORM:
2003 case OMP_CLAUSE_INBRANCH:
2004 case OMP_CLAUSE_NOTINBRANCH:
2005 /* The following clauses are only allowed on OpenMP cancel and
2006 cancellation point directives, which at this point have already
2007 been lowered into a function call. */
2008 case OMP_CLAUSE_FOR:
2009 case OMP_CLAUSE_PARALLEL:
2010 case OMP_CLAUSE_SECTIONS:
2011 case OMP_CLAUSE_TASKGROUP:
2012 /* The following clauses are only added during OMP lowering; nested
2013 function decomposition happens before that. */
2014 case OMP_CLAUSE__LOOPTEMP_:
2015 case OMP_CLAUSE__SIMDUID_:
2016 case OMP_CLAUSE__GRIDDIM_:
2017 /* Anything else. */
2018 default:
2019 gcc_unreachable ();
2023 info->suppress_expansion = new_suppress;
2025 if (need_stmts)
2026 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2027 switch (OMP_CLAUSE_CODE (clause))
2029 case OMP_CLAUSE_REDUCTION:
2030 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2032 tree old_context
2033 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2034 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2035 = info->context;
2036 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2037 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2038 = info->context;
2039 walk_body (convert_local_reference_stmt,
2040 convert_local_reference_op, info,
2041 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2042 walk_body (convert_local_reference_stmt,
2043 convert_local_reference_op, info,
2044 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2045 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2046 = old_context;
2047 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2048 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2049 = old_context;
2051 break;
2053 case OMP_CLAUSE_LASTPRIVATE:
2054 walk_body (convert_local_reference_stmt,
2055 convert_local_reference_op, info,
2056 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2057 break;
2059 case OMP_CLAUSE_LINEAR:
2060 walk_body (convert_local_reference_stmt,
2061 convert_local_reference_op, info,
2062 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2063 break;
2065 default:
2066 break;
2069 return need_frame;
2073 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2074 and PARM_DECLs that were referenced by inner nested functions.
2075 The rewrite will be a structure reference to the local frame variable. */
2077 static tree
2078 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2079 struct walk_stmt_info *wi)
2081 struct nesting_info *info = (struct nesting_info *) wi->info;
2082 tree save_local_var_chain;
2083 bitmap save_suppress;
2084 char save_static_chain_added;
2085 bool frame_decl_added;
2086 gimple *stmt = gsi_stmt (*gsi);
2088 switch (gimple_code (stmt))
2090 case GIMPLE_OMP_PARALLEL:
2091 case GIMPLE_OMP_TASK:
2092 save_suppress = info->suppress_expansion;
2093 frame_decl_added = false;
2094 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2095 wi))
2097 tree c = build_omp_clause (gimple_location (stmt),
2098 OMP_CLAUSE_SHARED);
2099 (void) get_frame_type (info);
2100 OMP_CLAUSE_DECL (c) = info->frame_decl;
2101 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2102 gimple_omp_taskreg_set_clauses (stmt, c);
2103 info->static_chain_added |= 4;
2104 frame_decl_added = true;
2107 save_local_var_chain = info->new_local_var_chain;
2108 save_static_chain_added = info->static_chain_added;
2109 info->new_local_var_chain = NULL;
2110 info->static_chain_added = 0;
2112 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2113 gimple_omp_body_ptr (stmt));
2115 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2117 tree c = build_omp_clause (gimple_location (stmt),
2118 OMP_CLAUSE_SHARED);
2119 (void) get_frame_type (info);
2120 OMP_CLAUSE_DECL (c) = info->frame_decl;
2121 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2122 info->static_chain_added |= 4;
2123 gimple_omp_taskreg_set_clauses (stmt, c);
2125 if (info->new_local_var_chain)
2126 declare_vars (info->new_local_var_chain,
2127 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2128 info->new_local_var_chain = save_local_var_chain;
2129 info->suppress_expansion = save_suppress;
2130 info->static_chain_added |= save_static_chain_added;
2131 break;
2133 case GIMPLE_OMP_FOR:
2134 save_suppress = info->suppress_expansion;
2135 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2136 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2137 convert_local_reference_stmt,
2138 convert_local_reference_op, info);
2139 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2140 info, gimple_omp_body_ptr (stmt));
2141 info->suppress_expansion = save_suppress;
2142 break;
2144 case GIMPLE_OMP_SECTIONS:
2145 save_suppress = info->suppress_expansion;
2146 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2147 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2148 info, gimple_omp_body_ptr (stmt));
2149 info->suppress_expansion = save_suppress;
2150 break;
2152 case GIMPLE_OMP_SINGLE:
2153 save_suppress = info->suppress_expansion;
2154 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2155 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2156 info, gimple_omp_body_ptr (stmt));
2157 info->suppress_expansion = save_suppress;
2158 break;
2160 case GIMPLE_OMP_TARGET:
2161 if (!is_gimple_omp_offloaded (stmt))
2163 save_suppress = info->suppress_expansion;
2164 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2165 info->suppress_expansion = save_suppress;
2166 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2167 info, gimple_omp_body_ptr (stmt));
2168 break;
2170 save_suppress = info->suppress_expansion;
2171 frame_decl_added = false;
2172 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2174 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2175 (void) get_frame_type (info);
2176 OMP_CLAUSE_DECL (c) = info->frame_decl;
2177 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2178 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2179 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2180 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2181 info->static_chain_added |= 4;
2182 frame_decl_added = true;
2185 save_local_var_chain = info->new_local_var_chain;
2186 save_static_chain_added = info->static_chain_added;
2187 info->new_local_var_chain = NULL;
2188 info->static_chain_added = 0;
2190 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2191 gimple_omp_body_ptr (stmt));
2193 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2195 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2196 (void) get_frame_type (info);
2197 OMP_CLAUSE_DECL (c) = info->frame_decl;
2198 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2199 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2200 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2201 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2202 info->static_chain_added |= 4;
2205 if (info->new_local_var_chain)
2206 declare_vars (info->new_local_var_chain,
2207 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2208 info->new_local_var_chain = save_local_var_chain;
2209 info->suppress_expansion = save_suppress;
2210 info->static_chain_added |= save_static_chain_added;
2211 break;
2213 case GIMPLE_OMP_TEAMS:
2214 save_suppress = info->suppress_expansion;
2215 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2216 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2217 info, gimple_omp_body_ptr (stmt));
2218 info->suppress_expansion = save_suppress;
2219 break;
2221 case GIMPLE_OMP_SECTION:
2222 case GIMPLE_OMP_MASTER:
2223 case GIMPLE_OMP_TASKGROUP:
2224 case GIMPLE_OMP_ORDERED:
2225 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2226 info, gimple_omp_body_ptr (stmt));
2227 break;
2229 case GIMPLE_COND:
2230 wi->val_only = true;
2231 wi->is_lhs = false;
2232 *handled_ops_p = false;
2233 return NULL_TREE;
2235 case GIMPLE_ASSIGN:
2236 if (gimple_clobber_p (stmt))
2238 tree lhs = gimple_assign_lhs (stmt);
2239 if (!use_pointer_in_frame (lhs)
2240 && lookup_field_for_decl (info, lhs, NO_INSERT))
2242 gsi_replace (gsi, gimple_build_nop (), true);
2243 break;
2246 *handled_ops_p = false;
2247 return NULL_TREE;
2249 case GIMPLE_BIND:
2250 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2251 var;
2252 var = DECL_CHAIN (var))
2253 if (TREE_CODE (var) == NAMELIST_DECL)
2255 /* Adjust decls mentioned in NAMELIST_DECL. */
2256 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2257 tree decl;
2258 unsigned int i;
2260 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2262 if (VAR_P (decl)
2263 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2264 continue;
2265 if (decl_function_context (decl) == info->context
2266 && !use_pointer_in_frame (decl))
2268 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2269 if (field)
2271 CONSTRUCTOR_ELT (decls, i)->value
2272 = get_local_debug_decl (info, decl, field);
2278 *handled_ops_p = false;
2279 return NULL_TREE;
2281 default:
2282 /* For every other statement that we are not interested in
2283 handling here, let the walker traverse the operands. */
2284 *handled_ops_p = false;
2285 return NULL_TREE;
2288 /* Indicate that we have handled all the operands ourselves. */
2289 *handled_ops_p = true;
2290 return NULL_TREE;
2294 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2295 that reference labels from outer functions. The rewrite will be a
2296 call to __builtin_nonlocal_goto. */
2298 static tree
2299 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2300 struct walk_stmt_info *wi)
2302 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2303 tree label, new_label, target_context, x, field;
2304 gcall *call;
2305 gimple *stmt = gsi_stmt (*gsi);
2307 if (gimple_code (stmt) != GIMPLE_GOTO)
2309 *handled_ops_p = false;
2310 return NULL_TREE;
2313 label = gimple_goto_dest (stmt);
2314 if (TREE_CODE (label) != LABEL_DECL)
2316 *handled_ops_p = false;
2317 return NULL_TREE;
2320 target_context = decl_function_context (label);
2321 if (target_context == info->context)
2323 *handled_ops_p = false;
2324 return NULL_TREE;
2327 for (i = info->outer; target_context != i->context; i = i->outer)
2328 continue;
2330 /* The original user label may also be use for a normal goto, therefore
2331 we must create a new label that will actually receive the abnormal
2332 control transfer. This new label will be marked LABEL_NONLOCAL; this
2333 mark will trigger proper behavior in the cfg, as well as cause the
2334 (hairy target-specific) non-local goto receiver code to be generated
2335 when we expand rtl. Enter this association into var_map so that we
2336 can insert the new label into the IL during a second pass. */
2337 tree *slot = &i->var_map->get_or_insert (label);
2338 if (*slot == NULL)
2340 new_label = create_artificial_label (UNKNOWN_LOCATION);
2341 DECL_NONLOCAL (new_label) = 1;
2342 *slot = new_label;
2344 else
2345 new_label = *slot;
2347 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2348 field = get_nl_goto_field (i);
2349 x = get_frame_field (info, target_context, field, gsi);
2350 x = build_addr (x);
2351 x = gsi_gimplify_val (info, x, gsi);
2352 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2353 2, build_addr (new_label), x);
2354 gsi_replace (gsi, call, false);
2356 /* We have handled all of STMT's operands, no need to keep going. */
2357 *handled_ops_p = true;
2358 return NULL_TREE;
2362 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2363 are referenced via nonlocal goto from a nested function. The rewrite
2364 will involve installing a newly generated DECL_NONLOCAL label, and
2365 (potentially) a branch around the rtl gunk that is assumed to be
2366 attached to such a label. */
2368 static tree
2369 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2370 struct walk_stmt_info *wi)
2372 struct nesting_info *const info = (struct nesting_info *) wi->info;
2373 tree label, new_label;
2374 gimple_stmt_iterator tmp_gsi;
2375 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2377 if (!stmt)
2379 *handled_ops_p = false;
2380 return NULL_TREE;
2383 label = gimple_label_label (stmt);
2385 tree *slot = info->var_map->get (label);
2386 if (!slot)
2388 *handled_ops_p = false;
2389 return NULL_TREE;
2392 /* If there's any possibility that the previous statement falls through,
2393 then we must branch around the new non-local label. */
2394 tmp_gsi = wi->gsi;
2395 gsi_prev (&tmp_gsi);
2396 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2398 gimple *stmt = gimple_build_goto (label);
2399 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2402 new_label = (tree) *slot;
2403 stmt = gimple_build_label (new_label);
2404 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2406 *handled_ops_p = true;
2407 return NULL_TREE;
2411 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2412 of nested functions that require the use of trampolines. The rewrite
2413 will involve a reference a trampoline generated for the occasion. */
2415 static tree
2416 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2418 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2419 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2420 tree t = *tp, decl, target_context, x, builtin;
2421 bool descr;
2422 gcall *call;
2424 *walk_subtrees = 0;
2425 switch (TREE_CODE (t))
2427 case ADDR_EXPR:
2428 /* Build
2429 T.1 = &CHAIN->tramp;
2430 T.2 = __builtin_adjust_trampoline (T.1);
2431 T.3 = (func_type)T.2;
2434 decl = TREE_OPERAND (t, 0);
2435 if (TREE_CODE (decl) != FUNCTION_DECL)
2436 break;
2438 /* Only need to process nested functions. */
2439 target_context = decl_function_context (decl);
2440 if (!target_context)
2441 break;
2443 /* If the nested function doesn't use a static chain, then
2444 it doesn't need a trampoline. */
2445 if (!DECL_STATIC_CHAIN (decl))
2446 break;
2448 /* If we don't want a trampoline, then don't build one. */
2449 if (TREE_NO_TRAMPOLINE (t))
2450 break;
2452 /* Lookup the immediate parent of the callee, as that's where
2453 we need to insert the trampoline. */
2454 for (i = info; i->context != target_context; i = i->outer)
2455 continue;
2457 /* Decide whether to generate a descriptor or a trampoline. */
2458 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2460 if (descr)
2461 x = lookup_descr_for_decl (i, decl, INSERT);
2462 else
2463 x = lookup_tramp_for_decl (i, decl, INSERT);
2465 /* Compute the address of the field holding the trampoline. */
2466 x = get_frame_field (info, target_context, x, &wi->gsi);
2467 x = build_addr (x);
2468 x = gsi_gimplify_val (info, x, &wi->gsi);
2470 /* Do machine-specific ugliness. Normally this will involve
2471 computing extra alignment, but it can really be anything. */
2472 if (descr)
2473 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2474 else
2475 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2476 call = gimple_build_call (builtin, 1, x);
2477 x = init_tmp_var_with_call (info, &wi->gsi, call);
2479 /* Cast back to the proper function type. */
2480 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2481 x = init_tmp_var (info, x, &wi->gsi);
2483 *tp = x;
2484 break;
2486 default:
2487 if (!IS_TYPE_OR_DECL_P (t))
2488 *walk_subtrees = 1;
2489 break;
2492 return NULL_TREE;
2496 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2497 to addresses of nested functions that require the use of
2498 trampolines. The rewrite will involve a reference a trampoline
2499 generated for the occasion. */
2501 static tree
2502 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2503 struct walk_stmt_info *wi)
2505 struct nesting_info *info = (struct nesting_info *) wi->info;
2506 gimple *stmt = gsi_stmt (*gsi);
2508 switch (gimple_code (stmt))
2510 case GIMPLE_CALL:
2512 /* Only walk call arguments, lest we generate trampolines for
2513 direct calls. */
2514 unsigned long i, nargs = gimple_call_num_args (stmt);
2515 for (i = 0; i < nargs; i++)
2516 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2517 wi, NULL);
2518 break;
2521 case GIMPLE_OMP_TARGET:
2522 if (!is_gimple_omp_offloaded (stmt))
2524 *handled_ops_p = false;
2525 return NULL_TREE;
2527 /* FALLTHRU */
2528 case GIMPLE_OMP_PARALLEL:
2529 case GIMPLE_OMP_TASK:
2531 tree save_local_var_chain = info->new_local_var_chain;
2532 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2533 info->new_local_var_chain = NULL;
2534 char save_static_chain_added = info->static_chain_added;
2535 info->static_chain_added = 0;
2536 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2537 info, gimple_omp_body_ptr (stmt));
2538 if (info->new_local_var_chain)
2539 declare_vars (info->new_local_var_chain,
2540 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2541 false);
2542 for (int i = 0; i < 2; i++)
2544 tree c, decl;
2545 if ((info->static_chain_added & (1 << i)) == 0)
2546 continue;
2547 decl = i ? get_chain_decl (info) : info->frame_decl;
2548 /* Don't add CHAIN.* or FRAME.* twice. */
2549 for (c = gimple_omp_taskreg_clauses (stmt);
2551 c = OMP_CLAUSE_CHAIN (c))
2552 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2553 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2554 && OMP_CLAUSE_DECL (c) == decl)
2555 break;
2556 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2558 c = build_omp_clause (gimple_location (stmt),
2559 i ? OMP_CLAUSE_FIRSTPRIVATE
2560 : OMP_CLAUSE_SHARED);
2561 OMP_CLAUSE_DECL (c) = decl;
2562 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2563 gimple_omp_taskreg_set_clauses (stmt, c);
2565 else if (c == NULL)
2567 c = build_omp_clause (gimple_location (stmt),
2568 OMP_CLAUSE_MAP);
2569 OMP_CLAUSE_DECL (c) = decl;
2570 OMP_CLAUSE_SET_MAP_KIND (c,
2571 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2572 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2573 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2574 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2578 info->new_local_var_chain = save_local_var_chain;
2579 info->static_chain_added |= save_static_chain_added;
2581 break;
2583 default:
2584 *handled_ops_p = false;
2585 return NULL_TREE;
2588 *handled_ops_p = true;
2589 return NULL_TREE;
2594 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2595 that reference nested functions to make sure that the static chain
2596 is set up properly for the call. */
2598 static tree
2599 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2600 struct walk_stmt_info *wi)
2602 struct nesting_info *const info = (struct nesting_info *) wi->info;
2603 tree decl, target_context;
2604 char save_static_chain_added;
2605 int i;
2606 gimple *stmt = gsi_stmt (*gsi);
2608 switch (gimple_code (stmt))
2610 case GIMPLE_CALL:
2611 if (gimple_call_chain (stmt))
2612 break;
2613 decl = gimple_call_fndecl (stmt);
2614 if (!decl)
2615 break;
2616 target_context = decl_function_context (decl);
2617 if (target_context && DECL_STATIC_CHAIN (decl))
2619 gimple_call_set_chain (as_a <gcall *> (stmt),
2620 get_static_chain (info, target_context,
2621 &wi->gsi));
2622 info->static_chain_added |= (1 << (info->context != target_context));
2624 break;
2626 case GIMPLE_OMP_PARALLEL:
2627 case GIMPLE_OMP_TASK:
2628 save_static_chain_added = info->static_chain_added;
2629 info->static_chain_added = 0;
2630 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2631 for (i = 0; i < 2; i++)
2633 tree c, decl;
2634 if ((info->static_chain_added & (1 << i)) == 0)
2635 continue;
2636 decl = i ? get_chain_decl (info) : info->frame_decl;
2637 /* Don't add CHAIN.* or FRAME.* twice. */
2638 for (c = gimple_omp_taskreg_clauses (stmt);
2640 c = OMP_CLAUSE_CHAIN (c))
2641 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2642 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2643 && OMP_CLAUSE_DECL (c) == decl)
2644 break;
2645 if (c == NULL)
2647 c = build_omp_clause (gimple_location (stmt),
2648 i ? OMP_CLAUSE_FIRSTPRIVATE
2649 : OMP_CLAUSE_SHARED);
2650 OMP_CLAUSE_DECL (c) = decl;
2651 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2652 gimple_omp_taskreg_set_clauses (stmt, c);
2655 info->static_chain_added |= save_static_chain_added;
2656 break;
2658 case GIMPLE_OMP_TARGET:
2659 if (!is_gimple_omp_offloaded (stmt))
2661 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2662 break;
2664 save_static_chain_added = info->static_chain_added;
2665 info->static_chain_added = 0;
2666 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2667 for (i = 0; i < 2; i++)
2669 tree c, decl;
2670 if ((info->static_chain_added & (1 << i)) == 0)
2671 continue;
2672 decl = i ? get_chain_decl (info) : info->frame_decl;
2673 /* Don't add CHAIN.* or FRAME.* twice. */
2674 for (c = gimple_omp_target_clauses (stmt);
2676 c = OMP_CLAUSE_CHAIN (c))
2677 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2678 && OMP_CLAUSE_DECL (c) == decl)
2679 break;
2680 if (c == NULL)
2682 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2683 OMP_CLAUSE_DECL (c) = decl;
2684 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2685 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2686 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2687 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2691 info->static_chain_added |= save_static_chain_added;
2692 break;
2694 case GIMPLE_OMP_FOR:
2695 walk_body (convert_gimple_call, NULL, info,
2696 gimple_omp_for_pre_body_ptr (stmt));
2697 /* FALLTHRU */
2698 case GIMPLE_OMP_SECTIONS:
2699 case GIMPLE_OMP_SECTION:
2700 case GIMPLE_OMP_SINGLE:
2701 case GIMPLE_OMP_TEAMS:
2702 case GIMPLE_OMP_MASTER:
2703 case GIMPLE_OMP_TASKGROUP:
2704 case GIMPLE_OMP_ORDERED:
2705 case GIMPLE_OMP_CRITICAL:
2706 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2707 break;
2709 default:
2710 /* Keep looking for other operands. */
2711 *handled_ops_p = false;
2712 return NULL_TREE;
2715 *handled_ops_p = true;
2716 return NULL_TREE;
2719 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2720 call expressions. At the same time, determine if a nested function
2721 actually uses its static chain; if not, remember that. */
2723 static void
2724 convert_all_function_calls (struct nesting_info *root)
2726 unsigned int chain_count = 0, old_chain_count, iter_count;
2727 struct nesting_info *n;
2729 /* First, optimistically clear static_chain for all decls that haven't
2730 used the static chain already for variable access. But always create
2731 it if not optimizing. This makes it possible to reconstruct the static
2732 nesting tree at run time and thus to resolve up-level references from
2733 within the debugger. */
2734 FOR_EACH_NEST_INFO (n, root)
2736 tree decl = n->context;
2737 if (!optimize)
2739 if (n->inner)
2740 (void) get_frame_type (n);
2741 if (n->outer)
2742 (void) get_chain_decl (n);
2744 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2746 DECL_STATIC_CHAIN (decl) = 0;
2747 if (dump_file && (dump_flags & TDF_DETAILS))
2748 fprintf (dump_file, "Guessing no static-chain for %s\n",
2749 lang_hooks.decl_printable_name (decl, 2));
2751 else
2752 DECL_STATIC_CHAIN (decl) = 1;
2753 chain_count += DECL_STATIC_CHAIN (decl);
2756 /* Walk the functions and perform transformations. Note that these
2757 transformations can induce new uses of the static chain, which in turn
2758 require re-examining all users of the decl. */
2759 /* ??? It would make sense to try to use the call graph to speed this up,
2760 but the call graph hasn't really been built yet. Even if it did, we
2761 would still need to iterate in this loop since address-of references
2762 wouldn't show up in the callgraph anyway. */
2763 iter_count = 0;
2766 old_chain_count = chain_count;
2767 chain_count = 0;
2768 iter_count++;
2770 if (dump_file && (dump_flags & TDF_DETAILS))
2771 fputc ('\n', dump_file);
2773 FOR_EACH_NEST_INFO (n, root)
2775 tree decl = n->context;
2776 walk_function (convert_tramp_reference_stmt,
2777 convert_tramp_reference_op, n);
2778 walk_function (convert_gimple_call, NULL, n);
2779 chain_count += DECL_STATIC_CHAIN (decl);
2782 while (chain_count != old_chain_count);
2784 if (dump_file && (dump_flags & TDF_DETAILS))
2785 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2786 iter_count);
2789 struct nesting_copy_body_data
2791 copy_body_data cb;
2792 struct nesting_info *root;
2795 /* A helper subroutine for debug_var_chain type remapping. */
2797 static tree
2798 nesting_copy_decl (tree decl, copy_body_data *id)
2800 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2801 tree *slot = nid->root->var_map->get (decl);
2803 if (slot)
2804 return (tree) *slot;
2806 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2808 tree new_decl = copy_decl_no_change (decl, id);
2809 DECL_ORIGINAL_TYPE (new_decl)
2810 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2811 return new_decl;
2814 if (VAR_P (decl)
2815 || TREE_CODE (decl) == PARM_DECL
2816 || TREE_CODE (decl) == RESULT_DECL)
2817 return decl;
2819 return copy_decl_no_change (decl, id);
2822 /* A helper function for remap_vla_decls. See if *TP contains
2823 some remapped variables. */
2825 static tree
2826 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2828 struct nesting_info *root = (struct nesting_info *) data;
2829 tree t = *tp;
2831 if (DECL_P (t))
2833 *walk_subtrees = 0;
2834 tree *slot = root->var_map->get (t);
2836 if (slot)
2837 return *slot;
2839 return NULL;
2842 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2843 involved. */
2845 static void
2846 remap_vla_decls (tree block, struct nesting_info *root)
2848 tree var, subblock, val, type;
2849 struct nesting_copy_body_data id;
2851 for (subblock = BLOCK_SUBBLOCKS (block);
2852 subblock;
2853 subblock = BLOCK_CHAIN (subblock))
2854 remap_vla_decls (subblock, root);
2856 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2857 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2859 val = DECL_VALUE_EXPR (var);
2860 type = TREE_TYPE (var);
2862 if (!(TREE_CODE (val) == INDIRECT_REF
2863 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2864 && variably_modified_type_p (type, NULL)))
2865 continue;
2867 if (root->var_map->get (TREE_OPERAND (val, 0))
2868 || walk_tree (&type, contains_remapped_vars, root, NULL))
2869 break;
2872 if (var == NULL_TREE)
2873 return;
2875 memset (&id, 0, sizeof (id));
2876 id.cb.copy_decl = nesting_copy_decl;
2877 id.cb.decl_map = new hash_map<tree, tree>;
2878 id.root = root;
2880 for (; var; var = DECL_CHAIN (var))
2881 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2883 struct nesting_info *i;
2884 tree newt, context;
2886 val = DECL_VALUE_EXPR (var);
2887 type = TREE_TYPE (var);
2889 if (!(TREE_CODE (val) == INDIRECT_REF
2890 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2891 && variably_modified_type_p (type, NULL)))
2892 continue;
2894 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2895 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2896 continue;
2898 context = decl_function_context (var);
2899 for (i = root; i; i = i->outer)
2900 if (i->context == context)
2901 break;
2903 if (i == NULL)
2904 continue;
2906 /* Fully expand value expressions. This avoids having debug variables
2907 only referenced from them and that can be swept during GC. */
2908 if (slot)
2910 tree t = (tree) *slot;
2911 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2912 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2915 id.cb.src_fn = i->context;
2916 id.cb.dst_fn = i->context;
2917 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2919 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2920 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2922 newt = TREE_TYPE (newt);
2923 type = TREE_TYPE (type);
2925 if (TYPE_NAME (newt)
2926 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2927 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2928 && newt != type
2929 && TYPE_NAME (newt) == TYPE_NAME (type))
2930 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2932 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2933 if (val != DECL_VALUE_EXPR (var))
2934 SET_DECL_VALUE_EXPR (var, val);
2937 delete id.cb.decl_map;
2940 /* Fold the MEM_REF *E. */
2941 bool
2942 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2944 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2945 *ref_p = fold (*ref_p);
2946 return true;
2949 /* Given DECL, a nested function, build an initialization call for FIELD,
2950 the trampoline or descriptor for DECL, using FUNC as the function. */
2952 static gcall *
2953 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
2954 tree func)
2956 tree arg1, arg2, arg3, x;
2958 gcc_assert (DECL_STATIC_CHAIN (decl));
2959 arg3 = build_addr (info->frame_decl);
2961 arg2 = build_addr (decl);
2963 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2964 info->frame_decl, field, NULL_TREE);
2965 arg1 = build_addr (x);
2967 return gimple_build_call (func, 3, arg1, arg2, arg3);
2970 /* Do "everything else" to clean up or complete state collected by the various
2971 walking passes -- create a field to hold the frame base address, lay out the
2972 types and decls, generate code to initialize the frame decl, store critical
2973 expressions in the struct function for rtl to find. */
2975 static void
2976 finalize_nesting_tree_1 (struct nesting_info *root)
2978 gimple_seq stmt_list;
2979 gimple *stmt;
2980 tree context = root->context;
2981 struct function *sf;
2983 stmt_list = NULL;
2985 /* If we created a non-local frame type or decl, we need to lay them
2986 out at this time. */
2987 if (root->frame_type)
2989 /* Debugging information needs to compute the frame base address of the
2990 parent frame out of the static chain from the nested frame.
2992 The static chain is the address of the FRAME record, so one could
2993 imagine it would be possible to compute the frame base address just
2994 adding a constant offset to this address. Unfortunately, this is not
2995 possible: if the FRAME object has alignment constraints that are
2996 stronger than the stack, then the offset between the frame base and
2997 the FRAME object will be dynamic.
2999 What we do instead is to append a field to the FRAME object that holds
3000 the frame base address: then debug info just has to fetch this
3001 field. */
3003 /* Debugging information will refer to the CFA as the frame base
3004 address: we will do the same here. */
3005 const tree frame_addr_fndecl
3006 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3008 /* Create a field in the FRAME record to hold the frame base address for
3009 this stack frame. Since it will be used only by the debugger, put it
3010 at the end of the record in order not to shift all other offsets. */
3011 tree fb_decl = make_node (FIELD_DECL);
3013 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3014 TREE_TYPE (fb_decl) = ptr_type_node;
3015 TREE_ADDRESSABLE (fb_decl) = 1;
3016 DECL_CONTEXT (fb_decl) = root->frame_type;
3017 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3018 fb_decl);
3020 /* In some cases the frame type will trigger the -Wpadded warning.
3021 This is not helpful; suppress it. */
3022 int save_warn_padded = warn_padded;
3023 warn_padded = 0;
3024 layout_type (root->frame_type);
3025 warn_padded = save_warn_padded;
3026 layout_decl (root->frame_decl, 0);
3028 /* Initialize the frame base address field. If the builtin we need is
3029 not available, set it to NULL so that debugging information does not
3030 reference junk. */
3031 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3032 root->frame_decl, fb_decl, NULL_TREE);
3033 tree fb_tmp;
3035 if (frame_addr_fndecl != NULL_TREE)
3037 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3038 integer_zero_node);
3039 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3041 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3043 else
3044 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3045 gimple_seq_add_stmt (&stmt_list,
3046 gimple_build_assign (fb_ref, fb_tmp));
3048 /* Remove root->frame_decl from root->new_local_var_chain, so
3049 that we can declare it also in the lexical blocks, which
3050 helps ensure virtual regs that end up appearing in its RTL
3051 expression get substituted in instantiate_virtual_regs(). */
3052 tree *adjust;
3053 for (adjust = &root->new_local_var_chain;
3054 *adjust != root->frame_decl;
3055 adjust = &DECL_CHAIN (*adjust))
3056 gcc_assert (DECL_CHAIN (*adjust));
3057 *adjust = DECL_CHAIN (*adjust);
3059 DECL_CHAIN (root->frame_decl) = NULL_TREE;
3060 declare_vars (root->frame_decl,
3061 gimple_seq_first_stmt (gimple_body (context)), true);
3064 /* If any parameters were referenced non-locally, then we need to
3065 insert a copy. Likewise, if any variables were referenced by
3066 pointer, we need to initialize the address. */
3067 if (root->any_parm_remapped)
3069 tree p;
3070 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3072 tree field, x, y;
3074 field = lookup_field_for_decl (root, p, NO_INSERT);
3075 if (!field)
3076 continue;
3078 if (use_pointer_in_frame (p))
3079 x = build_addr (p);
3080 else
3081 x = p;
3083 /* If the assignment is from a non-register the stmt is
3084 not valid gimple. Make it so by using a temporary instead. */
3085 if (!is_gimple_reg (x)
3086 && is_gimple_reg_type (TREE_TYPE (x)))
3088 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3089 x = init_tmp_var (root, x, &gsi);
3092 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3093 root->frame_decl, field, NULL_TREE);
3094 stmt = gimple_build_assign (y, x);
3095 gimple_seq_add_stmt (&stmt_list, stmt);
3099 /* If a chain_field was created, then it needs to be initialized
3100 from chain_decl. */
3101 if (root->chain_field)
3103 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3104 root->frame_decl, root->chain_field, NULL_TREE);
3105 stmt = gimple_build_assign (x, get_chain_decl (root));
3106 gimple_seq_add_stmt (&stmt_list, stmt);
3109 /* If trampolines were created, then we need to initialize them. */
3110 if (root->any_tramp_created)
3112 struct nesting_info *i;
3113 for (i = root->inner; i ; i = i->next)
3115 tree field, x;
3117 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3118 if (!field)
3119 continue;
3121 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3122 stmt = build_init_call_stmt (root, i->context, field, x);
3123 gimple_seq_add_stmt (&stmt_list, stmt);
3127 /* If descriptors were created, then we need to initialize them. */
3128 if (root->any_descr_created)
3130 struct nesting_info *i;
3131 for (i = root->inner; i ; i = i->next)
3133 tree field, x;
3135 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3136 if (!field)
3137 continue;
3139 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3140 stmt = build_init_call_stmt (root, i->context, field, x);
3141 gimple_seq_add_stmt (&stmt_list, stmt);
3145 /* If we created initialization statements, insert them. */
3146 if (stmt_list)
3148 gbind *bind;
3149 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3150 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3151 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3152 gimple_bind_set_body (bind, stmt_list);
3155 /* If a chain_decl was created, then it needs to be registered with
3156 struct function so that it gets initialized from the static chain
3157 register at the beginning of the function. */
3158 sf = DECL_STRUCT_FUNCTION (root->context);
3159 sf->static_chain_decl = root->chain_decl;
3161 /* Similarly for the non-local goto save area. */
3162 if (root->nl_goto_field)
3164 sf->nonlocal_goto_save_area
3165 = get_frame_field (root, context, root->nl_goto_field, NULL);
3166 sf->has_nonlocal_label = 1;
3169 /* Make sure all new local variables get inserted into the
3170 proper BIND_EXPR. */
3171 if (root->new_local_var_chain)
3172 declare_vars (root->new_local_var_chain,
3173 gimple_seq_first_stmt (gimple_body (root->context)),
3174 false);
3176 if (root->debug_var_chain)
3178 tree debug_var;
3179 gbind *scope;
3181 remap_vla_decls (DECL_INITIAL (root->context), root);
3183 for (debug_var = root->debug_var_chain; debug_var;
3184 debug_var = DECL_CHAIN (debug_var))
3185 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3186 break;
3188 /* If there are any debug decls with variable length types,
3189 remap those types using other debug_var_chain variables. */
3190 if (debug_var)
3192 struct nesting_copy_body_data id;
3194 memset (&id, 0, sizeof (id));
3195 id.cb.copy_decl = nesting_copy_decl;
3196 id.cb.decl_map = new hash_map<tree, tree>;
3197 id.root = root;
3199 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3200 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3202 tree type = TREE_TYPE (debug_var);
3203 tree newt, t = type;
3204 struct nesting_info *i;
3206 for (i = root; i; i = i->outer)
3207 if (variably_modified_type_p (type, i->context))
3208 break;
3210 if (i == NULL)
3211 continue;
3213 id.cb.src_fn = i->context;
3214 id.cb.dst_fn = i->context;
3215 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3217 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3218 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3220 newt = TREE_TYPE (newt);
3221 t = TREE_TYPE (t);
3223 if (TYPE_NAME (newt)
3224 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3225 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3226 && newt != t
3227 && TYPE_NAME (newt) == TYPE_NAME (t))
3228 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3231 delete id.cb.decl_map;
3234 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3235 if (gimple_bind_block (scope))
3236 declare_vars (root->debug_var_chain, scope, true);
3237 else
3238 BLOCK_VARS (DECL_INITIAL (root->context))
3239 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3240 root->debug_var_chain);
3243 /* Fold the rewritten MEM_REF trees. */
3244 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3246 /* Dump the translated tree function. */
3247 if (dump_file)
3249 fputs ("\n\n", dump_file);
3250 dump_function_to_file (root->context, dump_file, dump_flags);
3254 static void
3255 finalize_nesting_tree (struct nesting_info *root)
3257 struct nesting_info *n;
3258 FOR_EACH_NEST_INFO (n, root)
3259 finalize_nesting_tree_1 (n);
3262 /* Unnest the nodes and pass them to cgraph. */
3264 static void
3265 unnest_nesting_tree_1 (struct nesting_info *root)
3267 struct cgraph_node *node = cgraph_node::get (root->context);
3269 /* For nested functions update the cgraph to reflect unnesting.
3270 We also delay finalizing of these functions up to this point. */
3271 if (node->origin)
3273 node->unnest ();
3274 cgraph_node::finalize_function (root->context, true);
3278 static void
3279 unnest_nesting_tree (struct nesting_info *root)
3281 struct nesting_info *n;
3282 FOR_EACH_NEST_INFO (n, root)
3283 unnest_nesting_tree_1 (n);
3286 /* Free the data structures allocated during this pass. */
3288 static void
3289 free_nesting_tree (struct nesting_info *root)
3291 struct nesting_info *node, *next;
3293 node = iter_nestinfo_start (root);
3296 next = iter_nestinfo_next (node);
3297 delete node->var_map;
3298 delete node->field_map;
3299 delete node->mem_refs;
3300 free (node);
3301 node = next;
3303 while (node);
3306 /* Gimplify a function and all its nested functions. */
3307 static void
3308 gimplify_all_functions (struct cgraph_node *root)
3310 struct cgraph_node *iter;
3311 if (!gimple_body (root->decl))
3312 gimplify_function_tree (root->decl);
3313 for (iter = root->nested; iter; iter = iter->next_nested)
3314 gimplify_all_functions (iter);
3317 /* Main entry point for this pass. Process FNDECL and all of its nested
3318 subroutines and turn them into something less tightly bound. */
3320 void
3321 lower_nested_functions (tree fndecl)
3323 struct cgraph_node *cgn;
3324 struct nesting_info *root;
3326 /* If there are no nested functions, there's nothing to do. */
3327 cgn = cgraph_node::get (fndecl);
3328 if (!cgn->nested)
3329 return;
3331 gimplify_all_functions (cgn);
3333 dump_file = dump_begin (TDI_nested, &dump_flags);
3334 if (dump_file)
3335 fprintf (dump_file, "\n;; Function %s\n\n",
3336 lang_hooks.decl_printable_name (fndecl, 2));
3338 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3339 root = create_nesting_tree (cgn);
3341 walk_all_functions (convert_nonlocal_reference_stmt,
3342 convert_nonlocal_reference_op,
3343 root);
3344 walk_all_functions (convert_local_reference_stmt,
3345 convert_local_reference_op,
3346 root);
3347 walk_all_functions (convert_nl_goto_reference, NULL, root);
3348 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3350 convert_all_function_calls (root);
3351 finalize_nesting_tree (root);
3352 unnest_nesting_tree (root);
3354 free_nesting_tree (root);
3355 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3357 if (dump_file)
3359 dump_end (TDI_nested, dump_file);
3360 dump_file = NULL;
3364 #include "gt-tree-nested.h"