Fix warnings building linux-atomic.c and fptr.c on hppa64-linux
[official-gcc.git] / gcc / tree-nested.c
blobc7f50ebd21c2f7e8dcfaa34560fec8f3443b2791
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2021 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.c 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 align = TRAMPOLINE_ALIGNMENT;
615 size = TRAMPOLINE_SIZE;
617 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
618 then allocate extra space so that we can do dynamic alignment. */
619 if (align > STACK_BOUNDARY)
621 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
622 align = STACK_BOUNDARY;
625 t = build_index_type (size_int (size - 1));
626 t = build_array_type (char_type_node, t);
627 t = build_decl (DECL_SOURCE_LOCATION (info->context),
628 FIELD_DECL, get_identifier ("__data"), t);
629 SET_DECL_ALIGN (t, align);
630 DECL_USER_ALIGN (t) = 1;
632 trampoline_type = make_node (RECORD_TYPE);
633 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
634 TYPE_FIELDS (trampoline_type) = t;
635 layout_type (trampoline_type);
636 DECL_CONTEXT (t) = trampoline_type;
638 return trampoline_type;
641 /* Build or return the type used to represent a nested function descriptor. */
643 static GTY(()) tree descriptor_type;
645 static tree
646 get_descriptor_type (struct nesting_info *info)
648 /* The base alignment is that of a function. */
649 const unsigned align = FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY);
650 tree t;
652 if (descriptor_type)
653 return descriptor_type;
655 t = build_index_type (integer_one_node);
656 t = build_array_type (ptr_type_node, t);
657 t = build_decl (DECL_SOURCE_LOCATION (info->context),
658 FIELD_DECL, get_identifier ("__data"), t);
659 SET_DECL_ALIGN (t, MAX (TYPE_ALIGN (ptr_type_node), align));
660 DECL_USER_ALIGN (t) = 1;
662 descriptor_type = make_node (RECORD_TYPE);
663 TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
664 TYPE_FIELDS (descriptor_type) = t;
665 layout_type (descriptor_type);
666 DECL_CONTEXT (t) = descriptor_type;
668 return descriptor_type;
671 /* Given DECL, a nested function, find or create an element in the
672 var map for this function. */
674 static tree
675 lookup_element_for_decl (struct nesting_info *info, tree decl,
676 enum insert_option insert)
678 if (insert == NO_INSERT)
680 tree *slot = info->var_map->get (decl);
681 return slot ? *slot : NULL_TREE;
684 tree *slot = &info->var_map->get_or_insert (decl);
685 if (!*slot)
686 *slot = build_tree_list (NULL_TREE, NULL_TREE);
688 return (tree) *slot;
691 /* Given DECL, a nested function, create a field in the non-local
692 frame structure for this function. */
694 static tree
695 create_field_for_decl (struct nesting_info *info, tree decl, tree type)
697 tree field = make_node (FIELD_DECL);
698 DECL_NAME (field) = DECL_NAME (decl);
699 TREE_TYPE (field) = type;
700 TREE_ADDRESSABLE (field) = 1;
701 insert_field_into_struct (get_frame_type (info), field);
702 return field;
705 /* Given DECL, a nested function, find or create a field in the non-local
706 frame structure for a trampoline for this function. */
708 static tree
709 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
710 enum insert_option insert)
712 tree elt, field;
714 elt = lookup_element_for_decl (info, decl, insert);
715 if (!elt)
716 return NULL_TREE;
718 field = TREE_PURPOSE (elt);
720 if (!field && insert == INSERT)
722 field = create_field_for_decl (info, decl, get_trampoline_type (info));
723 TREE_PURPOSE (elt) = field;
724 info->any_tramp_created = true;
727 return field;
730 /* Given DECL, a nested function, find or create a field in the non-local
731 frame structure for a descriptor for this function. */
733 static tree
734 lookup_descr_for_decl (struct nesting_info *info, tree decl,
735 enum insert_option insert)
737 tree elt, field;
739 elt = lookup_element_for_decl (info, decl, insert);
740 if (!elt)
741 return NULL_TREE;
743 field = TREE_VALUE (elt);
745 if (!field && insert == INSERT)
747 field = create_field_for_decl (info, decl, get_descriptor_type (info));
748 TREE_VALUE (elt) = field;
749 info->any_descr_created = true;
752 return field;
755 /* Build or return the field within the non-local frame state that holds
756 the non-local goto "jmp_buf". The buffer itself is maintained by the
757 rtl middle-end as dynamic stack space is allocated. */
759 static tree
760 get_nl_goto_field (struct nesting_info *info)
762 tree field = info->nl_goto_field;
763 if (!field)
765 unsigned size;
766 tree type;
768 /* For __builtin_nonlocal_goto, we need N words. The first is the
769 frame pointer, the rest is for the target's stack pointer save
770 area. The number of words is controlled by STACK_SAVEAREA_MODE;
771 not the best interface, but it'll do for now. */
772 if (Pmode == ptr_mode)
773 type = ptr_type_node;
774 else
775 type = lang_hooks.types.type_for_mode (Pmode, 1);
777 scalar_int_mode mode
778 = as_a <scalar_int_mode> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
779 size = GET_MODE_SIZE (mode);
780 size = size / GET_MODE_SIZE (Pmode);
781 size = size + 1;
783 type = build_array_type
784 (type, build_index_type (size_int (size)));
786 field = make_node (FIELD_DECL);
787 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
788 TREE_TYPE (field) = type;
789 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
790 TREE_ADDRESSABLE (field) = 1;
792 insert_field_into_struct (get_frame_type (info), field);
794 info->nl_goto_field = field;
797 return field;
800 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
802 static void
803 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
804 struct nesting_info *info, gimple_seq *pseq)
806 struct walk_stmt_info wi;
808 memset (&wi, 0, sizeof (wi));
809 wi.info = info;
810 wi.val_only = true;
811 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
815 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
817 static inline void
818 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
819 struct nesting_info *info)
821 gimple_seq body = gimple_body (info->context);
822 walk_body (callback_stmt, callback_op, info, &body);
823 gimple_set_body (info->context, body);
826 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
828 static void
829 walk_gimple_omp_for (gomp_for *for_stmt,
830 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
831 struct nesting_info *info)
833 struct walk_stmt_info wi;
834 gimple_seq seq;
835 tree t;
836 size_t i;
838 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
840 seq = NULL;
841 memset (&wi, 0, sizeof (wi));
842 wi.info = info;
843 wi.gsi = gsi_last (seq);
845 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
847 wi.val_only = false;
848 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
849 &wi, NULL);
850 wi.val_only = true;
851 wi.is_lhs = false;
852 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
853 &wi, NULL);
855 wi.val_only = true;
856 wi.is_lhs = false;
857 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
858 &wi, NULL);
860 t = gimple_omp_for_incr (for_stmt, i);
861 gcc_assert (BINARY_CLASS_P (t));
862 wi.val_only = false;
863 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
864 wi.val_only = true;
865 wi.is_lhs = false;
866 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
869 seq = gsi_seq (wi.gsi);
870 if (!gimple_seq_empty_p (seq))
872 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
873 annotate_all_with_location (seq, gimple_location (for_stmt));
874 gimple_seq_add_seq (&pre_body, seq);
875 gimple_omp_for_set_pre_body (for_stmt, pre_body);
879 /* Similarly for ROOT and all functions nested underneath, depth first. */
881 static void
882 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
883 struct nesting_info *root)
885 struct nesting_info *n;
886 FOR_EACH_NEST_INFO (n, root)
887 walk_function (callback_stmt, callback_op, n);
891 /* We have to check for a fairly pathological case. The operands of function
892 nested function are to be interpreted in the context of the enclosing
893 function. So if any are variably-sized, they will get remapped when the
894 enclosing function is inlined. But that remapping would also have to be
895 done in the types of the PARM_DECLs of the nested function, meaning the
896 argument types of that function will disagree with the arguments in the
897 calls to that function. So we'd either have to make a copy of the nested
898 function corresponding to each time the enclosing function was inlined or
899 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
900 function. The former is not practical. The latter would still require
901 detecting this case to know when to add the conversions. So, for now at
902 least, we don't inline such an enclosing function.
904 We have to do that check recursively, so here return indicating whether
905 FNDECL has such a nested function. ORIG_FN is the function we were
906 trying to inline to use for checking whether any argument is variably
907 modified by anything in it.
909 It would be better to do this in tree-inline.c so that we could give
910 the appropriate warning for why a function can't be inlined, but that's
911 too late since the nesting structure has already been flattened and
912 adding a flag just to record this fact seems a waste of a flag. */
914 static bool
915 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
917 struct cgraph_node *cgn = cgraph_node::get (fndecl);
918 tree arg;
920 for (cgn = first_nested_function (cgn); cgn;
921 cgn = next_nested_function (cgn))
923 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
924 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
925 return true;
927 if (check_for_nested_with_variably_modified (cgn->decl,
928 orig_fndecl))
929 return true;
932 return false;
935 /* Construct our local datastructure describing the function nesting
936 tree rooted by CGN. */
938 static struct nesting_info *
939 create_nesting_tree (struct cgraph_node *cgn)
941 struct nesting_info *info = XCNEW (struct nesting_info);
942 info->field_map = new hash_map<tree, tree>;
943 info->var_map = new hash_map<tree, tree>;
944 info->mem_refs = new hash_set<tree *>;
945 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
946 info->context = cgn->decl;
947 info->thunk_p = cgn->thunk;
949 for (cgn = first_nested_function (cgn); cgn;
950 cgn = next_nested_function (cgn))
952 struct nesting_info *sub = create_nesting_tree (cgn);
953 sub->outer = info;
954 sub->next = info->inner;
955 info->inner = sub;
958 /* See discussion at check_for_nested_with_variably_modified for a
959 discussion of why this has to be here. */
960 if (check_for_nested_with_variably_modified (info->context, info->context))
961 DECL_UNINLINABLE (info->context) = true;
963 return info;
966 /* Return an expression computing the static chain for TARGET_CONTEXT
967 from INFO->CONTEXT. Insert any necessary computations before TSI. */
969 static tree
970 get_static_chain (struct nesting_info *info, tree target_context,
971 gimple_stmt_iterator *gsi)
973 struct nesting_info *i;
974 tree x;
976 if (info->context == target_context)
978 x = build_addr (info->frame_decl);
979 info->static_chain_added |= 1;
981 else
983 x = get_chain_decl (info);
984 info->static_chain_added |= 2;
986 for (i = info->outer; i->context != target_context; i = i->outer)
988 tree field = get_chain_field (i);
990 x = build_simple_mem_ref_notrap (x);
991 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
992 x = init_tmp_var (info, x, gsi);
996 return x;
1000 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
1001 frame as seen from INFO->CONTEXT. Insert any necessary computations
1002 before GSI. */
1004 static tree
1005 get_frame_field (struct nesting_info *info, tree target_context,
1006 tree field, gimple_stmt_iterator *gsi)
1008 struct nesting_info *i;
1009 tree x;
1011 if (info->context == target_context)
1013 /* Make sure frame_decl gets created. */
1014 (void) get_frame_type (info);
1015 x = info->frame_decl;
1016 info->static_chain_added |= 1;
1018 else
1020 x = get_chain_decl (info);
1021 info->static_chain_added |= 2;
1023 for (i = info->outer; i->context != target_context; i = i->outer)
1025 tree field = get_chain_field (i);
1027 x = build_simple_mem_ref_notrap (x);
1028 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1029 x = init_tmp_var (info, x, gsi);
1032 x = build_simple_mem_ref_notrap (x);
1035 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1036 TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (field);
1037 return x;
1040 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
1042 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
1043 in the nested function with DECL_VALUE_EXPR set to reference the true
1044 variable in the parent function. This is used both for debug info
1045 and in OMP lowering. */
1047 static tree
1048 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
1050 tree target_context;
1051 struct nesting_info *i;
1052 tree x, field, new_decl;
1054 tree *slot = &info->var_map->get_or_insert (decl);
1056 if (*slot)
1057 return *slot;
1059 target_context = decl_function_context (decl);
1061 /* A copy of the code in get_frame_field, but without the temporaries. */
1062 if (info->context == target_context)
1064 /* Make sure frame_decl gets created. */
1065 (void) get_frame_type (info);
1066 x = info->frame_decl;
1067 i = info;
1068 info->static_chain_added |= 1;
1070 else
1072 x = get_chain_decl (info);
1073 info->static_chain_added |= 2;
1074 for (i = info->outer; i->context != target_context; i = i->outer)
1076 field = get_chain_field (i);
1077 x = build_simple_mem_ref_notrap (x);
1078 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1080 x = build_simple_mem_ref_notrap (x);
1083 field = lookup_field_for_decl (i, decl, INSERT);
1084 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1085 if (use_pointer_in_frame (decl))
1086 x = build_simple_mem_ref_notrap (x);
1088 /* ??? We should be remapping types as well, surely. */
1089 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1090 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1091 DECL_CONTEXT (new_decl) = info->context;
1092 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1093 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1094 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1095 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1096 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1097 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1098 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1099 if ((TREE_CODE (decl) == PARM_DECL
1100 || TREE_CODE (decl) == RESULT_DECL
1101 || VAR_P (decl))
1102 && DECL_BY_REFERENCE (decl))
1103 DECL_BY_REFERENCE (new_decl) = 1;
1105 SET_DECL_VALUE_EXPR (new_decl, x);
1106 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1108 *slot = new_decl;
1109 DECL_CHAIN (new_decl) = info->debug_var_chain;
1110 info->debug_var_chain = new_decl;
1112 if (!optimize
1113 && info->context != target_context
1114 && variably_modified_type_p (TREE_TYPE (decl), NULL))
1115 note_nonlocal_vla_type (info, TREE_TYPE (decl));
1117 return new_decl;
1121 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1122 and PARM_DECLs that belong to outer functions.
1124 The rewrite will involve some number of structure accesses back up
1125 the static chain. E.g. for a variable FOO up one nesting level it'll
1126 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1127 indirections apply to decls for which use_pointer_in_frame is true. */
1129 static tree
1130 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
1132 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1133 struct nesting_info *const info = (struct nesting_info *) wi->info;
1134 tree t = *tp;
1136 *walk_subtrees = 0;
1137 switch (TREE_CODE (t))
1139 case VAR_DECL:
1140 /* Non-automatic variables are never processed. */
1141 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1142 break;
1143 /* FALLTHRU */
1145 case PARM_DECL:
1147 tree x, target_context = decl_function_context (t);
1149 if (info->context == target_context)
1150 break;
1152 wi->changed = true;
1154 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1155 x = get_nonlocal_debug_decl (info, t);
1156 else
1158 struct nesting_info *i = info;
1159 while (i && i->context != target_context)
1160 i = i->outer;
1161 /* If none of the outer contexts is the target context, this means
1162 that the VAR or PARM_DECL is referenced in a wrong context. */
1163 if (!i)
1164 internal_error ("%s from %s referenced in %s",
1165 IDENTIFIER_POINTER (DECL_NAME (t)),
1166 IDENTIFIER_POINTER (DECL_NAME (target_context)),
1167 IDENTIFIER_POINTER (DECL_NAME (info->context)));
1169 x = lookup_field_for_decl (i, t, INSERT);
1170 x = get_frame_field (info, target_context, x, &wi->gsi);
1171 if (use_pointer_in_frame (t))
1173 x = init_tmp_var (info, x, &wi->gsi);
1174 x = build_simple_mem_ref_notrap (x);
1178 if (wi->val_only)
1180 if (wi->is_lhs)
1181 x = save_tmp_var (info, x, &wi->gsi);
1182 else
1183 x = init_tmp_var (info, x, &wi->gsi);
1186 *tp = x;
1188 break;
1190 case LABEL_DECL:
1191 /* We're taking the address of a label from a parent function, but
1192 this is not itself a non-local goto. Mark the label such that it
1193 will not be deleted, much as we would with a label address in
1194 static storage. */
1195 if (decl_function_context (t) != info->context)
1196 FORCED_LABEL (t) = 1;
1197 break;
1199 case ADDR_EXPR:
1201 bool save_val_only = wi->val_only;
1203 wi->val_only = false;
1204 wi->is_lhs = false;
1205 wi->changed = false;
1206 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1207 wi->val_only = true;
1209 if (wi->changed)
1211 tree save_context;
1213 /* If we changed anything, we might no longer be directly
1214 referencing a decl. */
1215 save_context = current_function_decl;
1216 current_function_decl = info->context;
1217 recompute_tree_invariant_for_addr_expr (t);
1219 /* If the callback converted the address argument in a context
1220 where we only accept variables (and min_invariant, presumably),
1221 then compute the address into a temporary. */
1222 if (save_val_only)
1223 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1224 t, &wi->gsi);
1225 current_function_decl = save_context;
1228 break;
1230 case REALPART_EXPR:
1231 case IMAGPART_EXPR:
1232 case COMPONENT_REF:
1233 case ARRAY_REF:
1234 case ARRAY_RANGE_REF:
1235 case BIT_FIELD_REF:
1236 /* Go down this entire nest and just look at the final prefix and
1237 anything that describes the references. Otherwise, we lose track
1238 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1239 wi->val_only = true;
1240 wi->is_lhs = false;
1241 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1243 if (TREE_CODE (t) == COMPONENT_REF)
1244 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1245 NULL);
1246 else if (TREE_CODE (t) == ARRAY_REF
1247 || TREE_CODE (t) == ARRAY_RANGE_REF)
1249 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1250 wi, NULL);
1251 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1252 wi, NULL);
1253 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1254 wi, NULL);
1257 wi->val_only = false;
1258 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1259 break;
1261 case VIEW_CONVERT_EXPR:
1262 /* Just request to look at the subtrees, leaving val_only and lhs
1263 untouched. This might actually be for !val_only + lhs, in which
1264 case we don't want to force a replacement by a temporary. */
1265 *walk_subtrees = 1;
1266 break;
1268 default:
1269 if (!IS_TYPE_OR_DECL_P (t))
1271 *walk_subtrees = 1;
1272 wi->val_only = true;
1273 wi->is_lhs = false;
1275 break;
1278 return NULL_TREE;
1281 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1282 struct walk_stmt_info *);
1284 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1285 and PARM_DECLs that belong to outer functions. */
1287 static bool
1288 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1290 struct nesting_info *const info = (struct nesting_info *) wi->info;
1291 bool need_chain = false, need_stmts = false;
1292 tree clause, decl, *pdecl;
1293 int dummy;
1294 bitmap new_suppress;
1296 new_suppress = BITMAP_GGC_ALLOC ();
1297 bitmap_copy (new_suppress, info->suppress_expansion);
1299 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1301 pdecl = NULL;
1302 switch (OMP_CLAUSE_CODE (clause))
1304 case OMP_CLAUSE_REDUCTION:
1305 case OMP_CLAUSE_IN_REDUCTION:
1306 case OMP_CLAUSE_TASK_REDUCTION:
1307 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1308 need_stmts = true;
1309 if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
1311 pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
1312 if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
1313 pdecl = &TREE_OPERAND (*pdecl, 0);
1314 if (TREE_CODE (*pdecl) == INDIRECT_REF
1315 || TREE_CODE (*pdecl) == ADDR_EXPR)
1316 pdecl = &TREE_OPERAND (*pdecl, 0);
1318 goto do_decl_clause;
1320 case OMP_CLAUSE_LASTPRIVATE:
1321 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1322 need_stmts = true;
1323 goto do_decl_clause;
1325 case OMP_CLAUSE_LINEAR:
1326 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1327 need_stmts = true;
1328 wi->val_only = true;
1329 wi->is_lhs = false;
1330 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1331 &dummy, wi);
1332 goto do_decl_clause;
1334 case OMP_CLAUSE_PRIVATE:
1335 case OMP_CLAUSE_FIRSTPRIVATE:
1336 case OMP_CLAUSE_COPYPRIVATE:
1337 case OMP_CLAUSE_SHARED:
1338 case OMP_CLAUSE_TO_DECLARE:
1339 case OMP_CLAUSE_LINK:
1340 case OMP_CLAUSE_USE_DEVICE_PTR:
1341 case OMP_CLAUSE_USE_DEVICE_ADDR:
1342 case OMP_CLAUSE_IS_DEVICE_PTR:
1343 case OMP_CLAUSE_DETACH:
1344 do_decl_clause:
1345 if (pdecl == NULL)
1346 pdecl = &OMP_CLAUSE_DECL (clause);
1347 decl = *pdecl;
1348 if (VAR_P (decl)
1349 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1350 break;
1351 if (decl_function_context (decl) != info->context)
1353 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1354 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1355 bitmap_set_bit (new_suppress, DECL_UID (decl));
1356 *pdecl = get_nonlocal_debug_decl (info, decl);
1357 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1358 need_chain = true;
1360 break;
1362 case OMP_CLAUSE_SCHEDULE:
1363 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1364 break;
1365 /* FALLTHRU */
1366 case OMP_CLAUSE_FINAL:
1367 case OMP_CLAUSE_IF:
1368 case OMP_CLAUSE_NUM_THREADS:
1369 case OMP_CLAUSE_DEPEND:
1370 case OMP_CLAUSE_DEVICE:
1371 case OMP_CLAUSE_NUM_TEAMS:
1372 case OMP_CLAUSE_THREAD_LIMIT:
1373 case OMP_CLAUSE_SAFELEN:
1374 case OMP_CLAUSE_SIMDLEN:
1375 case OMP_CLAUSE_PRIORITY:
1376 case OMP_CLAUSE_GRAINSIZE:
1377 case OMP_CLAUSE_NUM_TASKS:
1378 case OMP_CLAUSE_HINT:
1379 case OMP_CLAUSE_FILTER:
1380 case OMP_CLAUSE_NUM_GANGS:
1381 case OMP_CLAUSE_NUM_WORKERS:
1382 case OMP_CLAUSE_VECTOR_LENGTH:
1383 case OMP_CLAUSE_GANG:
1384 case OMP_CLAUSE_WORKER:
1385 case OMP_CLAUSE_VECTOR:
1386 case OMP_CLAUSE_ASYNC:
1387 case OMP_CLAUSE_WAIT:
1388 /* Several OpenACC clauses have optional arguments. Check if they
1389 are present. */
1390 if (OMP_CLAUSE_OPERAND (clause, 0))
1392 wi->val_only = true;
1393 wi->is_lhs = false;
1394 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1395 &dummy, wi);
1398 /* The gang clause accepts two arguments. */
1399 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1400 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1402 wi->val_only = true;
1403 wi->is_lhs = false;
1404 convert_nonlocal_reference_op
1405 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1407 break;
1409 case OMP_CLAUSE_DIST_SCHEDULE:
1410 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1412 wi->val_only = true;
1413 wi->is_lhs = false;
1414 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1415 &dummy, wi);
1417 break;
1419 case OMP_CLAUSE_MAP:
1420 case OMP_CLAUSE_TO:
1421 case OMP_CLAUSE_FROM:
1422 if (OMP_CLAUSE_SIZE (clause))
1424 wi->val_only = true;
1425 wi->is_lhs = false;
1426 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1427 &dummy, wi);
1429 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1430 goto do_decl_clause;
1431 wi->val_only = true;
1432 wi->is_lhs = false;
1433 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1434 wi, NULL);
1435 break;
1437 case OMP_CLAUSE_ALIGNED:
1438 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1440 wi->val_only = true;
1441 wi->is_lhs = false;
1442 convert_nonlocal_reference_op
1443 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1445 /* FALLTHRU */
1446 case OMP_CLAUSE_NONTEMPORAL:
1447 do_decl_clause_no_supp:
1448 /* Like do_decl_clause, but don't add any suppression. */
1449 decl = OMP_CLAUSE_DECL (clause);
1450 if (VAR_P (decl)
1451 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1452 break;
1453 if (decl_function_context (decl) != info->context)
1455 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1456 need_chain = true;
1458 break;
1460 case OMP_CLAUSE_ALLOCATE:
1461 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause))
1463 wi->val_only = true;
1464 wi->is_lhs = false;
1465 convert_nonlocal_reference_op
1466 (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause), &dummy, wi);
1468 goto do_decl_clause_no_supp;
1470 case OMP_CLAUSE_NOWAIT:
1471 case OMP_CLAUSE_ORDERED:
1472 case OMP_CLAUSE_DEFAULT:
1473 case OMP_CLAUSE_COPYIN:
1474 case OMP_CLAUSE_COLLAPSE:
1475 case OMP_CLAUSE_TILE:
1476 case OMP_CLAUSE_UNTIED:
1477 case OMP_CLAUSE_MERGEABLE:
1478 case OMP_CLAUSE_PROC_BIND:
1479 case OMP_CLAUSE_NOGROUP:
1480 case OMP_CLAUSE_THREADS:
1481 case OMP_CLAUSE_SIMD:
1482 case OMP_CLAUSE_DEFAULTMAP:
1483 case OMP_CLAUSE_ORDER:
1484 case OMP_CLAUSE_SEQ:
1485 case OMP_CLAUSE_INDEPENDENT:
1486 case OMP_CLAUSE_AUTO:
1487 case OMP_CLAUSE_IF_PRESENT:
1488 case OMP_CLAUSE_FINALIZE:
1489 case OMP_CLAUSE_BIND:
1490 case OMP_CLAUSE__CONDTEMP_:
1491 case OMP_CLAUSE__SCANTEMP_:
1492 break;
1494 /* The following clause belongs to the OpenACC cache directive, which
1495 is discarded during gimplification. */
1496 case OMP_CLAUSE__CACHE_:
1497 /* The following clauses are only allowed in the OpenMP declare simd
1498 directive, so not seen here. */
1499 case OMP_CLAUSE_UNIFORM:
1500 case OMP_CLAUSE_INBRANCH:
1501 case OMP_CLAUSE_NOTINBRANCH:
1502 /* The following clauses are only allowed on OpenMP cancel and
1503 cancellation point directives, which at this point have already
1504 been lowered into a function call. */
1505 case OMP_CLAUSE_FOR:
1506 case OMP_CLAUSE_PARALLEL:
1507 case OMP_CLAUSE_SECTIONS:
1508 case OMP_CLAUSE_TASKGROUP:
1509 /* The following clauses are only added during OMP lowering; nested
1510 function decomposition happens before that. */
1511 case OMP_CLAUSE__LOOPTEMP_:
1512 case OMP_CLAUSE__REDUCTEMP_:
1513 case OMP_CLAUSE__SIMDUID_:
1514 case OMP_CLAUSE__SIMT_:
1515 /* The following clauses are only allowed on OpenACC 'routine'
1516 directives, not seen here. */
1517 case OMP_CLAUSE_NOHOST:
1518 /* Anything else. */
1519 default:
1520 gcc_unreachable ();
1524 info->suppress_expansion = new_suppress;
1526 if (need_stmts)
1527 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1528 switch (OMP_CLAUSE_CODE (clause))
1530 case OMP_CLAUSE_REDUCTION:
1531 case OMP_CLAUSE_IN_REDUCTION:
1532 case OMP_CLAUSE_TASK_REDUCTION:
1533 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1535 tree old_context
1536 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1537 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1538 = info->context;
1539 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1540 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1541 = info->context;
1542 tree save_local_var_chain = info->new_local_var_chain;
1543 info->new_local_var_chain = NULL;
1544 gimple_seq *seq = &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause);
1545 walk_body (convert_nonlocal_reference_stmt,
1546 convert_nonlocal_reference_op, info, seq);
1547 if (info->new_local_var_chain)
1548 declare_vars (info->new_local_var_chain,
1549 gimple_seq_first_stmt (*seq), false);
1550 info->new_local_var_chain = NULL;
1551 seq = &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause);
1552 walk_body (convert_nonlocal_reference_stmt,
1553 convert_nonlocal_reference_op, info, seq);
1554 if (info->new_local_var_chain)
1555 declare_vars (info->new_local_var_chain,
1556 gimple_seq_first_stmt (*seq), false);
1557 info->new_local_var_chain = save_local_var_chain;
1558 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1559 = old_context;
1560 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1561 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1562 = old_context;
1564 break;
1566 case OMP_CLAUSE_LASTPRIVATE:
1568 tree save_local_var_chain = info->new_local_var_chain;
1569 info->new_local_var_chain = NULL;
1570 gimple_seq *seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause);
1571 walk_body (convert_nonlocal_reference_stmt,
1572 convert_nonlocal_reference_op, info, seq);
1573 if (info->new_local_var_chain)
1574 declare_vars (info->new_local_var_chain,
1575 gimple_seq_first_stmt (*seq), false);
1576 info->new_local_var_chain = save_local_var_chain;
1578 break;
1580 case OMP_CLAUSE_LINEAR:
1582 tree save_local_var_chain = info->new_local_var_chain;
1583 info->new_local_var_chain = NULL;
1584 gimple_seq *seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause);
1585 walk_body (convert_nonlocal_reference_stmt,
1586 convert_nonlocal_reference_op, info, seq);
1587 if (info->new_local_var_chain)
1588 declare_vars (info->new_local_var_chain,
1589 gimple_seq_first_stmt (*seq), false);
1590 info->new_local_var_chain = save_local_var_chain;
1592 break;
1594 default:
1595 break;
1598 return need_chain;
1601 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1603 static void
1604 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1606 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1607 type = TREE_TYPE (type);
1609 if (TYPE_NAME (type)
1610 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1611 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1612 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1614 while (POINTER_TYPE_P (type)
1615 || TREE_CODE (type) == VECTOR_TYPE
1616 || TREE_CODE (type) == FUNCTION_TYPE
1617 || TREE_CODE (type) == METHOD_TYPE)
1618 type = TREE_TYPE (type);
1620 if (TREE_CODE (type) == ARRAY_TYPE)
1622 tree domain, t;
1624 note_nonlocal_vla_type (info, TREE_TYPE (type));
1625 domain = TYPE_DOMAIN (type);
1626 if (domain)
1628 t = TYPE_MIN_VALUE (domain);
1629 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1630 && decl_function_context (t) != info->context)
1631 get_nonlocal_debug_decl (info, t);
1632 t = TYPE_MAX_VALUE (domain);
1633 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1634 && decl_function_context (t) != info->context)
1635 get_nonlocal_debug_decl (info, t);
1640 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1641 PARM_DECLs that belong to outer functions. This handles statements
1642 that are not handled via the standard recursion done in
1643 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1644 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1645 operands of STMT have been handled by this function. */
1647 static tree
1648 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1649 struct walk_stmt_info *wi)
1651 struct nesting_info *info = (struct nesting_info *) wi->info;
1652 tree save_local_var_chain;
1653 bitmap save_suppress;
1654 gimple *stmt = gsi_stmt (*gsi);
1656 switch (gimple_code (stmt))
1658 case GIMPLE_GOTO:
1659 /* Don't walk non-local gotos for now. */
1660 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1662 wi->val_only = true;
1663 wi->is_lhs = false;
1664 *handled_ops_p = false;
1665 return NULL_TREE;
1667 break;
1669 case GIMPLE_OMP_TEAMS:
1670 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
1672 save_suppress = info->suppress_expansion;
1673 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt),
1674 wi);
1675 walk_body (convert_nonlocal_reference_stmt,
1676 convert_nonlocal_reference_op, info,
1677 gimple_omp_body_ptr (stmt));
1678 info->suppress_expansion = save_suppress;
1679 break;
1681 /* FALLTHRU */
1683 case GIMPLE_OMP_PARALLEL:
1684 case GIMPLE_OMP_TASK:
1685 save_suppress = info->suppress_expansion;
1686 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1687 wi))
1689 tree c, decl;
1690 decl = get_chain_decl (info);
1691 c = build_omp_clause (gimple_location (stmt),
1692 OMP_CLAUSE_FIRSTPRIVATE);
1693 OMP_CLAUSE_DECL (c) = decl;
1694 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1695 gimple_omp_taskreg_set_clauses (stmt, c);
1698 save_local_var_chain = info->new_local_var_chain;
1699 info->new_local_var_chain = NULL;
1701 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1702 info, gimple_omp_body_ptr (stmt));
1704 if (info->new_local_var_chain)
1705 declare_vars (info->new_local_var_chain,
1706 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1707 false);
1708 info->new_local_var_chain = save_local_var_chain;
1709 info->suppress_expansion = save_suppress;
1710 break;
1712 case GIMPLE_OMP_FOR:
1713 save_suppress = info->suppress_expansion;
1714 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1715 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1716 convert_nonlocal_reference_stmt,
1717 convert_nonlocal_reference_op, info);
1718 walk_body (convert_nonlocal_reference_stmt,
1719 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1720 info->suppress_expansion = save_suppress;
1721 break;
1723 case GIMPLE_OMP_SECTIONS:
1724 save_suppress = info->suppress_expansion;
1725 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1726 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1727 info, gimple_omp_body_ptr (stmt));
1728 info->suppress_expansion = save_suppress;
1729 break;
1731 case GIMPLE_OMP_SINGLE:
1732 save_suppress = info->suppress_expansion;
1733 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1734 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1735 info, gimple_omp_body_ptr (stmt));
1736 info->suppress_expansion = save_suppress;
1737 break;
1739 case GIMPLE_OMP_SCOPE:
1740 save_suppress = info->suppress_expansion;
1741 convert_nonlocal_omp_clauses (gimple_omp_scope_clauses_ptr (stmt), wi);
1742 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1743 info, gimple_omp_body_ptr (stmt));
1744 info->suppress_expansion = save_suppress;
1745 break;
1747 case GIMPLE_OMP_TASKGROUP:
1748 save_suppress = info->suppress_expansion;
1749 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
1750 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1751 info, gimple_omp_body_ptr (stmt));
1752 info->suppress_expansion = save_suppress;
1753 break;
1755 case GIMPLE_OMP_TARGET:
1756 if (!is_gimple_omp_offloaded (stmt))
1758 save_suppress = info->suppress_expansion;
1759 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1760 wi);
1761 info->suppress_expansion = save_suppress;
1762 walk_body (convert_nonlocal_reference_stmt,
1763 convert_nonlocal_reference_op, info,
1764 gimple_omp_body_ptr (stmt));
1765 break;
1767 save_suppress = info->suppress_expansion;
1768 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1769 wi))
1771 tree c, decl;
1772 decl = get_chain_decl (info);
1773 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1774 OMP_CLAUSE_DECL (c) = decl;
1775 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1776 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1777 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1778 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1781 save_local_var_chain = info->new_local_var_chain;
1782 info->new_local_var_chain = NULL;
1784 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1785 info, gimple_omp_body_ptr (stmt));
1787 if (info->new_local_var_chain)
1788 declare_vars (info->new_local_var_chain,
1789 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1790 false);
1791 info->new_local_var_chain = save_local_var_chain;
1792 info->suppress_expansion = save_suppress;
1793 break;
1795 case GIMPLE_OMP_SECTION:
1796 case GIMPLE_OMP_MASTER:
1797 case GIMPLE_OMP_MASKED:
1798 case GIMPLE_OMP_ORDERED:
1799 case GIMPLE_OMP_SCAN:
1800 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1801 info, gimple_omp_body_ptr (stmt));
1802 break;
1804 case GIMPLE_BIND:
1806 gbind *bind_stmt = as_a <gbind *> (stmt);
1808 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1809 if (TREE_CODE (var) == NAMELIST_DECL)
1811 /* Adjust decls mentioned in NAMELIST_DECL. */
1812 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1813 tree decl;
1814 unsigned int i;
1816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1818 if (VAR_P (decl)
1819 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1820 continue;
1821 if (decl_function_context (decl) != info->context)
1822 CONSTRUCTOR_ELT (decls, i)->value
1823 = get_nonlocal_debug_decl (info, decl);
1827 *handled_ops_p = false;
1828 return NULL_TREE;
1830 case GIMPLE_COND:
1831 wi->val_only = true;
1832 wi->is_lhs = false;
1833 *handled_ops_p = false;
1834 return NULL_TREE;
1836 case GIMPLE_ASSIGN:
1837 if (gimple_clobber_p (stmt))
1839 tree lhs = gimple_assign_lhs (stmt);
1840 if (DECL_P (lhs)
1841 && !(TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
1842 && decl_function_context (lhs) != info->context)
1844 gsi_replace (gsi, gimple_build_nop (), true);
1845 break;
1848 *handled_ops_p = false;
1849 return NULL_TREE;
1851 default:
1852 /* For every other statement that we are not interested in
1853 handling here, let the walker traverse the operands. */
1854 *handled_ops_p = false;
1855 return NULL_TREE;
1858 /* We have handled all of STMT operands, no need to traverse the operands. */
1859 *handled_ops_p = true;
1860 return NULL_TREE;
1864 /* A subroutine of convert_local_reference. Create a local variable
1865 in the parent function with DECL_VALUE_EXPR set to reference the
1866 field in FRAME. This is used both for debug info and in OMP
1867 lowering. */
1869 static tree
1870 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1872 tree x, new_decl;
1874 tree *slot = &info->var_map->get_or_insert (decl);
1875 if (*slot)
1876 return *slot;
1878 /* Make sure frame_decl gets created. */
1879 (void) get_frame_type (info);
1880 x = info->frame_decl;
1881 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1883 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1884 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1885 DECL_CONTEXT (new_decl) = info->context;
1886 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1887 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1888 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1889 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1890 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1891 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1892 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1893 if ((TREE_CODE (decl) == PARM_DECL
1894 || TREE_CODE (decl) == RESULT_DECL
1895 || VAR_P (decl))
1896 && DECL_BY_REFERENCE (decl))
1897 DECL_BY_REFERENCE (new_decl) = 1;
1899 SET_DECL_VALUE_EXPR (new_decl, x);
1900 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1901 *slot = new_decl;
1903 DECL_CHAIN (new_decl) = info->debug_var_chain;
1904 info->debug_var_chain = new_decl;
1906 /* Do not emit debug info twice. */
1907 DECL_IGNORED_P (decl) = 1;
1909 return new_decl;
1913 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1914 and PARM_DECLs that were referenced by inner nested functions.
1915 The rewrite will be a structure reference to the local frame variable. */
1917 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1919 static tree
1920 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1922 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1923 struct nesting_info *const info = (struct nesting_info *) wi->info;
1924 tree t = *tp, field, x;
1925 bool save_val_only;
1927 *walk_subtrees = 0;
1928 switch (TREE_CODE (t))
1930 case VAR_DECL:
1931 /* Non-automatic variables are never processed. */
1932 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1933 break;
1934 /* FALLTHRU */
1936 case PARM_DECL:
1937 if (t != info->frame_decl && decl_function_context (t) == info->context)
1939 /* If we copied a pointer to the frame, then the original decl
1940 is used unchanged in the parent function. */
1941 if (use_pointer_in_frame (t))
1942 break;
1944 /* No need to transform anything if no child references the
1945 variable. */
1946 field = lookup_field_for_decl (info, t, NO_INSERT);
1947 if (!field)
1948 break;
1949 wi->changed = true;
1951 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1952 x = get_local_debug_decl (info, t, field);
1953 else
1954 x = get_frame_field (info, info->context, field, &wi->gsi);
1956 if (wi->val_only)
1958 if (wi->is_lhs)
1959 x = save_tmp_var (info, x, &wi->gsi);
1960 else
1961 x = init_tmp_var (info, x, &wi->gsi);
1964 *tp = x;
1966 break;
1968 case ADDR_EXPR:
1969 save_val_only = wi->val_only;
1970 wi->val_only = false;
1971 wi->is_lhs = false;
1972 wi->changed = false;
1973 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1974 wi->val_only = save_val_only;
1976 /* If we converted anything ... */
1977 if (wi->changed)
1979 tree save_context;
1981 /* Then the frame decl is now addressable. */
1982 TREE_ADDRESSABLE (info->frame_decl) = 1;
1984 save_context = current_function_decl;
1985 current_function_decl = info->context;
1986 recompute_tree_invariant_for_addr_expr (t);
1988 /* If we are in a context where we only accept values, then
1989 compute the address into a temporary. */
1990 if (save_val_only)
1991 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1992 t, &wi->gsi);
1993 current_function_decl = save_context;
1995 break;
1997 case REALPART_EXPR:
1998 case IMAGPART_EXPR:
1999 case COMPONENT_REF:
2000 case ARRAY_REF:
2001 case ARRAY_RANGE_REF:
2002 case BIT_FIELD_REF:
2003 /* Go down this entire nest and just look at the final prefix and
2004 anything that describes the references. Otherwise, we lose track
2005 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
2006 save_val_only = wi->val_only;
2007 wi->val_only = true;
2008 wi->is_lhs = false;
2009 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
2011 if (TREE_CODE (t) == COMPONENT_REF)
2012 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
2013 NULL);
2014 else if (TREE_CODE (t) == ARRAY_REF
2015 || TREE_CODE (t) == ARRAY_RANGE_REF)
2017 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
2018 NULL);
2019 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
2020 NULL);
2021 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
2022 NULL);
2025 wi->val_only = false;
2026 walk_tree (tp, convert_local_reference_op, wi, NULL);
2027 wi->val_only = save_val_only;
2028 break;
2030 case MEM_REF:
2031 save_val_only = wi->val_only;
2032 wi->val_only = true;
2033 wi->is_lhs = false;
2034 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
2035 wi, NULL);
2036 /* We need to re-fold the MEM_REF as component references as
2037 part of a ADDR_EXPR address are not allowed. But we cannot
2038 fold here, as the chain record type is not yet finalized. */
2039 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2040 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
2041 info->mem_refs->add (tp);
2042 wi->val_only = save_val_only;
2043 break;
2045 case VIEW_CONVERT_EXPR:
2046 /* Just request to look at the subtrees, leaving val_only and lhs
2047 untouched. This might actually be for !val_only + lhs, in which
2048 case we don't want to force a replacement by a temporary. */
2049 *walk_subtrees = 1;
2050 break;
2052 default:
2053 if (!IS_TYPE_OR_DECL_P (t))
2055 *walk_subtrees = 1;
2056 wi->val_only = true;
2057 wi->is_lhs = false;
2059 break;
2062 return NULL_TREE;
2065 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
2066 struct walk_stmt_info *);
2068 /* Helper for convert_local_reference. Convert all the references in
2069 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
2071 static bool
2072 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
2074 struct nesting_info *const info = (struct nesting_info *) wi->info;
2075 bool need_frame = false, need_stmts = false;
2076 tree clause, decl, *pdecl;
2077 int dummy;
2078 bitmap new_suppress;
2080 new_suppress = BITMAP_GGC_ALLOC ();
2081 bitmap_copy (new_suppress, info->suppress_expansion);
2083 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2085 pdecl = NULL;
2086 switch (OMP_CLAUSE_CODE (clause))
2088 case OMP_CLAUSE_REDUCTION:
2089 case OMP_CLAUSE_IN_REDUCTION:
2090 case OMP_CLAUSE_TASK_REDUCTION:
2091 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2092 need_stmts = true;
2093 if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
2095 pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
2096 if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
2097 pdecl = &TREE_OPERAND (*pdecl, 0);
2098 if (TREE_CODE (*pdecl) == INDIRECT_REF
2099 || TREE_CODE (*pdecl) == ADDR_EXPR)
2100 pdecl = &TREE_OPERAND (*pdecl, 0);
2102 goto do_decl_clause;
2104 case OMP_CLAUSE_LASTPRIVATE:
2105 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
2106 need_stmts = true;
2107 goto do_decl_clause;
2109 case OMP_CLAUSE_LINEAR:
2110 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
2111 need_stmts = true;
2112 wi->val_only = true;
2113 wi->is_lhs = false;
2114 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
2115 wi);
2116 goto do_decl_clause;
2118 case OMP_CLAUSE_PRIVATE:
2119 case OMP_CLAUSE_FIRSTPRIVATE:
2120 case OMP_CLAUSE_COPYPRIVATE:
2121 case OMP_CLAUSE_SHARED:
2122 case OMP_CLAUSE_TO_DECLARE:
2123 case OMP_CLAUSE_LINK:
2124 case OMP_CLAUSE_USE_DEVICE_PTR:
2125 case OMP_CLAUSE_USE_DEVICE_ADDR:
2126 case OMP_CLAUSE_IS_DEVICE_PTR:
2127 case OMP_CLAUSE_DETACH:
2128 do_decl_clause:
2129 if (pdecl == NULL)
2130 pdecl = &OMP_CLAUSE_DECL (clause);
2131 decl = *pdecl;
2132 if (VAR_P (decl)
2133 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2134 break;
2135 if (decl_function_context (decl) == info->context
2136 && !use_pointer_in_frame (decl))
2138 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2139 if (field)
2141 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
2142 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
2143 bitmap_set_bit (new_suppress, DECL_UID (decl));
2144 *pdecl = get_local_debug_decl (info, decl, field);
2145 need_frame = true;
2148 break;
2150 case OMP_CLAUSE_SCHEDULE:
2151 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
2152 break;
2153 /* FALLTHRU */
2154 case OMP_CLAUSE_FINAL:
2155 case OMP_CLAUSE_IF:
2156 case OMP_CLAUSE_NUM_THREADS:
2157 case OMP_CLAUSE_DEPEND:
2158 case OMP_CLAUSE_DEVICE:
2159 case OMP_CLAUSE_NUM_TEAMS:
2160 case OMP_CLAUSE_THREAD_LIMIT:
2161 case OMP_CLAUSE_SAFELEN:
2162 case OMP_CLAUSE_SIMDLEN:
2163 case OMP_CLAUSE_PRIORITY:
2164 case OMP_CLAUSE_GRAINSIZE:
2165 case OMP_CLAUSE_NUM_TASKS:
2166 case OMP_CLAUSE_HINT:
2167 case OMP_CLAUSE_FILTER:
2168 case OMP_CLAUSE_NUM_GANGS:
2169 case OMP_CLAUSE_NUM_WORKERS:
2170 case OMP_CLAUSE_VECTOR_LENGTH:
2171 case OMP_CLAUSE_GANG:
2172 case OMP_CLAUSE_WORKER:
2173 case OMP_CLAUSE_VECTOR:
2174 case OMP_CLAUSE_ASYNC:
2175 case OMP_CLAUSE_WAIT:
2176 /* Several OpenACC clauses have optional arguments. Check if they
2177 are present. */
2178 if (OMP_CLAUSE_OPERAND (clause, 0))
2180 wi->val_only = true;
2181 wi->is_lhs = false;
2182 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2183 &dummy, wi);
2186 /* The gang clause accepts two arguments. */
2187 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
2188 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
2190 wi->val_only = true;
2191 wi->is_lhs = false;
2192 convert_nonlocal_reference_op
2193 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
2195 break;
2197 case OMP_CLAUSE_DIST_SCHEDULE:
2198 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
2200 wi->val_only = true;
2201 wi->is_lhs = false;
2202 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2203 &dummy, wi);
2205 break;
2207 case OMP_CLAUSE_MAP:
2208 case OMP_CLAUSE_TO:
2209 case OMP_CLAUSE_FROM:
2210 if (OMP_CLAUSE_SIZE (clause))
2212 wi->val_only = true;
2213 wi->is_lhs = false;
2214 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
2215 &dummy, wi);
2217 if (DECL_P (OMP_CLAUSE_DECL (clause)))
2218 goto do_decl_clause;
2219 wi->val_only = true;
2220 wi->is_lhs = false;
2221 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
2222 wi, NULL);
2223 break;
2225 case OMP_CLAUSE_ALIGNED:
2226 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2228 wi->val_only = true;
2229 wi->is_lhs = false;
2230 convert_local_reference_op
2231 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2233 /* FALLTHRU */
2234 case OMP_CLAUSE_NONTEMPORAL:
2235 do_decl_clause_no_supp:
2236 /* Like do_decl_clause, but don't add any suppression. */
2237 decl = OMP_CLAUSE_DECL (clause);
2238 if (VAR_P (decl)
2239 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2240 break;
2241 if (decl_function_context (decl) == info->context
2242 && !use_pointer_in_frame (decl))
2244 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2245 if (field)
2247 OMP_CLAUSE_DECL (clause)
2248 = get_local_debug_decl (info, decl, field);
2249 need_frame = true;
2252 break;
2254 case OMP_CLAUSE_ALLOCATE:
2255 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause))
2257 wi->val_only = true;
2258 wi->is_lhs = false;
2259 convert_local_reference_op
2260 (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause), &dummy, wi);
2262 goto do_decl_clause_no_supp;
2264 case OMP_CLAUSE_NOWAIT:
2265 case OMP_CLAUSE_ORDERED:
2266 case OMP_CLAUSE_DEFAULT:
2267 case OMP_CLAUSE_COPYIN:
2268 case OMP_CLAUSE_COLLAPSE:
2269 case OMP_CLAUSE_TILE:
2270 case OMP_CLAUSE_UNTIED:
2271 case OMP_CLAUSE_MERGEABLE:
2272 case OMP_CLAUSE_PROC_BIND:
2273 case OMP_CLAUSE_NOGROUP:
2274 case OMP_CLAUSE_THREADS:
2275 case OMP_CLAUSE_SIMD:
2276 case OMP_CLAUSE_DEFAULTMAP:
2277 case OMP_CLAUSE_ORDER:
2278 case OMP_CLAUSE_SEQ:
2279 case OMP_CLAUSE_INDEPENDENT:
2280 case OMP_CLAUSE_AUTO:
2281 case OMP_CLAUSE_IF_PRESENT:
2282 case OMP_CLAUSE_FINALIZE:
2283 case OMP_CLAUSE_BIND:
2284 case OMP_CLAUSE__CONDTEMP_:
2285 case OMP_CLAUSE__SCANTEMP_:
2286 break;
2288 /* The following clause belongs to the OpenACC cache directive, which
2289 is discarded during gimplification. */
2290 case OMP_CLAUSE__CACHE_:
2291 /* The following clauses are only allowed in the OpenMP declare simd
2292 directive, so not seen here. */
2293 case OMP_CLAUSE_UNIFORM:
2294 case OMP_CLAUSE_INBRANCH:
2295 case OMP_CLAUSE_NOTINBRANCH:
2296 /* The following clauses are only allowed on OpenMP cancel and
2297 cancellation point directives, which at this point have already
2298 been lowered into a function call. */
2299 case OMP_CLAUSE_FOR:
2300 case OMP_CLAUSE_PARALLEL:
2301 case OMP_CLAUSE_SECTIONS:
2302 case OMP_CLAUSE_TASKGROUP:
2303 /* The following clauses are only added during OMP lowering; nested
2304 function decomposition happens before that. */
2305 case OMP_CLAUSE__LOOPTEMP_:
2306 case OMP_CLAUSE__REDUCTEMP_:
2307 case OMP_CLAUSE__SIMDUID_:
2308 case OMP_CLAUSE__SIMT_:
2309 /* The following clauses are only allowed on OpenACC 'routine'
2310 directives, not seen here. */
2311 case OMP_CLAUSE_NOHOST:
2312 /* Anything else. */
2313 default:
2314 gcc_unreachable ();
2318 info->suppress_expansion = new_suppress;
2320 if (need_stmts)
2321 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2322 switch (OMP_CLAUSE_CODE (clause))
2324 case OMP_CLAUSE_REDUCTION:
2325 case OMP_CLAUSE_IN_REDUCTION:
2326 case OMP_CLAUSE_TASK_REDUCTION:
2327 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2329 tree old_context
2330 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2331 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2332 = info->context;
2333 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2334 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2335 = info->context;
2336 walk_body (convert_local_reference_stmt,
2337 convert_local_reference_op, info,
2338 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2339 walk_body (convert_local_reference_stmt,
2340 convert_local_reference_op, info,
2341 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2342 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2343 = old_context;
2344 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2345 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2346 = old_context;
2348 break;
2350 case OMP_CLAUSE_LASTPRIVATE:
2351 walk_body (convert_local_reference_stmt,
2352 convert_local_reference_op, info,
2353 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2354 break;
2356 case OMP_CLAUSE_LINEAR:
2357 walk_body (convert_local_reference_stmt,
2358 convert_local_reference_op, info,
2359 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2360 break;
2362 default:
2363 break;
2366 return need_frame;
2370 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2371 and PARM_DECLs that were referenced by inner nested functions.
2372 The rewrite will be a structure reference to the local frame variable. */
2374 static tree
2375 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2376 struct walk_stmt_info *wi)
2378 struct nesting_info *info = (struct nesting_info *) wi->info;
2379 tree save_local_var_chain;
2380 bitmap save_suppress;
2381 char save_static_chain_added;
2382 bool frame_decl_added;
2383 gimple *stmt = gsi_stmt (*gsi);
2385 switch (gimple_code (stmt))
2387 case GIMPLE_OMP_TEAMS:
2388 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2390 save_suppress = info->suppress_expansion;
2391 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2392 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2393 info, gimple_omp_body_ptr (stmt));
2394 info->suppress_expansion = save_suppress;
2395 break;
2397 /* FALLTHRU */
2399 case GIMPLE_OMP_PARALLEL:
2400 case GIMPLE_OMP_TASK:
2401 save_suppress = info->suppress_expansion;
2402 frame_decl_added = false;
2403 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2404 wi))
2406 tree c = build_omp_clause (gimple_location (stmt),
2407 OMP_CLAUSE_SHARED);
2408 (void) get_frame_type (info);
2409 OMP_CLAUSE_DECL (c) = info->frame_decl;
2410 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2411 gimple_omp_taskreg_set_clauses (stmt, c);
2412 info->static_chain_added |= 4;
2413 frame_decl_added = true;
2416 save_local_var_chain = info->new_local_var_chain;
2417 save_static_chain_added = info->static_chain_added;
2418 info->new_local_var_chain = NULL;
2419 info->static_chain_added = 0;
2421 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2422 gimple_omp_body_ptr (stmt));
2424 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
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 info->static_chain_added |= 4;
2432 gimple_omp_taskreg_set_clauses (stmt, c);
2434 if (info->new_local_var_chain)
2435 declare_vars (info->new_local_var_chain,
2436 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2437 info->new_local_var_chain = save_local_var_chain;
2438 info->suppress_expansion = save_suppress;
2439 info->static_chain_added |= save_static_chain_added;
2440 break;
2442 case GIMPLE_OMP_FOR:
2443 save_suppress = info->suppress_expansion;
2444 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2445 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2446 convert_local_reference_stmt,
2447 convert_local_reference_op, info);
2448 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2449 info, gimple_omp_body_ptr (stmt));
2450 info->suppress_expansion = save_suppress;
2451 break;
2453 case GIMPLE_OMP_SECTIONS:
2454 save_suppress = info->suppress_expansion;
2455 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2456 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2457 info, gimple_omp_body_ptr (stmt));
2458 info->suppress_expansion = save_suppress;
2459 break;
2461 case GIMPLE_OMP_SINGLE:
2462 save_suppress = info->suppress_expansion;
2463 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2464 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2465 info, gimple_omp_body_ptr (stmt));
2466 info->suppress_expansion = save_suppress;
2467 break;
2469 case GIMPLE_OMP_SCOPE:
2470 save_suppress = info->suppress_expansion;
2471 convert_local_omp_clauses (gimple_omp_scope_clauses_ptr (stmt), wi);
2472 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2473 info, gimple_omp_body_ptr (stmt));
2474 info->suppress_expansion = save_suppress;
2475 break;
2477 case GIMPLE_OMP_TASKGROUP:
2478 save_suppress = info->suppress_expansion;
2479 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
2480 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2481 info, gimple_omp_body_ptr (stmt));
2482 info->suppress_expansion = save_suppress;
2483 break;
2485 case GIMPLE_OMP_TARGET:
2486 if (!is_gimple_omp_offloaded (stmt))
2488 save_suppress = info->suppress_expansion;
2489 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2490 info->suppress_expansion = save_suppress;
2491 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2492 info, gimple_omp_body_ptr (stmt));
2493 break;
2495 save_suppress = info->suppress_expansion;
2496 frame_decl_added = false;
2497 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2499 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2500 (void) get_frame_type (info);
2501 OMP_CLAUSE_DECL (c) = info->frame_decl;
2502 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2503 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2504 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2505 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2506 info->static_chain_added |= 4;
2507 frame_decl_added = true;
2510 save_local_var_chain = info->new_local_var_chain;
2511 save_static_chain_added = info->static_chain_added;
2512 info->new_local_var_chain = NULL;
2513 info->static_chain_added = 0;
2515 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2516 gimple_omp_body_ptr (stmt));
2518 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2520 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2521 (void) get_frame_type (info);
2522 OMP_CLAUSE_DECL (c) = info->frame_decl;
2523 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2524 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2525 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2526 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2527 info->static_chain_added |= 4;
2530 if (info->new_local_var_chain)
2531 declare_vars (info->new_local_var_chain,
2532 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2533 info->new_local_var_chain = save_local_var_chain;
2534 info->suppress_expansion = save_suppress;
2535 info->static_chain_added |= save_static_chain_added;
2536 break;
2538 case GIMPLE_OMP_SECTION:
2539 case GIMPLE_OMP_MASTER:
2540 case GIMPLE_OMP_MASKED:
2541 case GIMPLE_OMP_ORDERED:
2542 case GIMPLE_OMP_SCAN:
2543 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2544 info, gimple_omp_body_ptr (stmt));
2545 break;
2547 case GIMPLE_COND:
2548 wi->val_only = true;
2549 wi->is_lhs = false;
2550 *handled_ops_p = false;
2551 return NULL_TREE;
2553 case GIMPLE_ASSIGN:
2554 if (gimple_clobber_p (stmt))
2556 tree lhs = gimple_assign_lhs (stmt);
2557 if (DECL_P (lhs)
2558 && decl_function_context (lhs) == info->context
2559 && !use_pointer_in_frame (lhs)
2560 && lookup_field_for_decl (info, lhs, NO_INSERT))
2562 gsi_replace (gsi, gimple_build_nop (), true);
2563 break;
2566 *handled_ops_p = false;
2567 return NULL_TREE;
2569 case GIMPLE_BIND:
2570 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2571 var;
2572 var = DECL_CHAIN (var))
2573 if (TREE_CODE (var) == NAMELIST_DECL)
2575 /* Adjust decls mentioned in NAMELIST_DECL. */
2576 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2577 tree decl;
2578 unsigned int i;
2580 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2582 if (VAR_P (decl)
2583 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2584 continue;
2585 if (decl_function_context (decl) == info->context
2586 && !use_pointer_in_frame (decl))
2588 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2589 if (field)
2591 CONSTRUCTOR_ELT (decls, i)->value
2592 = get_local_debug_decl (info, decl, field);
2598 *handled_ops_p = false;
2599 return NULL_TREE;
2601 default:
2602 /* For every other statement that we are not interested in
2603 handling here, let the walker traverse the operands. */
2604 *handled_ops_p = false;
2605 return NULL_TREE;
2608 /* Indicate that we have handled all the operands ourselves. */
2609 *handled_ops_p = true;
2610 return NULL_TREE;
2614 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2615 that reference labels from outer functions. The rewrite will be a
2616 call to __builtin_nonlocal_goto. */
2618 static tree
2619 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2620 struct walk_stmt_info *wi)
2622 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2623 tree label, new_label, target_context, x, field;
2624 gcall *call;
2625 gimple *stmt = gsi_stmt (*gsi);
2627 if (gimple_code (stmt) != GIMPLE_GOTO)
2629 *handled_ops_p = false;
2630 return NULL_TREE;
2633 label = gimple_goto_dest (stmt);
2634 if (TREE_CODE (label) != LABEL_DECL)
2636 *handled_ops_p = false;
2637 return NULL_TREE;
2640 target_context = decl_function_context (label);
2641 if (target_context == info->context)
2643 *handled_ops_p = false;
2644 return NULL_TREE;
2647 for (i = info->outer; target_context != i->context; i = i->outer)
2648 continue;
2650 /* The original user label may also be use for a normal goto, therefore
2651 we must create a new label that will actually receive the abnormal
2652 control transfer. This new label will be marked LABEL_NONLOCAL; this
2653 mark will trigger proper behavior in the cfg, as well as cause the
2654 (hairy target-specific) non-local goto receiver code to be generated
2655 when we expand rtl. Enter this association into var_map so that we
2656 can insert the new label into the IL during a second pass. */
2657 tree *slot = &i->var_map->get_or_insert (label);
2658 if (*slot == NULL)
2660 new_label = create_artificial_label (UNKNOWN_LOCATION);
2661 DECL_NONLOCAL (new_label) = 1;
2662 *slot = new_label;
2664 else
2665 new_label = *slot;
2667 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2668 field = get_nl_goto_field (i);
2669 x = get_frame_field (info, target_context, field, gsi);
2670 x = build_addr (x);
2671 x = gsi_gimplify_val (info, x, gsi);
2672 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2673 2, build_addr (new_label), x);
2674 gsi_replace (gsi, call, false);
2676 /* We have handled all of STMT's operands, no need to keep going. */
2677 *handled_ops_p = true;
2678 return NULL_TREE;
2682 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2683 are referenced via nonlocal goto from a nested function. The rewrite
2684 will involve installing a newly generated DECL_NONLOCAL label, and
2685 (potentially) a branch around the rtl gunk that is assumed to be
2686 attached to such a label. */
2688 static tree
2689 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2690 struct walk_stmt_info *wi)
2692 struct nesting_info *const info = (struct nesting_info *) wi->info;
2693 tree label, new_label;
2694 gimple_stmt_iterator tmp_gsi;
2695 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2697 if (!stmt)
2699 *handled_ops_p = false;
2700 return NULL_TREE;
2703 label = gimple_label_label (stmt);
2705 tree *slot = info->var_map->get (label);
2706 if (!slot)
2708 *handled_ops_p = false;
2709 return NULL_TREE;
2712 /* If there's any possibility that the previous statement falls through,
2713 then we must branch around the new non-local label. */
2714 tmp_gsi = wi->gsi;
2715 gsi_prev (&tmp_gsi);
2716 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2718 gimple *stmt = gimple_build_goto (label);
2719 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2722 new_label = (tree) *slot;
2723 stmt = gimple_build_label (new_label);
2724 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2726 *handled_ops_p = true;
2727 return NULL_TREE;
2731 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2732 of nested functions that require the use of trampolines. The rewrite
2733 will involve a reference a trampoline generated for the occasion. */
2735 static tree
2736 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2738 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2739 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2740 tree t = *tp, decl, target_context, x, builtin;
2741 bool descr;
2742 gcall *call;
2744 *walk_subtrees = 0;
2745 switch (TREE_CODE (t))
2747 case ADDR_EXPR:
2748 /* Build
2749 T.1 = &CHAIN->tramp;
2750 T.2 = __builtin_adjust_trampoline (T.1);
2751 T.3 = (func_type)T.2;
2754 decl = TREE_OPERAND (t, 0);
2755 if (TREE_CODE (decl) != FUNCTION_DECL)
2756 break;
2758 /* Only need to process nested functions. */
2759 target_context = decl_function_context (decl);
2760 if (!target_context)
2761 break;
2763 /* If the nested function doesn't use a static chain, then
2764 it doesn't need a trampoline. */
2765 if (!DECL_STATIC_CHAIN (decl))
2766 break;
2768 /* If we don't want a trampoline, then don't build one. */
2769 if (TREE_NO_TRAMPOLINE (t))
2770 break;
2772 /* Lookup the immediate parent of the callee, as that's where
2773 we need to insert the trampoline. */
2774 for (i = info; i->context != target_context; i = i->outer)
2775 continue;
2777 /* Decide whether to generate a descriptor or a trampoline. */
2778 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2780 if (descr)
2781 x = lookup_descr_for_decl (i, decl, INSERT);
2782 else
2783 x = lookup_tramp_for_decl (i, decl, INSERT);
2785 /* Compute the address of the field holding the trampoline. */
2786 x = get_frame_field (info, target_context, x, &wi->gsi);
2787 x = build_addr (x);
2788 x = gsi_gimplify_val (info, x, &wi->gsi);
2790 /* Do machine-specific ugliness. Normally this will involve
2791 computing extra alignment, but it can really be anything. */
2792 if (descr)
2793 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2794 else
2795 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2796 call = gimple_build_call (builtin, 1, x);
2797 x = init_tmp_var_with_call (info, &wi->gsi, call);
2799 /* Cast back to the proper function type. */
2800 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2801 x = init_tmp_var (info, x, &wi->gsi);
2803 *tp = x;
2804 break;
2806 default:
2807 if (!IS_TYPE_OR_DECL_P (t))
2808 *walk_subtrees = 1;
2809 break;
2812 return NULL_TREE;
2816 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2817 to addresses of nested functions that require the use of
2818 trampolines. The rewrite will involve a reference a trampoline
2819 generated for the occasion. */
2821 static tree
2822 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2823 struct walk_stmt_info *wi)
2825 struct nesting_info *info = (struct nesting_info *) wi->info;
2826 gimple *stmt = gsi_stmt (*gsi);
2828 switch (gimple_code (stmt))
2830 case GIMPLE_CALL:
2832 /* Only walk call arguments, lest we generate trampolines for
2833 direct calls. */
2834 unsigned long i, nargs = gimple_call_num_args (stmt);
2835 for (i = 0; i < nargs; i++)
2836 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2837 wi, NULL);
2838 break;
2841 case GIMPLE_OMP_TEAMS:
2842 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2844 *handled_ops_p = false;
2845 return NULL_TREE;
2847 goto do_parallel;
2849 case GIMPLE_OMP_TARGET:
2850 if (!is_gimple_omp_offloaded (stmt))
2852 *handled_ops_p = false;
2853 return NULL_TREE;
2855 /* FALLTHRU */
2856 case GIMPLE_OMP_PARALLEL:
2857 case GIMPLE_OMP_TASK:
2858 do_parallel:
2860 tree save_local_var_chain = info->new_local_var_chain;
2861 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2862 info->new_local_var_chain = NULL;
2863 char save_static_chain_added = info->static_chain_added;
2864 info->static_chain_added = 0;
2865 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2866 info, gimple_omp_body_ptr (stmt));
2867 if (info->new_local_var_chain)
2868 declare_vars (info->new_local_var_chain,
2869 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2870 false);
2871 for (int i = 0; i < 2; i++)
2873 tree c, decl;
2874 if ((info->static_chain_added & (1 << i)) == 0)
2875 continue;
2876 decl = i ? get_chain_decl (info) : info->frame_decl;
2877 /* Don't add CHAIN.* or FRAME.* twice. */
2878 for (c = gimple_omp_taskreg_clauses (stmt);
2880 c = OMP_CLAUSE_CHAIN (c))
2881 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2882 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2883 && OMP_CLAUSE_DECL (c) == decl)
2884 break;
2885 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2887 c = build_omp_clause (gimple_location (stmt),
2888 i ? OMP_CLAUSE_FIRSTPRIVATE
2889 : OMP_CLAUSE_SHARED);
2890 OMP_CLAUSE_DECL (c) = decl;
2891 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2892 gimple_omp_taskreg_set_clauses (stmt, c);
2894 else if (c == NULL)
2896 c = build_omp_clause (gimple_location (stmt),
2897 OMP_CLAUSE_MAP);
2898 OMP_CLAUSE_DECL (c) = decl;
2899 OMP_CLAUSE_SET_MAP_KIND (c,
2900 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2901 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2902 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2903 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2907 info->new_local_var_chain = save_local_var_chain;
2908 info->static_chain_added |= save_static_chain_added;
2910 break;
2912 default:
2913 *handled_ops_p = false;
2914 return NULL_TREE;
2917 *handled_ops_p = true;
2918 return NULL_TREE;
2923 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2924 that reference nested functions to make sure that the static chain
2925 is set up properly for the call. */
2927 static tree
2928 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2929 struct walk_stmt_info *wi)
2931 struct nesting_info *const info = (struct nesting_info *) wi->info;
2932 tree decl, target_context;
2933 char save_static_chain_added;
2934 int i;
2935 gimple *stmt = gsi_stmt (*gsi);
2937 switch (gimple_code (stmt))
2939 case GIMPLE_CALL:
2940 if (gimple_call_chain (stmt))
2941 break;
2942 decl = gimple_call_fndecl (stmt);
2943 if (!decl)
2944 break;
2945 target_context = decl_function_context (decl);
2946 if (target_context && DECL_STATIC_CHAIN (decl))
2948 struct nesting_info *i = info;
2949 while (i && i->context != target_context)
2950 i = i->outer;
2951 /* If none of the outer contexts is the target context, this means
2952 that the function is called in a wrong context. */
2953 if (!i)
2954 internal_error ("%s from %s called in %s",
2955 IDENTIFIER_POINTER (DECL_NAME (decl)),
2956 IDENTIFIER_POINTER (DECL_NAME (target_context)),
2957 IDENTIFIER_POINTER (DECL_NAME (info->context)));
2959 gimple_call_set_chain (as_a <gcall *> (stmt),
2960 get_static_chain (info, target_context,
2961 &wi->gsi));
2962 info->static_chain_added |= (1 << (info->context != target_context));
2964 break;
2966 case GIMPLE_OMP_TEAMS:
2967 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2969 walk_body (convert_gimple_call, NULL, info,
2970 gimple_omp_body_ptr (stmt));
2971 break;
2973 /* FALLTHRU */
2975 case GIMPLE_OMP_PARALLEL:
2976 case GIMPLE_OMP_TASK:
2977 save_static_chain_added = info->static_chain_added;
2978 info->static_chain_added = 0;
2979 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2980 for (i = 0; i < 2; i++)
2982 tree c, decl;
2983 if ((info->static_chain_added & (1 << i)) == 0)
2984 continue;
2985 decl = i ? get_chain_decl (info) : info->frame_decl;
2986 /* Don't add CHAIN.* or FRAME.* twice. */
2987 for (c = gimple_omp_taskreg_clauses (stmt);
2989 c = OMP_CLAUSE_CHAIN (c))
2990 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2991 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2992 && OMP_CLAUSE_DECL (c) == decl)
2993 break;
2994 if (c == NULL)
2996 c = build_omp_clause (gimple_location (stmt),
2997 i ? OMP_CLAUSE_FIRSTPRIVATE
2998 : OMP_CLAUSE_SHARED);
2999 OMP_CLAUSE_DECL (c) = decl;
3000 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
3001 gimple_omp_taskreg_set_clauses (stmt, c);
3004 info->static_chain_added |= save_static_chain_added;
3005 break;
3007 case GIMPLE_OMP_TARGET:
3008 if (!is_gimple_omp_offloaded (stmt))
3010 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3011 break;
3013 save_static_chain_added = info->static_chain_added;
3014 info->static_chain_added = 0;
3015 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3016 for (i = 0; i < 2; i++)
3018 tree c, decl;
3019 if ((info->static_chain_added & (1 << i)) == 0)
3020 continue;
3021 decl = i ? get_chain_decl (info) : info->frame_decl;
3022 /* Don't add CHAIN.* or FRAME.* twice. */
3023 for (c = gimple_omp_target_clauses (stmt);
3025 c = OMP_CLAUSE_CHAIN (c))
3026 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
3027 && OMP_CLAUSE_DECL (c) == decl)
3028 break;
3029 if (c == NULL)
3031 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
3032 OMP_CLAUSE_DECL (c) = decl;
3033 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
3034 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
3035 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
3036 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
3040 info->static_chain_added |= save_static_chain_added;
3041 break;
3043 case GIMPLE_OMP_FOR:
3044 walk_body (convert_gimple_call, NULL, info,
3045 gimple_omp_for_pre_body_ptr (stmt));
3046 /* FALLTHRU */
3047 case GIMPLE_OMP_SECTIONS:
3048 case GIMPLE_OMP_SECTION:
3049 case GIMPLE_OMP_SINGLE:
3050 case GIMPLE_OMP_SCOPE:
3051 case GIMPLE_OMP_MASTER:
3052 case GIMPLE_OMP_MASKED:
3053 case GIMPLE_OMP_TASKGROUP:
3054 case GIMPLE_OMP_ORDERED:
3055 case GIMPLE_OMP_SCAN:
3056 case GIMPLE_OMP_CRITICAL:
3057 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3058 break;
3060 default:
3061 /* Keep looking for other operands. */
3062 *handled_ops_p = false;
3063 return NULL_TREE;
3066 *handled_ops_p = true;
3067 return NULL_TREE;
3070 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
3071 call expressions. At the same time, determine if a nested function
3072 actually uses its static chain; if not, remember that. */
3074 static void
3075 convert_all_function_calls (struct nesting_info *root)
3077 unsigned int chain_count = 0, old_chain_count, iter_count;
3078 struct nesting_info *n;
3080 /* First, optimistically clear static_chain for all decls that haven't
3081 used the static chain already for variable access. But always create
3082 it if not optimizing. This makes it possible to reconstruct the static
3083 nesting tree at run time and thus to resolve up-level references from
3084 within the debugger. */
3085 FOR_EACH_NEST_INFO (n, root)
3087 if (n->thunk_p)
3088 continue;
3089 tree decl = n->context;
3090 if (!optimize)
3092 if (n->inner)
3093 (void) get_frame_type (n);
3094 if (n->outer)
3095 (void) get_chain_decl (n);
3097 else if (!n->outer || (!n->chain_decl && !n->chain_field))
3099 DECL_STATIC_CHAIN (decl) = 0;
3100 if (dump_file && (dump_flags & TDF_DETAILS))
3101 fprintf (dump_file, "Guessing no static-chain for %s\n",
3102 lang_hooks.decl_printable_name (decl, 2));
3104 else
3105 DECL_STATIC_CHAIN (decl) = 1;
3106 chain_count += DECL_STATIC_CHAIN (decl);
3109 FOR_EACH_NEST_INFO (n, root)
3110 if (n->thunk_p)
3112 tree decl = n->context;
3113 tree alias = thunk_info::get (cgraph_node::get (decl))->alias;
3114 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
3117 /* Walk the functions and perform transformations. Note that these
3118 transformations can induce new uses of the static chain, which in turn
3119 require re-examining all users of the decl. */
3120 /* ??? It would make sense to try to use the call graph to speed this up,
3121 but the call graph hasn't really been built yet. Even if it did, we
3122 would still need to iterate in this loop since address-of references
3123 wouldn't show up in the callgraph anyway. */
3124 iter_count = 0;
3127 old_chain_count = chain_count;
3128 chain_count = 0;
3129 iter_count++;
3131 if (dump_file && (dump_flags & TDF_DETAILS))
3132 fputc ('\n', dump_file);
3134 FOR_EACH_NEST_INFO (n, root)
3136 if (n->thunk_p)
3137 continue;
3138 tree decl = n->context;
3139 walk_function (convert_tramp_reference_stmt,
3140 convert_tramp_reference_op, n);
3141 walk_function (convert_gimple_call, NULL, n);
3142 chain_count += DECL_STATIC_CHAIN (decl);
3145 FOR_EACH_NEST_INFO (n, root)
3146 if (n->thunk_p)
3148 tree decl = n->context;
3149 tree alias = thunk_info::get (cgraph_node::get (decl))->alias;
3150 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
3153 while (chain_count != old_chain_count);
3155 if (dump_file && (dump_flags & TDF_DETAILS))
3156 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
3157 iter_count);
3160 struct nesting_copy_body_data
3162 copy_body_data cb;
3163 struct nesting_info *root;
3166 /* A helper subroutine for debug_var_chain type remapping. */
3168 static tree
3169 nesting_copy_decl (tree decl, copy_body_data *id)
3171 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
3172 tree *slot = nid->root->var_map->get (decl);
3174 if (slot)
3175 return (tree) *slot;
3177 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
3179 tree new_decl = copy_decl_no_change (decl, id);
3180 DECL_ORIGINAL_TYPE (new_decl)
3181 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
3182 return new_decl;
3185 if (VAR_P (decl)
3186 || TREE_CODE (decl) == PARM_DECL
3187 || TREE_CODE (decl) == RESULT_DECL)
3188 return decl;
3190 return copy_decl_no_change (decl, id);
3193 /* A helper function for remap_vla_decls. See if *TP contains
3194 some remapped variables. */
3196 static tree
3197 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
3199 struct nesting_info *root = (struct nesting_info *) data;
3200 tree t = *tp;
3202 if (DECL_P (t))
3204 *walk_subtrees = 0;
3205 tree *slot = root->var_map->get (t);
3207 if (slot)
3208 return *slot;
3210 return NULL;
3213 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
3214 involved. */
3216 static void
3217 remap_vla_decls (tree block, struct nesting_info *root)
3219 tree var, subblock, val, type;
3220 struct nesting_copy_body_data id;
3222 for (subblock = BLOCK_SUBBLOCKS (block);
3223 subblock;
3224 subblock = BLOCK_CHAIN (subblock))
3225 remap_vla_decls (subblock, root);
3227 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3228 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3230 val = DECL_VALUE_EXPR (var);
3231 type = TREE_TYPE (var);
3233 if (!(TREE_CODE (val) == INDIRECT_REF
3234 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3235 && variably_modified_type_p (type, NULL)))
3236 continue;
3238 if (root->var_map->get (TREE_OPERAND (val, 0))
3239 || walk_tree (&type, contains_remapped_vars, root, NULL))
3240 break;
3243 if (var == NULL_TREE)
3244 return;
3246 memset (&id, 0, sizeof (id));
3247 id.cb.copy_decl = nesting_copy_decl;
3248 id.cb.decl_map = new hash_map<tree, tree>;
3249 id.root = root;
3251 for (; var; var = DECL_CHAIN (var))
3252 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3254 struct nesting_info *i;
3255 tree newt, context;
3257 val = DECL_VALUE_EXPR (var);
3258 type = TREE_TYPE (var);
3260 if (!(TREE_CODE (val) == INDIRECT_REF
3261 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3262 && variably_modified_type_p (type, NULL)))
3263 continue;
3265 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
3266 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
3267 continue;
3269 context = decl_function_context (var);
3270 for (i = root; i; i = i->outer)
3271 if (i->context == context)
3272 break;
3274 if (i == NULL)
3275 continue;
3277 /* Fully expand value expressions. This avoids having debug variables
3278 only referenced from them and that can be swept during GC. */
3279 if (slot)
3281 tree t = (tree) *slot;
3282 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
3283 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
3286 id.cb.src_fn = i->context;
3287 id.cb.dst_fn = i->context;
3288 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3290 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3291 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3293 newt = TREE_TYPE (newt);
3294 type = TREE_TYPE (type);
3296 if (TYPE_NAME (newt)
3297 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3298 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3299 && newt != type
3300 && TYPE_NAME (newt) == TYPE_NAME (type))
3301 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3303 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3304 if (val != DECL_VALUE_EXPR (var))
3305 SET_DECL_VALUE_EXPR (var, val);
3308 delete id.cb.decl_map;
3311 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3312 involved. */
3314 static void
3315 fixup_vla_decls (tree block)
3317 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3318 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3320 tree val = DECL_VALUE_EXPR (var);
3322 if (!(TREE_CODE (val) == INDIRECT_REF
3323 && VAR_P (TREE_OPERAND (val, 0))
3324 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3325 continue;
3327 /* Fully expand value expressions. This avoids having debug variables
3328 only referenced from them and that can be swept during GC. */
3329 val = build1 (INDIRECT_REF, TREE_TYPE (val),
3330 DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3331 SET_DECL_VALUE_EXPR (var, val);
3334 for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3335 fixup_vla_decls (sub);
3338 /* Fold the MEM_REF *E. */
3339 bool
3340 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3342 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
3343 *ref_p = fold (*ref_p);
3344 return true;
3347 /* Given DECL, a nested function, build an initialization call for FIELD,
3348 the trampoline or descriptor for DECL, using FUNC as the function. */
3350 static gcall *
3351 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3352 tree func)
3354 tree arg1, arg2, arg3, x;
3356 gcc_assert (DECL_STATIC_CHAIN (decl));
3357 arg3 = build_addr (info->frame_decl);
3359 arg2 = build_addr (decl);
3361 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3362 info->frame_decl, field, NULL_TREE);
3363 arg1 = build_addr (x);
3365 return gimple_build_call (func, 3, arg1, arg2, arg3);
3368 /* Do "everything else" to clean up or complete state collected by the various
3369 walking passes -- create a field to hold the frame base address, lay out the
3370 types and decls, generate code to initialize the frame decl, store critical
3371 expressions in the struct function for rtl to find. */
3373 static void
3374 finalize_nesting_tree_1 (struct nesting_info *root)
3376 gimple_seq stmt_list = NULL;
3377 gimple *stmt;
3378 tree context = root->context;
3379 struct function *sf;
3381 if (root->thunk_p)
3382 return;
3384 /* If we created a non-local frame type or decl, we need to lay them
3385 out at this time. */
3386 if (root->frame_type)
3388 /* Debugging information needs to compute the frame base address of the
3389 parent frame out of the static chain from the nested frame.
3391 The static chain is the address of the FRAME record, so one could
3392 imagine it would be possible to compute the frame base address just
3393 adding a constant offset to this address. Unfortunately, this is not
3394 possible: if the FRAME object has alignment constraints that are
3395 stronger than the stack, then the offset between the frame base and
3396 the FRAME object will be dynamic.
3398 What we do instead is to append a field to the FRAME object that holds
3399 the frame base address: then debug info just has to fetch this
3400 field. */
3402 /* Debugging information will refer to the CFA as the frame base
3403 address: we will do the same here. */
3404 const tree frame_addr_fndecl
3405 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3407 /* Create a field in the FRAME record to hold the frame base address for
3408 this stack frame. Since it will be used only by the debugger, put it
3409 at the end of the record in order not to shift all other offsets. */
3410 tree fb_decl = make_node (FIELD_DECL);
3412 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3413 TREE_TYPE (fb_decl) = ptr_type_node;
3414 TREE_ADDRESSABLE (fb_decl) = 1;
3415 DECL_CONTEXT (fb_decl) = root->frame_type;
3416 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3417 fb_decl);
3419 /* In some cases the frame type will trigger the -Wpadded warning.
3420 This is not helpful; suppress it. */
3421 int save_warn_padded = warn_padded;
3422 warn_padded = 0;
3423 layout_type (root->frame_type);
3424 warn_padded = save_warn_padded;
3425 layout_decl (root->frame_decl, 0);
3427 /* Initialize the frame base address field. If the builtin we need is
3428 not available, set it to NULL so that debugging information does not
3429 reference junk. */
3430 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3431 root->frame_decl, fb_decl, NULL_TREE);
3432 tree fb_tmp;
3434 if (frame_addr_fndecl != NULL_TREE)
3436 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3437 integer_zero_node);
3438 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3440 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3442 else
3443 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3444 gimple_seq_add_stmt (&stmt_list,
3445 gimple_build_assign (fb_ref, fb_tmp));
3447 declare_vars (root->frame_decl,
3448 gimple_seq_first_stmt (gimple_body (context)), true);
3451 /* If any parameters were referenced non-locally, then we need to insert
3452 a copy or a pointer. */
3453 if (root->any_parm_remapped)
3455 tree p;
3456 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3458 tree field, x, y;
3460 field = lookup_field_for_decl (root, p, NO_INSERT);
3461 if (!field)
3462 continue;
3464 if (use_pointer_in_frame (p))
3465 x = build_addr (p);
3466 else
3467 x = p;
3469 /* If the assignment is from a non-register the stmt is
3470 not valid gimple. Make it so by using a temporary instead. */
3471 if (!is_gimple_reg (x)
3472 && is_gimple_reg_type (TREE_TYPE (x)))
3474 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3475 x = init_tmp_var (root, x, &gsi);
3478 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3479 root->frame_decl, field, NULL_TREE);
3480 stmt = gimple_build_assign (y, x);
3481 gimple_seq_add_stmt (&stmt_list, stmt);
3485 /* If a chain_field was created, then it needs to be initialized
3486 from chain_decl. */
3487 if (root->chain_field)
3489 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3490 root->frame_decl, root->chain_field, NULL_TREE);
3491 stmt = gimple_build_assign (x, get_chain_decl (root));
3492 gimple_seq_add_stmt (&stmt_list, stmt);
3495 /* If trampolines were created, then we need to initialize them. */
3496 if (root->any_tramp_created)
3498 struct nesting_info *i;
3499 for (i = root->inner; i ; i = i->next)
3501 tree field, x;
3503 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3504 if (!field)
3505 continue;
3507 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3508 stmt = build_init_call_stmt (root, i->context, field, x);
3509 gimple_seq_add_stmt (&stmt_list, stmt);
3513 /* If descriptors were created, then we need to initialize them. */
3514 if (root->any_descr_created)
3516 struct nesting_info *i;
3517 for (i = root->inner; i ; i = i->next)
3519 tree field, x;
3521 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3522 if (!field)
3523 continue;
3525 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3526 stmt = build_init_call_stmt (root, i->context, field, x);
3527 gimple_seq_add_stmt (&stmt_list, stmt);
3531 /* If we created initialization statements, insert them. */
3532 if (stmt_list)
3534 gbind *bind;
3535 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3536 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3537 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3538 gimple_bind_set_body (bind, stmt_list);
3541 /* If a chain_decl was created, then it needs to be registered with
3542 struct function so that it gets initialized from the static chain
3543 register at the beginning of the function. */
3544 sf = DECL_STRUCT_FUNCTION (root->context);
3545 sf->static_chain_decl = root->chain_decl;
3547 /* Similarly for the non-local goto save area. */
3548 if (root->nl_goto_field)
3550 sf->nonlocal_goto_save_area
3551 = get_frame_field (root, context, root->nl_goto_field, NULL);
3552 sf->has_nonlocal_label = 1;
3555 /* Make sure all new local variables get inserted into the
3556 proper BIND_EXPR. */
3557 if (root->new_local_var_chain)
3558 declare_vars (root->new_local_var_chain,
3559 gimple_seq_first_stmt (gimple_body (root->context)),
3560 false);
3562 if (root->debug_var_chain)
3564 tree debug_var;
3565 gbind *scope;
3567 remap_vla_decls (DECL_INITIAL (root->context), root);
3569 for (debug_var = root->debug_var_chain; debug_var;
3570 debug_var = DECL_CHAIN (debug_var))
3571 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3572 break;
3574 /* If there are any debug decls with variable length types,
3575 remap those types using other debug_var_chain variables. */
3576 if (debug_var)
3578 struct nesting_copy_body_data id;
3580 memset (&id, 0, sizeof (id));
3581 id.cb.copy_decl = nesting_copy_decl;
3582 id.cb.decl_map = new hash_map<tree, tree>;
3583 id.root = root;
3585 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3586 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3588 tree type = TREE_TYPE (debug_var);
3589 tree newt, t = type;
3590 struct nesting_info *i;
3592 for (i = root; i; i = i->outer)
3593 if (variably_modified_type_p (type, i->context))
3594 break;
3596 if (i == NULL)
3597 continue;
3599 id.cb.src_fn = i->context;
3600 id.cb.dst_fn = i->context;
3601 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3603 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3604 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3606 newt = TREE_TYPE (newt);
3607 t = TREE_TYPE (t);
3609 if (TYPE_NAME (newt)
3610 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3611 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3612 && newt != t
3613 && TYPE_NAME (newt) == TYPE_NAME (t))
3614 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3617 delete id.cb.decl_map;
3620 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3621 if (gimple_bind_block (scope))
3622 declare_vars (root->debug_var_chain, scope, true);
3623 else
3624 BLOCK_VARS (DECL_INITIAL (root->context))
3625 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3626 root->debug_var_chain);
3628 else
3629 fixup_vla_decls (DECL_INITIAL (root->context));
3631 /* Fold the rewritten MEM_REF trees. */
3632 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3634 /* Dump the translated tree function. */
3635 if (dump_file)
3637 fputs ("\n\n", dump_file);
3638 dump_function_to_file (root->context, dump_file, dump_flags);
3642 static void
3643 finalize_nesting_tree (struct nesting_info *root)
3645 struct nesting_info *n;
3646 FOR_EACH_NEST_INFO (n, root)
3647 finalize_nesting_tree_1 (n);
3650 /* Unnest the nodes and pass them to cgraph. */
3652 static void
3653 unnest_nesting_tree_1 (struct nesting_info *root)
3655 struct cgraph_node *node = cgraph_node::get (root->context);
3657 /* For nested functions update the cgraph to reflect unnesting.
3658 We also delay finalizing of these functions up to this point. */
3659 if (nested_function_info::get (node)->origin)
3661 unnest_function (node);
3662 if (!root->thunk_p)
3663 cgraph_node::finalize_function (root->context, true);
3667 static void
3668 unnest_nesting_tree (struct nesting_info *root)
3670 struct nesting_info *n;
3671 FOR_EACH_NEST_INFO (n, root)
3672 unnest_nesting_tree_1 (n);
3675 /* Free the data structures allocated during this pass. */
3677 static void
3678 free_nesting_tree (struct nesting_info *root)
3680 struct nesting_info *node, *next;
3682 node = iter_nestinfo_start (root);
3685 next = iter_nestinfo_next (node);
3686 delete node->var_map;
3687 delete node->field_map;
3688 delete node->mem_refs;
3689 free (node);
3690 node = next;
3692 while (node);
3695 /* Gimplify a function and all its nested functions. */
3696 static void
3697 gimplify_all_functions (struct cgraph_node *root)
3699 struct cgraph_node *iter;
3700 if (!gimple_body (root->decl))
3701 gimplify_function_tree (root->decl);
3702 for (iter = first_nested_function (root); iter;
3703 iter = next_nested_function (iter))
3704 if (!iter->thunk)
3705 gimplify_all_functions (iter);
3708 /* Main entry point for this pass. Process FNDECL and all of its nested
3709 subroutines and turn them into something less tightly bound. */
3711 void
3712 lower_nested_functions (tree fndecl)
3714 struct cgraph_node *cgn;
3715 struct nesting_info *root;
3717 /* If there are no nested functions, there's nothing to do. */
3718 cgn = cgraph_node::get (fndecl);
3719 if (!first_nested_function (cgn))
3720 return;
3722 gimplify_all_functions (cgn);
3724 set_dump_file (dump_begin (TDI_nested, &dump_flags));
3725 if (dump_file)
3726 fprintf (dump_file, "\n;; Function %s\n\n",
3727 lang_hooks.decl_printable_name (fndecl, 2));
3729 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3730 root = create_nesting_tree (cgn);
3732 walk_all_functions (convert_nonlocal_reference_stmt,
3733 convert_nonlocal_reference_op,
3734 root);
3735 walk_all_functions (convert_local_reference_stmt,
3736 convert_local_reference_op,
3737 root);
3738 walk_all_functions (convert_nl_goto_reference, NULL, root);
3739 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3741 convert_all_function_calls (root);
3742 finalize_nesting_tree (root);
3743 unnest_nesting_tree (root);
3745 free_nesting_tree (root);
3746 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3748 if (dump_file)
3750 dump_end (TDI_nested, dump_file);
3751 set_dump_file (NULL);
3755 #include "gt-tree-nested.h"