PR rtl-optimization/88018
[official-gcc.git] / gcc / tree-nested.c
blobc964b7a15b2d7b82fa45de03f7536cb0855f4200
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2018 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"
47 /* The object of this pass is to lower the representation of a set of nested
48 functions in order to expose all of the gory details of the various
49 nonlocal references. We want to do this sooner rather than later, in
50 order to give us more freedom in emitting all of the functions in question.
52 Back in olden times, when gcc was young, we developed an insanely
53 complicated scheme whereby variables which were referenced nonlocally
54 were forced to live in the stack of the declaring function, and then
55 the nested functions magically discovered where these variables were
56 placed. In order for this scheme to function properly, it required
57 that the outer function be partially expanded, then we switch to
58 compiling the inner function, and once done with those we switch back
59 to compiling the outer function. Such delicate ordering requirements
60 makes it difficult to do whole translation unit optimizations
61 involving such functions.
63 The implementation here is much more direct. Everything that can be
64 referenced by an inner function is a member of an explicitly created
65 structure herein called the "nonlocal frame struct". The incoming
66 static chain for a nested function is a pointer to this struct in
67 the parent. In this way, we settle on known offsets from a known
68 base, and so are decoupled from the logic that places objects in the
69 function's stack frame. More importantly, we don't have to wait for
70 that to happen -- since the compilation of the inner function is no
71 longer tied to a real stack frame, the nonlocal frame struct can be
72 allocated anywhere. Which means that the outer function is now
73 inlinable.
75 Theory of operation here is very simple. Iterate over all the
76 statements in all the functions (depth first) several times,
77 allocating structures and fields on demand. In general we want to
78 examine inner functions first, so that we can avoid making changes
79 to outer functions which are unnecessary.
81 The order of the passes matters a bit, in that later passes will be
82 skipped if it is discovered that the functions don't actually interact
83 at all. That is, they're nested in the lexical sense but could have
84 been written as independent functions without change. */
87 struct nesting_info
89 struct nesting_info *outer;
90 struct nesting_info *inner;
91 struct nesting_info *next;
93 hash_map<tree, tree> *field_map;
94 hash_map<tree, tree> *var_map;
95 hash_set<tree *> *mem_refs;
96 bitmap suppress_expansion;
98 tree context;
99 tree new_local_var_chain;
100 tree debug_var_chain;
101 tree frame_type;
102 tree frame_decl;
103 tree chain_field;
104 tree chain_decl;
105 tree nl_goto_field;
107 bool thunk_p;
108 bool any_parm_remapped;
109 bool any_tramp_created;
110 bool any_descr_created;
111 char static_chain_added;
115 /* Iterate over the nesting tree, starting with ROOT, depth first. */
117 static inline struct nesting_info *
118 iter_nestinfo_start (struct nesting_info *root)
120 while (root->inner)
121 root = root->inner;
122 return root;
125 static inline struct nesting_info *
126 iter_nestinfo_next (struct nesting_info *node)
128 if (node->next)
129 return iter_nestinfo_start (node->next);
130 return node->outer;
133 #define FOR_EACH_NEST_INFO(I, ROOT) \
134 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
136 /* Obstack used for the bitmaps in the struct above. */
137 static struct bitmap_obstack nesting_info_bitmap_obstack;
140 /* We're working in so many different function contexts simultaneously,
141 that create_tmp_var is dangerous. Prevent mishap. */
142 #define create_tmp_var cant_use_create_tmp_var_here_dummy
144 /* Like create_tmp_var, except record the variable for registration at
145 the given nesting level. */
147 static tree
148 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
150 tree tmp_var;
152 /* If the type is of variable size or a type which must be created by the
153 frontend, something is wrong. Note that we explicitly allow
154 incomplete types here, since we create them ourselves here. */
155 gcc_assert (!TREE_ADDRESSABLE (type));
156 gcc_assert (!TYPE_SIZE_UNIT (type)
157 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
159 tmp_var = create_tmp_var_raw (type, prefix);
160 DECL_CONTEXT (tmp_var) = info->context;
161 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
162 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
163 if (TREE_CODE (type) == COMPLEX_TYPE
164 || TREE_CODE (type) == VECTOR_TYPE)
165 DECL_GIMPLE_REG_P (tmp_var) = 1;
167 info->new_local_var_chain = tmp_var;
169 return tmp_var;
172 /* Take the address of EXP to be used within function CONTEXT.
173 Mark it for addressability as necessary. */
175 tree
176 build_addr (tree exp)
178 mark_addressable (exp);
179 return build_fold_addr_expr (exp);
182 /* Insert FIELD into TYPE, sorted by alignment requirements. */
184 void
185 insert_field_into_struct (tree type, tree field)
187 tree *p;
189 DECL_CONTEXT (field) = type;
191 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
192 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
193 break;
195 DECL_CHAIN (field) = *p;
196 *p = field;
198 /* Set correct alignment for frame struct type. */
199 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
200 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
203 /* Build or return the RECORD_TYPE that describes the frame state that is
204 shared between INFO->CONTEXT and its nested functions. This record will
205 not be complete until finalize_nesting_tree; up until that point we'll
206 be adding fields as necessary.
208 We also build the DECL that represents this frame in the function. */
210 static tree
211 get_frame_type (struct nesting_info *info)
213 tree type = info->frame_type;
214 if (!type)
216 char *name;
218 type = make_node (RECORD_TYPE);
220 name = concat ("FRAME.",
221 IDENTIFIER_POINTER (DECL_NAME (info->context)),
222 NULL);
223 TYPE_NAME (type) = get_identifier (name);
224 free (name);
226 info->frame_type = type;
228 /* Do not put info->frame_decl on info->new_local_var_chain,
229 so that we can declare it in the lexical blocks, which
230 makes sure virtual regs that end up appearing in its RTL
231 expression get substituted in instantiate_virtual_regs. */
232 info->frame_decl = create_tmp_var_raw (type, "FRAME");
233 DECL_CONTEXT (info->frame_decl) = info->context;
234 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
235 DECL_SEEN_IN_BIND_EXPR_P (info->frame_decl) = 1;
237 /* ??? Always make it addressable for now, since it is meant to
238 be pointed to by the static chain pointer. This pessimizes
239 when it turns out that no static chains are needed because
240 the nested functions referencing non-local variables are not
241 reachable, but the true pessimization is to create the non-
242 local frame structure in the first place. */
243 TREE_ADDRESSABLE (info->frame_decl) = 1;
246 return type;
249 /* Return true if DECL should be referenced by pointer in the non-local frame
250 structure. */
252 static bool
253 use_pointer_in_frame (tree decl)
255 if (TREE_CODE (decl) == PARM_DECL)
257 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
258 sized DECLs, and inefficient to copy large aggregates. Don't bother
259 moving anything but scalar parameters. */
260 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
262 else
264 /* Variable-sized DECLs can only come from OMP clauses at this point
265 since the gimplifier has already turned the regular variables into
266 pointers. Do the same as the gimplifier. */
267 return !DECL_SIZE (decl) || TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST;
271 /* Given DECL, a non-locally accessed variable, find or create a field
272 in the non-local frame structure for the given nesting context. */
274 static tree
275 lookup_field_for_decl (struct nesting_info *info, tree decl,
276 enum insert_option insert)
278 gcc_checking_assert (decl_function_context (decl) == info->context);
280 if (insert == NO_INSERT)
282 tree *slot = info->field_map->get (decl);
283 return slot ? *slot : NULL_TREE;
286 tree *slot = &info->field_map->get_or_insert (decl);
287 if (!*slot)
289 tree type = get_frame_type (info);
290 tree field = make_node (FIELD_DECL);
291 DECL_NAME (field) = DECL_NAME (decl);
293 if (use_pointer_in_frame (decl))
295 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
296 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
297 DECL_NONADDRESSABLE_P (field) = 1;
299 else
301 TREE_TYPE (field) = TREE_TYPE (decl);
302 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
303 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
304 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
305 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
306 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
307 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
309 /* Declare the transformation and adjust the original DECL. For a
310 variable or for a parameter when not optimizing, we make it point
311 to the field in the frame directly. For a parameter, we don't do
312 it when optimizing because the variable tracking pass will already
313 do the job, */
314 if (VAR_P (decl) || !optimize)
316 tree x
317 = build3 (COMPONENT_REF, TREE_TYPE (field), info->frame_decl,
318 field, NULL_TREE);
320 /* If the next declaration is a PARM_DECL pointing to the DECL,
321 we need to adjust its VALUE_EXPR directly, since chains of
322 VALUE_EXPRs run afoul of garbage collection. This occurs
323 in Ada for Out parameters that aren't copied in. */
324 tree next = DECL_CHAIN (decl);
325 if (next
326 && TREE_CODE (next) == PARM_DECL
327 && DECL_HAS_VALUE_EXPR_P (next)
328 && DECL_VALUE_EXPR (next) == decl)
329 SET_DECL_VALUE_EXPR (next, x);
331 SET_DECL_VALUE_EXPR (decl, x);
332 DECL_HAS_VALUE_EXPR_P (decl) = 1;
336 insert_field_into_struct (type, field);
337 *slot = field;
339 if (TREE_CODE (decl) == PARM_DECL)
340 info->any_parm_remapped = true;
343 return *slot;
346 /* Build or return the variable that holds the static chain within
347 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
349 static tree
350 get_chain_decl (struct nesting_info *info)
352 tree decl = info->chain_decl;
354 if (!decl)
356 tree type;
358 type = get_frame_type (info->outer);
359 type = build_pointer_type (type);
361 /* Note that this variable is *not* entered into any BIND_EXPR;
362 the construction of this variable is handled specially in
363 expand_function_start and initialize_inlined_parameters.
364 Note also that it's represented as a parameter. This is more
365 close to the truth, since the initial value does come from
366 the caller. */
367 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
368 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
369 DECL_ARTIFICIAL (decl) = 1;
370 DECL_IGNORED_P (decl) = 1;
371 TREE_USED (decl) = 1;
372 DECL_CONTEXT (decl) = info->context;
373 DECL_ARG_TYPE (decl) = type;
375 /* Tell tree-inline.c that we never write to this variable, so
376 it can copy-prop the replacement value immediately. */
377 TREE_READONLY (decl) = 1;
379 info->chain_decl = decl;
381 if (dump_file
382 && (dump_flags & TDF_DETAILS)
383 && !DECL_STATIC_CHAIN (info->context))
384 fprintf (dump_file, "Setting static-chain for %s\n",
385 lang_hooks.decl_printable_name (info->context, 2));
387 DECL_STATIC_CHAIN (info->context) = 1;
389 return decl;
392 /* Build or return the field within the non-local frame state that holds
393 the static chain for INFO->CONTEXT. This is the way to walk back up
394 multiple nesting levels. */
396 static tree
397 get_chain_field (struct nesting_info *info)
399 tree field = info->chain_field;
401 if (!field)
403 tree type = build_pointer_type (get_frame_type (info->outer));
405 field = make_node (FIELD_DECL);
406 DECL_NAME (field) = get_identifier ("__chain");
407 TREE_TYPE (field) = type;
408 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
409 DECL_NONADDRESSABLE_P (field) = 1;
411 insert_field_into_struct (get_frame_type (info), field);
413 info->chain_field = field;
415 if (dump_file
416 && (dump_flags & TDF_DETAILS)
417 && !DECL_STATIC_CHAIN (info->context))
418 fprintf (dump_file, "Setting static-chain for %s\n",
419 lang_hooks.decl_printable_name (info->context, 2));
421 DECL_STATIC_CHAIN (info->context) = 1;
423 return field;
426 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
428 static tree
429 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
430 gcall *call)
432 tree t;
434 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
435 gimple_call_set_lhs (call, t);
436 if (! gsi_end_p (*gsi))
437 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
438 gsi_insert_before (gsi, call, GSI_SAME_STMT);
440 return t;
444 /* Copy EXP into a temporary. Allocate the temporary in the context of
445 INFO and insert the initialization statement before GSI. */
447 static tree
448 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
450 tree t;
451 gimple *stmt;
453 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
454 stmt = gimple_build_assign (t, exp);
455 if (! gsi_end_p (*gsi))
456 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
457 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
459 return t;
463 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
465 static tree
466 gsi_gimplify_val (struct nesting_info *info, tree exp,
467 gimple_stmt_iterator *gsi)
469 if (is_gimple_val (exp))
470 return exp;
471 else
472 return init_tmp_var (info, exp, gsi);
475 /* Similarly, but copy from the temporary and insert the statement
476 after the iterator. */
478 static tree
479 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
481 tree t;
482 gimple *stmt;
484 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
485 stmt = gimple_build_assign (exp, t);
486 if (! gsi_end_p (*gsi))
487 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
488 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
490 return t;
493 /* Build or return the type used to represent a nested function trampoline. */
495 static GTY(()) tree trampoline_type;
497 static tree
498 get_trampoline_type (struct nesting_info *info)
500 unsigned align, size;
501 tree t;
503 if (trampoline_type)
504 return trampoline_type;
506 align = TRAMPOLINE_ALIGNMENT;
507 size = TRAMPOLINE_SIZE;
509 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
510 then allocate extra space so that we can do dynamic alignment. */
511 if (align > STACK_BOUNDARY)
513 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
514 align = STACK_BOUNDARY;
517 t = build_index_type (size_int (size - 1));
518 t = build_array_type (char_type_node, t);
519 t = build_decl (DECL_SOURCE_LOCATION (info->context),
520 FIELD_DECL, get_identifier ("__data"), t);
521 SET_DECL_ALIGN (t, align);
522 DECL_USER_ALIGN (t) = 1;
524 trampoline_type = make_node (RECORD_TYPE);
525 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
526 TYPE_FIELDS (trampoline_type) = t;
527 layout_type (trampoline_type);
528 DECL_CONTEXT (t) = trampoline_type;
530 return trampoline_type;
533 /* Build or return the type used to represent a nested function descriptor. */
535 static GTY(()) tree descriptor_type;
537 static tree
538 get_descriptor_type (struct nesting_info *info)
540 /* The base alignment is that of a function. */
541 const unsigned align = FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY);
542 tree t;
544 if (descriptor_type)
545 return descriptor_type;
547 t = build_index_type (integer_one_node);
548 t = build_array_type (ptr_type_node, t);
549 t = build_decl (DECL_SOURCE_LOCATION (info->context),
550 FIELD_DECL, get_identifier ("__data"), t);
551 SET_DECL_ALIGN (t, MAX (TYPE_ALIGN (ptr_type_node), align));
552 DECL_USER_ALIGN (t) = 1;
554 descriptor_type = make_node (RECORD_TYPE);
555 TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
556 TYPE_FIELDS (descriptor_type) = t;
557 layout_type (descriptor_type);
558 DECL_CONTEXT (t) = descriptor_type;
560 return descriptor_type;
563 /* Given DECL, a nested function, find or create an element in the
564 var map for this function. */
566 static tree
567 lookup_element_for_decl (struct nesting_info *info, tree decl,
568 enum insert_option insert)
570 if (insert == NO_INSERT)
572 tree *slot = info->var_map->get (decl);
573 return slot ? *slot : NULL_TREE;
576 tree *slot = &info->var_map->get_or_insert (decl);
577 if (!*slot)
578 *slot = build_tree_list (NULL_TREE, NULL_TREE);
580 return (tree) *slot;
583 /* Given DECL, a nested function, create a field in the non-local
584 frame structure for this function. */
586 static tree
587 create_field_for_decl (struct nesting_info *info, tree decl, tree type)
589 tree field = make_node (FIELD_DECL);
590 DECL_NAME (field) = DECL_NAME (decl);
591 TREE_TYPE (field) = type;
592 TREE_ADDRESSABLE (field) = 1;
593 insert_field_into_struct (get_frame_type (info), field);
594 return field;
597 /* Given DECL, a nested function, find or create a field in the non-local
598 frame structure for a trampoline for this function. */
600 static tree
601 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
602 enum insert_option insert)
604 tree elt, field;
606 elt = lookup_element_for_decl (info, decl, insert);
607 if (!elt)
608 return NULL_TREE;
610 field = TREE_PURPOSE (elt);
612 if (!field && insert == INSERT)
614 field = create_field_for_decl (info, decl, get_trampoline_type (info));
615 TREE_PURPOSE (elt) = field;
616 info->any_tramp_created = true;
619 return field;
622 /* Given DECL, a nested function, find or create a field in the non-local
623 frame structure for a descriptor for this function. */
625 static tree
626 lookup_descr_for_decl (struct nesting_info *info, tree decl,
627 enum insert_option insert)
629 tree elt, field;
631 elt = lookup_element_for_decl (info, decl, insert);
632 if (!elt)
633 return NULL_TREE;
635 field = TREE_VALUE (elt);
637 if (!field && insert == INSERT)
639 field = create_field_for_decl (info, decl, get_descriptor_type (info));
640 TREE_VALUE (elt) = field;
641 info->any_descr_created = true;
644 return field;
647 /* Build or return the field within the non-local frame state that holds
648 the non-local goto "jmp_buf". The buffer itself is maintained by the
649 rtl middle-end as dynamic stack space is allocated. */
651 static tree
652 get_nl_goto_field (struct nesting_info *info)
654 tree field = info->nl_goto_field;
655 if (!field)
657 unsigned size;
658 tree type;
660 /* For __builtin_nonlocal_goto, we need N words. The first is the
661 frame pointer, the rest is for the target's stack pointer save
662 area. The number of words is controlled by STACK_SAVEAREA_MODE;
663 not the best interface, but it'll do for now. */
664 if (Pmode == ptr_mode)
665 type = ptr_type_node;
666 else
667 type = lang_hooks.types.type_for_mode (Pmode, 1);
669 scalar_int_mode mode
670 = as_a <scalar_int_mode> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
671 size = GET_MODE_SIZE (mode);
672 size = size / GET_MODE_SIZE (Pmode);
673 size = size + 1;
675 type = build_array_type
676 (type, build_index_type (size_int (size)));
678 field = make_node (FIELD_DECL);
679 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
680 TREE_TYPE (field) = type;
681 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
682 TREE_ADDRESSABLE (field) = 1;
684 insert_field_into_struct (get_frame_type (info), field);
686 info->nl_goto_field = field;
689 return field;
692 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
694 static void
695 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
696 struct nesting_info *info, gimple_seq *pseq)
698 struct walk_stmt_info wi;
700 memset (&wi, 0, sizeof (wi));
701 wi.info = info;
702 wi.val_only = true;
703 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
707 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
709 static inline void
710 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
711 struct nesting_info *info)
713 gimple_seq body = gimple_body (info->context);
714 walk_body (callback_stmt, callback_op, info, &body);
715 gimple_set_body (info->context, body);
718 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
720 static void
721 walk_gimple_omp_for (gomp_for *for_stmt,
722 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
723 struct nesting_info *info)
725 struct walk_stmt_info wi;
726 gimple_seq seq;
727 tree t;
728 size_t i;
730 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
732 seq = NULL;
733 memset (&wi, 0, sizeof (wi));
734 wi.info = info;
735 wi.gsi = gsi_last (seq);
737 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
739 wi.val_only = false;
740 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
741 &wi, NULL);
742 wi.val_only = true;
743 wi.is_lhs = false;
744 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
745 &wi, NULL);
747 wi.val_only = true;
748 wi.is_lhs = false;
749 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
750 &wi, NULL);
752 t = gimple_omp_for_incr (for_stmt, i);
753 gcc_assert (BINARY_CLASS_P (t));
754 wi.val_only = false;
755 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
756 wi.val_only = true;
757 wi.is_lhs = false;
758 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
761 seq = gsi_seq (wi.gsi);
762 if (!gimple_seq_empty_p (seq))
764 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
765 annotate_all_with_location (seq, gimple_location (for_stmt));
766 gimple_seq_add_seq (&pre_body, seq);
767 gimple_omp_for_set_pre_body (for_stmt, pre_body);
771 /* Similarly for ROOT and all functions nested underneath, depth first. */
773 static void
774 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
775 struct nesting_info *root)
777 struct nesting_info *n;
778 FOR_EACH_NEST_INFO (n, root)
779 walk_function (callback_stmt, callback_op, n);
783 /* We have to check for a fairly pathological case. The operands of function
784 nested function are to be interpreted in the context of the enclosing
785 function. So if any are variably-sized, they will get remapped when the
786 enclosing function is inlined. But that remapping would also have to be
787 done in the types of the PARM_DECLs of the nested function, meaning the
788 argument types of that function will disagree with the arguments in the
789 calls to that function. So we'd either have to make a copy of the nested
790 function corresponding to each time the enclosing function was inlined or
791 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
792 function. The former is not practical. The latter would still require
793 detecting this case to know when to add the conversions. So, for now at
794 least, we don't inline such an enclosing function.
796 We have to do that check recursively, so here return indicating whether
797 FNDECL has such a nested function. ORIG_FN is the function we were
798 trying to inline to use for checking whether any argument is variably
799 modified by anything in it.
801 It would be better to do this in tree-inline.c so that we could give
802 the appropriate warning for why a function can't be inlined, but that's
803 too late since the nesting structure has already been flattened and
804 adding a flag just to record this fact seems a waste of a flag. */
806 static bool
807 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
809 struct cgraph_node *cgn = cgraph_node::get (fndecl);
810 tree arg;
812 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
814 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
815 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
816 return true;
818 if (check_for_nested_with_variably_modified (cgn->decl,
819 orig_fndecl))
820 return true;
823 return false;
826 /* Construct our local datastructure describing the function nesting
827 tree rooted by CGN. */
829 static struct nesting_info *
830 create_nesting_tree (struct cgraph_node *cgn)
832 struct nesting_info *info = XCNEW (struct nesting_info);
833 info->field_map = new hash_map<tree, tree>;
834 info->var_map = new hash_map<tree, tree>;
835 info->mem_refs = new hash_set<tree *>;
836 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
837 info->context = cgn->decl;
838 info->thunk_p = cgn->thunk.thunk_p;
840 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
842 struct nesting_info *sub = create_nesting_tree (cgn);
843 sub->outer = info;
844 sub->next = info->inner;
845 info->inner = sub;
848 /* See discussion at check_for_nested_with_variably_modified for a
849 discussion of why this has to be here. */
850 if (check_for_nested_with_variably_modified (info->context, info->context))
851 DECL_UNINLINABLE (info->context) = true;
853 return info;
856 /* Return an expression computing the static chain for TARGET_CONTEXT
857 from INFO->CONTEXT. Insert any necessary computations before TSI. */
859 static tree
860 get_static_chain (struct nesting_info *info, tree target_context,
861 gimple_stmt_iterator *gsi)
863 struct nesting_info *i;
864 tree x;
866 if (info->context == target_context)
868 x = build_addr (info->frame_decl);
869 info->static_chain_added |= 1;
871 else
873 x = get_chain_decl (info);
874 info->static_chain_added |= 2;
876 for (i = info->outer; i->context != target_context; i = i->outer)
878 tree field = get_chain_field (i);
880 x = build_simple_mem_ref (x);
881 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
882 x = init_tmp_var (info, x, gsi);
886 return x;
890 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
891 frame as seen from INFO->CONTEXT. Insert any necessary computations
892 before GSI. */
894 static tree
895 get_frame_field (struct nesting_info *info, tree target_context,
896 tree field, gimple_stmt_iterator *gsi)
898 struct nesting_info *i;
899 tree x;
901 if (info->context == target_context)
903 /* Make sure frame_decl gets created. */
904 (void) get_frame_type (info);
905 x = info->frame_decl;
906 info->static_chain_added |= 1;
908 else
910 x = get_chain_decl (info);
911 info->static_chain_added |= 2;
913 for (i = info->outer; i->context != target_context; i = i->outer)
915 tree field = get_chain_field (i);
917 x = build_simple_mem_ref (x);
918 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
919 x = init_tmp_var (info, x, gsi);
922 x = build_simple_mem_ref (x);
925 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
926 return x;
929 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
931 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
932 in the nested function with DECL_VALUE_EXPR set to reference the true
933 variable in the parent function. This is used both for debug info
934 and in OMP lowering. */
936 static tree
937 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
939 tree target_context;
940 struct nesting_info *i;
941 tree x, field, new_decl;
943 tree *slot = &info->var_map->get_or_insert (decl);
945 if (*slot)
946 return *slot;
948 target_context = decl_function_context (decl);
950 /* A copy of the code in get_frame_field, but without the temporaries. */
951 if (info->context == target_context)
953 /* Make sure frame_decl gets created. */
954 (void) get_frame_type (info);
955 x = info->frame_decl;
956 i = info;
957 info->static_chain_added |= 1;
959 else
961 x = get_chain_decl (info);
962 info->static_chain_added |= 2;
963 for (i = info->outer; i->context != target_context; i = i->outer)
965 field = get_chain_field (i);
966 x = build_simple_mem_ref (x);
967 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
969 x = build_simple_mem_ref (x);
972 field = lookup_field_for_decl (i, decl, INSERT);
973 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
974 if (use_pointer_in_frame (decl))
975 x = build_simple_mem_ref (x);
977 /* ??? We should be remapping types as well, surely. */
978 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
979 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
980 DECL_CONTEXT (new_decl) = info->context;
981 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
982 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
983 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
984 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
985 TREE_READONLY (new_decl) = TREE_READONLY (decl);
986 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
987 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
988 if ((TREE_CODE (decl) == PARM_DECL
989 || TREE_CODE (decl) == RESULT_DECL
990 || VAR_P (decl))
991 && DECL_BY_REFERENCE (decl))
992 DECL_BY_REFERENCE (new_decl) = 1;
994 SET_DECL_VALUE_EXPR (new_decl, x);
995 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
997 *slot = new_decl;
998 DECL_CHAIN (new_decl) = info->debug_var_chain;
999 info->debug_var_chain = new_decl;
1001 if (!optimize
1002 && info->context != target_context
1003 && variably_modified_type_p (TREE_TYPE (decl), NULL))
1004 note_nonlocal_vla_type (info, TREE_TYPE (decl));
1006 return new_decl;
1010 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1011 and PARM_DECLs that belong to outer functions.
1013 The rewrite will involve some number of structure accesses back up
1014 the static chain. E.g. for a variable FOO up one nesting level it'll
1015 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1016 indirections apply to decls for which use_pointer_in_frame is true. */
1018 static tree
1019 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
1021 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1022 struct nesting_info *const info = (struct nesting_info *) wi->info;
1023 tree t = *tp;
1025 *walk_subtrees = 0;
1026 switch (TREE_CODE (t))
1028 case VAR_DECL:
1029 /* Non-automatic variables are never processed. */
1030 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1031 break;
1032 /* FALLTHRU */
1034 case PARM_DECL:
1036 tree x, target_context = decl_function_context (t);
1038 if (info->context == target_context)
1039 break;
1041 wi->changed = true;
1043 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1044 x = get_nonlocal_debug_decl (info, t);
1045 else
1047 struct nesting_info *i = info;
1048 while (i && i->context != target_context)
1049 i = i->outer;
1050 /* If none of the outer contexts is the target context, this means
1051 that the VAR or PARM_DECL is referenced in a wrong context. */
1052 if (!i)
1053 internal_error ("%s from %s referenced in %s",
1054 IDENTIFIER_POINTER (DECL_NAME (t)),
1055 IDENTIFIER_POINTER (DECL_NAME (target_context)),
1056 IDENTIFIER_POINTER (DECL_NAME (info->context)));
1058 x = lookup_field_for_decl (i, t, INSERT);
1059 x = get_frame_field (info, target_context, x, &wi->gsi);
1060 if (use_pointer_in_frame (t))
1062 x = init_tmp_var (info, x, &wi->gsi);
1063 x = build_simple_mem_ref (x);
1067 if (wi->val_only)
1069 if (wi->is_lhs)
1070 x = save_tmp_var (info, x, &wi->gsi);
1071 else
1072 x = init_tmp_var (info, x, &wi->gsi);
1075 *tp = x;
1077 break;
1079 case LABEL_DECL:
1080 /* We're taking the address of a label from a parent function, but
1081 this is not itself a non-local goto. Mark the label such that it
1082 will not be deleted, much as we would with a label address in
1083 static storage. */
1084 if (decl_function_context (t) != info->context)
1085 FORCED_LABEL (t) = 1;
1086 break;
1088 case ADDR_EXPR:
1090 bool save_val_only = wi->val_only;
1092 wi->val_only = false;
1093 wi->is_lhs = false;
1094 wi->changed = false;
1095 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1096 wi->val_only = true;
1098 if (wi->changed)
1100 tree save_context;
1102 /* If we changed anything, we might no longer be directly
1103 referencing a decl. */
1104 save_context = current_function_decl;
1105 current_function_decl = info->context;
1106 recompute_tree_invariant_for_addr_expr (t);
1107 current_function_decl = save_context;
1109 /* If the callback converted the address argument in a context
1110 where we only accept variables (and min_invariant, presumably),
1111 then compute the address into a temporary. */
1112 if (save_val_only)
1113 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1114 t, &wi->gsi);
1117 break;
1119 case REALPART_EXPR:
1120 case IMAGPART_EXPR:
1121 case COMPONENT_REF:
1122 case ARRAY_REF:
1123 case ARRAY_RANGE_REF:
1124 case BIT_FIELD_REF:
1125 /* Go down this entire nest and just look at the final prefix and
1126 anything that describes the references. Otherwise, we lose track
1127 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1128 wi->val_only = true;
1129 wi->is_lhs = false;
1130 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1132 if (TREE_CODE (t) == COMPONENT_REF)
1133 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1134 NULL);
1135 else if (TREE_CODE (t) == ARRAY_REF
1136 || TREE_CODE (t) == ARRAY_RANGE_REF)
1138 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1139 wi, NULL);
1140 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1141 wi, NULL);
1142 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1143 wi, NULL);
1146 wi->val_only = false;
1147 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1148 break;
1150 case VIEW_CONVERT_EXPR:
1151 /* Just request to look at the subtrees, leaving val_only and lhs
1152 untouched. This might actually be for !val_only + lhs, in which
1153 case we don't want to force a replacement by a temporary. */
1154 *walk_subtrees = 1;
1155 break;
1157 default:
1158 if (!IS_TYPE_OR_DECL_P (t))
1160 *walk_subtrees = 1;
1161 wi->val_only = true;
1162 wi->is_lhs = false;
1164 break;
1167 return NULL_TREE;
1170 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1171 struct walk_stmt_info *);
1173 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1174 and PARM_DECLs that belong to outer functions. */
1176 static bool
1177 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1179 struct nesting_info *const info = (struct nesting_info *) wi->info;
1180 bool need_chain = false, need_stmts = false;
1181 tree clause, decl;
1182 int dummy;
1183 bitmap new_suppress;
1185 new_suppress = BITMAP_GGC_ALLOC ();
1186 bitmap_copy (new_suppress, info->suppress_expansion);
1188 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1190 switch (OMP_CLAUSE_CODE (clause))
1192 case OMP_CLAUSE_REDUCTION:
1193 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1194 need_stmts = true;
1195 goto do_decl_clause;
1197 case OMP_CLAUSE_LASTPRIVATE:
1198 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1199 need_stmts = true;
1200 goto do_decl_clause;
1202 case OMP_CLAUSE_LINEAR:
1203 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1204 need_stmts = true;
1205 wi->val_only = true;
1206 wi->is_lhs = false;
1207 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1208 &dummy, wi);
1209 goto do_decl_clause;
1211 case OMP_CLAUSE_PRIVATE:
1212 case OMP_CLAUSE_FIRSTPRIVATE:
1213 case OMP_CLAUSE_COPYPRIVATE:
1214 case OMP_CLAUSE_SHARED:
1215 case OMP_CLAUSE_TO_DECLARE:
1216 case OMP_CLAUSE_LINK:
1217 case OMP_CLAUSE_USE_DEVICE_PTR:
1218 case OMP_CLAUSE_IS_DEVICE_PTR:
1219 do_decl_clause:
1220 decl = OMP_CLAUSE_DECL (clause);
1221 if (VAR_P (decl)
1222 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1223 break;
1224 if (decl_function_context (decl) != info->context)
1226 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1227 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1228 bitmap_set_bit (new_suppress, DECL_UID (decl));
1229 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1230 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1231 need_chain = true;
1233 break;
1235 case OMP_CLAUSE_SCHEDULE:
1236 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1237 break;
1238 /* FALLTHRU */
1239 case OMP_CLAUSE_FINAL:
1240 case OMP_CLAUSE_IF:
1241 case OMP_CLAUSE_NUM_THREADS:
1242 case OMP_CLAUSE_DEPEND:
1243 case OMP_CLAUSE_DEVICE:
1244 case OMP_CLAUSE_NUM_TEAMS:
1245 case OMP_CLAUSE_THREAD_LIMIT:
1246 case OMP_CLAUSE_SAFELEN:
1247 case OMP_CLAUSE_SIMDLEN:
1248 case OMP_CLAUSE_PRIORITY:
1249 case OMP_CLAUSE_GRAINSIZE:
1250 case OMP_CLAUSE_NUM_TASKS:
1251 case OMP_CLAUSE_HINT:
1252 case OMP_CLAUSE_NUM_GANGS:
1253 case OMP_CLAUSE_NUM_WORKERS:
1254 case OMP_CLAUSE_VECTOR_LENGTH:
1255 case OMP_CLAUSE_GANG:
1256 case OMP_CLAUSE_WORKER:
1257 case OMP_CLAUSE_VECTOR:
1258 case OMP_CLAUSE_ASYNC:
1259 case OMP_CLAUSE_WAIT:
1260 /* Several OpenACC clauses have optional arguments. Check if they
1261 are present. */
1262 if (OMP_CLAUSE_OPERAND (clause, 0))
1264 wi->val_only = true;
1265 wi->is_lhs = false;
1266 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1267 &dummy, wi);
1270 /* The gang clause accepts two arguments. */
1271 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1272 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1274 wi->val_only = true;
1275 wi->is_lhs = false;
1276 convert_nonlocal_reference_op
1277 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1279 break;
1281 case OMP_CLAUSE_DIST_SCHEDULE:
1282 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1284 wi->val_only = true;
1285 wi->is_lhs = false;
1286 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1287 &dummy, wi);
1289 break;
1291 case OMP_CLAUSE_MAP:
1292 case OMP_CLAUSE_TO:
1293 case OMP_CLAUSE_FROM:
1294 if (OMP_CLAUSE_SIZE (clause))
1296 wi->val_only = true;
1297 wi->is_lhs = false;
1298 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1299 &dummy, wi);
1301 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1302 goto do_decl_clause;
1303 wi->val_only = true;
1304 wi->is_lhs = false;
1305 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1306 wi, NULL);
1307 break;
1309 case OMP_CLAUSE_ALIGNED:
1310 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1312 wi->val_only = true;
1313 wi->is_lhs = false;
1314 convert_nonlocal_reference_op
1315 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1317 /* FALLTHRU */
1318 case OMP_CLAUSE_NONTEMPORAL:
1319 /* Like do_decl_clause, but don't add any suppression. */
1320 decl = OMP_CLAUSE_DECL (clause);
1321 if (VAR_P (decl)
1322 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1323 break;
1324 if (decl_function_context (decl) != info->context)
1326 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1327 need_chain = true;
1329 break;
1331 case OMP_CLAUSE_NOWAIT:
1332 case OMP_CLAUSE_ORDERED:
1333 case OMP_CLAUSE_DEFAULT:
1334 case OMP_CLAUSE_COPYIN:
1335 case OMP_CLAUSE_COLLAPSE:
1336 case OMP_CLAUSE_TILE:
1337 case OMP_CLAUSE_UNTIED:
1338 case OMP_CLAUSE_MERGEABLE:
1339 case OMP_CLAUSE_PROC_BIND:
1340 case OMP_CLAUSE_NOGROUP:
1341 case OMP_CLAUSE_THREADS:
1342 case OMP_CLAUSE_SIMD:
1343 case OMP_CLAUSE_DEFAULTMAP:
1344 case OMP_CLAUSE_SEQ:
1345 case OMP_CLAUSE_INDEPENDENT:
1346 case OMP_CLAUSE_AUTO:
1347 case OMP_CLAUSE_IF_PRESENT:
1348 case OMP_CLAUSE_FINALIZE:
1349 break;
1351 /* The following clause belongs to the OpenACC cache directive, which
1352 is discarded during gimplification. */
1353 case OMP_CLAUSE__CACHE_:
1354 /* The following clauses are only allowed in the OpenMP declare simd
1355 directive, so not seen here. */
1356 case OMP_CLAUSE_UNIFORM:
1357 case OMP_CLAUSE_INBRANCH:
1358 case OMP_CLAUSE_NOTINBRANCH:
1359 /* The following clauses are only allowed on OpenMP cancel and
1360 cancellation point directives, which at this point have already
1361 been lowered into a function call. */
1362 case OMP_CLAUSE_FOR:
1363 case OMP_CLAUSE_PARALLEL:
1364 case OMP_CLAUSE_SECTIONS:
1365 case OMP_CLAUSE_TASKGROUP:
1366 /* The following clauses are only added during OMP lowering; nested
1367 function decomposition happens before that. */
1368 case OMP_CLAUSE__LOOPTEMP_:
1369 case OMP_CLAUSE__REDUCTEMP_:
1370 case OMP_CLAUSE__SIMDUID_:
1371 case OMP_CLAUSE__GRIDDIM_:
1372 /* Anything else. */
1373 default:
1374 gcc_unreachable ();
1378 info->suppress_expansion = new_suppress;
1380 if (need_stmts)
1381 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1382 switch (OMP_CLAUSE_CODE (clause))
1384 case OMP_CLAUSE_REDUCTION:
1385 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1387 tree old_context
1388 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1389 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1390 = info->context;
1391 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1392 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1393 = info->context;
1394 walk_body (convert_nonlocal_reference_stmt,
1395 convert_nonlocal_reference_op, info,
1396 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1397 walk_body (convert_nonlocal_reference_stmt,
1398 convert_nonlocal_reference_op, info,
1399 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1400 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1401 = old_context;
1402 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1403 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1404 = old_context;
1406 break;
1408 case OMP_CLAUSE_LASTPRIVATE:
1409 walk_body (convert_nonlocal_reference_stmt,
1410 convert_nonlocal_reference_op, info,
1411 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1412 break;
1414 case OMP_CLAUSE_LINEAR:
1415 walk_body (convert_nonlocal_reference_stmt,
1416 convert_nonlocal_reference_op, info,
1417 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1418 break;
1420 default:
1421 break;
1424 return need_chain;
1427 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1429 static void
1430 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1432 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1433 type = TREE_TYPE (type);
1435 if (TYPE_NAME (type)
1436 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1437 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1438 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1440 while (POINTER_TYPE_P (type)
1441 || TREE_CODE (type) == VECTOR_TYPE
1442 || TREE_CODE (type) == FUNCTION_TYPE
1443 || TREE_CODE (type) == METHOD_TYPE)
1444 type = TREE_TYPE (type);
1446 if (TREE_CODE (type) == ARRAY_TYPE)
1448 tree domain, t;
1450 note_nonlocal_vla_type (info, TREE_TYPE (type));
1451 domain = TYPE_DOMAIN (type);
1452 if (domain)
1454 t = TYPE_MIN_VALUE (domain);
1455 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1456 && decl_function_context (t) != info->context)
1457 get_nonlocal_debug_decl (info, t);
1458 t = TYPE_MAX_VALUE (domain);
1459 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1460 && decl_function_context (t) != info->context)
1461 get_nonlocal_debug_decl (info, t);
1466 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1467 PARM_DECLs that belong to outer functions. This handles statements
1468 that are not handled via the standard recursion done in
1469 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1470 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1471 operands of STMT have been handled by this function. */
1473 static tree
1474 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1475 struct walk_stmt_info *wi)
1477 struct nesting_info *info = (struct nesting_info *) wi->info;
1478 tree save_local_var_chain;
1479 bitmap save_suppress;
1480 gimple *stmt = gsi_stmt (*gsi);
1482 switch (gimple_code (stmt))
1484 case GIMPLE_GOTO:
1485 /* Don't walk non-local gotos for now. */
1486 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1488 wi->val_only = true;
1489 wi->is_lhs = false;
1490 *handled_ops_p = false;
1491 return NULL_TREE;
1493 break;
1495 case GIMPLE_OMP_PARALLEL:
1496 case GIMPLE_OMP_TASK:
1497 save_suppress = info->suppress_expansion;
1498 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1499 wi))
1501 tree c, decl;
1502 decl = get_chain_decl (info);
1503 c = build_omp_clause (gimple_location (stmt),
1504 OMP_CLAUSE_FIRSTPRIVATE);
1505 OMP_CLAUSE_DECL (c) = decl;
1506 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1507 gimple_omp_taskreg_set_clauses (stmt, c);
1510 save_local_var_chain = info->new_local_var_chain;
1511 info->new_local_var_chain = NULL;
1513 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1514 info, gimple_omp_body_ptr (stmt));
1516 if (info->new_local_var_chain)
1517 declare_vars (info->new_local_var_chain,
1518 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1519 false);
1520 info->new_local_var_chain = save_local_var_chain;
1521 info->suppress_expansion = save_suppress;
1522 break;
1524 case GIMPLE_OMP_FOR:
1525 save_suppress = info->suppress_expansion;
1526 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1527 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1528 convert_nonlocal_reference_stmt,
1529 convert_nonlocal_reference_op, info);
1530 walk_body (convert_nonlocal_reference_stmt,
1531 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1532 info->suppress_expansion = save_suppress;
1533 break;
1535 case GIMPLE_OMP_SECTIONS:
1536 save_suppress = info->suppress_expansion;
1537 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1538 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1539 info, gimple_omp_body_ptr (stmt));
1540 info->suppress_expansion = save_suppress;
1541 break;
1543 case GIMPLE_OMP_SINGLE:
1544 save_suppress = info->suppress_expansion;
1545 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1546 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1547 info, gimple_omp_body_ptr (stmt));
1548 info->suppress_expansion = save_suppress;
1549 break;
1551 case GIMPLE_OMP_TARGET:
1552 if (!is_gimple_omp_offloaded (stmt))
1554 save_suppress = info->suppress_expansion;
1555 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1556 wi);
1557 info->suppress_expansion = save_suppress;
1558 walk_body (convert_nonlocal_reference_stmt,
1559 convert_nonlocal_reference_op, info,
1560 gimple_omp_body_ptr (stmt));
1561 break;
1563 save_suppress = info->suppress_expansion;
1564 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1565 wi))
1567 tree c, decl;
1568 decl = get_chain_decl (info);
1569 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1570 OMP_CLAUSE_DECL (c) = decl;
1571 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1572 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1573 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1574 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1577 save_local_var_chain = info->new_local_var_chain;
1578 info->new_local_var_chain = NULL;
1580 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1581 info, gimple_omp_body_ptr (stmt));
1583 if (info->new_local_var_chain)
1584 declare_vars (info->new_local_var_chain,
1585 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1586 false);
1587 info->new_local_var_chain = save_local_var_chain;
1588 info->suppress_expansion = save_suppress;
1589 break;
1591 case GIMPLE_OMP_TEAMS:
1592 save_suppress = info->suppress_expansion;
1593 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1594 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1595 info, gimple_omp_body_ptr (stmt));
1596 info->suppress_expansion = save_suppress;
1597 break;
1599 case GIMPLE_OMP_SECTION:
1600 case GIMPLE_OMP_MASTER:
1601 case GIMPLE_OMP_TASKGROUP:
1602 case GIMPLE_OMP_ORDERED:
1603 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1604 info, gimple_omp_body_ptr (stmt));
1605 break;
1607 case GIMPLE_BIND:
1609 gbind *bind_stmt = as_a <gbind *> (stmt);
1611 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1612 if (TREE_CODE (var) == NAMELIST_DECL)
1614 /* Adjust decls mentioned in NAMELIST_DECL. */
1615 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1616 tree decl;
1617 unsigned int i;
1619 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1621 if (VAR_P (decl)
1622 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1623 continue;
1624 if (decl_function_context (decl) != info->context)
1625 CONSTRUCTOR_ELT (decls, i)->value
1626 = get_nonlocal_debug_decl (info, decl);
1630 *handled_ops_p = false;
1631 return NULL_TREE;
1633 case GIMPLE_COND:
1634 wi->val_only = true;
1635 wi->is_lhs = false;
1636 *handled_ops_p = false;
1637 return NULL_TREE;
1639 default:
1640 /* For every other statement that we are not interested in
1641 handling here, let the walker traverse the operands. */
1642 *handled_ops_p = false;
1643 return NULL_TREE;
1646 /* We have handled all of STMT operands, no need to traverse the operands. */
1647 *handled_ops_p = true;
1648 return NULL_TREE;
1652 /* A subroutine of convert_local_reference. Create a local variable
1653 in the parent function with DECL_VALUE_EXPR set to reference the
1654 field in FRAME. This is used both for debug info and in OMP
1655 lowering. */
1657 static tree
1658 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1660 tree x, new_decl;
1662 tree *slot = &info->var_map->get_or_insert (decl);
1663 if (*slot)
1664 return *slot;
1666 /* Make sure frame_decl gets created. */
1667 (void) get_frame_type (info);
1668 x = info->frame_decl;
1669 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1671 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1672 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1673 DECL_CONTEXT (new_decl) = info->context;
1674 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1675 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1676 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1677 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1678 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1679 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1680 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1681 if ((TREE_CODE (decl) == PARM_DECL
1682 || TREE_CODE (decl) == RESULT_DECL
1683 || VAR_P (decl))
1684 && DECL_BY_REFERENCE (decl))
1685 DECL_BY_REFERENCE (new_decl) = 1;
1687 SET_DECL_VALUE_EXPR (new_decl, x);
1688 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1689 *slot = new_decl;
1691 DECL_CHAIN (new_decl) = info->debug_var_chain;
1692 info->debug_var_chain = new_decl;
1694 /* Do not emit debug info twice. */
1695 DECL_IGNORED_P (decl) = 1;
1697 return new_decl;
1701 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1702 and PARM_DECLs that were referenced by inner nested functions.
1703 The rewrite will be a structure reference to the local frame variable. */
1705 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1707 static tree
1708 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1710 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1711 struct nesting_info *const info = (struct nesting_info *) wi->info;
1712 tree t = *tp, field, x;
1713 bool save_val_only;
1715 *walk_subtrees = 0;
1716 switch (TREE_CODE (t))
1718 case VAR_DECL:
1719 /* Non-automatic variables are never processed. */
1720 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1721 break;
1722 /* FALLTHRU */
1724 case PARM_DECL:
1725 if (t != info->frame_decl && decl_function_context (t) == info->context)
1727 /* If we copied a pointer to the frame, then the original decl
1728 is used unchanged in the parent function. */
1729 if (use_pointer_in_frame (t))
1730 break;
1732 /* No need to transform anything if no child references the
1733 variable. */
1734 field = lookup_field_for_decl (info, t, NO_INSERT);
1735 if (!field)
1736 break;
1737 wi->changed = true;
1739 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1740 x = get_local_debug_decl (info, t, field);
1741 else
1742 x = get_frame_field (info, info->context, field, &wi->gsi);
1744 if (wi->val_only)
1746 if (wi->is_lhs)
1747 x = save_tmp_var (info, x, &wi->gsi);
1748 else
1749 x = init_tmp_var (info, x, &wi->gsi);
1752 *tp = x;
1754 break;
1756 case ADDR_EXPR:
1757 save_val_only = wi->val_only;
1758 wi->val_only = false;
1759 wi->is_lhs = false;
1760 wi->changed = false;
1761 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1762 wi->val_only = save_val_only;
1764 /* If we converted anything ... */
1765 if (wi->changed)
1767 tree save_context;
1769 /* Then the frame decl is now addressable. */
1770 TREE_ADDRESSABLE (info->frame_decl) = 1;
1772 save_context = current_function_decl;
1773 current_function_decl = info->context;
1774 recompute_tree_invariant_for_addr_expr (t);
1775 current_function_decl = save_context;
1777 /* If we are in a context where we only accept values, then
1778 compute the address into a temporary. */
1779 if (save_val_only)
1780 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1781 t, &wi->gsi);
1783 break;
1785 case REALPART_EXPR:
1786 case IMAGPART_EXPR:
1787 case COMPONENT_REF:
1788 case ARRAY_REF:
1789 case ARRAY_RANGE_REF:
1790 case BIT_FIELD_REF:
1791 /* Go down this entire nest and just look at the final prefix and
1792 anything that describes the references. Otherwise, we lose track
1793 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1794 save_val_only = wi->val_only;
1795 wi->val_only = true;
1796 wi->is_lhs = false;
1797 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1799 if (TREE_CODE (t) == COMPONENT_REF)
1800 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1801 NULL);
1802 else if (TREE_CODE (t) == ARRAY_REF
1803 || TREE_CODE (t) == ARRAY_RANGE_REF)
1805 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1806 NULL);
1807 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1808 NULL);
1809 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1810 NULL);
1813 wi->val_only = false;
1814 walk_tree (tp, convert_local_reference_op, wi, NULL);
1815 wi->val_only = save_val_only;
1816 break;
1818 case MEM_REF:
1819 save_val_only = wi->val_only;
1820 wi->val_only = true;
1821 wi->is_lhs = false;
1822 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1823 wi, NULL);
1824 /* We need to re-fold the MEM_REF as component references as
1825 part of a ADDR_EXPR address are not allowed. But we cannot
1826 fold here, as the chain record type is not yet finalized. */
1827 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1828 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1829 info->mem_refs->add (tp);
1830 wi->val_only = save_val_only;
1831 break;
1833 case VIEW_CONVERT_EXPR:
1834 /* Just request to look at the subtrees, leaving val_only and lhs
1835 untouched. This might actually be for !val_only + lhs, in which
1836 case we don't want to force a replacement by a temporary. */
1837 *walk_subtrees = 1;
1838 break;
1840 default:
1841 if (!IS_TYPE_OR_DECL_P (t))
1843 *walk_subtrees = 1;
1844 wi->val_only = true;
1845 wi->is_lhs = false;
1847 break;
1850 return NULL_TREE;
1853 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1854 struct walk_stmt_info *);
1856 /* Helper for convert_local_reference. Convert all the references in
1857 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1859 static bool
1860 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1862 struct nesting_info *const info = (struct nesting_info *) wi->info;
1863 bool need_frame = false, need_stmts = false;
1864 tree clause, decl;
1865 int dummy;
1866 bitmap new_suppress;
1868 new_suppress = BITMAP_GGC_ALLOC ();
1869 bitmap_copy (new_suppress, info->suppress_expansion);
1871 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1873 switch (OMP_CLAUSE_CODE (clause))
1875 case OMP_CLAUSE_REDUCTION:
1876 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1877 need_stmts = true;
1878 goto do_decl_clause;
1880 case OMP_CLAUSE_LASTPRIVATE:
1881 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1882 need_stmts = true;
1883 goto do_decl_clause;
1885 case OMP_CLAUSE_LINEAR:
1886 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1887 need_stmts = true;
1888 wi->val_only = true;
1889 wi->is_lhs = false;
1890 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1891 wi);
1892 goto do_decl_clause;
1894 case OMP_CLAUSE_PRIVATE:
1895 case OMP_CLAUSE_FIRSTPRIVATE:
1896 case OMP_CLAUSE_COPYPRIVATE:
1897 case OMP_CLAUSE_SHARED:
1898 case OMP_CLAUSE_TO_DECLARE:
1899 case OMP_CLAUSE_LINK:
1900 case OMP_CLAUSE_USE_DEVICE_PTR:
1901 case OMP_CLAUSE_IS_DEVICE_PTR:
1902 do_decl_clause:
1903 decl = OMP_CLAUSE_DECL (clause);
1904 if (VAR_P (decl)
1905 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1906 break;
1907 if (decl_function_context (decl) == info->context
1908 && !use_pointer_in_frame (decl))
1910 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1911 if (field)
1913 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1914 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1915 bitmap_set_bit (new_suppress, DECL_UID (decl));
1916 OMP_CLAUSE_DECL (clause)
1917 = get_local_debug_decl (info, decl, field);
1918 need_frame = true;
1921 break;
1923 case OMP_CLAUSE_SCHEDULE:
1924 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1925 break;
1926 /* FALLTHRU */
1927 case OMP_CLAUSE_FINAL:
1928 case OMP_CLAUSE_IF:
1929 case OMP_CLAUSE_NUM_THREADS:
1930 case OMP_CLAUSE_DEPEND:
1931 case OMP_CLAUSE_DEVICE:
1932 case OMP_CLAUSE_NUM_TEAMS:
1933 case OMP_CLAUSE_THREAD_LIMIT:
1934 case OMP_CLAUSE_SAFELEN:
1935 case OMP_CLAUSE_SIMDLEN:
1936 case OMP_CLAUSE_PRIORITY:
1937 case OMP_CLAUSE_GRAINSIZE:
1938 case OMP_CLAUSE_NUM_TASKS:
1939 case OMP_CLAUSE_HINT:
1940 case OMP_CLAUSE_NUM_GANGS:
1941 case OMP_CLAUSE_NUM_WORKERS:
1942 case OMP_CLAUSE_VECTOR_LENGTH:
1943 case OMP_CLAUSE_GANG:
1944 case OMP_CLAUSE_WORKER:
1945 case OMP_CLAUSE_VECTOR:
1946 case OMP_CLAUSE_ASYNC:
1947 case OMP_CLAUSE_WAIT:
1948 /* Several OpenACC clauses have optional arguments. Check if they
1949 are present. */
1950 if (OMP_CLAUSE_OPERAND (clause, 0))
1952 wi->val_only = true;
1953 wi->is_lhs = false;
1954 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1955 &dummy, wi);
1958 /* The gang clause accepts two arguments. */
1959 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1960 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1962 wi->val_only = true;
1963 wi->is_lhs = false;
1964 convert_nonlocal_reference_op
1965 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1967 break;
1969 case OMP_CLAUSE_DIST_SCHEDULE:
1970 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1972 wi->val_only = true;
1973 wi->is_lhs = false;
1974 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1975 &dummy, wi);
1977 break;
1979 case OMP_CLAUSE_MAP:
1980 case OMP_CLAUSE_TO:
1981 case OMP_CLAUSE_FROM:
1982 if (OMP_CLAUSE_SIZE (clause))
1984 wi->val_only = true;
1985 wi->is_lhs = false;
1986 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1987 &dummy, wi);
1989 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1990 goto do_decl_clause;
1991 wi->val_only = true;
1992 wi->is_lhs = false;
1993 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1994 wi, NULL);
1995 break;
1997 case OMP_CLAUSE_ALIGNED:
1998 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2000 wi->val_only = true;
2001 wi->is_lhs = false;
2002 convert_local_reference_op
2003 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2005 /* FALLTHRU */
2006 case OMP_CLAUSE_NONTEMPORAL:
2007 /* Like do_decl_clause, but don't add any suppression. */
2008 decl = OMP_CLAUSE_DECL (clause);
2009 if (VAR_P (decl)
2010 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2011 break;
2012 if (decl_function_context (decl) == info->context
2013 && !use_pointer_in_frame (decl))
2015 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2016 if (field)
2018 OMP_CLAUSE_DECL (clause)
2019 = get_local_debug_decl (info, decl, field);
2020 need_frame = true;
2023 break;
2025 case OMP_CLAUSE_NOWAIT:
2026 case OMP_CLAUSE_ORDERED:
2027 case OMP_CLAUSE_DEFAULT:
2028 case OMP_CLAUSE_COPYIN:
2029 case OMP_CLAUSE_COLLAPSE:
2030 case OMP_CLAUSE_TILE:
2031 case OMP_CLAUSE_UNTIED:
2032 case OMP_CLAUSE_MERGEABLE:
2033 case OMP_CLAUSE_PROC_BIND:
2034 case OMP_CLAUSE_NOGROUP:
2035 case OMP_CLAUSE_THREADS:
2036 case OMP_CLAUSE_SIMD:
2037 case OMP_CLAUSE_DEFAULTMAP:
2038 case OMP_CLAUSE_SEQ:
2039 case OMP_CLAUSE_INDEPENDENT:
2040 case OMP_CLAUSE_AUTO:
2041 case OMP_CLAUSE_IF_PRESENT:
2042 case OMP_CLAUSE_FINALIZE:
2043 break;
2045 /* The following clause belongs to the OpenACC cache directive, which
2046 is discarded during gimplification. */
2047 case OMP_CLAUSE__CACHE_:
2048 /* The following clauses are only allowed in the OpenMP declare simd
2049 directive, so not seen here. */
2050 case OMP_CLAUSE_UNIFORM:
2051 case OMP_CLAUSE_INBRANCH:
2052 case OMP_CLAUSE_NOTINBRANCH:
2053 /* The following clauses are only allowed on OpenMP cancel and
2054 cancellation point directives, which at this point have already
2055 been lowered into a function call. */
2056 case OMP_CLAUSE_FOR:
2057 case OMP_CLAUSE_PARALLEL:
2058 case OMP_CLAUSE_SECTIONS:
2059 case OMP_CLAUSE_TASKGROUP:
2060 /* The following clauses are only added during OMP lowering; nested
2061 function decomposition happens before that. */
2062 case OMP_CLAUSE__LOOPTEMP_:
2063 case OMP_CLAUSE__REDUCTEMP_:
2064 case OMP_CLAUSE__SIMDUID_:
2065 case OMP_CLAUSE__GRIDDIM_:
2066 /* Anything else. */
2067 default:
2068 gcc_unreachable ();
2072 info->suppress_expansion = new_suppress;
2074 if (need_stmts)
2075 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2076 switch (OMP_CLAUSE_CODE (clause))
2078 case OMP_CLAUSE_REDUCTION:
2079 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2081 tree old_context
2082 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2083 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2084 = info->context;
2085 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2086 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2087 = info->context;
2088 walk_body (convert_local_reference_stmt,
2089 convert_local_reference_op, info,
2090 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2091 walk_body (convert_local_reference_stmt,
2092 convert_local_reference_op, info,
2093 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2094 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2095 = old_context;
2096 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2097 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2098 = old_context;
2100 break;
2102 case OMP_CLAUSE_LASTPRIVATE:
2103 walk_body (convert_local_reference_stmt,
2104 convert_local_reference_op, info,
2105 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2106 break;
2108 case OMP_CLAUSE_LINEAR:
2109 walk_body (convert_local_reference_stmt,
2110 convert_local_reference_op, info,
2111 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2112 break;
2114 default:
2115 break;
2118 return need_frame;
2122 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2123 and PARM_DECLs that were referenced by inner nested functions.
2124 The rewrite will be a structure reference to the local frame variable. */
2126 static tree
2127 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2128 struct walk_stmt_info *wi)
2130 struct nesting_info *info = (struct nesting_info *) wi->info;
2131 tree save_local_var_chain;
2132 bitmap save_suppress;
2133 char save_static_chain_added;
2134 bool frame_decl_added;
2135 gimple *stmt = gsi_stmt (*gsi);
2137 switch (gimple_code (stmt))
2139 case GIMPLE_OMP_PARALLEL:
2140 case GIMPLE_OMP_TASK:
2141 save_suppress = info->suppress_expansion;
2142 frame_decl_added = false;
2143 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2144 wi))
2146 tree c = build_omp_clause (gimple_location (stmt),
2147 OMP_CLAUSE_SHARED);
2148 (void) get_frame_type (info);
2149 OMP_CLAUSE_DECL (c) = info->frame_decl;
2150 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2151 gimple_omp_taskreg_set_clauses (stmt, c);
2152 info->static_chain_added |= 4;
2153 frame_decl_added = true;
2156 save_local_var_chain = info->new_local_var_chain;
2157 save_static_chain_added = info->static_chain_added;
2158 info->new_local_var_chain = NULL;
2159 info->static_chain_added = 0;
2161 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2162 gimple_omp_body_ptr (stmt));
2164 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2166 tree c = build_omp_clause (gimple_location (stmt),
2167 OMP_CLAUSE_SHARED);
2168 (void) get_frame_type (info);
2169 OMP_CLAUSE_DECL (c) = info->frame_decl;
2170 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2171 info->static_chain_added |= 4;
2172 gimple_omp_taskreg_set_clauses (stmt, c);
2174 if (info->new_local_var_chain)
2175 declare_vars (info->new_local_var_chain,
2176 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2177 info->new_local_var_chain = save_local_var_chain;
2178 info->suppress_expansion = save_suppress;
2179 info->static_chain_added |= save_static_chain_added;
2180 break;
2182 case GIMPLE_OMP_FOR:
2183 save_suppress = info->suppress_expansion;
2184 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2185 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2186 convert_local_reference_stmt,
2187 convert_local_reference_op, info);
2188 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2189 info, gimple_omp_body_ptr (stmt));
2190 info->suppress_expansion = save_suppress;
2191 break;
2193 case GIMPLE_OMP_SECTIONS:
2194 save_suppress = info->suppress_expansion;
2195 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2196 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2197 info, gimple_omp_body_ptr (stmt));
2198 info->suppress_expansion = save_suppress;
2199 break;
2201 case GIMPLE_OMP_SINGLE:
2202 save_suppress = info->suppress_expansion;
2203 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2204 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2205 info, gimple_omp_body_ptr (stmt));
2206 info->suppress_expansion = save_suppress;
2207 break;
2209 case GIMPLE_OMP_TARGET:
2210 if (!is_gimple_omp_offloaded (stmt))
2212 save_suppress = info->suppress_expansion;
2213 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2214 info->suppress_expansion = save_suppress;
2215 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2216 info, gimple_omp_body_ptr (stmt));
2217 break;
2219 save_suppress = info->suppress_expansion;
2220 frame_decl_added = false;
2221 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2223 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2224 (void) get_frame_type (info);
2225 OMP_CLAUSE_DECL (c) = info->frame_decl;
2226 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2227 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2228 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2229 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2230 info->static_chain_added |= 4;
2231 frame_decl_added = true;
2234 save_local_var_chain = info->new_local_var_chain;
2235 save_static_chain_added = info->static_chain_added;
2236 info->new_local_var_chain = NULL;
2237 info->static_chain_added = 0;
2239 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2240 gimple_omp_body_ptr (stmt));
2242 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2244 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2245 (void) get_frame_type (info);
2246 OMP_CLAUSE_DECL (c) = info->frame_decl;
2247 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2248 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2249 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2250 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2251 info->static_chain_added |= 4;
2254 if (info->new_local_var_chain)
2255 declare_vars (info->new_local_var_chain,
2256 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2257 info->new_local_var_chain = save_local_var_chain;
2258 info->suppress_expansion = save_suppress;
2259 info->static_chain_added |= save_static_chain_added;
2260 break;
2262 case GIMPLE_OMP_TEAMS:
2263 save_suppress = info->suppress_expansion;
2264 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2265 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2266 info, gimple_omp_body_ptr (stmt));
2267 info->suppress_expansion = save_suppress;
2268 break;
2270 case GIMPLE_OMP_SECTION:
2271 case GIMPLE_OMP_MASTER:
2272 case GIMPLE_OMP_TASKGROUP:
2273 case GIMPLE_OMP_ORDERED:
2274 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2275 info, gimple_omp_body_ptr (stmt));
2276 break;
2278 case GIMPLE_COND:
2279 wi->val_only = true;
2280 wi->is_lhs = false;
2281 *handled_ops_p = false;
2282 return NULL_TREE;
2284 case GIMPLE_ASSIGN:
2285 if (gimple_clobber_p (stmt))
2287 tree lhs = gimple_assign_lhs (stmt);
2288 if (!use_pointer_in_frame (lhs)
2289 && lookup_field_for_decl (info, lhs, NO_INSERT))
2291 gsi_replace (gsi, gimple_build_nop (), true);
2292 break;
2295 *handled_ops_p = false;
2296 return NULL_TREE;
2298 case GIMPLE_BIND:
2299 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2300 var;
2301 var = DECL_CHAIN (var))
2302 if (TREE_CODE (var) == NAMELIST_DECL)
2304 /* Adjust decls mentioned in NAMELIST_DECL. */
2305 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2306 tree decl;
2307 unsigned int i;
2309 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2311 if (VAR_P (decl)
2312 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2313 continue;
2314 if (decl_function_context (decl) == info->context
2315 && !use_pointer_in_frame (decl))
2317 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2318 if (field)
2320 CONSTRUCTOR_ELT (decls, i)->value
2321 = get_local_debug_decl (info, decl, field);
2327 *handled_ops_p = false;
2328 return NULL_TREE;
2330 default:
2331 /* For every other statement that we are not interested in
2332 handling here, let the walker traverse the operands. */
2333 *handled_ops_p = false;
2334 return NULL_TREE;
2337 /* Indicate that we have handled all the operands ourselves. */
2338 *handled_ops_p = true;
2339 return NULL_TREE;
2343 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2344 that reference labels from outer functions. The rewrite will be a
2345 call to __builtin_nonlocal_goto. */
2347 static tree
2348 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2349 struct walk_stmt_info *wi)
2351 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2352 tree label, new_label, target_context, x, field;
2353 gcall *call;
2354 gimple *stmt = gsi_stmt (*gsi);
2356 if (gimple_code (stmt) != GIMPLE_GOTO)
2358 *handled_ops_p = false;
2359 return NULL_TREE;
2362 label = gimple_goto_dest (stmt);
2363 if (TREE_CODE (label) != LABEL_DECL)
2365 *handled_ops_p = false;
2366 return NULL_TREE;
2369 target_context = decl_function_context (label);
2370 if (target_context == info->context)
2372 *handled_ops_p = false;
2373 return NULL_TREE;
2376 for (i = info->outer; target_context != i->context; i = i->outer)
2377 continue;
2379 /* The original user label may also be use for a normal goto, therefore
2380 we must create a new label that will actually receive the abnormal
2381 control transfer. This new label will be marked LABEL_NONLOCAL; this
2382 mark will trigger proper behavior in the cfg, as well as cause the
2383 (hairy target-specific) non-local goto receiver code to be generated
2384 when we expand rtl. Enter this association into var_map so that we
2385 can insert the new label into the IL during a second pass. */
2386 tree *slot = &i->var_map->get_or_insert (label);
2387 if (*slot == NULL)
2389 new_label = create_artificial_label (UNKNOWN_LOCATION);
2390 DECL_NONLOCAL (new_label) = 1;
2391 *slot = new_label;
2393 else
2394 new_label = *slot;
2396 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2397 field = get_nl_goto_field (i);
2398 x = get_frame_field (info, target_context, field, gsi);
2399 x = build_addr (x);
2400 x = gsi_gimplify_val (info, x, gsi);
2401 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2402 2, build_addr (new_label), x);
2403 gsi_replace (gsi, call, false);
2405 /* We have handled all of STMT's operands, no need to keep going. */
2406 *handled_ops_p = true;
2407 return NULL_TREE;
2411 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2412 are referenced via nonlocal goto from a nested function. The rewrite
2413 will involve installing a newly generated DECL_NONLOCAL label, and
2414 (potentially) a branch around the rtl gunk that is assumed to be
2415 attached to such a label. */
2417 static tree
2418 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2419 struct walk_stmt_info *wi)
2421 struct nesting_info *const info = (struct nesting_info *) wi->info;
2422 tree label, new_label;
2423 gimple_stmt_iterator tmp_gsi;
2424 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2426 if (!stmt)
2428 *handled_ops_p = false;
2429 return NULL_TREE;
2432 label = gimple_label_label (stmt);
2434 tree *slot = info->var_map->get (label);
2435 if (!slot)
2437 *handled_ops_p = false;
2438 return NULL_TREE;
2441 /* If there's any possibility that the previous statement falls through,
2442 then we must branch around the new non-local label. */
2443 tmp_gsi = wi->gsi;
2444 gsi_prev (&tmp_gsi);
2445 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2447 gimple *stmt = gimple_build_goto (label);
2448 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2451 new_label = (tree) *slot;
2452 stmt = gimple_build_label (new_label);
2453 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2455 *handled_ops_p = true;
2456 return NULL_TREE;
2460 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2461 of nested functions that require the use of trampolines. The rewrite
2462 will involve a reference a trampoline generated for the occasion. */
2464 static tree
2465 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2467 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2468 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2469 tree t = *tp, decl, target_context, x, builtin;
2470 bool descr;
2471 gcall *call;
2473 *walk_subtrees = 0;
2474 switch (TREE_CODE (t))
2476 case ADDR_EXPR:
2477 /* Build
2478 T.1 = &CHAIN->tramp;
2479 T.2 = __builtin_adjust_trampoline (T.1);
2480 T.3 = (func_type)T.2;
2483 decl = TREE_OPERAND (t, 0);
2484 if (TREE_CODE (decl) != FUNCTION_DECL)
2485 break;
2487 /* Only need to process nested functions. */
2488 target_context = decl_function_context (decl);
2489 if (!target_context)
2490 break;
2492 /* If the nested function doesn't use a static chain, then
2493 it doesn't need a trampoline. */
2494 if (!DECL_STATIC_CHAIN (decl))
2495 break;
2497 /* If we don't want a trampoline, then don't build one. */
2498 if (TREE_NO_TRAMPOLINE (t))
2499 break;
2501 /* Lookup the immediate parent of the callee, as that's where
2502 we need to insert the trampoline. */
2503 for (i = info; i->context != target_context; i = i->outer)
2504 continue;
2506 /* Decide whether to generate a descriptor or a trampoline. */
2507 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2509 if (descr)
2510 x = lookup_descr_for_decl (i, decl, INSERT);
2511 else
2512 x = lookup_tramp_for_decl (i, decl, INSERT);
2514 /* Compute the address of the field holding the trampoline. */
2515 x = get_frame_field (info, target_context, x, &wi->gsi);
2516 x = build_addr (x);
2517 x = gsi_gimplify_val (info, x, &wi->gsi);
2519 /* Do machine-specific ugliness. Normally this will involve
2520 computing extra alignment, but it can really be anything. */
2521 if (descr)
2522 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2523 else
2524 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2525 call = gimple_build_call (builtin, 1, x);
2526 x = init_tmp_var_with_call (info, &wi->gsi, call);
2528 /* Cast back to the proper function type. */
2529 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2530 x = init_tmp_var (info, x, &wi->gsi);
2532 *tp = x;
2533 break;
2535 default:
2536 if (!IS_TYPE_OR_DECL_P (t))
2537 *walk_subtrees = 1;
2538 break;
2541 return NULL_TREE;
2545 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2546 to addresses of nested functions that require the use of
2547 trampolines. The rewrite will involve a reference a trampoline
2548 generated for the occasion. */
2550 static tree
2551 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2552 struct walk_stmt_info *wi)
2554 struct nesting_info *info = (struct nesting_info *) wi->info;
2555 gimple *stmt = gsi_stmt (*gsi);
2557 switch (gimple_code (stmt))
2559 case GIMPLE_CALL:
2561 /* Only walk call arguments, lest we generate trampolines for
2562 direct calls. */
2563 unsigned long i, nargs = gimple_call_num_args (stmt);
2564 for (i = 0; i < nargs; i++)
2565 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2566 wi, NULL);
2567 break;
2570 case GIMPLE_OMP_TARGET:
2571 if (!is_gimple_omp_offloaded (stmt))
2573 *handled_ops_p = false;
2574 return NULL_TREE;
2576 /* FALLTHRU */
2577 case GIMPLE_OMP_PARALLEL:
2578 case GIMPLE_OMP_TASK:
2580 tree save_local_var_chain = info->new_local_var_chain;
2581 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2582 info->new_local_var_chain = NULL;
2583 char save_static_chain_added = info->static_chain_added;
2584 info->static_chain_added = 0;
2585 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2586 info, gimple_omp_body_ptr (stmt));
2587 if (info->new_local_var_chain)
2588 declare_vars (info->new_local_var_chain,
2589 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2590 false);
2591 for (int i = 0; i < 2; i++)
2593 tree c, decl;
2594 if ((info->static_chain_added & (1 << i)) == 0)
2595 continue;
2596 decl = i ? get_chain_decl (info) : info->frame_decl;
2597 /* Don't add CHAIN.* or FRAME.* twice. */
2598 for (c = gimple_omp_taskreg_clauses (stmt);
2600 c = OMP_CLAUSE_CHAIN (c))
2601 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2602 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2603 && OMP_CLAUSE_DECL (c) == decl)
2604 break;
2605 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2607 c = build_omp_clause (gimple_location (stmt),
2608 i ? OMP_CLAUSE_FIRSTPRIVATE
2609 : OMP_CLAUSE_SHARED);
2610 OMP_CLAUSE_DECL (c) = decl;
2611 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2612 gimple_omp_taskreg_set_clauses (stmt, c);
2614 else if (c == NULL)
2616 c = build_omp_clause (gimple_location (stmt),
2617 OMP_CLAUSE_MAP);
2618 OMP_CLAUSE_DECL (c) = decl;
2619 OMP_CLAUSE_SET_MAP_KIND (c,
2620 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2621 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2622 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2623 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2627 info->new_local_var_chain = save_local_var_chain;
2628 info->static_chain_added |= save_static_chain_added;
2630 break;
2632 default:
2633 *handled_ops_p = false;
2634 return NULL_TREE;
2637 *handled_ops_p = true;
2638 return NULL_TREE;
2643 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2644 that reference nested functions to make sure that the static chain
2645 is set up properly for the call. */
2647 static tree
2648 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2649 struct walk_stmt_info *wi)
2651 struct nesting_info *const info = (struct nesting_info *) wi->info;
2652 tree decl, target_context;
2653 char save_static_chain_added;
2654 int i;
2655 gimple *stmt = gsi_stmt (*gsi);
2657 switch (gimple_code (stmt))
2659 case GIMPLE_CALL:
2660 if (gimple_call_chain (stmt))
2661 break;
2662 decl = gimple_call_fndecl (stmt);
2663 if (!decl)
2664 break;
2665 target_context = decl_function_context (decl);
2666 if (target_context && DECL_STATIC_CHAIN (decl))
2668 struct nesting_info *i = info;
2669 while (i && i->context != target_context)
2670 i = i->outer;
2671 /* If none of the outer contexts is the target context, this means
2672 that the function is called in a wrong context. */
2673 if (!i)
2674 internal_error ("%s from %s called in %s",
2675 IDENTIFIER_POINTER (DECL_NAME (decl)),
2676 IDENTIFIER_POINTER (DECL_NAME (target_context)),
2677 IDENTIFIER_POINTER (DECL_NAME (info->context)));
2679 gimple_call_set_chain (as_a <gcall *> (stmt),
2680 get_static_chain (info, target_context,
2681 &wi->gsi));
2682 info->static_chain_added |= (1 << (info->context != target_context));
2684 break;
2686 case GIMPLE_OMP_PARALLEL:
2687 case GIMPLE_OMP_TASK:
2688 save_static_chain_added = info->static_chain_added;
2689 info->static_chain_added = 0;
2690 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2691 for (i = 0; i < 2; i++)
2693 tree c, decl;
2694 if ((info->static_chain_added & (1 << i)) == 0)
2695 continue;
2696 decl = i ? get_chain_decl (info) : info->frame_decl;
2697 /* Don't add CHAIN.* or FRAME.* twice. */
2698 for (c = gimple_omp_taskreg_clauses (stmt);
2700 c = OMP_CLAUSE_CHAIN (c))
2701 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2702 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2703 && OMP_CLAUSE_DECL (c) == decl)
2704 break;
2705 if (c == NULL)
2707 c = build_omp_clause (gimple_location (stmt),
2708 i ? OMP_CLAUSE_FIRSTPRIVATE
2709 : OMP_CLAUSE_SHARED);
2710 OMP_CLAUSE_DECL (c) = decl;
2711 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2712 gimple_omp_taskreg_set_clauses (stmt, c);
2715 info->static_chain_added |= save_static_chain_added;
2716 break;
2718 case GIMPLE_OMP_TARGET:
2719 if (!is_gimple_omp_offloaded (stmt))
2721 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2722 break;
2724 save_static_chain_added = info->static_chain_added;
2725 info->static_chain_added = 0;
2726 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2727 for (i = 0; i < 2; i++)
2729 tree c, decl;
2730 if ((info->static_chain_added & (1 << i)) == 0)
2731 continue;
2732 decl = i ? get_chain_decl (info) : info->frame_decl;
2733 /* Don't add CHAIN.* or FRAME.* twice. */
2734 for (c = gimple_omp_target_clauses (stmt);
2736 c = OMP_CLAUSE_CHAIN (c))
2737 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2738 && OMP_CLAUSE_DECL (c) == decl)
2739 break;
2740 if (c == NULL)
2742 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2743 OMP_CLAUSE_DECL (c) = decl;
2744 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2745 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2746 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2747 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2751 info->static_chain_added |= save_static_chain_added;
2752 break;
2754 case GIMPLE_OMP_FOR:
2755 walk_body (convert_gimple_call, NULL, info,
2756 gimple_omp_for_pre_body_ptr (stmt));
2757 /* FALLTHRU */
2758 case GIMPLE_OMP_SECTIONS:
2759 case GIMPLE_OMP_SECTION:
2760 case GIMPLE_OMP_SINGLE:
2761 case GIMPLE_OMP_TEAMS:
2762 case GIMPLE_OMP_MASTER:
2763 case GIMPLE_OMP_TASKGROUP:
2764 case GIMPLE_OMP_ORDERED:
2765 case GIMPLE_OMP_CRITICAL:
2766 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2767 break;
2769 default:
2770 /* Keep looking for other operands. */
2771 *handled_ops_p = false;
2772 return NULL_TREE;
2775 *handled_ops_p = true;
2776 return NULL_TREE;
2779 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2780 call expressions. At the same time, determine if a nested function
2781 actually uses its static chain; if not, remember that. */
2783 static void
2784 convert_all_function_calls (struct nesting_info *root)
2786 unsigned int chain_count = 0, old_chain_count, iter_count;
2787 struct nesting_info *n;
2789 /* First, optimistically clear static_chain for all decls that haven't
2790 used the static chain already for variable access. But always create
2791 it if not optimizing. This makes it possible to reconstruct the static
2792 nesting tree at run time and thus to resolve up-level references from
2793 within the debugger. */
2794 FOR_EACH_NEST_INFO (n, root)
2796 if (n->thunk_p)
2797 continue;
2798 tree decl = n->context;
2799 if (!optimize)
2801 if (n->inner)
2802 (void) get_frame_type (n);
2803 if (n->outer)
2804 (void) get_chain_decl (n);
2806 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2808 DECL_STATIC_CHAIN (decl) = 0;
2809 if (dump_file && (dump_flags & TDF_DETAILS))
2810 fprintf (dump_file, "Guessing no static-chain for %s\n",
2811 lang_hooks.decl_printable_name (decl, 2));
2813 else
2814 DECL_STATIC_CHAIN (decl) = 1;
2815 chain_count += DECL_STATIC_CHAIN (decl);
2818 FOR_EACH_NEST_INFO (n, root)
2819 if (n->thunk_p)
2821 tree decl = n->context;
2822 tree alias = cgraph_node::get (decl)->thunk.alias;
2823 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2826 /* Walk the functions and perform transformations. Note that these
2827 transformations can induce new uses of the static chain, which in turn
2828 require re-examining all users of the decl. */
2829 /* ??? It would make sense to try to use the call graph to speed this up,
2830 but the call graph hasn't really been built yet. Even if it did, we
2831 would still need to iterate in this loop since address-of references
2832 wouldn't show up in the callgraph anyway. */
2833 iter_count = 0;
2836 old_chain_count = chain_count;
2837 chain_count = 0;
2838 iter_count++;
2840 if (dump_file && (dump_flags & TDF_DETAILS))
2841 fputc ('\n', dump_file);
2843 FOR_EACH_NEST_INFO (n, root)
2845 if (n->thunk_p)
2846 continue;
2847 tree decl = n->context;
2848 walk_function (convert_tramp_reference_stmt,
2849 convert_tramp_reference_op, n);
2850 walk_function (convert_gimple_call, NULL, n);
2851 chain_count += DECL_STATIC_CHAIN (decl);
2854 FOR_EACH_NEST_INFO (n, root)
2855 if (n->thunk_p)
2857 tree decl = n->context;
2858 tree alias = cgraph_node::get (decl)->thunk.alias;
2859 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2862 while (chain_count != old_chain_count);
2864 if (dump_file && (dump_flags & TDF_DETAILS))
2865 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2866 iter_count);
2869 struct nesting_copy_body_data
2871 copy_body_data cb;
2872 struct nesting_info *root;
2875 /* A helper subroutine for debug_var_chain type remapping. */
2877 static tree
2878 nesting_copy_decl (tree decl, copy_body_data *id)
2880 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2881 tree *slot = nid->root->var_map->get (decl);
2883 if (slot)
2884 return (tree) *slot;
2886 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2888 tree new_decl = copy_decl_no_change (decl, id);
2889 DECL_ORIGINAL_TYPE (new_decl)
2890 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2891 return new_decl;
2894 if (VAR_P (decl)
2895 || TREE_CODE (decl) == PARM_DECL
2896 || TREE_CODE (decl) == RESULT_DECL)
2897 return decl;
2899 return copy_decl_no_change (decl, id);
2902 /* A helper function for remap_vla_decls. See if *TP contains
2903 some remapped variables. */
2905 static tree
2906 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2908 struct nesting_info *root = (struct nesting_info *) data;
2909 tree t = *tp;
2911 if (DECL_P (t))
2913 *walk_subtrees = 0;
2914 tree *slot = root->var_map->get (t);
2916 if (slot)
2917 return *slot;
2919 return NULL;
2922 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2923 involved. */
2925 static void
2926 remap_vla_decls (tree block, struct nesting_info *root)
2928 tree var, subblock, val, type;
2929 struct nesting_copy_body_data id;
2931 for (subblock = BLOCK_SUBBLOCKS (block);
2932 subblock;
2933 subblock = BLOCK_CHAIN (subblock))
2934 remap_vla_decls (subblock, root);
2936 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2937 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2939 val = DECL_VALUE_EXPR (var);
2940 type = TREE_TYPE (var);
2942 if (!(TREE_CODE (val) == INDIRECT_REF
2943 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2944 && variably_modified_type_p (type, NULL)))
2945 continue;
2947 if (root->var_map->get (TREE_OPERAND (val, 0))
2948 || walk_tree (&type, contains_remapped_vars, root, NULL))
2949 break;
2952 if (var == NULL_TREE)
2953 return;
2955 memset (&id, 0, sizeof (id));
2956 id.cb.copy_decl = nesting_copy_decl;
2957 id.cb.decl_map = new hash_map<tree, tree>;
2958 id.root = root;
2960 for (; var; var = DECL_CHAIN (var))
2961 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2963 struct nesting_info *i;
2964 tree newt, context;
2966 val = DECL_VALUE_EXPR (var);
2967 type = TREE_TYPE (var);
2969 if (!(TREE_CODE (val) == INDIRECT_REF
2970 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2971 && variably_modified_type_p (type, NULL)))
2972 continue;
2974 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2975 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2976 continue;
2978 context = decl_function_context (var);
2979 for (i = root; i; i = i->outer)
2980 if (i->context == context)
2981 break;
2983 if (i == NULL)
2984 continue;
2986 /* Fully expand value expressions. This avoids having debug variables
2987 only referenced from them and that can be swept during GC. */
2988 if (slot)
2990 tree t = (tree) *slot;
2991 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2992 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2995 id.cb.src_fn = i->context;
2996 id.cb.dst_fn = i->context;
2997 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2999 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3000 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3002 newt = TREE_TYPE (newt);
3003 type = TREE_TYPE (type);
3005 if (TYPE_NAME (newt)
3006 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3007 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3008 && newt != type
3009 && TYPE_NAME (newt) == TYPE_NAME (type))
3010 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3012 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3013 if (val != DECL_VALUE_EXPR (var))
3014 SET_DECL_VALUE_EXPR (var, val);
3017 delete id.cb.decl_map;
3020 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3021 involved. */
3023 static void
3024 fixup_vla_decls (tree block)
3026 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3027 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3029 tree val = DECL_VALUE_EXPR (var);
3031 if (!(TREE_CODE (val) == INDIRECT_REF
3032 && VAR_P (TREE_OPERAND (val, 0))
3033 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3034 continue;
3036 /* Fully expand value expressions. This avoids having debug variables
3037 only referenced from them and that can be swept during GC. */
3038 val = build1 (INDIRECT_REF, TREE_TYPE (val),
3039 DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3040 SET_DECL_VALUE_EXPR (var, val);
3043 for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3044 fixup_vla_decls (sub);
3047 /* Fold the MEM_REF *E. */
3048 bool
3049 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3051 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
3052 *ref_p = fold (*ref_p);
3053 return true;
3056 /* Given DECL, a nested function, build an initialization call for FIELD,
3057 the trampoline or descriptor for DECL, using FUNC as the function. */
3059 static gcall *
3060 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3061 tree func)
3063 tree arg1, arg2, arg3, x;
3065 gcc_assert (DECL_STATIC_CHAIN (decl));
3066 arg3 = build_addr (info->frame_decl);
3068 arg2 = build_addr (decl);
3070 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3071 info->frame_decl, field, NULL_TREE);
3072 arg1 = build_addr (x);
3074 return gimple_build_call (func, 3, arg1, arg2, arg3);
3077 /* Do "everything else" to clean up or complete state collected by the various
3078 walking passes -- create a field to hold the frame base address, lay out the
3079 types and decls, generate code to initialize the frame decl, store critical
3080 expressions in the struct function for rtl to find. */
3082 static void
3083 finalize_nesting_tree_1 (struct nesting_info *root)
3085 gimple_seq stmt_list = NULL;
3086 gimple *stmt;
3087 tree context = root->context;
3088 struct function *sf;
3090 if (root->thunk_p)
3091 return;
3093 /* If we created a non-local frame type or decl, we need to lay them
3094 out at this time. */
3095 if (root->frame_type)
3097 /* Debugging information needs to compute the frame base address of the
3098 parent frame out of the static chain from the nested frame.
3100 The static chain is the address of the FRAME record, so one could
3101 imagine it would be possible to compute the frame base address just
3102 adding a constant offset to this address. Unfortunately, this is not
3103 possible: if the FRAME object has alignment constraints that are
3104 stronger than the stack, then the offset between the frame base and
3105 the FRAME object will be dynamic.
3107 What we do instead is to append a field to the FRAME object that holds
3108 the frame base address: then debug info just has to fetch this
3109 field. */
3111 /* Debugging information will refer to the CFA as the frame base
3112 address: we will do the same here. */
3113 const tree frame_addr_fndecl
3114 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3116 /* Create a field in the FRAME record to hold the frame base address for
3117 this stack frame. Since it will be used only by the debugger, put it
3118 at the end of the record in order not to shift all other offsets. */
3119 tree fb_decl = make_node (FIELD_DECL);
3121 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3122 TREE_TYPE (fb_decl) = ptr_type_node;
3123 TREE_ADDRESSABLE (fb_decl) = 1;
3124 DECL_CONTEXT (fb_decl) = root->frame_type;
3125 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3126 fb_decl);
3128 /* In some cases the frame type will trigger the -Wpadded warning.
3129 This is not helpful; suppress it. */
3130 int save_warn_padded = warn_padded;
3131 warn_padded = 0;
3132 layout_type (root->frame_type);
3133 warn_padded = save_warn_padded;
3134 layout_decl (root->frame_decl, 0);
3136 /* Initialize the frame base address field. If the builtin we need is
3137 not available, set it to NULL so that debugging information does not
3138 reference junk. */
3139 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3140 root->frame_decl, fb_decl, NULL_TREE);
3141 tree fb_tmp;
3143 if (frame_addr_fndecl != NULL_TREE)
3145 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3146 integer_zero_node);
3147 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3149 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3151 else
3152 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3153 gimple_seq_add_stmt (&stmt_list,
3154 gimple_build_assign (fb_ref, fb_tmp));
3156 declare_vars (root->frame_decl,
3157 gimple_seq_first_stmt (gimple_body (context)), true);
3160 /* If any parameters were referenced non-locally, then we need to insert
3161 a copy or a pointer. */
3162 if (root->any_parm_remapped)
3164 tree p;
3165 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3167 tree field, x, y;
3169 field = lookup_field_for_decl (root, p, NO_INSERT);
3170 if (!field)
3171 continue;
3173 if (use_pointer_in_frame (p))
3174 x = build_addr (p);
3175 else
3176 x = p;
3178 /* If the assignment is from a non-register the stmt is
3179 not valid gimple. Make it so by using a temporary instead. */
3180 if (!is_gimple_reg (x)
3181 && is_gimple_reg_type (TREE_TYPE (x)))
3183 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3184 x = init_tmp_var (root, x, &gsi);
3187 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3188 root->frame_decl, field, NULL_TREE);
3189 stmt = gimple_build_assign (y, x);
3190 gimple_seq_add_stmt (&stmt_list, stmt);
3194 /* If a chain_field was created, then it needs to be initialized
3195 from chain_decl. */
3196 if (root->chain_field)
3198 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3199 root->frame_decl, root->chain_field, NULL_TREE);
3200 stmt = gimple_build_assign (x, get_chain_decl (root));
3201 gimple_seq_add_stmt (&stmt_list, stmt);
3204 /* If trampolines were created, then we need to initialize them. */
3205 if (root->any_tramp_created)
3207 struct nesting_info *i;
3208 for (i = root->inner; i ; i = i->next)
3210 tree field, x;
3212 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3213 if (!field)
3214 continue;
3216 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3217 stmt = build_init_call_stmt (root, i->context, field, x);
3218 gimple_seq_add_stmt (&stmt_list, stmt);
3222 /* If descriptors were created, then we need to initialize them. */
3223 if (root->any_descr_created)
3225 struct nesting_info *i;
3226 for (i = root->inner; i ; i = i->next)
3228 tree field, x;
3230 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3231 if (!field)
3232 continue;
3234 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3235 stmt = build_init_call_stmt (root, i->context, field, x);
3236 gimple_seq_add_stmt (&stmt_list, stmt);
3240 /* If we created initialization statements, insert them. */
3241 if (stmt_list)
3243 gbind *bind;
3244 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3245 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3246 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3247 gimple_bind_set_body (bind, stmt_list);
3250 /* If a chain_decl was created, then it needs to be registered with
3251 struct function so that it gets initialized from the static chain
3252 register at the beginning of the function. */
3253 sf = DECL_STRUCT_FUNCTION (root->context);
3254 sf->static_chain_decl = root->chain_decl;
3256 /* Similarly for the non-local goto save area. */
3257 if (root->nl_goto_field)
3259 sf->nonlocal_goto_save_area
3260 = get_frame_field (root, context, root->nl_goto_field, NULL);
3261 sf->has_nonlocal_label = 1;
3264 /* Make sure all new local variables get inserted into the
3265 proper BIND_EXPR. */
3266 if (root->new_local_var_chain)
3267 declare_vars (root->new_local_var_chain,
3268 gimple_seq_first_stmt (gimple_body (root->context)),
3269 false);
3271 if (root->debug_var_chain)
3273 tree debug_var;
3274 gbind *scope;
3276 remap_vla_decls (DECL_INITIAL (root->context), root);
3278 for (debug_var = root->debug_var_chain; debug_var;
3279 debug_var = DECL_CHAIN (debug_var))
3280 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3281 break;
3283 /* If there are any debug decls with variable length types,
3284 remap those types using other debug_var_chain variables. */
3285 if (debug_var)
3287 struct nesting_copy_body_data id;
3289 memset (&id, 0, sizeof (id));
3290 id.cb.copy_decl = nesting_copy_decl;
3291 id.cb.decl_map = new hash_map<tree, tree>;
3292 id.root = root;
3294 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3295 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3297 tree type = TREE_TYPE (debug_var);
3298 tree newt, t = type;
3299 struct nesting_info *i;
3301 for (i = root; i; i = i->outer)
3302 if (variably_modified_type_p (type, i->context))
3303 break;
3305 if (i == NULL)
3306 continue;
3308 id.cb.src_fn = i->context;
3309 id.cb.dst_fn = i->context;
3310 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3312 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3313 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3315 newt = TREE_TYPE (newt);
3316 t = TREE_TYPE (t);
3318 if (TYPE_NAME (newt)
3319 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3320 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3321 && newt != t
3322 && TYPE_NAME (newt) == TYPE_NAME (t))
3323 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3326 delete id.cb.decl_map;
3329 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3330 if (gimple_bind_block (scope))
3331 declare_vars (root->debug_var_chain, scope, true);
3332 else
3333 BLOCK_VARS (DECL_INITIAL (root->context))
3334 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3335 root->debug_var_chain);
3337 else
3338 fixup_vla_decls (DECL_INITIAL (root->context));
3340 /* Fold the rewritten MEM_REF trees. */
3341 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3343 /* Dump the translated tree function. */
3344 if (dump_file)
3346 fputs ("\n\n", dump_file);
3347 dump_function_to_file (root->context, dump_file, dump_flags);
3351 static void
3352 finalize_nesting_tree (struct nesting_info *root)
3354 struct nesting_info *n;
3355 FOR_EACH_NEST_INFO (n, root)
3356 finalize_nesting_tree_1 (n);
3359 /* Unnest the nodes and pass them to cgraph. */
3361 static void
3362 unnest_nesting_tree_1 (struct nesting_info *root)
3364 struct cgraph_node *node = cgraph_node::get (root->context);
3366 /* For nested functions update the cgraph to reflect unnesting.
3367 We also delay finalizing of these functions up to this point. */
3368 if (node->origin)
3370 node->unnest ();
3371 if (!root->thunk_p)
3372 cgraph_node::finalize_function (root->context, true);
3376 static void
3377 unnest_nesting_tree (struct nesting_info *root)
3379 struct nesting_info *n;
3380 FOR_EACH_NEST_INFO (n, root)
3381 unnest_nesting_tree_1 (n);
3384 /* Free the data structures allocated during this pass. */
3386 static void
3387 free_nesting_tree (struct nesting_info *root)
3389 struct nesting_info *node, *next;
3391 node = iter_nestinfo_start (root);
3394 next = iter_nestinfo_next (node);
3395 delete node->var_map;
3396 delete node->field_map;
3397 delete node->mem_refs;
3398 free (node);
3399 node = next;
3401 while (node);
3404 /* Gimplify a function and all its nested functions. */
3405 static void
3406 gimplify_all_functions (struct cgraph_node *root)
3408 struct cgraph_node *iter;
3409 if (!gimple_body (root->decl))
3410 gimplify_function_tree (root->decl);
3411 for (iter = root->nested; iter; iter = iter->next_nested)
3412 if (!iter->thunk.thunk_p)
3413 gimplify_all_functions (iter);
3416 /* Main entry point for this pass. Process FNDECL and all of its nested
3417 subroutines and turn them into something less tightly bound. */
3419 void
3420 lower_nested_functions (tree fndecl)
3422 struct cgraph_node *cgn;
3423 struct nesting_info *root;
3425 /* If there are no nested functions, there's nothing to do. */
3426 cgn = cgraph_node::get (fndecl);
3427 if (!cgn->nested)
3428 return;
3430 gimplify_all_functions (cgn);
3432 set_dump_file (dump_begin (TDI_nested, &dump_flags));
3433 if (dump_file)
3434 fprintf (dump_file, "\n;; Function %s\n\n",
3435 lang_hooks.decl_printable_name (fndecl, 2));
3437 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3438 root = create_nesting_tree (cgn);
3440 walk_all_functions (convert_nonlocal_reference_stmt,
3441 convert_nonlocal_reference_op,
3442 root);
3443 walk_all_functions (convert_local_reference_stmt,
3444 convert_local_reference_op,
3445 root);
3446 walk_all_functions (convert_nl_goto_reference, NULL, root);
3447 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3449 convert_all_function_calls (root);
3450 finalize_nesting_tree (root);
3451 unnest_nesting_tree (root);
3453 free_nesting_tree (root);
3454 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3456 if (dump_file)
3458 dump_end (TDI_nested, dump_file);
3459 set_dump_file (NULL);
3463 #include "gt-tree-nested.h"