c++: Implement C++26 P2573R2 - = delete("should have a reason"); [PR114458]
[official-gcc.git] / gcc / tree-nested.cc
blob4e5f3be76767e53fe52e5b4abbec60865a1f893e
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2024 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 "dumpfile.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"
44 #include "diagnostic.h"
45 #include "alloc-pool.h"
46 #include "tree-nested.h"
47 #include "symbol-summary.h"
48 #include "symtab-thunks.h"
50 /* Summary of nested functions. */
51 static function_summary <nested_function_info *>
52 *nested_function_sum = NULL;
54 /* Return nested_function_info, if available. */
55 nested_function_info *
56 nested_function_info::get (cgraph_node *node)
58 if (!nested_function_sum)
59 return NULL;
60 return nested_function_sum->get (node);
63 /* Return nested_function_info possibly creating new one. */
64 nested_function_info *
65 nested_function_info::get_create (cgraph_node *node)
67 if (!nested_function_sum)
69 nested_function_sum = new function_summary <nested_function_info *>
70 (symtab);
71 nested_function_sum->disable_insertion_hook ();
73 return nested_function_sum->get_create (node);
76 /* cgraph_node is no longer nested function; update cgraph accordingly. */
77 void
78 unnest_function (cgraph_node *node)
80 nested_function_info *info = nested_function_info::get (node);
81 cgraph_node **node2 = &nested_function_info::get
82 (nested_function_origin (node))->nested;
84 gcc_checking_assert (info->origin);
85 while (*node2 != node)
86 node2 = &nested_function_info::get (*node2)->next_nested;
87 *node2 = info->next_nested;
88 info->next_nested = NULL;
89 info->origin = NULL;
90 nested_function_sum->remove (node);
93 /* Destructor: unlink function from nested function lists. */
94 nested_function_info::~nested_function_info ()
96 cgraph_node *next;
97 for (cgraph_node *n = nested; n; n = next)
99 nested_function_info *info = nested_function_info::get (n);
100 next = info->next_nested;
101 info->origin = NULL;
102 info->next_nested = NULL;
104 nested = NULL;
105 if (origin)
107 cgraph_node **node2
108 = &nested_function_info::get (origin)->nested;
110 nested_function_info *info;
111 while ((info = nested_function_info::get (*node2)) != this && info)
112 node2 = &info->next_nested;
113 *node2 = next_nested;
117 /* Free nested function info summaries. */
118 void
119 nested_function_info::release ()
121 if (nested_function_sum)
122 delete (nested_function_sum);
123 nested_function_sum = NULL;
126 /* If NODE is nested function, record it. */
127 void
128 maybe_record_nested_function (cgraph_node *node)
130 /* All nested functions gets lowered during the construction of symtab. */
131 if (symtab->state > CONSTRUCTION)
132 return;
133 if (DECL_CONTEXT (node->decl)
134 && TREE_CODE (DECL_CONTEXT (node->decl)) == FUNCTION_DECL)
136 cgraph_node *origin = cgraph_node::get_create (DECL_CONTEXT (node->decl));
137 nested_function_info *info = nested_function_info::get_create (node);
138 nested_function_info *origin_info
139 = nested_function_info::get_create (origin);
141 info->origin = origin;
142 info->next_nested = origin_info->nested;
143 origin_info->nested = node;
147 /* The object of this pass is to lower the representation of a set of nested
148 functions in order to expose all of the gory details of the various
149 nonlocal references. We want to do this sooner rather than later, in
150 order to give us more freedom in emitting all of the functions in question.
152 Back in olden times, when gcc was young, we developed an insanely
153 complicated scheme whereby variables which were referenced nonlocally
154 were forced to live in the stack of the declaring function, and then
155 the nested functions magically discovered where these variables were
156 placed. In order for this scheme to function properly, it required
157 that the outer function be partially expanded, then we switch to
158 compiling the inner function, and once done with those we switch back
159 to compiling the outer function. Such delicate ordering requirements
160 makes it difficult to do whole translation unit optimizations
161 involving such functions.
163 The implementation here is much more direct. Everything that can be
164 referenced by an inner function is a member of an explicitly created
165 structure herein called the "nonlocal frame struct". The incoming
166 static chain for a nested function is a pointer to this struct in
167 the parent. In this way, we settle on known offsets from a known
168 base, and so are decoupled from the logic that places objects in the
169 function's stack frame. More importantly, we don't have to wait for
170 that to happen -- since the compilation of the inner function is no
171 longer tied to a real stack frame, the nonlocal frame struct can be
172 allocated anywhere. Which means that the outer function is now
173 inlinable.
175 Theory of operation here is very simple. Iterate over all the
176 statements in all the functions (depth first) several times,
177 allocating structures and fields on demand. In general we want to
178 examine inner functions first, so that we can avoid making changes
179 to outer functions which are unnecessary.
181 The order of the passes matters a bit, in that later passes will be
182 skipped if it is discovered that the functions don't actually interact
183 at all. That is, they're nested in the lexical sense but could have
184 been written as independent functions without change. */
187 struct nesting_info
189 struct nesting_info *outer;
190 struct nesting_info *inner;
191 struct nesting_info *next;
193 hash_map<tree, tree> *field_map;
194 hash_map<tree, tree> *var_map;
195 hash_set<tree *> *mem_refs;
196 bitmap suppress_expansion;
198 tree context;
199 tree new_local_var_chain;
200 tree debug_var_chain;
201 tree frame_type;
202 tree frame_decl;
203 tree chain_field;
204 tree chain_decl;
205 tree nl_goto_field;
207 bool thunk_p;
208 bool any_parm_remapped;
209 bool any_tramp_created;
210 bool any_descr_created;
211 char static_chain_added;
215 /* Iterate over the nesting tree, starting with ROOT, depth first. */
217 static inline struct nesting_info *
218 iter_nestinfo_start (struct nesting_info *root)
220 while (root->inner)
221 root = root->inner;
222 return root;
225 static inline struct nesting_info *
226 iter_nestinfo_next (struct nesting_info *node)
228 if (node->next)
229 return iter_nestinfo_start (node->next);
230 return node->outer;
233 #define FOR_EACH_NEST_INFO(I, ROOT) \
234 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
236 /* Obstack used for the bitmaps in the struct above. */
237 static struct bitmap_obstack nesting_info_bitmap_obstack;
240 /* We're working in so many different function contexts simultaneously,
241 that create_tmp_var is dangerous. Prevent mishap. */
242 #define create_tmp_var cant_use_create_tmp_var_here_dummy
244 /* Like create_tmp_var, except record the variable for registration at
245 the given nesting level. */
247 static tree
248 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
250 tree tmp_var;
252 /* If the type is of variable size or a type which must be created by the
253 frontend, something is wrong. Note that we explicitly allow
254 incomplete types here, since we create them ourselves here. */
255 gcc_assert (!TREE_ADDRESSABLE (type));
256 gcc_assert (!TYPE_SIZE_UNIT (type)
257 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
259 tmp_var = create_tmp_var_raw (type, prefix);
260 DECL_CONTEXT (tmp_var) = info->context;
261 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
262 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
264 info->new_local_var_chain = tmp_var;
266 return tmp_var;
269 /* Like build_simple_mem_ref, but set TREE_THIS_NOTRAP on the result. */
271 static tree
272 build_simple_mem_ref_notrap (tree ptr)
274 tree t = build_simple_mem_ref (ptr);
275 TREE_THIS_NOTRAP (t) = 1;
276 return t;
279 /* Take the address of EXP to be used within function CONTEXT.
280 Mark it for addressability as necessary. */
282 tree
283 build_addr (tree exp)
285 mark_addressable (exp);
286 return build_fold_addr_expr (exp);
289 /* Insert FIELD into TYPE, sorted by alignment requirements. */
291 void
292 insert_field_into_struct (tree type, tree field)
294 tree *p;
296 DECL_CONTEXT (field) = type;
298 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
299 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
300 break;
302 DECL_CHAIN (field) = *p;
303 *p = field;
305 /* Set correct alignment for frame struct type. */
306 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
307 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
310 /* Build or return the RECORD_TYPE that describes the frame state that is
311 shared between INFO->CONTEXT and its nested functions. This record will
312 not be complete until finalize_nesting_tree; up until that point we'll
313 be adding fields as necessary.
315 We also build the DECL that represents this frame in the function. */
317 static tree
318 get_frame_type (struct nesting_info *info)
320 tree type = info->frame_type;
321 if (!type)
323 char *name;
325 type = make_node (RECORD_TYPE);
327 name = concat ("FRAME.",
328 IDENTIFIER_POINTER (DECL_NAME (info->context)),
329 NULL);
330 TYPE_NAME (type) = get_identifier (name);
331 free (name);
333 info->frame_type = type;
335 /* Do not put info->frame_decl on info->new_local_var_chain,
336 so that we can declare it in the lexical blocks, which
337 makes sure virtual regs that end up appearing in its RTL
338 expression get substituted in instantiate_virtual_regs. */
339 info->frame_decl = create_tmp_var_raw (type, "FRAME");
340 DECL_CONTEXT (info->frame_decl) = info->context;
341 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
342 DECL_SEEN_IN_BIND_EXPR_P (info->frame_decl) = 1;
344 /* ??? Always make it addressable for now, since it is meant to
345 be pointed to by the static chain pointer. This pessimizes
346 when it turns out that no static chains are needed because
347 the nested functions referencing non-local variables are not
348 reachable, but the true pessimization is to create the non-
349 local frame structure in the first place. */
350 TREE_ADDRESSABLE (info->frame_decl) = 1;
353 return type;
356 /* Return true if DECL should be referenced by pointer in the non-local frame
357 structure. */
359 static bool
360 use_pointer_in_frame (tree decl)
362 if (TREE_CODE (decl) == PARM_DECL)
364 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
365 sized DECLs, and inefficient to copy large aggregates. Don't bother
366 moving anything but scalar parameters. */
367 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
369 else
371 /* Variable-sized DECLs can only come from OMP clauses at this point
372 since the gimplifier has already turned the regular variables into
373 pointers. Do the same as the gimplifier. */
374 return !DECL_SIZE (decl) || TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST;
378 /* Given DECL, a non-locally accessed variable, find or create a field
379 in the non-local frame structure for the given nesting context. */
381 static tree
382 lookup_field_for_decl (struct nesting_info *info, tree decl,
383 enum insert_option insert)
385 gcc_checking_assert (decl_function_context (decl) == info->context);
387 if (insert == NO_INSERT)
389 tree *slot = info->field_map->get (decl);
390 return slot ? *slot : NULL_TREE;
393 tree *slot = &info->field_map->get_or_insert (decl);
394 if (!*slot)
396 tree type = get_frame_type (info);
397 tree field = make_node (FIELD_DECL);
398 DECL_NAME (field) = DECL_NAME (decl);
400 if (use_pointer_in_frame (decl))
402 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
403 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
404 DECL_NONADDRESSABLE_P (field) = 1;
406 else
408 TREE_TYPE (field) = TREE_TYPE (decl);
409 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
410 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
411 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
412 DECL_IGNORED_P (field) = DECL_IGNORED_P (decl);
413 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
414 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
415 copy_warning (field, decl);
417 /* Declare the transformation and adjust the original DECL. For a
418 variable or for a parameter when not optimizing, we make it point
419 to the field in the frame directly. For a parameter, we don't do
420 it when optimizing because the variable tracking pass will already
421 do the job, */
422 if (VAR_P (decl) || !optimize)
424 tree x
425 = build3 (COMPONENT_REF, TREE_TYPE (field), info->frame_decl,
426 field, NULL_TREE);
428 /* If the next declaration is a PARM_DECL pointing to the DECL,
429 we need to adjust its VALUE_EXPR directly, since chains of
430 VALUE_EXPRs run afoul of garbage collection. This occurs
431 in Ada for Out parameters that aren't copied in. */
432 tree next = DECL_CHAIN (decl);
433 if (next
434 && TREE_CODE (next) == PARM_DECL
435 && DECL_HAS_VALUE_EXPR_P (next)
436 && DECL_VALUE_EXPR (next) == decl)
437 SET_DECL_VALUE_EXPR (next, x);
439 SET_DECL_VALUE_EXPR (decl, x);
440 DECL_HAS_VALUE_EXPR_P (decl) = 1;
444 insert_field_into_struct (type, field);
445 *slot = field;
447 if (TREE_CODE (decl) == PARM_DECL)
448 info->any_parm_remapped = true;
451 return *slot;
454 /* Build or return the variable that holds the static chain within
455 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
457 static tree
458 get_chain_decl (struct nesting_info *info)
460 tree decl = info->chain_decl;
462 if (!decl)
464 tree type;
466 type = get_frame_type (info->outer);
467 type = build_pointer_type (type);
469 /* Note that this variable is *not* entered into any BIND_EXPR;
470 the construction of this variable is handled specially in
471 expand_function_start and initialize_inlined_parameters.
472 Note also that it's represented as a parameter. This is more
473 close to the truth, since the initial value does come from
474 the caller. */
475 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
476 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
477 DECL_ARTIFICIAL (decl) = 1;
478 DECL_IGNORED_P (decl) = 1;
479 TREE_USED (decl) = 1;
480 DECL_CONTEXT (decl) = info->context;
481 DECL_ARG_TYPE (decl) = type;
483 /* Tell tree-inline.cc that we never write to this variable, so
484 it can copy-prop the replacement value immediately. */
485 TREE_READONLY (decl) = 1;
487 info->chain_decl = decl;
489 if (dump_file
490 && (dump_flags & TDF_DETAILS)
491 && !DECL_STATIC_CHAIN (info->context))
492 fprintf (dump_file, "Setting static-chain for %s\n",
493 lang_hooks.decl_printable_name (info->context, 2));
495 DECL_STATIC_CHAIN (info->context) = 1;
497 return decl;
500 /* Build or return the field within the non-local frame state that holds
501 the static chain for INFO->CONTEXT. This is the way to walk back up
502 multiple nesting levels. */
504 static tree
505 get_chain_field (struct nesting_info *info)
507 tree field = info->chain_field;
509 if (!field)
511 tree type = build_pointer_type (get_frame_type (info->outer));
513 field = make_node (FIELD_DECL);
514 DECL_NAME (field) = get_identifier ("__chain");
515 TREE_TYPE (field) = type;
516 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
517 DECL_NONADDRESSABLE_P (field) = 1;
519 insert_field_into_struct (get_frame_type (info), field);
521 info->chain_field = field;
523 if (dump_file
524 && (dump_flags & TDF_DETAILS)
525 && !DECL_STATIC_CHAIN (info->context))
526 fprintf (dump_file, "Setting static-chain for %s\n",
527 lang_hooks.decl_printable_name (info->context, 2));
529 DECL_STATIC_CHAIN (info->context) = 1;
531 return field;
534 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
536 static tree
537 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
538 gcall *call)
540 tree t;
542 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
543 gimple_call_set_lhs (call, t);
544 if (! gsi_end_p (*gsi))
545 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
546 gsi_insert_before (gsi, call, GSI_SAME_STMT);
548 return t;
552 /* Copy EXP into a temporary. Allocate the temporary in the context of
553 INFO and insert the initialization statement before GSI. */
555 static tree
556 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
558 tree t;
559 gimple *stmt;
561 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
562 stmt = gimple_build_assign (t, exp);
563 if (! gsi_end_p (*gsi))
564 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
565 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
567 return t;
571 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
573 static tree
574 gsi_gimplify_val (struct nesting_info *info, tree exp,
575 gimple_stmt_iterator *gsi)
577 if (is_gimple_val (exp))
578 return exp;
579 else
580 return init_tmp_var (info, exp, gsi);
583 /* Similarly, but copy from the temporary and insert the statement
584 after the iterator. */
586 static tree
587 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
589 tree t;
590 gimple *stmt;
592 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
593 stmt = gimple_build_assign (exp, t);
594 if (! gsi_end_p (*gsi))
595 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
596 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
598 return t;
601 /* Build or return the type used to represent a nested function trampoline. */
603 static GTY(()) tree trampoline_type;
605 static tree
606 get_trampoline_type (struct nesting_info *info)
608 unsigned align, size;
609 tree t;
611 if (trampoline_type)
612 return trampoline_type;
614 /* When trampolines are created off-stack then the only thing we need in the
615 local frame is a single pointer. */
616 if (flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
618 trampoline_type = build_pointer_type (void_type_node);
619 return trampoline_type;
622 align = TRAMPOLINE_ALIGNMENT;
623 size = TRAMPOLINE_SIZE;
625 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
626 then allocate extra space so that we can do dynamic alignment. */
627 if (align > STACK_BOUNDARY)
629 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
630 align = STACK_BOUNDARY;
633 t = build_index_type (size_int (size - 1));
634 t = build_array_type (char_type_node, t);
635 t = build_decl (DECL_SOURCE_LOCATION (info->context),
636 FIELD_DECL, get_identifier ("__data"), t);
637 SET_DECL_ALIGN (t, align);
638 DECL_USER_ALIGN (t) = 1;
640 trampoline_type = make_node (RECORD_TYPE);
641 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
642 TYPE_FIELDS (trampoline_type) = t;
643 layout_type (trampoline_type);
644 DECL_CONTEXT (t) = trampoline_type;
646 return trampoline_type;
649 /* Build or return the type used to represent a nested function descriptor. */
651 static GTY(()) tree descriptor_type;
653 static tree
654 get_descriptor_type (struct nesting_info *info)
656 /* The base alignment is that of a function. */
657 const unsigned align = FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY);
658 tree t;
660 if (descriptor_type)
661 return descriptor_type;
663 t = build_index_type (integer_one_node);
664 t = build_array_type (ptr_type_node, t);
665 t = build_decl (DECL_SOURCE_LOCATION (info->context),
666 FIELD_DECL, get_identifier ("__data"), t);
667 SET_DECL_ALIGN (t, MAX (TYPE_ALIGN (ptr_type_node), align));
668 DECL_USER_ALIGN (t) = 1;
670 descriptor_type = make_node (RECORD_TYPE);
671 TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
672 TYPE_FIELDS (descriptor_type) = t;
673 layout_type (descriptor_type);
674 DECL_CONTEXT (t) = descriptor_type;
676 return descriptor_type;
679 /* Given DECL, a nested function, find or create an element in the
680 var map for this function. */
682 static tree
683 lookup_element_for_decl (struct nesting_info *info, tree decl,
684 enum insert_option insert)
686 if (insert == NO_INSERT)
688 tree *slot = info->var_map->get (decl);
689 return slot ? *slot : NULL_TREE;
692 tree *slot = &info->var_map->get_or_insert (decl);
693 if (!*slot)
694 *slot = build_tree_list (NULL_TREE, NULL_TREE);
696 return (tree) *slot;
699 /* Given DECL, a nested function, create a field in the non-local
700 frame structure for this function. */
702 static tree
703 create_field_for_decl (struct nesting_info *info, tree decl, tree type)
705 tree field = make_node (FIELD_DECL);
706 DECL_NAME (field) = DECL_NAME (decl);
707 TREE_TYPE (field) = type;
708 TREE_ADDRESSABLE (field) = 1;
709 insert_field_into_struct (get_frame_type (info), field);
710 return field;
713 /* Given DECL, a nested function, find or create a field in the non-local
714 frame structure for a trampoline for this function. */
716 static tree
717 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
718 enum insert_option insert)
720 tree elt, field;
722 elt = lookup_element_for_decl (info, decl, insert);
723 if (!elt)
724 return NULL_TREE;
726 field = TREE_PURPOSE (elt);
728 if (!field && insert == INSERT)
730 field = create_field_for_decl (info, decl, get_trampoline_type (info));
731 TREE_PURPOSE (elt) = field;
732 info->any_tramp_created = true;
735 return field;
738 /* Given DECL, a nested function, find or create a field in the non-local
739 frame structure for a descriptor for this function. */
741 static tree
742 lookup_descr_for_decl (struct nesting_info *info, tree decl,
743 enum insert_option insert)
745 tree elt, field;
747 elt = lookup_element_for_decl (info, decl, insert);
748 if (!elt)
749 return NULL_TREE;
751 field = TREE_VALUE (elt);
753 if (!field && insert == INSERT)
755 field = create_field_for_decl (info, decl, get_descriptor_type (info));
756 TREE_VALUE (elt) = field;
757 info->any_descr_created = true;
760 return field;
763 /* Build or return the field within the non-local frame state that holds
764 the non-local goto "jmp_buf". The buffer itself is maintained by the
765 rtl middle-end as dynamic stack space is allocated. */
767 static tree
768 get_nl_goto_field (struct nesting_info *info)
770 tree field = info->nl_goto_field;
771 if (!field)
773 unsigned size;
774 tree type;
776 /* For __builtin_nonlocal_goto, we need N words. The first is the
777 frame pointer, the rest is for the target's stack pointer save
778 area. The number of words is controlled by STACK_SAVEAREA_MODE;
779 not the best interface, but it'll do for now. */
780 if (Pmode == ptr_mode)
781 type = ptr_type_node;
782 else
783 type = lang_hooks.types.type_for_mode (Pmode, 1);
785 scalar_int_mode mode
786 = as_a <scalar_int_mode> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
787 size = GET_MODE_SIZE (mode);
788 size = size / GET_MODE_SIZE (Pmode);
789 size = size + 1;
791 type = build_array_type
792 (type, build_index_type (size_int (size)));
794 field = make_node (FIELD_DECL);
795 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
796 TREE_TYPE (field) = type;
797 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
798 TREE_ADDRESSABLE (field) = 1;
800 insert_field_into_struct (get_frame_type (info), field);
802 info->nl_goto_field = field;
805 return field;
808 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
810 static void
811 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
812 struct nesting_info *info, gimple_seq *pseq)
814 struct walk_stmt_info wi;
816 memset (&wi, 0, sizeof (wi));
817 wi.info = info;
818 wi.val_only = true;
819 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
823 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
825 static inline void
826 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
827 struct nesting_info *info)
829 gimple_seq body = gimple_body (info->context);
830 walk_body (callback_stmt, callback_op, info, &body);
831 gimple_set_body (info->context, body);
834 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
836 static void
837 walk_gimple_omp_for (gomp_for *for_stmt,
838 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
839 struct nesting_info *info)
841 struct walk_stmt_info wi;
842 gimple_seq seq;
843 tree t;
844 size_t i;
846 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
848 seq = NULL;
849 memset (&wi, 0, sizeof (wi));
850 wi.info = info;
851 wi.gsi = gsi_last (seq);
853 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
855 wi.val_only = false;
856 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
857 &wi, NULL);
858 wi.val_only = true;
859 wi.is_lhs = false;
860 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
861 &wi, NULL);
863 wi.val_only = true;
864 wi.is_lhs = false;
865 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
866 &wi, NULL);
868 t = gimple_omp_for_incr (for_stmt, i);
869 gcc_assert (BINARY_CLASS_P (t));
870 wi.val_only = false;
871 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
872 wi.val_only = true;
873 wi.is_lhs = false;
874 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
877 seq = gsi_seq (wi.gsi);
878 if (!gimple_seq_empty_p (seq))
880 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
881 annotate_all_with_location (seq, gimple_location (for_stmt));
882 gimple_seq_add_seq (&pre_body, seq);
883 gimple_omp_for_set_pre_body (for_stmt, pre_body);
887 /* Similarly for ROOT and all functions nested underneath, depth first. */
889 static void
890 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
891 struct nesting_info *root)
893 struct nesting_info *n;
894 FOR_EACH_NEST_INFO (n, root)
895 walk_function (callback_stmt, callback_op, n);
899 /* We have to check for a fairly pathological case. The operands of function
900 nested function are to be interpreted in the context of the enclosing
901 function. So if any are variably-sized, they will get remapped when the
902 enclosing function is inlined. But that remapping would also have to be
903 done in the types of the PARM_DECLs of the nested function, meaning the
904 argument types of that function will disagree with the arguments in the
905 calls to that function. So we'd either have to make a copy of the nested
906 function corresponding to each time the enclosing function was inlined or
907 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
908 function. The former is not practical. The latter would still require
909 detecting this case to know when to add the conversions. So, for now at
910 least, we don't inline such an enclosing function.
912 We have to do that check recursively, so here return indicating whether
913 FNDECL has such a nested function. ORIG_FN is the function we were
914 trying to inline to use for checking whether any argument is variably
915 modified by anything in it.
917 It would be better to do this in tree-inline.cc so that we could give
918 the appropriate warning for why a function can't be inlined, but that's
919 too late since the nesting structure has already been flattened and
920 adding a flag just to record this fact seems a waste of a flag. */
922 static bool
923 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
925 struct cgraph_node *cgn = cgraph_node::get (fndecl);
926 tree arg;
928 for (cgn = first_nested_function (cgn); cgn;
929 cgn = next_nested_function (cgn))
931 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
932 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
933 return true;
935 if (check_for_nested_with_variably_modified (cgn->decl,
936 orig_fndecl))
937 return true;
940 return false;
943 /* Construct our local datastructure describing the function nesting
944 tree rooted by CGN. */
946 static struct nesting_info *
947 create_nesting_tree (struct cgraph_node *cgn)
949 struct nesting_info *info = XCNEW (struct nesting_info);
950 info->field_map = new hash_map<tree, tree>;
951 info->var_map = new hash_map<tree, tree>;
952 info->mem_refs = new hash_set<tree *>;
953 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
954 info->context = cgn->decl;
955 info->thunk_p = cgn->thunk;
957 for (cgn = first_nested_function (cgn); cgn;
958 cgn = next_nested_function (cgn))
960 struct nesting_info *sub = create_nesting_tree (cgn);
961 sub->outer = info;
962 sub->next = info->inner;
963 info->inner = sub;
966 /* See discussion at check_for_nested_with_variably_modified for a
967 discussion of why this has to be here. */
968 if (check_for_nested_with_variably_modified (info->context, info->context))
969 DECL_UNINLINABLE (info->context) = true;
971 return info;
974 /* Return an expression computing the static chain for TARGET_CONTEXT
975 from INFO->CONTEXT. Insert any necessary computations before TSI. */
977 static tree
978 get_static_chain (struct nesting_info *info, tree target_context,
979 gimple_stmt_iterator *gsi)
981 struct nesting_info *i;
982 tree x;
984 if (info->context == target_context)
986 x = build_addr (info->frame_decl);
987 info->static_chain_added |= 1;
989 else
991 x = get_chain_decl (info);
992 info->static_chain_added |= 2;
994 for (i = info->outer; i->context != target_context; i = i->outer)
996 tree field = get_chain_field (i);
998 x = build_simple_mem_ref_notrap (x);
999 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1000 x = init_tmp_var (info, x, gsi);
1004 return x;
1008 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
1009 frame as seen from INFO->CONTEXT. Insert any necessary computations
1010 before GSI. */
1012 static tree
1013 get_frame_field (struct nesting_info *info, tree target_context,
1014 tree field, gimple_stmt_iterator *gsi)
1016 struct nesting_info *i;
1017 tree x;
1019 if (info->context == target_context)
1021 /* Make sure frame_decl gets created. */
1022 (void) get_frame_type (info);
1023 x = info->frame_decl;
1024 info->static_chain_added |= 1;
1026 else
1028 x = get_chain_decl (info);
1029 info->static_chain_added |= 2;
1031 for (i = info->outer; i->context != target_context; i = i->outer)
1033 tree field = get_chain_field (i);
1035 x = build_simple_mem_ref_notrap (x);
1036 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1037 x = init_tmp_var (info, x, gsi);
1040 x = build_simple_mem_ref_notrap (x);
1043 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1044 TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (field);
1045 return x;
1048 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
1050 /* Helper for get_nonlocal_debug_decl and get_local_debug_decl. */
1052 static tree
1053 get_debug_decl (tree decl)
1055 tree new_decl
1056 = build_decl (DECL_SOURCE_LOCATION (decl),
1057 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1058 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1059 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1060 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1061 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1062 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1063 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1064 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1065 if ((TREE_CODE (decl) == PARM_DECL
1066 || TREE_CODE (decl) == RESULT_DECL
1067 || VAR_P (decl))
1068 && DECL_BY_REFERENCE (decl))
1069 DECL_BY_REFERENCE (new_decl) = 1;
1070 /* Copy DECL_LANG_SPECIFIC and DECL_LANG_FLAG_* for OpenMP langhook
1071 purposes. */
1072 DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (decl);
1073 #define COPY_DLF(n) DECL_LANG_FLAG_##n (new_decl) = DECL_LANG_FLAG_##n (decl)
1074 COPY_DLF (0); COPY_DLF (1); COPY_DLF (2); COPY_DLF (3);
1075 COPY_DLF (4); COPY_DLF (5); COPY_DLF (6); COPY_DLF (7);
1076 COPY_DLF (8);
1077 #undef COPY_DLF
1078 return new_decl;
1081 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
1082 in the nested function with DECL_VALUE_EXPR set to reference the true
1083 variable in the parent function. This is used both for debug info
1084 and in OMP lowering. */
1086 static tree
1087 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
1089 tree target_context;
1090 struct nesting_info *i;
1091 tree x, field, new_decl;
1093 tree *slot = &info->var_map->get_or_insert (decl);
1095 if (*slot)
1096 return *slot;
1098 target_context = decl_function_context (decl);
1100 /* A copy of the code in get_frame_field, but without the temporaries. */
1101 if (info->context == target_context)
1103 /* Make sure frame_decl gets created. */
1104 (void) get_frame_type (info);
1105 x = info->frame_decl;
1106 i = info;
1107 info->static_chain_added |= 1;
1109 else
1111 x = get_chain_decl (info);
1112 info->static_chain_added |= 2;
1113 for (i = info->outer; i->context != target_context; i = i->outer)
1115 field = get_chain_field (i);
1116 x = build_simple_mem_ref_notrap (x);
1117 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1119 x = build_simple_mem_ref_notrap (x);
1122 field = lookup_field_for_decl (i, decl, INSERT);
1123 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1124 if (use_pointer_in_frame (decl))
1125 x = build_simple_mem_ref_notrap (x);
1127 /* ??? We should be remapping types as well, surely. */
1128 new_decl = get_debug_decl (decl);
1129 DECL_CONTEXT (new_decl) = info->context;
1131 SET_DECL_VALUE_EXPR (new_decl, x);
1132 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1134 *slot = new_decl;
1135 DECL_CHAIN (new_decl) = info->debug_var_chain;
1136 info->debug_var_chain = new_decl;
1138 if (!optimize
1139 && info->context != target_context
1140 && variably_modified_type_p (TREE_TYPE (decl), NULL))
1141 note_nonlocal_vla_type (info, TREE_TYPE (decl));
1143 return new_decl;
1147 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1148 and PARM_DECLs that belong to outer functions.
1150 The rewrite will involve some number of structure accesses back up
1151 the static chain. E.g. for a variable FOO up one nesting level it'll
1152 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1153 indirections apply to decls for which use_pointer_in_frame is true. */
1155 static tree
1156 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
1158 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1159 struct nesting_info *const info = (struct nesting_info *) wi->info;
1160 tree t = *tp;
1162 *walk_subtrees = 0;
1163 switch (TREE_CODE (t))
1165 case VAR_DECL:
1166 /* Non-automatic variables are never processed. */
1167 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1168 break;
1169 /* FALLTHRU */
1171 case PARM_DECL:
1173 tree x, target_context = decl_function_context (t);
1175 if (info->context == target_context)
1176 break;
1178 wi->changed = true;
1180 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1181 x = get_nonlocal_debug_decl (info, t);
1182 else
1184 struct nesting_info *i = info;
1185 while (i && i->context != target_context)
1186 i = i->outer;
1187 /* If none of the outer contexts is the target context, this means
1188 that the VAR or PARM_DECL is referenced in a wrong context. */
1189 if (!i)
1190 internal_error ("%s from %s referenced in %s",
1191 IDENTIFIER_POINTER (DECL_NAME (t)),
1192 IDENTIFIER_POINTER (DECL_NAME (target_context)),
1193 IDENTIFIER_POINTER (DECL_NAME (info->context)));
1195 x = lookup_field_for_decl (i, t, INSERT);
1196 x = get_frame_field (info, target_context, x, &wi->gsi);
1197 if (use_pointer_in_frame (t))
1199 x = init_tmp_var (info, x, &wi->gsi);
1200 x = build_simple_mem_ref_notrap (x);
1204 if (wi->val_only)
1206 if (wi->is_lhs)
1207 x = save_tmp_var (info, x, &wi->gsi);
1208 else
1209 x = init_tmp_var (info, x, &wi->gsi);
1212 *tp = x;
1214 break;
1216 case LABEL_DECL:
1217 /* We're taking the address of a label from a parent function, but
1218 this is not itself a non-local goto. Mark the label such that it
1219 will not be deleted, much as we would with a label address in
1220 static storage. */
1221 if (decl_function_context (t) != info->context)
1222 FORCED_LABEL (t) = 1;
1223 break;
1225 case ADDR_EXPR:
1227 bool save_val_only = wi->val_only;
1229 wi->val_only = false;
1230 wi->is_lhs = false;
1231 wi->changed = false;
1232 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1233 wi->val_only = true;
1235 if (wi->changed)
1237 tree save_context;
1239 /* If we changed anything, we might no longer be directly
1240 referencing a decl. */
1241 save_context = current_function_decl;
1242 current_function_decl = info->context;
1243 recompute_tree_invariant_for_addr_expr (t);
1245 /* If the callback converted the address argument in a context
1246 where we only accept variables (and min_invariant, presumably),
1247 then compute the address into a temporary. */
1248 if (save_val_only)
1249 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1250 t, &wi->gsi);
1251 current_function_decl = save_context;
1254 break;
1256 case REALPART_EXPR:
1257 case IMAGPART_EXPR:
1258 case COMPONENT_REF:
1259 case ARRAY_REF:
1260 case ARRAY_RANGE_REF:
1261 case BIT_FIELD_REF:
1262 /* Go down this entire nest and just look at the final prefix and
1263 anything that describes the references. Otherwise, we lose track
1264 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1265 wi->val_only = true;
1266 wi->is_lhs = false;
1267 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1269 if (TREE_CODE (t) == COMPONENT_REF)
1270 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1271 NULL);
1272 else if (TREE_CODE (t) == ARRAY_REF
1273 || TREE_CODE (t) == ARRAY_RANGE_REF)
1275 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1276 wi, NULL);
1277 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1278 wi, NULL);
1279 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1280 wi, NULL);
1283 wi->val_only = false;
1284 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1285 break;
1287 case VIEW_CONVERT_EXPR:
1288 /* Just request to look at the subtrees, leaving val_only and lhs
1289 untouched. This might actually be for !val_only + lhs, in which
1290 case we don't want to force a replacement by a temporary. */
1291 *walk_subtrees = 1;
1292 break;
1294 default:
1295 if (!IS_TYPE_OR_DECL_P (t))
1297 *walk_subtrees = 1;
1298 wi->val_only = true;
1299 wi->is_lhs = false;
1301 break;
1304 return NULL_TREE;
1307 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1308 struct walk_stmt_info *);
1310 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1311 and PARM_DECLs that belong to outer functions. */
1313 static bool
1314 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1316 struct nesting_info *const info = (struct nesting_info *) wi->info;
1317 bool need_chain = false, need_stmts = false;
1318 tree clause, decl, *pdecl;
1319 int dummy;
1320 bitmap new_suppress;
1322 new_suppress = BITMAP_GGC_ALLOC ();
1323 bitmap_copy (new_suppress, info->suppress_expansion);
1325 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1327 pdecl = NULL;
1328 switch (OMP_CLAUSE_CODE (clause))
1330 case OMP_CLAUSE_REDUCTION:
1331 case OMP_CLAUSE_IN_REDUCTION:
1332 case OMP_CLAUSE_TASK_REDUCTION:
1333 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1334 need_stmts = true;
1335 if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
1337 pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
1338 if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
1339 pdecl = &TREE_OPERAND (*pdecl, 0);
1340 if (INDIRECT_REF_P (*pdecl)
1341 || TREE_CODE (*pdecl) == ADDR_EXPR)
1342 pdecl = &TREE_OPERAND (*pdecl, 0);
1344 goto do_decl_clause;
1346 case OMP_CLAUSE_LASTPRIVATE:
1347 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1348 need_stmts = true;
1349 goto do_decl_clause;
1351 case OMP_CLAUSE_LINEAR:
1352 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1353 need_stmts = true;
1354 wi->val_only = true;
1355 wi->is_lhs = false;
1356 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1357 &dummy, wi);
1358 goto do_decl_clause;
1360 case OMP_CLAUSE_PRIVATE:
1361 case OMP_CLAUSE_FIRSTPRIVATE:
1362 case OMP_CLAUSE_COPYPRIVATE:
1363 case OMP_CLAUSE_SHARED:
1364 case OMP_CLAUSE_ENTER:
1365 case OMP_CLAUSE_LINK:
1366 case OMP_CLAUSE_USE_DEVICE_PTR:
1367 case OMP_CLAUSE_USE_DEVICE_ADDR:
1368 case OMP_CLAUSE_HAS_DEVICE_ADDR:
1369 case OMP_CLAUSE_IS_DEVICE_PTR:
1370 case OMP_CLAUSE_DETACH:
1371 do_decl_clause:
1372 if (pdecl == NULL)
1373 pdecl = &OMP_CLAUSE_DECL (clause);
1374 decl = *pdecl;
1375 if (VAR_P (decl)
1376 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1377 break;
1378 if (decl_function_context (decl) != info->context)
1380 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1381 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1382 bitmap_set_bit (new_suppress, DECL_UID (decl));
1383 *pdecl = get_nonlocal_debug_decl (info, decl);
1384 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1385 need_chain = true;
1387 break;
1389 case OMP_CLAUSE_SCHEDULE:
1390 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1391 break;
1392 /* FALLTHRU */
1393 case OMP_CLAUSE_FINAL:
1394 case OMP_CLAUSE_IF:
1395 case OMP_CLAUSE_SELF:
1396 case OMP_CLAUSE_NUM_THREADS:
1397 case OMP_CLAUSE_DEPEND:
1398 case OMP_CLAUSE_DOACROSS:
1399 case OMP_CLAUSE_DEVICE:
1400 case OMP_CLAUSE_NUM_TEAMS:
1401 case OMP_CLAUSE_THREAD_LIMIT:
1402 case OMP_CLAUSE_SAFELEN:
1403 case OMP_CLAUSE_SIMDLEN:
1404 case OMP_CLAUSE_PRIORITY:
1405 case OMP_CLAUSE_GRAINSIZE:
1406 case OMP_CLAUSE_NUM_TASKS:
1407 case OMP_CLAUSE_HINT:
1408 case OMP_CLAUSE_FILTER:
1409 case OMP_CLAUSE_NUM_GANGS:
1410 case OMP_CLAUSE_NUM_WORKERS:
1411 case OMP_CLAUSE_VECTOR_LENGTH:
1412 case OMP_CLAUSE_GANG:
1413 case OMP_CLAUSE_WORKER:
1414 case OMP_CLAUSE_VECTOR:
1415 case OMP_CLAUSE_ASYNC:
1416 case OMP_CLAUSE_WAIT:
1417 /* Several OpenACC clauses have optional arguments. Check if they
1418 are present. */
1419 if (OMP_CLAUSE_OPERAND (clause, 0))
1421 wi->val_only = true;
1422 wi->is_lhs = false;
1423 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1424 &dummy, wi);
1427 /* The gang clause accepts two arguments. */
1428 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1429 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1431 wi->val_only = true;
1432 wi->is_lhs = false;
1433 convert_nonlocal_reference_op
1434 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1436 break;
1438 case OMP_CLAUSE_DIST_SCHEDULE:
1439 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1441 wi->val_only = true;
1442 wi->is_lhs = false;
1443 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1444 &dummy, wi);
1446 break;
1448 case OMP_CLAUSE_MAP:
1449 case OMP_CLAUSE_TO:
1450 case OMP_CLAUSE_FROM:
1451 if (OMP_CLAUSE_SIZE (clause))
1453 wi->val_only = true;
1454 wi->is_lhs = false;
1455 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1456 &dummy, wi);
1458 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1459 goto do_decl_clause;
1460 wi->val_only = true;
1461 wi->is_lhs = false;
1462 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1463 wi, NULL);
1464 break;
1466 case OMP_CLAUSE_ALIGNED:
1467 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1469 wi->val_only = true;
1470 wi->is_lhs = false;
1471 convert_nonlocal_reference_op
1472 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1474 /* FALLTHRU */
1475 case OMP_CLAUSE_NONTEMPORAL:
1476 do_decl_clause_no_supp:
1477 /* Like do_decl_clause, but don't add any suppression. */
1478 decl = OMP_CLAUSE_DECL (clause);
1479 if (VAR_P (decl)
1480 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1481 break;
1482 if (decl_function_context (decl) != info->context)
1484 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1485 need_chain = true;
1487 break;
1489 case OMP_CLAUSE_ALLOCATE:
1490 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause))
1492 wi->val_only = true;
1493 wi->is_lhs = false;
1494 convert_nonlocal_reference_op
1495 (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause), &dummy, wi);
1497 goto do_decl_clause_no_supp;
1499 case OMP_CLAUSE_NOWAIT:
1500 case OMP_CLAUSE_ORDERED:
1501 case OMP_CLAUSE_DEFAULT:
1502 case OMP_CLAUSE_COPYIN:
1503 case OMP_CLAUSE_COLLAPSE:
1504 case OMP_CLAUSE_TILE:
1505 case OMP_CLAUSE_UNTIED:
1506 case OMP_CLAUSE_MERGEABLE:
1507 case OMP_CLAUSE_PROC_BIND:
1508 case OMP_CLAUSE_NOGROUP:
1509 case OMP_CLAUSE_THREADS:
1510 case OMP_CLAUSE_SIMD:
1511 case OMP_CLAUSE_DEFAULTMAP:
1512 case OMP_CLAUSE_ORDER:
1513 case OMP_CLAUSE_SEQ:
1514 case OMP_CLAUSE_INDEPENDENT:
1515 case OMP_CLAUSE_AUTO:
1516 case OMP_CLAUSE_IF_PRESENT:
1517 case OMP_CLAUSE_FINALIZE:
1518 case OMP_CLAUSE_BIND:
1519 case OMP_CLAUSE__CONDTEMP_:
1520 case OMP_CLAUSE__SCANTEMP_:
1521 break;
1523 /* The following clause belongs to the OpenACC cache directive, which
1524 is discarded during gimplification. */
1525 case OMP_CLAUSE__CACHE_:
1526 /* The following clauses are only allowed in the OpenMP declare simd
1527 directive, so not seen here. */
1528 case OMP_CLAUSE_UNIFORM:
1529 case OMP_CLAUSE_INBRANCH:
1530 case OMP_CLAUSE_NOTINBRANCH:
1531 /* The following clauses are only allowed on OpenMP cancel and
1532 cancellation point directives, which at this point have already
1533 been lowered into a function call. */
1534 case OMP_CLAUSE_FOR:
1535 case OMP_CLAUSE_PARALLEL:
1536 case OMP_CLAUSE_SECTIONS:
1537 case OMP_CLAUSE_TASKGROUP:
1538 /* The following clauses are only added during OMP lowering; nested
1539 function decomposition happens before that. */
1540 case OMP_CLAUSE__LOOPTEMP_:
1541 case OMP_CLAUSE__REDUCTEMP_:
1542 case OMP_CLAUSE__SIMDUID_:
1543 case OMP_CLAUSE__SIMT_:
1544 /* The following clauses are only allowed on OpenACC 'routine'
1545 directives, not seen here. */
1546 case OMP_CLAUSE_NOHOST:
1547 /* Anything else. */
1548 default:
1549 gcc_unreachable ();
1553 info->suppress_expansion = new_suppress;
1555 if (need_stmts)
1556 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1557 switch (OMP_CLAUSE_CODE (clause))
1559 case OMP_CLAUSE_REDUCTION:
1560 case OMP_CLAUSE_IN_REDUCTION:
1561 case OMP_CLAUSE_TASK_REDUCTION:
1562 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1564 tree old_context
1565 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1566 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1567 = info->context;
1568 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1569 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1570 = info->context;
1571 tree save_local_var_chain = info->new_local_var_chain;
1572 info->new_local_var_chain = NULL;
1573 gimple_seq *seq = &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause);
1574 walk_body (convert_nonlocal_reference_stmt,
1575 convert_nonlocal_reference_op, info, seq);
1576 if (info->new_local_var_chain)
1577 declare_vars (info->new_local_var_chain,
1578 gimple_seq_first_stmt (*seq), false);
1579 info->new_local_var_chain = NULL;
1580 seq = &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause);
1581 walk_body (convert_nonlocal_reference_stmt,
1582 convert_nonlocal_reference_op, info, seq);
1583 if (info->new_local_var_chain)
1584 declare_vars (info->new_local_var_chain,
1585 gimple_seq_first_stmt (*seq), false);
1586 info->new_local_var_chain = save_local_var_chain;
1587 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1588 = old_context;
1589 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1590 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1591 = old_context;
1593 break;
1595 case OMP_CLAUSE_LASTPRIVATE:
1596 case OMP_CLAUSE_LINEAR:
1598 tree save_local_var_chain = info->new_local_var_chain;
1599 info->new_local_var_chain = NULL;
1600 gimple_seq *seq;
1601 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LASTPRIVATE)
1602 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause);
1603 else
1604 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause);
1605 walk_body (convert_nonlocal_reference_stmt,
1606 convert_nonlocal_reference_op, info, seq);
1607 if (info->new_local_var_chain)
1609 gimple *g = gimple_seq_first_stmt (*seq);
1610 if (gimple_code (g) != GIMPLE_BIND)
1612 g = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
1613 *seq = NULL;
1614 gimple_seq_add_stmt_without_update (seq, g);
1616 declare_vars (info->new_local_var_chain,
1617 gimple_seq_first_stmt (*seq), false);
1619 info->new_local_var_chain = save_local_var_chain;
1621 break;
1623 default:
1624 break;
1627 return need_chain;
1630 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1632 static void
1633 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1635 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1636 type = TREE_TYPE (type);
1638 if (TYPE_NAME (type)
1639 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1640 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1641 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1643 while (POINTER_TYPE_P (type)
1644 || VECTOR_TYPE_P (type)
1645 || TREE_CODE (type) == FUNCTION_TYPE
1646 || TREE_CODE (type) == METHOD_TYPE)
1647 type = TREE_TYPE (type);
1649 if (TREE_CODE (type) == ARRAY_TYPE)
1651 tree domain, t;
1653 note_nonlocal_vla_type (info, TREE_TYPE (type));
1654 domain = TYPE_DOMAIN (type);
1655 if (domain)
1657 t = TYPE_MIN_VALUE (domain);
1658 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1659 && decl_function_context (t) != info->context)
1660 get_nonlocal_debug_decl (info, t);
1661 t = TYPE_MAX_VALUE (domain);
1662 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1663 && decl_function_context (t) != info->context)
1664 get_nonlocal_debug_decl (info, t);
1669 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1670 PARM_DECLs that belong to outer functions. This handles statements
1671 that are not handled via the standard recursion done in
1672 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1673 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1674 operands of STMT have been handled by this function. */
1676 static tree
1677 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1678 struct walk_stmt_info *wi)
1680 struct nesting_info *info = (struct nesting_info *) wi->info;
1681 tree save_local_var_chain;
1682 bitmap save_suppress;
1683 gimple *stmt = gsi_stmt (*gsi);
1685 switch (gimple_code (stmt))
1687 case GIMPLE_GOTO:
1688 /* Don't walk non-local gotos for now. */
1689 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1691 wi->val_only = true;
1692 wi->is_lhs = false;
1693 *handled_ops_p = false;
1694 return NULL_TREE;
1696 break;
1698 case GIMPLE_OMP_TEAMS:
1699 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
1701 save_suppress = info->suppress_expansion;
1702 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt),
1703 wi);
1704 walk_body (convert_nonlocal_reference_stmt,
1705 convert_nonlocal_reference_op, info,
1706 gimple_omp_body_ptr (stmt));
1707 info->suppress_expansion = save_suppress;
1708 break;
1710 /* FALLTHRU */
1712 case GIMPLE_OMP_PARALLEL:
1713 case GIMPLE_OMP_TASK:
1714 save_suppress = info->suppress_expansion;
1715 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1716 wi))
1718 tree c, decl;
1719 decl = get_chain_decl (info);
1720 c = build_omp_clause (gimple_location (stmt),
1721 OMP_CLAUSE_FIRSTPRIVATE);
1722 OMP_CLAUSE_DECL (c) = decl;
1723 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1724 gimple_omp_taskreg_set_clauses (stmt, c);
1727 save_local_var_chain = info->new_local_var_chain;
1728 info->new_local_var_chain = NULL;
1730 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1731 info, gimple_omp_body_ptr (stmt));
1733 if (info->new_local_var_chain)
1734 declare_vars (info->new_local_var_chain,
1735 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1736 false);
1737 info->new_local_var_chain = save_local_var_chain;
1738 info->suppress_expansion = save_suppress;
1739 break;
1741 case GIMPLE_OMP_FOR:
1742 save_suppress = info->suppress_expansion;
1743 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1744 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1745 convert_nonlocal_reference_stmt,
1746 convert_nonlocal_reference_op, info);
1747 walk_body (convert_nonlocal_reference_stmt,
1748 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1749 info->suppress_expansion = save_suppress;
1750 break;
1752 case GIMPLE_OMP_SECTIONS:
1753 save_suppress = info->suppress_expansion;
1754 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1755 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1756 info, gimple_omp_body_ptr (stmt));
1757 info->suppress_expansion = save_suppress;
1758 break;
1760 case GIMPLE_OMP_SINGLE:
1761 save_suppress = info->suppress_expansion;
1762 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1763 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1764 info, gimple_omp_body_ptr (stmt));
1765 info->suppress_expansion = save_suppress;
1766 break;
1768 case GIMPLE_OMP_SCOPE:
1769 save_suppress = info->suppress_expansion;
1770 convert_nonlocal_omp_clauses (gimple_omp_scope_clauses_ptr (stmt), wi);
1771 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1772 info, gimple_omp_body_ptr (stmt));
1773 info->suppress_expansion = save_suppress;
1774 break;
1776 case GIMPLE_OMP_TASKGROUP:
1777 save_suppress = info->suppress_expansion;
1778 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
1779 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1780 info, gimple_omp_body_ptr (stmt));
1781 info->suppress_expansion = save_suppress;
1782 break;
1784 case GIMPLE_OMP_TARGET:
1785 if (!is_gimple_omp_offloaded (stmt))
1787 save_suppress = info->suppress_expansion;
1788 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1789 wi);
1790 info->suppress_expansion = save_suppress;
1791 walk_body (convert_nonlocal_reference_stmt,
1792 convert_nonlocal_reference_op, info,
1793 gimple_omp_body_ptr (stmt));
1794 break;
1796 save_suppress = info->suppress_expansion;
1797 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1798 wi))
1800 tree c, decl;
1801 decl = get_chain_decl (info);
1802 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1803 OMP_CLAUSE_DECL (c) = decl;
1804 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1805 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1806 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1807 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1810 save_local_var_chain = info->new_local_var_chain;
1811 info->new_local_var_chain = NULL;
1813 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1814 info, gimple_omp_body_ptr (stmt));
1816 if (info->new_local_var_chain)
1817 declare_vars (info->new_local_var_chain,
1818 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1819 false);
1820 info->new_local_var_chain = save_local_var_chain;
1821 info->suppress_expansion = save_suppress;
1822 break;
1824 case GIMPLE_OMP_SECTION:
1825 case GIMPLE_OMP_STRUCTURED_BLOCK:
1826 case GIMPLE_OMP_MASTER:
1827 case GIMPLE_OMP_MASKED:
1828 case GIMPLE_OMP_ORDERED:
1829 case GIMPLE_OMP_SCAN:
1830 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1831 info, gimple_omp_body_ptr (stmt));
1832 break;
1834 case GIMPLE_BIND:
1836 gbind *bind_stmt = as_a <gbind *> (stmt);
1838 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1839 if (TREE_CODE (var) == NAMELIST_DECL)
1841 /* Adjust decls mentioned in NAMELIST_DECL. */
1842 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1843 tree decl;
1844 unsigned int i;
1846 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1848 if (VAR_P (decl)
1849 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1850 continue;
1851 if (decl_function_context (decl) != info->context)
1852 CONSTRUCTOR_ELT (decls, i)->value
1853 = get_nonlocal_debug_decl (info, decl);
1857 *handled_ops_p = false;
1858 return NULL_TREE;
1860 case GIMPLE_COND:
1861 wi->val_only = true;
1862 wi->is_lhs = false;
1863 *handled_ops_p = false;
1864 return NULL_TREE;
1866 case GIMPLE_ASSIGN:
1867 if (gimple_clobber_p (stmt))
1869 tree lhs = gimple_assign_lhs (stmt);
1870 if (DECL_P (lhs)
1871 && !(TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
1872 && decl_function_context (lhs) != info->context)
1874 gsi_replace (gsi, gimple_build_nop (), true);
1875 break;
1878 *handled_ops_p = false;
1879 return NULL_TREE;
1881 default:
1882 /* For every other statement that we are not interested in
1883 handling here, let the walker traverse the operands. */
1884 *handled_ops_p = false;
1885 return NULL_TREE;
1888 /* We have handled all of STMT operands, no need to traverse the operands. */
1889 *handled_ops_p = true;
1890 return NULL_TREE;
1894 /* A subroutine of convert_local_reference. Create a local variable
1895 in the parent function with DECL_VALUE_EXPR set to reference the
1896 field in FRAME. This is used both for debug info and in OMP
1897 lowering. */
1899 static tree
1900 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1902 tree x, new_decl;
1904 tree *slot = &info->var_map->get_or_insert (decl);
1905 if (*slot)
1906 return *slot;
1908 /* Make sure frame_decl gets created. */
1909 (void) get_frame_type (info);
1910 x = info->frame_decl;
1911 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1913 new_decl = get_debug_decl (decl);
1914 DECL_CONTEXT (new_decl) = info->context;
1916 SET_DECL_VALUE_EXPR (new_decl, x);
1917 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1918 *slot = new_decl;
1920 DECL_CHAIN (new_decl) = info->debug_var_chain;
1921 info->debug_var_chain = new_decl;
1923 /* Do not emit debug info twice. */
1924 DECL_IGNORED_P (decl) = 1;
1926 return new_decl;
1930 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1931 and PARM_DECLs that were referenced by inner nested functions.
1932 The rewrite will be a structure reference to the local frame variable. */
1934 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1936 static tree
1937 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1939 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1940 struct nesting_info *const info = (struct nesting_info *) wi->info;
1941 tree t = *tp, field, x;
1942 bool save_val_only;
1944 *walk_subtrees = 0;
1945 switch (TREE_CODE (t))
1947 case VAR_DECL:
1948 /* Non-automatic variables are never processed. */
1949 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1950 break;
1951 /* FALLTHRU */
1953 case PARM_DECL:
1954 if (t != info->frame_decl && decl_function_context (t) == info->context)
1956 /* If we copied a pointer to the frame, then the original decl
1957 is used unchanged in the parent function. */
1958 if (use_pointer_in_frame (t))
1959 break;
1961 /* No need to transform anything if no child references the
1962 variable. */
1963 field = lookup_field_for_decl (info, t, NO_INSERT);
1964 if (!field)
1965 break;
1966 wi->changed = true;
1968 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1969 x = get_local_debug_decl (info, t, field);
1970 else
1971 x = get_frame_field (info, info->context, field, &wi->gsi);
1973 if (wi->val_only)
1975 if (wi->is_lhs)
1976 x = save_tmp_var (info, x, &wi->gsi);
1977 else
1978 x = init_tmp_var (info, x, &wi->gsi);
1981 *tp = x;
1983 break;
1985 case ADDR_EXPR:
1986 save_val_only = wi->val_only;
1987 wi->val_only = false;
1988 wi->is_lhs = false;
1989 wi->changed = false;
1990 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1991 wi->val_only = save_val_only;
1993 /* If we converted anything ... */
1994 if (wi->changed)
1996 tree save_context;
1998 /* Then the frame decl is now addressable. */
1999 TREE_ADDRESSABLE (info->frame_decl) = 1;
2001 save_context = current_function_decl;
2002 current_function_decl = info->context;
2003 recompute_tree_invariant_for_addr_expr (t);
2005 /* If we are in a context where we only accept values, then
2006 compute the address into a temporary. */
2007 if (save_val_only)
2008 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
2009 t, &wi->gsi);
2010 current_function_decl = save_context;
2012 break;
2014 case REALPART_EXPR:
2015 case IMAGPART_EXPR:
2016 case COMPONENT_REF:
2017 case ARRAY_REF:
2018 case ARRAY_RANGE_REF:
2019 case BIT_FIELD_REF:
2020 /* Go down this entire nest and just look at the final prefix and
2021 anything that describes the references. Otherwise, we lose track
2022 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
2023 save_val_only = wi->val_only;
2024 wi->val_only = true;
2025 wi->is_lhs = false;
2026 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
2028 if (TREE_CODE (t) == COMPONENT_REF)
2029 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
2030 NULL);
2031 else if (TREE_CODE (t) == ARRAY_REF
2032 || TREE_CODE (t) == ARRAY_RANGE_REF)
2034 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
2035 NULL);
2036 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
2037 NULL);
2038 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
2039 NULL);
2042 wi->val_only = false;
2043 walk_tree (tp, convert_local_reference_op, wi, NULL);
2044 wi->val_only = save_val_only;
2045 break;
2047 case MEM_REF:
2048 save_val_only = wi->val_only;
2049 wi->val_only = true;
2050 wi->is_lhs = false;
2051 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
2052 wi, NULL);
2053 /* We need to re-fold the MEM_REF as component references as
2054 part of a ADDR_EXPR address are not allowed. But we cannot
2055 fold here, as the chain record type is not yet finalized. */
2056 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2057 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
2058 info->mem_refs->add (tp);
2059 wi->val_only = save_val_only;
2060 break;
2062 case VIEW_CONVERT_EXPR:
2063 /* Just request to look at the subtrees, leaving val_only and lhs
2064 untouched. This might actually be for !val_only + lhs, in which
2065 case we don't want to force a replacement by a temporary. */
2066 *walk_subtrees = 1;
2067 break;
2069 default:
2070 if (!IS_TYPE_OR_DECL_P (t))
2072 *walk_subtrees = 1;
2073 wi->val_only = true;
2074 wi->is_lhs = false;
2076 break;
2079 return NULL_TREE;
2082 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
2083 struct walk_stmt_info *);
2085 /* Helper for convert_local_reference. Convert all the references in
2086 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
2088 static bool
2089 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
2091 struct nesting_info *const info = (struct nesting_info *) wi->info;
2092 bool need_frame = false, need_stmts = false;
2093 tree clause, decl, *pdecl;
2094 int dummy;
2095 bitmap new_suppress;
2097 new_suppress = BITMAP_GGC_ALLOC ();
2098 bitmap_copy (new_suppress, info->suppress_expansion);
2100 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2102 pdecl = NULL;
2103 switch (OMP_CLAUSE_CODE (clause))
2105 case OMP_CLAUSE_REDUCTION:
2106 case OMP_CLAUSE_IN_REDUCTION:
2107 case OMP_CLAUSE_TASK_REDUCTION:
2108 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2109 need_stmts = true;
2110 if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
2112 pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
2113 if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
2114 pdecl = &TREE_OPERAND (*pdecl, 0);
2115 if (INDIRECT_REF_P (*pdecl)
2116 || TREE_CODE (*pdecl) == ADDR_EXPR)
2117 pdecl = &TREE_OPERAND (*pdecl, 0);
2119 goto do_decl_clause;
2121 case OMP_CLAUSE_LASTPRIVATE:
2122 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
2123 need_stmts = true;
2124 goto do_decl_clause;
2126 case OMP_CLAUSE_LINEAR:
2127 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
2128 need_stmts = true;
2129 wi->val_only = true;
2130 wi->is_lhs = false;
2131 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
2132 wi);
2133 goto do_decl_clause;
2135 case OMP_CLAUSE_PRIVATE:
2136 case OMP_CLAUSE_FIRSTPRIVATE:
2137 case OMP_CLAUSE_COPYPRIVATE:
2138 case OMP_CLAUSE_SHARED:
2139 case OMP_CLAUSE_ENTER:
2140 case OMP_CLAUSE_LINK:
2141 case OMP_CLAUSE_USE_DEVICE_PTR:
2142 case OMP_CLAUSE_USE_DEVICE_ADDR:
2143 case OMP_CLAUSE_HAS_DEVICE_ADDR:
2144 case OMP_CLAUSE_IS_DEVICE_PTR:
2145 case OMP_CLAUSE_DETACH:
2146 do_decl_clause:
2147 if (pdecl == NULL)
2148 pdecl = &OMP_CLAUSE_DECL (clause);
2149 decl = *pdecl;
2150 if (VAR_P (decl)
2151 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2152 break;
2153 if (decl_function_context (decl) == info->context
2154 && !use_pointer_in_frame (decl))
2156 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2157 if (field)
2159 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
2160 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
2161 bitmap_set_bit (new_suppress, DECL_UID (decl));
2162 *pdecl = get_local_debug_decl (info, decl, field);
2163 need_frame = true;
2166 break;
2168 case OMP_CLAUSE_SCHEDULE:
2169 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
2170 break;
2171 /* FALLTHRU */
2172 case OMP_CLAUSE_FINAL:
2173 case OMP_CLAUSE_IF:
2174 case OMP_CLAUSE_SELF:
2175 case OMP_CLAUSE_NUM_THREADS:
2176 case OMP_CLAUSE_DEPEND:
2177 case OMP_CLAUSE_DOACROSS:
2178 case OMP_CLAUSE_DEVICE:
2179 case OMP_CLAUSE_NUM_TEAMS:
2180 case OMP_CLAUSE_THREAD_LIMIT:
2181 case OMP_CLAUSE_SAFELEN:
2182 case OMP_CLAUSE_SIMDLEN:
2183 case OMP_CLAUSE_PRIORITY:
2184 case OMP_CLAUSE_GRAINSIZE:
2185 case OMP_CLAUSE_NUM_TASKS:
2186 case OMP_CLAUSE_HINT:
2187 case OMP_CLAUSE_FILTER:
2188 case OMP_CLAUSE_NUM_GANGS:
2189 case OMP_CLAUSE_NUM_WORKERS:
2190 case OMP_CLAUSE_VECTOR_LENGTH:
2191 case OMP_CLAUSE_GANG:
2192 case OMP_CLAUSE_WORKER:
2193 case OMP_CLAUSE_VECTOR:
2194 case OMP_CLAUSE_ASYNC:
2195 case OMP_CLAUSE_WAIT:
2196 /* Several OpenACC clauses have optional arguments. Check if they
2197 are present. */
2198 if (OMP_CLAUSE_OPERAND (clause, 0))
2200 wi->val_only = true;
2201 wi->is_lhs = false;
2202 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2203 &dummy, wi);
2206 /* The gang clause accepts two arguments. */
2207 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
2208 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
2210 wi->val_only = true;
2211 wi->is_lhs = false;
2212 convert_nonlocal_reference_op
2213 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
2215 break;
2217 case OMP_CLAUSE_DIST_SCHEDULE:
2218 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
2220 wi->val_only = true;
2221 wi->is_lhs = false;
2222 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2223 &dummy, wi);
2225 break;
2227 case OMP_CLAUSE_MAP:
2228 case OMP_CLAUSE_TO:
2229 case OMP_CLAUSE_FROM:
2230 if (OMP_CLAUSE_SIZE (clause))
2232 wi->val_only = true;
2233 wi->is_lhs = false;
2234 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
2235 &dummy, wi);
2237 if (DECL_P (OMP_CLAUSE_DECL (clause)))
2238 goto do_decl_clause;
2239 wi->val_only = true;
2240 wi->is_lhs = false;
2241 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
2242 wi, NULL);
2243 break;
2245 case OMP_CLAUSE_ALIGNED:
2246 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2248 wi->val_only = true;
2249 wi->is_lhs = false;
2250 convert_local_reference_op
2251 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2253 /* FALLTHRU */
2254 case OMP_CLAUSE_NONTEMPORAL:
2255 do_decl_clause_no_supp:
2256 /* Like do_decl_clause, but don't add any suppression. */
2257 decl = OMP_CLAUSE_DECL (clause);
2258 if (VAR_P (decl)
2259 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2260 break;
2261 if (decl_function_context (decl) == info->context
2262 && !use_pointer_in_frame (decl))
2264 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2265 if (field)
2267 OMP_CLAUSE_DECL (clause)
2268 = get_local_debug_decl (info, decl, field);
2269 need_frame = true;
2272 break;
2274 case OMP_CLAUSE_ALLOCATE:
2275 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause))
2277 wi->val_only = true;
2278 wi->is_lhs = false;
2279 convert_local_reference_op
2280 (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause), &dummy, wi);
2282 goto do_decl_clause_no_supp;
2284 case OMP_CLAUSE_NOWAIT:
2285 case OMP_CLAUSE_ORDERED:
2286 case OMP_CLAUSE_DEFAULT:
2287 case OMP_CLAUSE_COPYIN:
2288 case OMP_CLAUSE_COLLAPSE:
2289 case OMP_CLAUSE_TILE:
2290 case OMP_CLAUSE_UNTIED:
2291 case OMP_CLAUSE_MERGEABLE:
2292 case OMP_CLAUSE_PROC_BIND:
2293 case OMP_CLAUSE_NOGROUP:
2294 case OMP_CLAUSE_THREADS:
2295 case OMP_CLAUSE_SIMD:
2296 case OMP_CLAUSE_DEFAULTMAP:
2297 case OMP_CLAUSE_ORDER:
2298 case OMP_CLAUSE_SEQ:
2299 case OMP_CLAUSE_INDEPENDENT:
2300 case OMP_CLAUSE_AUTO:
2301 case OMP_CLAUSE_IF_PRESENT:
2302 case OMP_CLAUSE_FINALIZE:
2303 case OMP_CLAUSE_BIND:
2304 case OMP_CLAUSE__CONDTEMP_:
2305 case OMP_CLAUSE__SCANTEMP_:
2306 break;
2308 /* The following clause belongs to the OpenACC cache directive, which
2309 is discarded during gimplification. */
2310 case OMP_CLAUSE__CACHE_:
2311 /* The following clauses are only allowed in the OpenMP declare simd
2312 directive, so not seen here. */
2313 case OMP_CLAUSE_UNIFORM:
2314 case OMP_CLAUSE_INBRANCH:
2315 case OMP_CLAUSE_NOTINBRANCH:
2316 /* The following clauses are only allowed on OpenMP cancel and
2317 cancellation point directives, which at this point have already
2318 been lowered into a function call. */
2319 case OMP_CLAUSE_FOR:
2320 case OMP_CLAUSE_PARALLEL:
2321 case OMP_CLAUSE_SECTIONS:
2322 case OMP_CLAUSE_TASKGROUP:
2323 /* The following clauses are only added during OMP lowering; nested
2324 function decomposition happens before that. */
2325 case OMP_CLAUSE__LOOPTEMP_:
2326 case OMP_CLAUSE__REDUCTEMP_:
2327 case OMP_CLAUSE__SIMDUID_:
2328 case OMP_CLAUSE__SIMT_:
2329 /* The following clauses are only allowed on OpenACC 'routine'
2330 directives, not seen here. */
2331 case OMP_CLAUSE_NOHOST:
2332 /* Anything else. */
2333 default:
2334 gcc_unreachable ();
2338 info->suppress_expansion = new_suppress;
2340 if (need_stmts)
2341 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2342 switch (OMP_CLAUSE_CODE (clause))
2344 case OMP_CLAUSE_REDUCTION:
2345 case OMP_CLAUSE_IN_REDUCTION:
2346 case OMP_CLAUSE_TASK_REDUCTION:
2347 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2349 tree old_context
2350 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2351 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2352 = info->context;
2353 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2354 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2355 = info->context;
2356 walk_body (convert_local_reference_stmt,
2357 convert_local_reference_op, info,
2358 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2359 walk_body (convert_local_reference_stmt,
2360 convert_local_reference_op, info,
2361 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2362 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2363 = old_context;
2364 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2365 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2366 = old_context;
2368 break;
2370 case OMP_CLAUSE_LASTPRIVATE:
2371 walk_body (convert_local_reference_stmt,
2372 convert_local_reference_op, info,
2373 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2374 break;
2376 case OMP_CLAUSE_LINEAR:
2377 walk_body (convert_local_reference_stmt,
2378 convert_local_reference_op, info,
2379 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2380 break;
2382 default:
2383 break;
2386 return need_frame;
2390 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2391 and PARM_DECLs that were referenced by inner nested functions.
2392 The rewrite will be a structure reference to the local frame variable. */
2394 static tree
2395 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2396 struct walk_stmt_info *wi)
2398 struct nesting_info *info = (struct nesting_info *) wi->info;
2399 tree save_local_var_chain;
2400 bitmap save_suppress;
2401 char save_static_chain_added;
2402 bool frame_decl_added;
2403 gimple *stmt = gsi_stmt (*gsi);
2405 switch (gimple_code (stmt))
2407 case GIMPLE_OMP_TEAMS:
2408 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2410 save_suppress = info->suppress_expansion;
2411 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2412 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2413 info, gimple_omp_body_ptr (stmt));
2414 info->suppress_expansion = save_suppress;
2415 break;
2417 /* FALLTHRU */
2419 case GIMPLE_OMP_PARALLEL:
2420 case GIMPLE_OMP_TASK:
2421 save_suppress = info->suppress_expansion;
2422 frame_decl_added = false;
2423 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2424 wi))
2426 tree c = build_omp_clause (gimple_location (stmt),
2427 OMP_CLAUSE_SHARED);
2428 (void) get_frame_type (info);
2429 OMP_CLAUSE_DECL (c) = info->frame_decl;
2430 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2431 gimple_omp_taskreg_set_clauses (stmt, c);
2432 info->static_chain_added |= 4;
2433 frame_decl_added = true;
2436 save_local_var_chain = info->new_local_var_chain;
2437 save_static_chain_added = info->static_chain_added;
2438 info->new_local_var_chain = NULL;
2439 info->static_chain_added = 0;
2441 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2442 gimple_omp_body_ptr (stmt));
2444 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2446 tree c = build_omp_clause (gimple_location (stmt),
2447 OMP_CLAUSE_SHARED);
2448 (void) get_frame_type (info);
2449 OMP_CLAUSE_DECL (c) = info->frame_decl;
2450 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2451 info->static_chain_added |= 4;
2452 gimple_omp_taskreg_set_clauses (stmt, c);
2454 if (info->new_local_var_chain)
2455 declare_vars (info->new_local_var_chain,
2456 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2457 info->new_local_var_chain = save_local_var_chain;
2458 info->suppress_expansion = save_suppress;
2459 info->static_chain_added |= save_static_chain_added;
2460 break;
2462 case GIMPLE_OMP_FOR:
2463 save_suppress = info->suppress_expansion;
2464 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2465 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2466 convert_local_reference_stmt,
2467 convert_local_reference_op, info);
2468 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2469 info, gimple_omp_body_ptr (stmt));
2470 info->suppress_expansion = save_suppress;
2471 break;
2473 case GIMPLE_OMP_SECTIONS:
2474 save_suppress = info->suppress_expansion;
2475 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2476 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2477 info, gimple_omp_body_ptr (stmt));
2478 info->suppress_expansion = save_suppress;
2479 break;
2481 case GIMPLE_OMP_SINGLE:
2482 save_suppress = info->suppress_expansion;
2483 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2484 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2485 info, gimple_omp_body_ptr (stmt));
2486 info->suppress_expansion = save_suppress;
2487 break;
2489 case GIMPLE_OMP_SCOPE:
2490 save_suppress = info->suppress_expansion;
2491 convert_local_omp_clauses (gimple_omp_scope_clauses_ptr (stmt), wi);
2492 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2493 info, gimple_omp_body_ptr (stmt));
2494 info->suppress_expansion = save_suppress;
2495 break;
2497 case GIMPLE_OMP_TASKGROUP:
2498 save_suppress = info->suppress_expansion;
2499 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
2500 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2501 info, gimple_omp_body_ptr (stmt));
2502 info->suppress_expansion = save_suppress;
2503 break;
2505 case GIMPLE_OMP_TARGET:
2506 if (!is_gimple_omp_offloaded (stmt))
2508 save_suppress = info->suppress_expansion;
2509 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2510 info->suppress_expansion = save_suppress;
2511 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2512 info, gimple_omp_body_ptr (stmt));
2513 break;
2515 save_suppress = info->suppress_expansion;
2516 frame_decl_added = false;
2517 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2519 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2520 (void) get_frame_type (info);
2521 OMP_CLAUSE_DECL (c) = info->frame_decl;
2522 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2523 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2524 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2525 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2526 info->static_chain_added |= 4;
2527 frame_decl_added = true;
2530 save_local_var_chain = info->new_local_var_chain;
2531 save_static_chain_added = info->static_chain_added;
2532 info->new_local_var_chain = NULL;
2533 info->static_chain_added = 0;
2535 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2536 gimple_omp_body_ptr (stmt));
2538 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2540 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2541 (void) get_frame_type (info);
2542 OMP_CLAUSE_DECL (c) = info->frame_decl;
2543 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2544 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2545 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2546 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2547 info->static_chain_added |= 4;
2550 if (info->new_local_var_chain)
2551 declare_vars (info->new_local_var_chain,
2552 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2553 info->new_local_var_chain = save_local_var_chain;
2554 info->suppress_expansion = save_suppress;
2555 info->static_chain_added |= save_static_chain_added;
2556 break;
2558 case GIMPLE_OMP_SECTION:
2559 case GIMPLE_OMP_STRUCTURED_BLOCK:
2560 case GIMPLE_OMP_MASTER:
2561 case GIMPLE_OMP_MASKED:
2562 case GIMPLE_OMP_ORDERED:
2563 case GIMPLE_OMP_SCAN:
2564 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2565 info, gimple_omp_body_ptr (stmt));
2566 break;
2568 case GIMPLE_COND:
2569 wi->val_only = true;
2570 wi->is_lhs = false;
2571 *handled_ops_p = false;
2572 return NULL_TREE;
2574 case GIMPLE_ASSIGN:
2575 if (gimple_clobber_p (stmt))
2577 tree lhs = gimple_assign_lhs (stmt);
2578 if (DECL_P (lhs)
2579 && decl_function_context (lhs) == info->context
2580 && !use_pointer_in_frame (lhs)
2581 && lookup_field_for_decl (info, lhs, NO_INSERT))
2583 gsi_replace (gsi, gimple_build_nop (), true);
2584 break;
2587 *handled_ops_p = false;
2588 return NULL_TREE;
2590 case GIMPLE_BIND:
2591 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2592 var;
2593 var = DECL_CHAIN (var))
2594 if (TREE_CODE (var) == NAMELIST_DECL)
2596 /* Adjust decls mentioned in NAMELIST_DECL. */
2597 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2598 tree decl;
2599 unsigned int i;
2601 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2603 if (VAR_P (decl)
2604 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2605 continue;
2606 if (decl_function_context (decl) == info->context
2607 && !use_pointer_in_frame (decl))
2609 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2610 if (field)
2612 CONSTRUCTOR_ELT (decls, i)->value
2613 = get_local_debug_decl (info, decl, field);
2619 *handled_ops_p = false;
2620 return NULL_TREE;
2622 default:
2623 /* For every other statement that we are not interested in
2624 handling here, let the walker traverse the operands. */
2625 *handled_ops_p = false;
2626 return NULL_TREE;
2629 /* Indicate that we have handled all the operands ourselves. */
2630 *handled_ops_p = true;
2631 return NULL_TREE;
2635 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2636 that reference labels from outer functions. The rewrite will be a
2637 call to __builtin_nonlocal_goto. */
2639 static tree
2640 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2641 struct walk_stmt_info *wi)
2643 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2644 tree label, new_label, target_context, x, field;
2645 gcall *call;
2646 gimple *stmt = gsi_stmt (*gsi);
2648 if (gimple_code (stmt) != GIMPLE_GOTO)
2650 *handled_ops_p = false;
2651 return NULL_TREE;
2654 label = gimple_goto_dest (stmt);
2655 if (TREE_CODE (label) != LABEL_DECL)
2657 *handled_ops_p = false;
2658 return NULL_TREE;
2661 target_context = decl_function_context (label);
2662 if (target_context == info->context)
2664 *handled_ops_p = false;
2665 return NULL_TREE;
2668 for (i = info->outer; target_context != i->context; i = i->outer)
2669 continue;
2671 /* The original user label may also be use for a normal goto, therefore
2672 we must create a new label that will actually receive the abnormal
2673 control transfer. This new label will be marked LABEL_NONLOCAL; this
2674 mark will trigger proper behavior in the cfg, as well as cause the
2675 (hairy target-specific) non-local goto receiver code to be generated
2676 when we expand rtl. Enter this association into var_map so that we
2677 can insert the new label into the IL during a second pass. */
2678 tree *slot = &i->var_map->get_or_insert (label);
2679 if (*slot == NULL)
2681 new_label = create_artificial_label (UNKNOWN_LOCATION);
2682 DECL_NONLOCAL (new_label) = 1;
2683 *slot = new_label;
2685 else
2686 new_label = *slot;
2688 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2689 field = get_nl_goto_field (i);
2690 x = get_frame_field (info, target_context, field, gsi);
2691 x = build_addr (x);
2692 x = gsi_gimplify_val (info, x, gsi);
2693 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2694 2, build_addr (new_label), x);
2695 gsi_replace (gsi, call, false);
2697 /* We have handled all of STMT's operands, no need to keep going. */
2698 *handled_ops_p = true;
2699 return NULL_TREE;
2703 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2704 are referenced via nonlocal goto from a nested function. The rewrite
2705 will involve installing a newly generated DECL_NONLOCAL label, and
2706 (potentially) a branch around the rtl gunk that is assumed to be
2707 attached to such a label. */
2709 static tree
2710 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2711 struct walk_stmt_info *wi)
2713 struct nesting_info *const info = (struct nesting_info *) wi->info;
2714 tree label, new_label;
2715 gimple_stmt_iterator tmp_gsi;
2716 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2718 if (!stmt)
2720 *handled_ops_p = false;
2721 return NULL_TREE;
2724 label = gimple_label_label (stmt);
2726 tree *slot = info->var_map->get (label);
2727 if (!slot)
2729 *handled_ops_p = false;
2730 return NULL_TREE;
2733 /* If there's any possibility that the previous statement falls through,
2734 then we must branch around the new non-local label. */
2735 tmp_gsi = wi->gsi;
2736 gsi_prev (&tmp_gsi);
2737 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2739 gimple *stmt = gimple_build_goto (label);
2740 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2743 new_label = (tree) *slot;
2744 stmt = gimple_build_label (new_label);
2745 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2747 *handled_ops_p = true;
2748 return NULL_TREE;
2752 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2753 of nested functions that require the use of trampolines. The rewrite
2754 will involve a reference a trampoline generated for the occasion. */
2756 static tree
2757 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2759 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2760 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2761 tree t = *tp, decl, target_context, x, builtin;
2762 bool descr;
2763 gcall *call;
2765 *walk_subtrees = 0;
2766 switch (TREE_CODE (t))
2768 case ADDR_EXPR:
2769 /* Build
2770 T.1 = &CHAIN->tramp;
2771 T.2 = __builtin_adjust_trampoline (T.1);
2772 T.3 = (func_type)T.2;
2775 decl = TREE_OPERAND (t, 0);
2776 if (TREE_CODE (decl) != FUNCTION_DECL)
2777 break;
2779 /* Only need to process nested functions. */
2780 target_context = decl_function_context (decl);
2781 if (!target_context)
2782 break;
2784 /* If the nested function doesn't use a static chain, then
2785 it doesn't need a trampoline. */
2786 if (!DECL_STATIC_CHAIN (decl))
2787 break;
2789 /* If we don't want a trampoline, then don't build one. */
2790 if (TREE_NO_TRAMPOLINE (t))
2791 break;
2793 /* Lookup the immediate parent of the callee, as that's where
2794 we need to insert the trampoline. */
2795 for (i = info; i->context != target_context; i = i->outer)
2796 continue;
2798 /* Decide whether to generate a descriptor or a trampoline. */
2799 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2801 if (descr)
2802 x = lookup_descr_for_decl (i, decl, INSERT);
2803 else
2804 x = lookup_tramp_for_decl (i, decl, INSERT);
2806 /* Compute the address of the field holding the trampoline. */
2807 x = get_frame_field (info, target_context, x, &wi->gsi);
2809 /* APB: We don't need to do the adjustment calls when using off-stack
2810 trampolines, any such adjustment will be done when the off-stack
2811 trampoline is created. */
2812 if (!descr && flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
2813 x = gsi_gimplify_val (info, x, &wi->gsi);
2814 else
2816 x = build_addr (x);
2818 x = gsi_gimplify_val (info, x, &wi->gsi);
2820 /* Do machine-specific ugliness. Normally this will involve
2821 computing extra alignment, but it can really be anything. */
2822 if (descr)
2823 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2824 else
2825 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2826 call = gimple_build_call (builtin, 1, x);
2827 x = init_tmp_var_with_call (info, &wi->gsi, call);
2830 /* Cast back to the proper function type. */
2831 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2832 x = init_tmp_var (info, x, &wi->gsi);
2834 *tp = x;
2835 break;
2837 default:
2838 if (!IS_TYPE_OR_DECL_P (t))
2839 *walk_subtrees = 1;
2840 break;
2843 return NULL_TREE;
2847 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2848 to addresses of nested functions that require the use of
2849 trampolines. The rewrite will involve a reference a trampoline
2850 generated for the occasion. */
2852 static tree
2853 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2854 struct walk_stmt_info *wi)
2856 struct nesting_info *info = (struct nesting_info *) wi->info;
2857 gimple *stmt = gsi_stmt (*gsi);
2859 switch (gimple_code (stmt))
2861 case GIMPLE_CALL:
2863 /* Only walk call arguments, lest we generate trampolines for
2864 direct calls. */
2865 unsigned long i, nargs = gimple_call_num_args (stmt);
2866 for (i = 0; i < nargs; i++)
2867 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2868 wi, NULL);
2869 break;
2872 case GIMPLE_OMP_TEAMS:
2873 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2875 *handled_ops_p = false;
2876 return NULL_TREE;
2878 goto do_parallel;
2880 case GIMPLE_OMP_TARGET:
2881 if (!is_gimple_omp_offloaded (stmt))
2883 *handled_ops_p = false;
2884 return NULL_TREE;
2886 /* FALLTHRU */
2887 case GIMPLE_OMP_PARALLEL:
2888 case GIMPLE_OMP_TASK:
2889 do_parallel:
2891 tree save_local_var_chain = info->new_local_var_chain;
2892 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2893 info->new_local_var_chain = NULL;
2894 char save_static_chain_added = info->static_chain_added;
2895 info->static_chain_added = 0;
2896 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2897 info, gimple_omp_body_ptr (stmt));
2898 if (info->new_local_var_chain)
2899 declare_vars (info->new_local_var_chain,
2900 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2901 false);
2902 for (int i = 0; i < 2; i++)
2904 tree c, decl;
2905 if ((info->static_chain_added & (1 << i)) == 0)
2906 continue;
2907 decl = i ? get_chain_decl (info) : info->frame_decl;
2908 /* Don't add CHAIN.* or FRAME.* twice. */
2909 for (c = gimple_omp_taskreg_clauses (stmt);
2911 c = OMP_CLAUSE_CHAIN (c))
2912 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2913 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2914 && OMP_CLAUSE_DECL (c) == decl)
2915 break;
2916 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2918 c = build_omp_clause (gimple_location (stmt),
2919 i ? OMP_CLAUSE_FIRSTPRIVATE
2920 : OMP_CLAUSE_SHARED);
2921 OMP_CLAUSE_DECL (c) = decl;
2922 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2923 gimple_omp_taskreg_set_clauses (stmt, c);
2925 else if (c == NULL)
2927 c = build_omp_clause (gimple_location (stmt),
2928 OMP_CLAUSE_MAP);
2929 OMP_CLAUSE_DECL (c) = decl;
2930 OMP_CLAUSE_SET_MAP_KIND (c,
2931 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2932 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2933 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2934 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2938 info->new_local_var_chain = save_local_var_chain;
2939 info->static_chain_added |= save_static_chain_added;
2941 break;
2943 default:
2944 *handled_ops_p = false;
2945 return NULL_TREE;
2948 *handled_ops_p = true;
2949 return NULL_TREE;
2954 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2955 that reference nested functions to make sure that the static chain
2956 is set up properly for the call. */
2958 static tree
2959 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2960 struct walk_stmt_info *wi)
2962 struct nesting_info *const info = (struct nesting_info *) wi->info;
2963 tree decl, target_context;
2964 char save_static_chain_added;
2965 int i;
2966 gimple *stmt = gsi_stmt (*gsi);
2968 switch (gimple_code (stmt))
2970 case GIMPLE_CALL:
2971 if (gimple_call_chain (stmt))
2972 break;
2973 decl = gimple_call_fndecl (stmt);
2974 if (!decl)
2975 break;
2976 target_context = decl_function_context (decl);
2977 if (target_context && DECL_STATIC_CHAIN (decl))
2979 struct nesting_info *i = info;
2980 while (i && i->context != target_context)
2981 i = i->outer;
2982 /* If none of the outer contexts is the target context, this means
2983 that the function is called in a wrong context. */
2984 if (!i)
2985 internal_error ("%s from %s called in %s",
2986 IDENTIFIER_POINTER (DECL_NAME (decl)),
2987 IDENTIFIER_POINTER (DECL_NAME (target_context)),
2988 IDENTIFIER_POINTER (DECL_NAME (info->context)));
2990 gimple_call_set_chain (as_a <gcall *> (stmt),
2991 get_static_chain (info, target_context,
2992 &wi->gsi));
2993 info->static_chain_added |= (1 << (info->context != target_context));
2995 break;
2997 case GIMPLE_OMP_TEAMS:
2998 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
3000 walk_body (convert_gimple_call, NULL, info,
3001 gimple_omp_body_ptr (stmt));
3002 break;
3004 /* FALLTHRU */
3006 case GIMPLE_OMP_PARALLEL:
3007 case GIMPLE_OMP_TASK:
3008 save_static_chain_added = info->static_chain_added;
3009 info->static_chain_added = 0;
3010 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3011 for (i = 0; i < 2; i++)
3013 tree c, decl;
3014 if ((info->static_chain_added & (1 << i)) == 0)
3015 continue;
3016 decl = i ? get_chain_decl (info) : info->frame_decl;
3017 /* Don't add CHAIN.* or FRAME.* twice. */
3018 for (c = gimple_omp_taskreg_clauses (stmt);
3020 c = OMP_CLAUSE_CHAIN (c))
3021 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
3022 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
3023 && OMP_CLAUSE_DECL (c) == decl)
3024 break;
3025 if (c == NULL)
3027 c = build_omp_clause (gimple_location (stmt),
3028 i ? OMP_CLAUSE_FIRSTPRIVATE
3029 : OMP_CLAUSE_SHARED);
3030 OMP_CLAUSE_DECL (c) = decl;
3031 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
3032 gimple_omp_taskreg_set_clauses (stmt, c);
3035 info->static_chain_added |= save_static_chain_added;
3036 break;
3038 case GIMPLE_OMP_TARGET:
3039 if (!is_gimple_omp_offloaded (stmt))
3041 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3042 break;
3044 save_static_chain_added = info->static_chain_added;
3045 info->static_chain_added = 0;
3046 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3047 for (i = 0; i < 2; i++)
3049 tree c, decl;
3050 if ((info->static_chain_added & (1 << i)) == 0)
3051 continue;
3052 decl = i ? get_chain_decl (info) : info->frame_decl;
3053 /* Don't add CHAIN.* or FRAME.* twice. */
3054 for (c = gimple_omp_target_clauses (stmt);
3056 c = OMP_CLAUSE_CHAIN (c))
3057 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
3058 && OMP_CLAUSE_DECL (c) == decl)
3059 break;
3060 if (c == NULL)
3062 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
3063 OMP_CLAUSE_DECL (c) = decl;
3064 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
3065 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
3066 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
3067 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
3071 info->static_chain_added |= save_static_chain_added;
3072 break;
3074 case GIMPLE_OMP_FOR:
3075 walk_body (convert_gimple_call, NULL, info,
3076 gimple_omp_for_pre_body_ptr (stmt));
3077 /* FALLTHRU */
3078 case GIMPLE_OMP_SECTIONS:
3079 case GIMPLE_OMP_SECTION:
3080 case GIMPLE_OMP_STRUCTURED_BLOCK:
3081 case GIMPLE_OMP_SINGLE:
3082 case GIMPLE_OMP_SCOPE:
3083 case GIMPLE_OMP_MASTER:
3084 case GIMPLE_OMP_MASKED:
3085 case GIMPLE_OMP_TASKGROUP:
3086 case GIMPLE_OMP_ORDERED:
3087 case GIMPLE_OMP_SCAN:
3088 case GIMPLE_OMP_CRITICAL:
3089 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3090 break;
3092 default:
3093 /* Keep looking for other operands. */
3094 *handled_ops_p = false;
3095 return NULL_TREE;
3098 *handled_ops_p = true;
3099 return NULL_TREE;
3102 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
3103 call expressions. At the same time, determine if a nested function
3104 actually uses its static chain; if not, remember that. */
3106 static void
3107 convert_all_function_calls (struct nesting_info *root)
3109 unsigned int chain_count = 0, old_chain_count, iter_count;
3110 struct nesting_info *n;
3112 /* First, optimistically clear static_chain for all decls that haven't
3113 used the static chain already for variable access. But always create
3114 it if not optimizing. This makes it possible to reconstruct the static
3115 nesting tree at run time and thus to resolve up-level references from
3116 within the debugger. */
3117 FOR_EACH_NEST_INFO (n, root)
3119 if (n->thunk_p)
3120 continue;
3121 tree decl = n->context;
3122 if (!optimize)
3124 if (n->inner)
3125 (void) get_frame_type (n);
3126 if (n->outer)
3127 (void) get_chain_decl (n);
3129 else if (!n->outer || (!n->chain_decl && !n->chain_field))
3131 DECL_STATIC_CHAIN (decl) = 0;
3132 if (dump_file && (dump_flags & TDF_DETAILS))
3133 fprintf (dump_file, "Guessing no static-chain for %s\n",
3134 lang_hooks.decl_printable_name (decl, 2));
3136 else
3137 DECL_STATIC_CHAIN (decl) = 1;
3138 chain_count += DECL_STATIC_CHAIN (decl);
3141 FOR_EACH_NEST_INFO (n, root)
3142 if (n->thunk_p)
3144 tree decl = n->context;
3145 tree alias = thunk_info::get (cgraph_node::get (decl))->alias;
3146 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
3149 /* Walk the functions and perform transformations. Note that these
3150 transformations can induce new uses of the static chain, which in turn
3151 require re-examining all users of the decl. */
3152 /* ??? It would make sense to try to use the call graph to speed this up,
3153 but the call graph hasn't really been built yet. Even if it did, we
3154 would still need to iterate in this loop since address-of references
3155 wouldn't show up in the callgraph anyway. */
3156 iter_count = 0;
3159 old_chain_count = chain_count;
3160 chain_count = 0;
3161 iter_count++;
3163 if (dump_file && (dump_flags & TDF_DETAILS))
3164 fputc ('\n', dump_file);
3166 FOR_EACH_NEST_INFO (n, root)
3168 if (n->thunk_p)
3169 continue;
3170 tree decl = n->context;
3171 walk_function (convert_tramp_reference_stmt,
3172 convert_tramp_reference_op, n);
3173 walk_function (convert_gimple_call, NULL, n);
3174 chain_count += DECL_STATIC_CHAIN (decl);
3177 FOR_EACH_NEST_INFO (n, root)
3178 if (n->thunk_p)
3180 tree decl = n->context;
3181 tree alias = thunk_info::get (cgraph_node::get (decl))->alias;
3182 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
3185 while (chain_count != old_chain_count);
3187 if (dump_file && (dump_flags & TDF_DETAILS))
3188 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
3189 iter_count);
3192 struct nesting_copy_body_data
3194 copy_body_data cb;
3195 struct nesting_info *root;
3198 /* A helper subroutine for debug_var_chain type remapping. */
3200 static tree
3201 nesting_copy_decl (tree decl, copy_body_data *id)
3203 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
3204 tree *slot = nid->root->var_map->get (decl);
3206 if (slot)
3207 return (tree) *slot;
3209 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
3211 tree new_decl = copy_decl_no_change (decl, id);
3212 DECL_ORIGINAL_TYPE (new_decl)
3213 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
3214 return new_decl;
3217 if (VAR_P (decl)
3218 || TREE_CODE (decl) == PARM_DECL
3219 || TREE_CODE (decl) == RESULT_DECL)
3220 return decl;
3222 return copy_decl_no_change (decl, id);
3225 /* A helper function for remap_vla_decls. See if *TP contains
3226 some remapped variables. */
3228 static tree
3229 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
3231 struct nesting_info *root = (struct nesting_info *) data;
3232 tree t = *tp;
3234 if (DECL_P (t))
3236 *walk_subtrees = 0;
3237 tree *slot = root->var_map->get (t);
3239 if (slot)
3240 return *slot;
3242 return NULL;
3245 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
3246 involved. */
3248 static void
3249 remap_vla_decls (tree block, struct nesting_info *root)
3251 tree var, subblock, val, type;
3252 struct nesting_copy_body_data id;
3254 for (subblock = BLOCK_SUBBLOCKS (block);
3255 subblock;
3256 subblock = BLOCK_CHAIN (subblock))
3257 remap_vla_decls (subblock, root);
3259 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3260 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3262 val = DECL_VALUE_EXPR (var);
3263 type = TREE_TYPE (var);
3265 if (! (INDIRECT_REF_P (val)
3266 && VAR_P (TREE_OPERAND (val, 0))
3267 && variably_modified_type_p (type, NULL)))
3268 continue;
3270 if (root->var_map->get (TREE_OPERAND (val, 0))
3271 || walk_tree (&type, contains_remapped_vars, root, NULL))
3272 break;
3275 if (var == NULL_TREE)
3276 return;
3278 memset (&id, 0, sizeof (id));
3279 id.cb.copy_decl = nesting_copy_decl;
3280 id.cb.decl_map = new hash_map<tree, tree>;
3281 id.root = root;
3283 for (; var; var = DECL_CHAIN (var))
3284 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3286 struct nesting_info *i;
3287 tree newt, context;
3289 val = DECL_VALUE_EXPR (var);
3290 type = TREE_TYPE (var);
3292 if (! (INDIRECT_REF_P (val)
3293 && VAR_P (TREE_OPERAND (val, 0))
3294 && variably_modified_type_p (type, NULL)))
3295 continue;
3297 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
3298 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
3299 continue;
3301 context = decl_function_context (var);
3302 for (i = root; i; i = i->outer)
3303 if (i->context == context)
3304 break;
3306 if (i == NULL)
3307 continue;
3309 /* Fully expand value expressions. This avoids having debug variables
3310 only referenced from them and that can be swept during GC. */
3311 if (slot)
3313 tree t = (tree) *slot;
3314 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
3315 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
3318 id.cb.src_fn = i->context;
3319 id.cb.dst_fn = i->context;
3320 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3322 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3323 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3325 newt = TREE_TYPE (newt);
3326 type = TREE_TYPE (type);
3328 if (TYPE_NAME (newt)
3329 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3330 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3331 && newt != type
3332 && TYPE_NAME (newt) == TYPE_NAME (type))
3333 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3335 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3336 if (val != DECL_VALUE_EXPR (var))
3337 SET_DECL_VALUE_EXPR (var, val);
3340 delete id.cb.decl_map;
3343 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3344 involved. */
3346 static void
3347 fixup_vla_decls (tree block)
3349 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3350 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3352 tree val = DECL_VALUE_EXPR (var);
3354 if (! (INDIRECT_REF_P (val)
3355 && VAR_P (TREE_OPERAND (val, 0))
3356 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3357 continue;
3359 /* Fully expand value expressions. This avoids having debug variables
3360 only referenced from them and that can be swept during GC. */
3361 val = build1 (INDIRECT_REF, TREE_TYPE (val),
3362 DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3363 SET_DECL_VALUE_EXPR (var, val);
3366 for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3367 fixup_vla_decls (sub);
3370 /* Fold the MEM_REF *E. */
3371 bool
3372 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3374 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
3375 *ref_p = fold (*ref_p);
3376 return true;
3379 /* Given DECL, a nested function, build an initialization call for FIELD,
3380 the trampoline or descriptor for DECL, using FUNC as the function. */
3382 static gcall *
3383 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3384 tree func)
3386 tree arg1, arg2, arg3, x;
3388 gcc_assert (DECL_STATIC_CHAIN (decl));
3389 arg3 = build_addr (info->frame_decl);
3391 arg2 = build_addr (decl);
3393 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3394 info->frame_decl, field, NULL_TREE);
3395 arg1 = build_addr (x);
3397 return gimple_build_call (func, 3, arg1, arg2, arg3);
3400 /* Do "everything else" to clean up or complete state collected by the various
3401 walking passes -- create a field to hold the frame base address, lay out the
3402 types and decls, generate code to initialize the frame decl, store critical
3403 expressions in the struct function for rtl to find. */
3405 static void
3406 finalize_nesting_tree_1 (struct nesting_info *root)
3408 gimple_seq cleanup_list = NULL;
3409 gimple_seq stmt_list = NULL;
3410 gimple *stmt;
3411 tree context = root->context;
3412 struct function *sf;
3414 if (root->thunk_p)
3415 return;
3417 /* If we created a non-local frame type or decl, we need to lay them
3418 out at this time. */
3419 if (root->frame_type)
3421 /* Debugging information needs to compute the frame base address of the
3422 parent frame out of the static chain from the nested frame.
3424 The static chain is the address of the FRAME record, so one could
3425 imagine it would be possible to compute the frame base address just
3426 adding a constant offset to this address. Unfortunately, this is not
3427 possible: if the FRAME object has alignment constraints that are
3428 stronger than the stack, then the offset between the frame base and
3429 the FRAME object will be dynamic.
3431 What we do instead is to append a field to the FRAME object that holds
3432 the frame base address: then debug info just has to fetch this
3433 field. */
3435 /* Debugging information will refer to the CFA as the frame base
3436 address: we will do the same here. */
3437 const tree frame_addr_fndecl
3438 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3440 /* Create a field in the FRAME record to hold the frame base address for
3441 this stack frame. Since it will be used only by the debugger, put it
3442 at the end of the record in order not to shift all other offsets. */
3443 tree fb_decl = make_node (FIELD_DECL);
3445 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3446 TREE_TYPE (fb_decl) = ptr_type_node;
3447 TREE_ADDRESSABLE (fb_decl) = 1;
3448 DECL_CONTEXT (fb_decl) = root->frame_type;
3449 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3450 fb_decl);
3452 /* In some cases the frame type will trigger the -Wpadded warning.
3453 This is not helpful; suppress it. */
3454 int save_warn_padded = warn_padded;
3455 warn_padded = 0;
3456 layout_type (root->frame_type);
3457 warn_padded = save_warn_padded;
3458 layout_decl (root->frame_decl, 0);
3460 /* Initialize the frame base address field. If the builtin we need is
3461 not available, set it to NULL so that debugging information does not
3462 reference junk. */
3463 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3464 root->frame_decl, fb_decl, NULL_TREE);
3465 tree fb_tmp;
3467 if (frame_addr_fndecl != NULL_TREE)
3469 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3470 integer_zero_node);
3471 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3473 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3475 else
3476 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3477 gimple_seq_add_stmt (&stmt_list,
3478 gimple_build_assign (fb_ref, fb_tmp));
3480 declare_vars (root->frame_decl,
3481 gimple_seq_first_stmt (gimple_body (context)), true);
3484 /* If any parameters were referenced non-locally, then we need to insert
3485 a copy or a pointer. */
3486 if (root->any_parm_remapped)
3488 tree p;
3489 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3491 tree field, x, y;
3493 field = lookup_field_for_decl (root, p, NO_INSERT);
3494 if (!field)
3495 continue;
3497 if (use_pointer_in_frame (p))
3498 x = build_addr (p);
3499 else
3500 x = p;
3502 /* If the assignment is from a non-register the stmt is
3503 not valid gimple. Make it so by using a temporary instead. */
3504 if (!is_gimple_reg (x)
3505 && is_gimple_reg_type (TREE_TYPE (x)))
3507 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3508 x = init_tmp_var (root, x, &gsi);
3511 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3512 root->frame_decl, field, NULL_TREE);
3513 stmt = gimple_build_assign (y, x);
3514 gimple_seq_add_stmt (&stmt_list, stmt);
3518 /* If a chain_field was created, then it needs to be initialized
3519 from chain_decl. */
3520 if (root->chain_field)
3522 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3523 root->frame_decl, root->chain_field, NULL_TREE);
3524 stmt = gimple_build_assign (x, get_chain_decl (root));
3525 gimple_seq_add_stmt (&stmt_list, stmt);
3528 /* If trampolines were created, then we need to initialize them. */
3529 if (root->any_tramp_created)
3531 struct nesting_info *i;
3532 for (i = root->inner; i ; i = i->next)
3534 tree field, x;
3536 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3537 if (!field)
3538 continue;
3540 if (flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
3542 /* We pass a whole bunch of arguments to the builtin function that
3543 creates the off-stack trampoline, these are
3544 1. The nested function chain value (that must be passed to the
3545 nested function so it can find the function arguments).
3546 2. A pointer to the nested function implementation,
3547 3. The address in the local stack frame where we should write
3548 the address of the trampoline.
3550 When this code was originally written I just kind of threw
3551 everything at the builtin, figuring I'd work out what was
3552 actually needed later, I think, the stack pointer could
3553 certainly be dropped, arguments #2 and #4 are based off the
3554 stack pointer anyway, so #1 doesn't seem to add much value. */
3555 tree arg1, arg2, arg3;
3557 gcc_assert (DECL_STATIC_CHAIN (i->context));
3558 arg1 = build_addr (root->frame_decl);
3559 arg2 = build_addr (i->context);
3561 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3562 root->frame_decl, field, NULL_TREE);
3563 arg3 = build_addr (x);
3565 x = builtin_decl_explicit (BUILT_IN_GCC_NESTED_PTR_CREATED);
3566 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
3567 gimple_seq_add_stmt (&stmt_list, stmt);
3569 /* This call to delete the nested function trampoline is added to
3570 the cleanup list, and called when we exit the current scope. */
3571 x = builtin_decl_explicit (BUILT_IN_GCC_NESTED_PTR_DELETED);
3572 stmt = gimple_build_call (x, 0);
3573 gimple_seq_add_stmt (&cleanup_list, stmt);
3575 else
3577 /* Original code to initialise the on stack trampoline. */
3578 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3579 stmt = build_init_call_stmt (root, i->context, field, x);
3580 gimple_seq_add_stmt (&stmt_list, stmt);
3585 /* If descriptors were created, then we need to initialize them. */
3586 if (root->any_descr_created)
3588 struct nesting_info *i;
3589 for (i = root->inner; i ; i = i->next)
3591 tree field, x;
3593 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3594 if (!field)
3595 continue;
3597 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3598 stmt = build_init_call_stmt (root, i->context, field, x);
3599 gimple_seq_add_stmt (&stmt_list, stmt);
3603 /* If we created initialization statements, insert them. */
3604 if (stmt_list)
3606 if (flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
3608 /* Handle off-stack trampolines. */
3609 gbind *bind;
3610 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3611 annotate_all_with_location (cleanup_list, DECL_SOURCE_LOCATION (context));
3612 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3613 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3615 gimple_seq xxx_list = NULL;
3617 if (cleanup_list != NULL)
3619 /* Maybe we shouldn't be creating this try/finally if -fno-exceptions is
3620 in use. If this is the case, then maybe we should, instead, be
3621 inserting the cleanup code onto every path out of this function? Not
3622 yet figured out how we would do this. */
3623 gtry *t = gimple_build_try (stmt_list, cleanup_list, GIMPLE_TRY_FINALLY);
3624 gimple_seq_add_stmt (&xxx_list, t);
3626 else
3627 xxx_list = stmt_list;
3629 gimple_bind_set_body (bind, xxx_list);
3631 else
3633 /* The traditional, on stack trampolines. */
3634 gbind *bind;
3635 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3636 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3637 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3638 gimple_bind_set_body (bind, stmt_list);
3642 /* If a chain_decl was created, then it needs to be registered with
3643 struct function so that it gets initialized from the static chain
3644 register at the beginning of the function. */
3645 sf = DECL_STRUCT_FUNCTION (root->context);
3646 sf->static_chain_decl = root->chain_decl;
3648 /* Similarly for the non-local goto save area. */
3649 if (root->nl_goto_field)
3651 sf->nonlocal_goto_save_area
3652 = get_frame_field (root, context, root->nl_goto_field, NULL);
3653 sf->has_nonlocal_label = 1;
3656 /* Make sure all new local variables get inserted into the
3657 proper BIND_EXPR. */
3658 if (root->new_local_var_chain)
3659 declare_vars (root->new_local_var_chain,
3660 gimple_seq_first_stmt (gimple_body (root->context)),
3661 false);
3663 if (root->debug_var_chain)
3665 tree debug_var;
3666 gbind *scope;
3668 remap_vla_decls (DECL_INITIAL (root->context), root);
3670 for (debug_var = root->debug_var_chain; debug_var;
3671 debug_var = DECL_CHAIN (debug_var))
3672 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3673 break;
3675 /* If there are any debug decls with variable length types,
3676 remap those types using other debug_var_chain variables. */
3677 if (debug_var)
3679 struct nesting_copy_body_data id;
3681 memset (&id, 0, sizeof (id));
3682 id.cb.copy_decl = nesting_copy_decl;
3683 id.cb.decl_map = new hash_map<tree, tree>;
3684 id.root = root;
3686 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3687 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3689 tree type = TREE_TYPE (debug_var);
3690 tree newt, t = type;
3691 struct nesting_info *i;
3693 for (i = root; i; i = i->outer)
3694 if (variably_modified_type_p (type, i->context))
3695 break;
3697 if (i == NULL)
3698 continue;
3700 id.cb.src_fn = i->context;
3701 id.cb.dst_fn = i->context;
3702 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3704 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3705 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3707 newt = TREE_TYPE (newt);
3708 t = TREE_TYPE (t);
3710 if (TYPE_NAME (newt)
3711 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3712 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3713 && newt != t
3714 && TYPE_NAME (newt) == TYPE_NAME (t))
3715 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3718 delete id.cb.decl_map;
3721 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3722 if (gimple_bind_block (scope))
3723 declare_vars (root->debug_var_chain, scope, true);
3724 else
3725 BLOCK_VARS (DECL_INITIAL (root->context))
3726 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3727 root->debug_var_chain);
3729 else
3730 fixup_vla_decls (DECL_INITIAL (root->context));
3732 /* Fold the rewritten MEM_REF trees. */
3733 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3735 /* Dump the translated tree function. */
3736 if (dump_file)
3738 fputs ("\n\n", dump_file);
3739 dump_function_to_file (root->context, dump_file, dump_flags);
3743 static void
3744 finalize_nesting_tree (struct nesting_info *root)
3746 struct nesting_info *n;
3747 FOR_EACH_NEST_INFO (n, root)
3748 finalize_nesting_tree_1 (n);
3751 /* Unnest the nodes and pass them to cgraph. */
3753 static void
3754 unnest_nesting_tree_1 (struct nesting_info *root)
3756 struct cgraph_node *node = cgraph_node::get (root->context);
3758 /* For nested functions update the cgraph to reflect unnesting.
3759 We also delay finalizing of these functions up to this point. */
3760 if (nested_function_info::get (node)->origin)
3762 unnest_function (node);
3763 if (!root->thunk_p)
3764 cgraph_node::finalize_function (root->context, true);
3768 static void
3769 unnest_nesting_tree (struct nesting_info *root)
3771 struct nesting_info *n;
3772 FOR_EACH_NEST_INFO (n, root)
3773 unnest_nesting_tree_1 (n);
3776 /* Free the data structures allocated during this pass. */
3778 static void
3779 free_nesting_tree (struct nesting_info *root)
3781 struct nesting_info *node, *next;
3783 node = iter_nestinfo_start (root);
3786 next = iter_nestinfo_next (node);
3787 delete node->var_map;
3788 delete node->field_map;
3789 delete node->mem_refs;
3790 free (node);
3791 node = next;
3793 while (node);
3796 /* Gimplify a function and all its nested functions. */
3797 static void
3798 gimplify_all_functions (struct cgraph_node *root)
3800 struct cgraph_node *iter;
3801 if (!gimple_body (root->decl))
3802 gimplify_function_tree (root->decl);
3803 for (iter = first_nested_function (root); iter;
3804 iter = next_nested_function (iter))
3805 if (!iter->thunk)
3806 gimplify_all_functions (iter);
3809 /* Main entry point for this pass. Process FNDECL and all of its nested
3810 subroutines and turn them into something less tightly bound. */
3812 void
3813 lower_nested_functions (tree fndecl)
3815 struct cgraph_node *cgn;
3816 struct nesting_info *root;
3818 /* If there are no nested functions, there's nothing to do. */
3819 cgn = cgraph_node::get (fndecl);
3820 if (!first_nested_function (cgn))
3821 return;
3823 gimplify_all_functions (cgn);
3825 set_dump_file (dump_begin (TDI_nested, &dump_flags));
3826 if (dump_file)
3827 fprintf (dump_file, "\n;; Function %s\n\n",
3828 lang_hooks.decl_printable_name (fndecl, 2));
3830 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3831 root = create_nesting_tree (cgn);
3833 walk_all_functions (convert_nonlocal_reference_stmt,
3834 convert_nonlocal_reference_op,
3835 root);
3836 walk_all_functions (convert_local_reference_stmt,
3837 convert_local_reference_op,
3838 root);
3839 walk_all_functions (convert_nl_goto_reference, NULL, root);
3840 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3842 convert_all_function_calls (root);
3843 finalize_nesting_tree (root);
3844 unnest_nesting_tree (root);
3846 free_nesting_tree (root);
3847 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3849 if (dump_file)
3851 dump_end (TDI_nested, dump_file);
3852 set_dump_file (NULL);
3856 #include "gt-tree-nested.h"