match_asm_constraints: Use copy_rtx where needed (PR88001)
[official-gcc.git] / gcc / tree-nested.c
blob0ad469ada4982a4b88c3a770f9f29b743b78b52d
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 case OMP_CLAUSE_IN_REDUCTION:
1194 case OMP_CLAUSE_TASK_REDUCTION:
1195 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1196 need_stmts = true;
1197 goto do_decl_clause;
1199 case OMP_CLAUSE_LASTPRIVATE:
1200 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1201 need_stmts = true;
1202 goto do_decl_clause;
1204 case OMP_CLAUSE_LINEAR:
1205 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1206 need_stmts = true;
1207 wi->val_only = true;
1208 wi->is_lhs = false;
1209 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1210 &dummy, wi);
1211 goto do_decl_clause;
1213 case OMP_CLAUSE_PRIVATE:
1214 case OMP_CLAUSE_FIRSTPRIVATE:
1215 case OMP_CLAUSE_COPYPRIVATE:
1216 case OMP_CLAUSE_SHARED:
1217 case OMP_CLAUSE_TO_DECLARE:
1218 case OMP_CLAUSE_LINK:
1219 case OMP_CLAUSE_USE_DEVICE_PTR:
1220 case OMP_CLAUSE_IS_DEVICE_PTR:
1221 do_decl_clause:
1222 decl = OMP_CLAUSE_DECL (clause);
1223 if (VAR_P (decl)
1224 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1225 break;
1226 if (decl_function_context (decl) != info->context)
1228 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1229 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1230 bitmap_set_bit (new_suppress, DECL_UID (decl));
1231 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1232 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1233 need_chain = true;
1235 break;
1237 case OMP_CLAUSE_SCHEDULE:
1238 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1239 break;
1240 /* FALLTHRU */
1241 case OMP_CLAUSE_FINAL:
1242 case OMP_CLAUSE_IF:
1243 case OMP_CLAUSE_NUM_THREADS:
1244 case OMP_CLAUSE_DEPEND:
1245 case OMP_CLAUSE_DEVICE:
1246 case OMP_CLAUSE_NUM_TEAMS:
1247 case OMP_CLAUSE_THREAD_LIMIT:
1248 case OMP_CLAUSE_SAFELEN:
1249 case OMP_CLAUSE_SIMDLEN:
1250 case OMP_CLAUSE_PRIORITY:
1251 case OMP_CLAUSE_GRAINSIZE:
1252 case OMP_CLAUSE_NUM_TASKS:
1253 case OMP_CLAUSE_HINT:
1254 case OMP_CLAUSE_NUM_GANGS:
1255 case OMP_CLAUSE_NUM_WORKERS:
1256 case OMP_CLAUSE_VECTOR_LENGTH:
1257 case OMP_CLAUSE_GANG:
1258 case OMP_CLAUSE_WORKER:
1259 case OMP_CLAUSE_VECTOR:
1260 case OMP_CLAUSE_ASYNC:
1261 case OMP_CLAUSE_WAIT:
1262 /* Several OpenACC clauses have optional arguments. Check if they
1263 are present. */
1264 if (OMP_CLAUSE_OPERAND (clause, 0))
1266 wi->val_only = true;
1267 wi->is_lhs = false;
1268 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1269 &dummy, wi);
1272 /* The gang clause accepts two arguments. */
1273 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1274 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1276 wi->val_only = true;
1277 wi->is_lhs = false;
1278 convert_nonlocal_reference_op
1279 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1281 break;
1283 case OMP_CLAUSE_DIST_SCHEDULE:
1284 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1286 wi->val_only = true;
1287 wi->is_lhs = false;
1288 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1289 &dummy, wi);
1291 break;
1293 case OMP_CLAUSE_MAP:
1294 case OMP_CLAUSE_TO:
1295 case OMP_CLAUSE_FROM:
1296 if (OMP_CLAUSE_SIZE (clause))
1298 wi->val_only = true;
1299 wi->is_lhs = false;
1300 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1301 &dummy, wi);
1303 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1304 goto do_decl_clause;
1305 wi->val_only = true;
1306 wi->is_lhs = false;
1307 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1308 wi, NULL);
1309 break;
1311 case OMP_CLAUSE_ALIGNED:
1312 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1314 wi->val_only = true;
1315 wi->is_lhs = false;
1316 convert_nonlocal_reference_op
1317 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1319 /* FALLTHRU */
1320 case OMP_CLAUSE_NONTEMPORAL:
1321 /* Like do_decl_clause, but don't add any suppression. */
1322 decl = OMP_CLAUSE_DECL (clause);
1323 if (VAR_P (decl)
1324 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1325 break;
1326 if (decl_function_context (decl) != info->context)
1328 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1329 need_chain = true;
1331 break;
1333 case OMP_CLAUSE_NOWAIT:
1334 case OMP_CLAUSE_ORDERED:
1335 case OMP_CLAUSE_DEFAULT:
1336 case OMP_CLAUSE_COPYIN:
1337 case OMP_CLAUSE_COLLAPSE:
1338 case OMP_CLAUSE_TILE:
1339 case OMP_CLAUSE_UNTIED:
1340 case OMP_CLAUSE_MERGEABLE:
1341 case OMP_CLAUSE_PROC_BIND:
1342 case OMP_CLAUSE_NOGROUP:
1343 case OMP_CLAUSE_THREADS:
1344 case OMP_CLAUSE_SIMD:
1345 case OMP_CLAUSE_DEFAULTMAP:
1346 case OMP_CLAUSE_SEQ:
1347 case OMP_CLAUSE_INDEPENDENT:
1348 case OMP_CLAUSE_AUTO:
1349 case OMP_CLAUSE_IF_PRESENT:
1350 case OMP_CLAUSE_FINALIZE:
1351 break;
1353 /* The following clause belongs to the OpenACC cache directive, which
1354 is discarded during gimplification. */
1355 case OMP_CLAUSE__CACHE_:
1356 /* The following clauses are only allowed in the OpenMP declare simd
1357 directive, so not seen here. */
1358 case OMP_CLAUSE_UNIFORM:
1359 case OMP_CLAUSE_INBRANCH:
1360 case OMP_CLAUSE_NOTINBRANCH:
1361 /* The following clauses are only allowed on OpenMP cancel and
1362 cancellation point directives, which at this point have already
1363 been lowered into a function call. */
1364 case OMP_CLAUSE_FOR:
1365 case OMP_CLAUSE_PARALLEL:
1366 case OMP_CLAUSE_SECTIONS:
1367 case OMP_CLAUSE_TASKGROUP:
1368 /* The following clauses are only added during OMP lowering; nested
1369 function decomposition happens before that. */
1370 case OMP_CLAUSE__LOOPTEMP_:
1371 case OMP_CLAUSE__REDUCTEMP_:
1372 case OMP_CLAUSE__SIMDUID_:
1373 case OMP_CLAUSE__GRIDDIM_:
1374 case OMP_CLAUSE__SIMT_:
1375 /* Anything else. */
1376 default:
1377 gcc_unreachable ();
1381 info->suppress_expansion = new_suppress;
1383 if (need_stmts)
1384 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1385 switch (OMP_CLAUSE_CODE (clause))
1387 case OMP_CLAUSE_REDUCTION:
1388 case OMP_CLAUSE_IN_REDUCTION:
1389 case OMP_CLAUSE_TASK_REDUCTION:
1390 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1392 tree old_context
1393 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1394 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1395 = info->context;
1396 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1397 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1398 = info->context;
1399 walk_body (convert_nonlocal_reference_stmt,
1400 convert_nonlocal_reference_op, info,
1401 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1402 walk_body (convert_nonlocal_reference_stmt,
1403 convert_nonlocal_reference_op, info,
1404 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1405 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1406 = old_context;
1407 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1408 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1409 = old_context;
1411 break;
1413 case OMP_CLAUSE_LASTPRIVATE:
1414 walk_body (convert_nonlocal_reference_stmt,
1415 convert_nonlocal_reference_op, info,
1416 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1417 break;
1419 case OMP_CLAUSE_LINEAR:
1420 walk_body (convert_nonlocal_reference_stmt,
1421 convert_nonlocal_reference_op, info,
1422 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1423 break;
1425 default:
1426 break;
1429 return need_chain;
1432 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1434 static void
1435 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1437 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1438 type = TREE_TYPE (type);
1440 if (TYPE_NAME (type)
1441 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1442 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1443 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1445 while (POINTER_TYPE_P (type)
1446 || TREE_CODE (type) == VECTOR_TYPE
1447 || TREE_CODE (type) == FUNCTION_TYPE
1448 || TREE_CODE (type) == METHOD_TYPE)
1449 type = TREE_TYPE (type);
1451 if (TREE_CODE (type) == ARRAY_TYPE)
1453 tree domain, t;
1455 note_nonlocal_vla_type (info, TREE_TYPE (type));
1456 domain = TYPE_DOMAIN (type);
1457 if (domain)
1459 t = TYPE_MIN_VALUE (domain);
1460 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1461 && decl_function_context (t) != info->context)
1462 get_nonlocal_debug_decl (info, t);
1463 t = TYPE_MAX_VALUE (domain);
1464 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1465 && decl_function_context (t) != info->context)
1466 get_nonlocal_debug_decl (info, t);
1471 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1472 PARM_DECLs that belong to outer functions. This handles statements
1473 that are not handled via the standard recursion done in
1474 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1475 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1476 operands of STMT have been handled by this function. */
1478 static tree
1479 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1480 struct walk_stmt_info *wi)
1482 struct nesting_info *info = (struct nesting_info *) wi->info;
1483 tree save_local_var_chain;
1484 bitmap save_suppress;
1485 gimple *stmt = gsi_stmt (*gsi);
1487 switch (gimple_code (stmt))
1489 case GIMPLE_GOTO:
1490 /* Don't walk non-local gotos for now. */
1491 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1493 wi->val_only = true;
1494 wi->is_lhs = false;
1495 *handled_ops_p = false;
1496 return NULL_TREE;
1498 break;
1500 case GIMPLE_OMP_PARALLEL:
1501 case GIMPLE_OMP_TASK:
1502 save_suppress = info->suppress_expansion;
1503 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1504 wi))
1506 tree c, decl;
1507 decl = get_chain_decl (info);
1508 c = build_omp_clause (gimple_location (stmt),
1509 OMP_CLAUSE_FIRSTPRIVATE);
1510 OMP_CLAUSE_DECL (c) = decl;
1511 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1512 gimple_omp_taskreg_set_clauses (stmt, c);
1515 save_local_var_chain = info->new_local_var_chain;
1516 info->new_local_var_chain = NULL;
1518 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1519 info, gimple_omp_body_ptr (stmt));
1521 if (info->new_local_var_chain)
1522 declare_vars (info->new_local_var_chain,
1523 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1524 false);
1525 info->new_local_var_chain = save_local_var_chain;
1526 info->suppress_expansion = save_suppress;
1527 break;
1529 case GIMPLE_OMP_FOR:
1530 save_suppress = info->suppress_expansion;
1531 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1532 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1533 convert_nonlocal_reference_stmt,
1534 convert_nonlocal_reference_op, info);
1535 walk_body (convert_nonlocal_reference_stmt,
1536 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1537 info->suppress_expansion = save_suppress;
1538 break;
1540 case GIMPLE_OMP_SECTIONS:
1541 save_suppress = info->suppress_expansion;
1542 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1543 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1544 info, gimple_omp_body_ptr (stmt));
1545 info->suppress_expansion = save_suppress;
1546 break;
1548 case GIMPLE_OMP_SINGLE:
1549 save_suppress = info->suppress_expansion;
1550 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1551 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1552 info, gimple_omp_body_ptr (stmt));
1553 info->suppress_expansion = save_suppress;
1554 break;
1556 case GIMPLE_OMP_TASKGROUP:
1557 save_suppress = info->suppress_expansion;
1558 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
1559 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1560 info, gimple_omp_body_ptr (stmt));
1561 info->suppress_expansion = save_suppress;
1562 break;
1564 case GIMPLE_OMP_TARGET:
1565 if (!is_gimple_omp_offloaded (stmt))
1567 save_suppress = info->suppress_expansion;
1568 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1569 wi);
1570 info->suppress_expansion = save_suppress;
1571 walk_body (convert_nonlocal_reference_stmt,
1572 convert_nonlocal_reference_op, info,
1573 gimple_omp_body_ptr (stmt));
1574 break;
1576 save_suppress = info->suppress_expansion;
1577 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1578 wi))
1580 tree c, decl;
1581 decl = get_chain_decl (info);
1582 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1583 OMP_CLAUSE_DECL (c) = decl;
1584 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1585 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1586 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1587 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1590 save_local_var_chain = info->new_local_var_chain;
1591 info->new_local_var_chain = NULL;
1593 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1594 info, gimple_omp_body_ptr (stmt));
1596 if (info->new_local_var_chain)
1597 declare_vars (info->new_local_var_chain,
1598 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1599 false);
1600 info->new_local_var_chain = save_local_var_chain;
1601 info->suppress_expansion = save_suppress;
1602 break;
1604 case GIMPLE_OMP_TEAMS:
1605 save_suppress = info->suppress_expansion;
1606 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1607 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1608 info, gimple_omp_body_ptr (stmt));
1609 info->suppress_expansion = save_suppress;
1610 break;
1612 case GIMPLE_OMP_SECTION:
1613 case GIMPLE_OMP_MASTER:
1614 case GIMPLE_OMP_ORDERED:
1615 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1616 info, gimple_omp_body_ptr (stmt));
1617 break;
1619 case GIMPLE_BIND:
1621 gbind *bind_stmt = as_a <gbind *> (stmt);
1623 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1624 if (TREE_CODE (var) == NAMELIST_DECL)
1626 /* Adjust decls mentioned in NAMELIST_DECL. */
1627 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1628 tree decl;
1629 unsigned int i;
1631 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1633 if (VAR_P (decl)
1634 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1635 continue;
1636 if (decl_function_context (decl) != info->context)
1637 CONSTRUCTOR_ELT (decls, i)->value
1638 = get_nonlocal_debug_decl (info, decl);
1642 *handled_ops_p = false;
1643 return NULL_TREE;
1645 case GIMPLE_COND:
1646 wi->val_only = true;
1647 wi->is_lhs = false;
1648 *handled_ops_p = false;
1649 return NULL_TREE;
1651 case GIMPLE_ASSIGN:
1652 if (gimple_clobber_p (stmt))
1654 tree lhs = gimple_assign_lhs (stmt);
1655 if (DECL_P (lhs)
1656 && !(TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
1657 && decl_function_context (lhs) != info->context)
1659 gsi_replace (gsi, gimple_build_nop (), true);
1660 break;
1663 *handled_ops_p = false;
1664 return NULL_TREE;
1666 default:
1667 /* For every other statement that we are not interested in
1668 handling here, let the walker traverse the operands. */
1669 *handled_ops_p = false;
1670 return NULL_TREE;
1673 /* We have handled all of STMT operands, no need to traverse the operands. */
1674 *handled_ops_p = true;
1675 return NULL_TREE;
1679 /* A subroutine of convert_local_reference. Create a local variable
1680 in the parent function with DECL_VALUE_EXPR set to reference the
1681 field in FRAME. This is used both for debug info and in OMP
1682 lowering. */
1684 static tree
1685 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1687 tree x, new_decl;
1689 tree *slot = &info->var_map->get_or_insert (decl);
1690 if (*slot)
1691 return *slot;
1693 /* Make sure frame_decl gets created. */
1694 (void) get_frame_type (info);
1695 x = info->frame_decl;
1696 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1698 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1699 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1700 DECL_CONTEXT (new_decl) = info->context;
1701 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1702 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1703 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1704 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1705 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1706 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1707 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1708 if ((TREE_CODE (decl) == PARM_DECL
1709 || TREE_CODE (decl) == RESULT_DECL
1710 || VAR_P (decl))
1711 && DECL_BY_REFERENCE (decl))
1712 DECL_BY_REFERENCE (new_decl) = 1;
1714 SET_DECL_VALUE_EXPR (new_decl, x);
1715 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1716 *slot = new_decl;
1718 DECL_CHAIN (new_decl) = info->debug_var_chain;
1719 info->debug_var_chain = new_decl;
1721 /* Do not emit debug info twice. */
1722 DECL_IGNORED_P (decl) = 1;
1724 return new_decl;
1728 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1729 and PARM_DECLs that were referenced by inner nested functions.
1730 The rewrite will be a structure reference to the local frame variable. */
1732 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1734 static tree
1735 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1737 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1738 struct nesting_info *const info = (struct nesting_info *) wi->info;
1739 tree t = *tp, field, x;
1740 bool save_val_only;
1742 *walk_subtrees = 0;
1743 switch (TREE_CODE (t))
1745 case VAR_DECL:
1746 /* Non-automatic variables are never processed. */
1747 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1748 break;
1749 /* FALLTHRU */
1751 case PARM_DECL:
1752 if (t != info->frame_decl && decl_function_context (t) == info->context)
1754 /* If we copied a pointer to the frame, then the original decl
1755 is used unchanged in the parent function. */
1756 if (use_pointer_in_frame (t))
1757 break;
1759 /* No need to transform anything if no child references the
1760 variable. */
1761 field = lookup_field_for_decl (info, t, NO_INSERT);
1762 if (!field)
1763 break;
1764 wi->changed = true;
1766 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1767 x = get_local_debug_decl (info, t, field);
1768 else
1769 x = get_frame_field (info, info->context, field, &wi->gsi);
1771 if (wi->val_only)
1773 if (wi->is_lhs)
1774 x = save_tmp_var (info, x, &wi->gsi);
1775 else
1776 x = init_tmp_var (info, x, &wi->gsi);
1779 *tp = x;
1781 break;
1783 case ADDR_EXPR:
1784 save_val_only = wi->val_only;
1785 wi->val_only = false;
1786 wi->is_lhs = false;
1787 wi->changed = false;
1788 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1789 wi->val_only = save_val_only;
1791 /* If we converted anything ... */
1792 if (wi->changed)
1794 tree save_context;
1796 /* Then the frame decl is now addressable. */
1797 TREE_ADDRESSABLE (info->frame_decl) = 1;
1799 save_context = current_function_decl;
1800 current_function_decl = info->context;
1801 recompute_tree_invariant_for_addr_expr (t);
1802 current_function_decl = save_context;
1804 /* If we are in a context where we only accept values, then
1805 compute the address into a temporary. */
1806 if (save_val_only)
1807 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1808 t, &wi->gsi);
1810 break;
1812 case REALPART_EXPR:
1813 case IMAGPART_EXPR:
1814 case COMPONENT_REF:
1815 case ARRAY_REF:
1816 case ARRAY_RANGE_REF:
1817 case BIT_FIELD_REF:
1818 /* Go down this entire nest and just look at the final prefix and
1819 anything that describes the references. Otherwise, we lose track
1820 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1821 save_val_only = wi->val_only;
1822 wi->val_only = true;
1823 wi->is_lhs = false;
1824 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1826 if (TREE_CODE (t) == COMPONENT_REF)
1827 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1828 NULL);
1829 else if (TREE_CODE (t) == ARRAY_REF
1830 || TREE_CODE (t) == ARRAY_RANGE_REF)
1832 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1833 NULL);
1834 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1835 NULL);
1836 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1837 NULL);
1840 wi->val_only = false;
1841 walk_tree (tp, convert_local_reference_op, wi, NULL);
1842 wi->val_only = save_val_only;
1843 break;
1845 case MEM_REF:
1846 save_val_only = wi->val_only;
1847 wi->val_only = true;
1848 wi->is_lhs = false;
1849 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1850 wi, NULL);
1851 /* We need to re-fold the MEM_REF as component references as
1852 part of a ADDR_EXPR address are not allowed. But we cannot
1853 fold here, as the chain record type is not yet finalized. */
1854 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1855 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1856 info->mem_refs->add (tp);
1857 wi->val_only = save_val_only;
1858 break;
1860 case VIEW_CONVERT_EXPR:
1861 /* Just request to look at the subtrees, leaving val_only and lhs
1862 untouched. This might actually be for !val_only + lhs, in which
1863 case we don't want to force a replacement by a temporary. */
1864 *walk_subtrees = 1;
1865 break;
1867 default:
1868 if (!IS_TYPE_OR_DECL_P (t))
1870 *walk_subtrees = 1;
1871 wi->val_only = true;
1872 wi->is_lhs = false;
1874 break;
1877 return NULL_TREE;
1880 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1881 struct walk_stmt_info *);
1883 /* Helper for convert_local_reference. Convert all the references in
1884 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1886 static bool
1887 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1889 struct nesting_info *const info = (struct nesting_info *) wi->info;
1890 bool need_frame = false, need_stmts = false;
1891 tree clause, decl;
1892 int dummy;
1893 bitmap new_suppress;
1895 new_suppress = BITMAP_GGC_ALLOC ();
1896 bitmap_copy (new_suppress, info->suppress_expansion);
1898 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1900 switch (OMP_CLAUSE_CODE (clause))
1902 case OMP_CLAUSE_REDUCTION:
1903 case OMP_CLAUSE_IN_REDUCTION:
1904 case OMP_CLAUSE_TASK_REDUCTION:
1905 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1906 need_stmts = true;
1907 goto do_decl_clause;
1909 case OMP_CLAUSE_LASTPRIVATE:
1910 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1911 need_stmts = true;
1912 goto do_decl_clause;
1914 case OMP_CLAUSE_LINEAR:
1915 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1916 need_stmts = true;
1917 wi->val_only = true;
1918 wi->is_lhs = false;
1919 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1920 wi);
1921 goto do_decl_clause;
1923 case OMP_CLAUSE_PRIVATE:
1924 case OMP_CLAUSE_FIRSTPRIVATE:
1925 case OMP_CLAUSE_COPYPRIVATE:
1926 case OMP_CLAUSE_SHARED:
1927 case OMP_CLAUSE_TO_DECLARE:
1928 case OMP_CLAUSE_LINK:
1929 case OMP_CLAUSE_USE_DEVICE_PTR:
1930 case OMP_CLAUSE_IS_DEVICE_PTR:
1931 do_decl_clause:
1932 decl = OMP_CLAUSE_DECL (clause);
1933 if (VAR_P (decl)
1934 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1935 break;
1936 if (decl_function_context (decl) == info->context
1937 && !use_pointer_in_frame (decl))
1939 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1940 if (field)
1942 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1943 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1944 bitmap_set_bit (new_suppress, DECL_UID (decl));
1945 OMP_CLAUSE_DECL (clause)
1946 = get_local_debug_decl (info, decl, field);
1947 need_frame = true;
1950 break;
1952 case OMP_CLAUSE_SCHEDULE:
1953 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1954 break;
1955 /* FALLTHRU */
1956 case OMP_CLAUSE_FINAL:
1957 case OMP_CLAUSE_IF:
1958 case OMP_CLAUSE_NUM_THREADS:
1959 case OMP_CLAUSE_DEPEND:
1960 case OMP_CLAUSE_DEVICE:
1961 case OMP_CLAUSE_NUM_TEAMS:
1962 case OMP_CLAUSE_THREAD_LIMIT:
1963 case OMP_CLAUSE_SAFELEN:
1964 case OMP_CLAUSE_SIMDLEN:
1965 case OMP_CLAUSE_PRIORITY:
1966 case OMP_CLAUSE_GRAINSIZE:
1967 case OMP_CLAUSE_NUM_TASKS:
1968 case OMP_CLAUSE_HINT:
1969 case OMP_CLAUSE_NUM_GANGS:
1970 case OMP_CLAUSE_NUM_WORKERS:
1971 case OMP_CLAUSE_VECTOR_LENGTH:
1972 case OMP_CLAUSE_GANG:
1973 case OMP_CLAUSE_WORKER:
1974 case OMP_CLAUSE_VECTOR:
1975 case OMP_CLAUSE_ASYNC:
1976 case OMP_CLAUSE_WAIT:
1977 /* Several OpenACC clauses have optional arguments. Check if they
1978 are present. */
1979 if (OMP_CLAUSE_OPERAND (clause, 0))
1981 wi->val_only = true;
1982 wi->is_lhs = false;
1983 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1984 &dummy, wi);
1987 /* The gang clause accepts two arguments. */
1988 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1989 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1991 wi->val_only = true;
1992 wi->is_lhs = false;
1993 convert_nonlocal_reference_op
1994 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1996 break;
1998 case OMP_CLAUSE_DIST_SCHEDULE:
1999 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
2001 wi->val_only = true;
2002 wi->is_lhs = false;
2003 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2004 &dummy, wi);
2006 break;
2008 case OMP_CLAUSE_MAP:
2009 case OMP_CLAUSE_TO:
2010 case OMP_CLAUSE_FROM:
2011 if (OMP_CLAUSE_SIZE (clause))
2013 wi->val_only = true;
2014 wi->is_lhs = false;
2015 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
2016 &dummy, wi);
2018 if (DECL_P (OMP_CLAUSE_DECL (clause)))
2019 goto do_decl_clause;
2020 wi->val_only = true;
2021 wi->is_lhs = false;
2022 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
2023 wi, NULL);
2024 break;
2026 case OMP_CLAUSE_ALIGNED:
2027 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2029 wi->val_only = true;
2030 wi->is_lhs = false;
2031 convert_local_reference_op
2032 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2034 /* FALLTHRU */
2035 case OMP_CLAUSE_NONTEMPORAL:
2036 /* Like do_decl_clause, but don't add any suppression. */
2037 decl = OMP_CLAUSE_DECL (clause);
2038 if (VAR_P (decl)
2039 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2040 break;
2041 if (decl_function_context (decl) == info->context
2042 && !use_pointer_in_frame (decl))
2044 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2045 if (field)
2047 OMP_CLAUSE_DECL (clause)
2048 = get_local_debug_decl (info, decl, field);
2049 need_frame = true;
2052 break;
2054 case OMP_CLAUSE_NOWAIT:
2055 case OMP_CLAUSE_ORDERED:
2056 case OMP_CLAUSE_DEFAULT:
2057 case OMP_CLAUSE_COPYIN:
2058 case OMP_CLAUSE_COLLAPSE:
2059 case OMP_CLAUSE_TILE:
2060 case OMP_CLAUSE_UNTIED:
2061 case OMP_CLAUSE_MERGEABLE:
2062 case OMP_CLAUSE_PROC_BIND:
2063 case OMP_CLAUSE_NOGROUP:
2064 case OMP_CLAUSE_THREADS:
2065 case OMP_CLAUSE_SIMD:
2066 case OMP_CLAUSE_DEFAULTMAP:
2067 case OMP_CLAUSE_SEQ:
2068 case OMP_CLAUSE_INDEPENDENT:
2069 case OMP_CLAUSE_AUTO:
2070 case OMP_CLAUSE_IF_PRESENT:
2071 case OMP_CLAUSE_FINALIZE:
2072 break;
2074 /* The following clause belongs to the OpenACC cache directive, which
2075 is discarded during gimplification. */
2076 case OMP_CLAUSE__CACHE_:
2077 /* The following clauses are only allowed in the OpenMP declare simd
2078 directive, so not seen here. */
2079 case OMP_CLAUSE_UNIFORM:
2080 case OMP_CLAUSE_INBRANCH:
2081 case OMP_CLAUSE_NOTINBRANCH:
2082 /* The following clauses are only allowed on OpenMP cancel and
2083 cancellation point directives, which at this point have already
2084 been lowered into a function call. */
2085 case OMP_CLAUSE_FOR:
2086 case OMP_CLAUSE_PARALLEL:
2087 case OMP_CLAUSE_SECTIONS:
2088 case OMP_CLAUSE_TASKGROUP:
2089 /* The following clauses are only added during OMP lowering; nested
2090 function decomposition happens before that. */
2091 case OMP_CLAUSE__LOOPTEMP_:
2092 case OMP_CLAUSE__REDUCTEMP_:
2093 case OMP_CLAUSE__SIMDUID_:
2094 case OMP_CLAUSE__GRIDDIM_:
2095 case OMP_CLAUSE__SIMT_:
2096 /* Anything else. */
2097 default:
2098 gcc_unreachable ();
2102 info->suppress_expansion = new_suppress;
2104 if (need_stmts)
2105 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2106 switch (OMP_CLAUSE_CODE (clause))
2108 case OMP_CLAUSE_REDUCTION:
2109 case OMP_CLAUSE_IN_REDUCTION:
2110 case OMP_CLAUSE_TASK_REDUCTION:
2111 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2113 tree old_context
2114 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2115 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2116 = info->context;
2117 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2118 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2119 = info->context;
2120 walk_body (convert_local_reference_stmt,
2121 convert_local_reference_op, info,
2122 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2123 walk_body (convert_local_reference_stmt,
2124 convert_local_reference_op, info,
2125 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2126 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2127 = old_context;
2128 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2129 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2130 = old_context;
2132 break;
2134 case OMP_CLAUSE_LASTPRIVATE:
2135 walk_body (convert_local_reference_stmt,
2136 convert_local_reference_op, info,
2137 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2138 break;
2140 case OMP_CLAUSE_LINEAR:
2141 walk_body (convert_local_reference_stmt,
2142 convert_local_reference_op, info,
2143 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2144 break;
2146 default:
2147 break;
2150 return need_frame;
2154 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2155 and PARM_DECLs that were referenced by inner nested functions.
2156 The rewrite will be a structure reference to the local frame variable. */
2158 static tree
2159 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2160 struct walk_stmt_info *wi)
2162 struct nesting_info *info = (struct nesting_info *) wi->info;
2163 tree save_local_var_chain;
2164 bitmap save_suppress;
2165 char save_static_chain_added;
2166 bool frame_decl_added;
2167 gimple *stmt = gsi_stmt (*gsi);
2169 switch (gimple_code (stmt))
2171 case GIMPLE_OMP_PARALLEL:
2172 case GIMPLE_OMP_TASK:
2173 save_suppress = info->suppress_expansion;
2174 frame_decl_added = false;
2175 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2176 wi))
2178 tree c = build_omp_clause (gimple_location (stmt),
2179 OMP_CLAUSE_SHARED);
2180 (void) get_frame_type (info);
2181 OMP_CLAUSE_DECL (c) = info->frame_decl;
2182 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2183 gimple_omp_taskreg_set_clauses (stmt, c);
2184 info->static_chain_added |= 4;
2185 frame_decl_added = true;
2188 save_local_var_chain = info->new_local_var_chain;
2189 save_static_chain_added = info->static_chain_added;
2190 info->new_local_var_chain = NULL;
2191 info->static_chain_added = 0;
2193 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2194 gimple_omp_body_ptr (stmt));
2196 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2198 tree c = build_omp_clause (gimple_location (stmt),
2199 OMP_CLAUSE_SHARED);
2200 (void) get_frame_type (info);
2201 OMP_CLAUSE_DECL (c) = info->frame_decl;
2202 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2203 info->static_chain_added |= 4;
2204 gimple_omp_taskreg_set_clauses (stmt, c);
2206 if (info->new_local_var_chain)
2207 declare_vars (info->new_local_var_chain,
2208 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2209 info->new_local_var_chain = save_local_var_chain;
2210 info->suppress_expansion = save_suppress;
2211 info->static_chain_added |= save_static_chain_added;
2212 break;
2214 case GIMPLE_OMP_FOR:
2215 save_suppress = info->suppress_expansion;
2216 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2217 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2218 convert_local_reference_stmt,
2219 convert_local_reference_op, info);
2220 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2221 info, gimple_omp_body_ptr (stmt));
2222 info->suppress_expansion = save_suppress;
2223 break;
2225 case GIMPLE_OMP_SECTIONS:
2226 save_suppress = info->suppress_expansion;
2227 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2228 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2229 info, gimple_omp_body_ptr (stmt));
2230 info->suppress_expansion = save_suppress;
2231 break;
2233 case GIMPLE_OMP_SINGLE:
2234 save_suppress = info->suppress_expansion;
2235 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2236 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2237 info, gimple_omp_body_ptr (stmt));
2238 info->suppress_expansion = save_suppress;
2239 break;
2241 case GIMPLE_OMP_TASKGROUP:
2242 save_suppress = info->suppress_expansion;
2243 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
2244 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2245 info, gimple_omp_body_ptr (stmt));
2246 info->suppress_expansion = save_suppress;
2247 break;
2249 case GIMPLE_OMP_TARGET:
2250 if (!is_gimple_omp_offloaded (stmt))
2252 save_suppress = info->suppress_expansion;
2253 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2254 info->suppress_expansion = save_suppress;
2255 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2256 info, gimple_omp_body_ptr (stmt));
2257 break;
2259 save_suppress = info->suppress_expansion;
2260 frame_decl_added = false;
2261 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2263 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2264 (void) get_frame_type (info);
2265 OMP_CLAUSE_DECL (c) = info->frame_decl;
2266 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2267 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2268 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2269 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2270 info->static_chain_added |= 4;
2271 frame_decl_added = true;
2274 save_local_var_chain = info->new_local_var_chain;
2275 save_static_chain_added = info->static_chain_added;
2276 info->new_local_var_chain = NULL;
2277 info->static_chain_added = 0;
2279 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2280 gimple_omp_body_ptr (stmt));
2282 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2284 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2285 (void) get_frame_type (info);
2286 OMP_CLAUSE_DECL (c) = info->frame_decl;
2287 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2288 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2289 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2290 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2291 info->static_chain_added |= 4;
2294 if (info->new_local_var_chain)
2295 declare_vars (info->new_local_var_chain,
2296 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2297 info->new_local_var_chain = save_local_var_chain;
2298 info->suppress_expansion = save_suppress;
2299 info->static_chain_added |= save_static_chain_added;
2300 break;
2302 case GIMPLE_OMP_TEAMS:
2303 save_suppress = info->suppress_expansion;
2304 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2305 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2306 info, gimple_omp_body_ptr (stmt));
2307 info->suppress_expansion = save_suppress;
2308 break;
2310 case GIMPLE_OMP_SECTION:
2311 case GIMPLE_OMP_MASTER:
2312 case GIMPLE_OMP_ORDERED:
2313 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2314 info, gimple_omp_body_ptr (stmt));
2315 break;
2317 case GIMPLE_COND:
2318 wi->val_only = true;
2319 wi->is_lhs = false;
2320 *handled_ops_p = false;
2321 return NULL_TREE;
2323 case GIMPLE_ASSIGN:
2324 if (gimple_clobber_p (stmt))
2326 tree lhs = gimple_assign_lhs (stmt);
2327 if (DECL_P (lhs)
2328 && !use_pointer_in_frame (lhs)
2329 && lookup_field_for_decl (info, lhs, NO_INSERT))
2331 gsi_replace (gsi, gimple_build_nop (), true);
2332 break;
2335 *handled_ops_p = false;
2336 return NULL_TREE;
2338 case GIMPLE_BIND:
2339 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2340 var;
2341 var = DECL_CHAIN (var))
2342 if (TREE_CODE (var) == NAMELIST_DECL)
2344 /* Adjust decls mentioned in NAMELIST_DECL. */
2345 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2346 tree decl;
2347 unsigned int i;
2349 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2351 if (VAR_P (decl)
2352 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2353 continue;
2354 if (decl_function_context (decl) == info->context
2355 && !use_pointer_in_frame (decl))
2357 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2358 if (field)
2360 CONSTRUCTOR_ELT (decls, i)->value
2361 = get_local_debug_decl (info, decl, field);
2367 *handled_ops_p = false;
2368 return NULL_TREE;
2370 default:
2371 /* For every other statement that we are not interested in
2372 handling here, let the walker traverse the operands. */
2373 *handled_ops_p = false;
2374 return NULL_TREE;
2377 /* Indicate that we have handled all the operands ourselves. */
2378 *handled_ops_p = true;
2379 return NULL_TREE;
2383 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2384 that reference labels from outer functions. The rewrite will be a
2385 call to __builtin_nonlocal_goto. */
2387 static tree
2388 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2389 struct walk_stmt_info *wi)
2391 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2392 tree label, new_label, target_context, x, field;
2393 gcall *call;
2394 gimple *stmt = gsi_stmt (*gsi);
2396 if (gimple_code (stmt) != GIMPLE_GOTO)
2398 *handled_ops_p = false;
2399 return NULL_TREE;
2402 label = gimple_goto_dest (stmt);
2403 if (TREE_CODE (label) != LABEL_DECL)
2405 *handled_ops_p = false;
2406 return NULL_TREE;
2409 target_context = decl_function_context (label);
2410 if (target_context == info->context)
2412 *handled_ops_p = false;
2413 return NULL_TREE;
2416 for (i = info->outer; target_context != i->context; i = i->outer)
2417 continue;
2419 /* The original user label may also be use for a normal goto, therefore
2420 we must create a new label that will actually receive the abnormal
2421 control transfer. This new label will be marked LABEL_NONLOCAL; this
2422 mark will trigger proper behavior in the cfg, as well as cause the
2423 (hairy target-specific) non-local goto receiver code to be generated
2424 when we expand rtl. Enter this association into var_map so that we
2425 can insert the new label into the IL during a second pass. */
2426 tree *slot = &i->var_map->get_or_insert (label);
2427 if (*slot == NULL)
2429 new_label = create_artificial_label (UNKNOWN_LOCATION);
2430 DECL_NONLOCAL (new_label) = 1;
2431 *slot = new_label;
2433 else
2434 new_label = *slot;
2436 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2437 field = get_nl_goto_field (i);
2438 x = get_frame_field (info, target_context, field, gsi);
2439 x = build_addr (x);
2440 x = gsi_gimplify_val (info, x, gsi);
2441 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2442 2, build_addr (new_label), x);
2443 gsi_replace (gsi, call, false);
2445 /* We have handled all of STMT's operands, no need to keep going. */
2446 *handled_ops_p = true;
2447 return NULL_TREE;
2451 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2452 are referenced via nonlocal goto from a nested function. The rewrite
2453 will involve installing a newly generated DECL_NONLOCAL label, and
2454 (potentially) a branch around the rtl gunk that is assumed to be
2455 attached to such a label. */
2457 static tree
2458 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2459 struct walk_stmt_info *wi)
2461 struct nesting_info *const info = (struct nesting_info *) wi->info;
2462 tree label, new_label;
2463 gimple_stmt_iterator tmp_gsi;
2464 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2466 if (!stmt)
2468 *handled_ops_p = false;
2469 return NULL_TREE;
2472 label = gimple_label_label (stmt);
2474 tree *slot = info->var_map->get (label);
2475 if (!slot)
2477 *handled_ops_p = false;
2478 return NULL_TREE;
2481 /* If there's any possibility that the previous statement falls through,
2482 then we must branch around the new non-local label. */
2483 tmp_gsi = wi->gsi;
2484 gsi_prev (&tmp_gsi);
2485 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2487 gimple *stmt = gimple_build_goto (label);
2488 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2491 new_label = (tree) *slot;
2492 stmt = gimple_build_label (new_label);
2493 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2495 *handled_ops_p = true;
2496 return NULL_TREE;
2500 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2501 of nested functions that require the use of trampolines. The rewrite
2502 will involve a reference a trampoline generated for the occasion. */
2504 static tree
2505 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2507 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2508 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2509 tree t = *tp, decl, target_context, x, builtin;
2510 bool descr;
2511 gcall *call;
2513 *walk_subtrees = 0;
2514 switch (TREE_CODE (t))
2516 case ADDR_EXPR:
2517 /* Build
2518 T.1 = &CHAIN->tramp;
2519 T.2 = __builtin_adjust_trampoline (T.1);
2520 T.3 = (func_type)T.2;
2523 decl = TREE_OPERAND (t, 0);
2524 if (TREE_CODE (decl) != FUNCTION_DECL)
2525 break;
2527 /* Only need to process nested functions. */
2528 target_context = decl_function_context (decl);
2529 if (!target_context)
2530 break;
2532 /* If the nested function doesn't use a static chain, then
2533 it doesn't need a trampoline. */
2534 if (!DECL_STATIC_CHAIN (decl))
2535 break;
2537 /* If we don't want a trampoline, then don't build one. */
2538 if (TREE_NO_TRAMPOLINE (t))
2539 break;
2541 /* Lookup the immediate parent of the callee, as that's where
2542 we need to insert the trampoline. */
2543 for (i = info; i->context != target_context; i = i->outer)
2544 continue;
2546 /* Decide whether to generate a descriptor or a trampoline. */
2547 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2549 if (descr)
2550 x = lookup_descr_for_decl (i, decl, INSERT);
2551 else
2552 x = lookup_tramp_for_decl (i, decl, INSERT);
2554 /* Compute the address of the field holding the trampoline. */
2555 x = get_frame_field (info, target_context, x, &wi->gsi);
2556 x = build_addr (x);
2557 x = gsi_gimplify_val (info, x, &wi->gsi);
2559 /* Do machine-specific ugliness. Normally this will involve
2560 computing extra alignment, but it can really be anything. */
2561 if (descr)
2562 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2563 else
2564 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2565 call = gimple_build_call (builtin, 1, x);
2566 x = init_tmp_var_with_call (info, &wi->gsi, call);
2568 /* Cast back to the proper function type. */
2569 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2570 x = init_tmp_var (info, x, &wi->gsi);
2572 *tp = x;
2573 break;
2575 default:
2576 if (!IS_TYPE_OR_DECL_P (t))
2577 *walk_subtrees = 1;
2578 break;
2581 return NULL_TREE;
2585 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2586 to addresses of nested functions that require the use of
2587 trampolines. The rewrite will involve a reference a trampoline
2588 generated for the occasion. */
2590 static tree
2591 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2592 struct walk_stmt_info *wi)
2594 struct nesting_info *info = (struct nesting_info *) wi->info;
2595 gimple *stmt = gsi_stmt (*gsi);
2597 switch (gimple_code (stmt))
2599 case GIMPLE_CALL:
2601 /* Only walk call arguments, lest we generate trampolines for
2602 direct calls. */
2603 unsigned long i, nargs = gimple_call_num_args (stmt);
2604 for (i = 0; i < nargs; i++)
2605 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2606 wi, NULL);
2607 break;
2610 case GIMPLE_OMP_TARGET:
2611 if (!is_gimple_omp_offloaded (stmt))
2613 *handled_ops_p = false;
2614 return NULL_TREE;
2616 /* FALLTHRU */
2617 case GIMPLE_OMP_PARALLEL:
2618 case GIMPLE_OMP_TASK:
2620 tree save_local_var_chain = info->new_local_var_chain;
2621 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2622 info->new_local_var_chain = NULL;
2623 char save_static_chain_added = info->static_chain_added;
2624 info->static_chain_added = 0;
2625 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2626 info, gimple_omp_body_ptr (stmt));
2627 if (info->new_local_var_chain)
2628 declare_vars (info->new_local_var_chain,
2629 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2630 false);
2631 for (int i = 0; i < 2; i++)
2633 tree c, decl;
2634 if ((info->static_chain_added & (1 << i)) == 0)
2635 continue;
2636 decl = i ? get_chain_decl (info) : info->frame_decl;
2637 /* Don't add CHAIN.* or FRAME.* twice. */
2638 for (c = gimple_omp_taskreg_clauses (stmt);
2640 c = OMP_CLAUSE_CHAIN (c))
2641 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2642 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2643 && OMP_CLAUSE_DECL (c) == decl)
2644 break;
2645 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2647 c = build_omp_clause (gimple_location (stmt),
2648 i ? OMP_CLAUSE_FIRSTPRIVATE
2649 : OMP_CLAUSE_SHARED);
2650 OMP_CLAUSE_DECL (c) = decl;
2651 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2652 gimple_omp_taskreg_set_clauses (stmt, c);
2654 else if (c == NULL)
2656 c = build_omp_clause (gimple_location (stmt),
2657 OMP_CLAUSE_MAP);
2658 OMP_CLAUSE_DECL (c) = decl;
2659 OMP_CLAUSE_SET_MAP_KIND (c,
2660 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2661 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2662 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2663 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2667 info->new_local_var_chain = save_local_var_chain;
2668 info->static_chain_added |= save_static_chain_added;
2670 break;
2672 default:
2673 *handled_ops_p = false;
2674 return NULL_TREE;
2677 *handled_ops_p = true;
2678 return NULL_TREE;
2683 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2684 that reference nested functions to make sure that the static chain
2685 is set up properly for the call. */
2687 static tree
2688 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2689 struct walk_stmt_info *wi)
2691 struct nesting_info *const info = (struct nesting_info *) wi->info;
2692 tree decl, target_context;
2693 char save_static_chain_added;
2694 int i;
2695 gimple *stmt = gsi_stmt (*gsi);
2697 switch (gimple_code (stmt))
2699 case GIMPLE_CALL:
2700 if (gimple_call_chain (stmt))
2701 break;
2702 decl = gimple_call_fndecl (stmt);
2703 if (!decl)
2704 break;
2705 target_context = decl_function_context (decl);
2706 if (target_context && DECL_STATIC_CHAIN (decl))
2708 struct nesting_info *i = info;
2709 while (i && i->context != target_context)
2710 i = i->outer;
2711 /* If none of the outer contexts is the target context, this means
2712 that the function is called in a wrong context. */
2713 if (!i)
2714 internal_error ("%s from %s called in %s",
2715 IDENTIFIER_POINTER (DECL_NAME (decl)),
2716 IDENTIFIER_POINTER (DECL_NAME (target_context)),
2717 IDENTIFIER_POINTER (DECL_NAME (info->context)));
2719 gimple_call_set_chain (as_a <gcall *> (stmt),
2720 get_static_chain (info, target_context,
2721 &wi->gsi));
2722 info->static_chain_added |= (1 << (info->context != target_context));
2724 break;
2726 case GIMPLE_OMP_PARALLEL:
2727 case GIMPLE_OMP_TASK:
2728 save_static_chain_added = info->static_chain_added;
2729 info->static_chain_added = 0;
2730 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2731 for (i = 0; i < 2; i++)
2733 tree c, decl;
2734 if ((info->static_chain_added & (1 << i)) == 0)
2735 continue;
2736 decl = i ? get_chain_decl (info) : info->frame_decl;
2737 /* Don't add CHAIN.* or FRAME.* twice. */
2738 for (c = gimple_omp_taskreg_clauses (stmt);
2740 c = OMP_CLAUSE_CHAIN (c))
2741 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2742 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2743 && OMP_CLAUSE_DECL (c) == decl)
2744 break;
2745 if (c == NULL)
2747 c = build_omp_clause (gimple_location (stmt),
2748 i ? OMP_CLAUSE_FIRSTPRIVATE
2749 : OMP_CLAUSE_SHARED);
2750 OMP_CLAUSE_DECL (c) = decl;
2751 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2752 gimple_omp_taskreg_set_clauses (stmt, c);
2755 info->static_chain_added |= save_static_chain_added;
2756 break;
2758 case GIMPLE_OMP_TARGET:
2759 if (!is_gimple_omp_offloaded (stmt))
2761 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2762 break;
2764 save_static_chain_added = info->static_chain_added;
2765 info->static_chain_added = 0;
2766 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2767 for (i = 0; i < 2; i++)
2769 tree c, decl;
2770 if ((info->static_chain_added & (1 << i)) == 0)
2771 continue;
2772 decl = i ? get_chain_decl (info) : info->frame_decl;
2773 /* Don't add CHAIN.* or FRAME.* twice. */
2774 for (c = gimple_omp_target_clauses (stmt);
2776 c = OMP_CLAUSE_CHAIN (c))
2777 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2778 && OMP_CLAUSE_DECL (c) == decl)
2779 break;
2780 if (c == NULL)
2782 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2783 OMP_CLAUSE_DECL (c) = decl;
2784 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2785 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2786 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2787 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2791 info->static_chain_added |= save_static_chain_added;
2792 break;
2794 case GIMPLE_OMP_FOR:
2795 walk_body (convert_gimple_call, NULL, info,
2796 gimple_omp_for_pre_body_ptr (stmt));
2797 /* FALLTHRU */
2798 case GIMPLE_OMP_SECTIONS:
2799 case GIMPLE_OMP_SECTION:
2800 case GIMPLE_OMP_SINGLE:
2801 case GIMPLE_OMP_TEAMS:
2802 case GIMPLE_OMP_MASTER:
2803 case GIMPLE_OMP_TASKGROUP:
2804 case GIMPLE_OMP_ORDERED:
2805 case GIMPLE_OMP_CRITICAL:
2806 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2807 break;
2809 default:
2810 /* Keep looking for other operands. */
2811 *handled_ops_p = false;
2812 return NULL_TREE;
2815 *handled_ops_p = true;
2816 return NULL_TREE;
2819 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2820 call expressions. At the same time, determine if a nested function
2821 actually uses its static chain; if not, remember that. */
2823 static void
2824 convert_all_function_calls (struct nesting_info *root)
2826 unsigned int chain_count = 0, old_chain_count, iter_count;
2827 struct nesting_info *n;
2829 /* First, optimistically clear static_chain for all decls that haven't
2830 used the static chain already for variable access. But always create
2831 it if not optimizing. This makes it possible to reconstruct the static
2832 nesting tree at run time and thus to resolve up-level references from
2833 within the debugger. */
2834 FOR_EACH_NEST_INFO (n, root)
2836 if (n->thunk_p)
2837 continue;
2838 tree decl = n->context;
2839 if (!optimize)
2841 if (n->inner)
2842 (void) get_frame_type (n);
2843 if (n->outer)
2844 (void) get_chain_decl (n);
2846 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2848 DECL_STATIC_CHAIN (decl) = 0;
2849 if (dump_file && (dump_flags & TDF_DETAILS))
2850 fprintf (dump_file, "Guessing no static-chain for %s\n",
2851 lang_hooks.decl_printable_name (decl, 2));
2853 else
2854 DECL_STATIC_CHAIN (decl) = 1;
2855 chain_count += DECL_STATIC_CHAIN (decl);
2858 FOR_EACH_NEST_INFO (n, root)
2859 if (n->thunk_p)
2861 tree decl = n->context;
2862 tree alias = cgraph_node::get (decl)->thunk.alias;
2863 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2866 /* Walk the functions and perform transformations. Note that these
2867 transformations can induce new uses of the static chain, which in turn
2868 require re-examining all users of the decl. */
2869 /* ??? It would make sense to try to use the call graph to speed this up,
2870 but the call graph hasn't really been built yet. Even if it did, we
2871 would still need to iterate in this loop since address-of references
2872 wouldn't show up in the callgraph anyway. */
2873 iter_count = 0;
2876 old_chain_count = chain_count;
2877 chain_count = 0;
2878 iter_count++;
2880 if (dump_file && (dump_flags & TDF_DETAILS))
2881 fputc ('\n', dump_file);
2883 FOR_EACH_NEST_INFO (n, root)
2885 if (n->thunk_p)
2886 continue;
2887 tree decl = n->context;
2888 walk_function (convert_tramp_reference_stmt,
2889 convert_tramp_reference_op, n);
2890 walk_function (convert_gimple_call, NULL, n);
2891 chain_count += DECL_STATIC_CHAIN (decl);
2894 FOR_EACH_NEST_INFO (n, root)
2895 if (n->thunk_p)
2897 tree decl = n->context;
2898 tree alias = cgraph_node::get (decl)->thunk.alias;
2899 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2902 while (chain_count != old_chain_count);
2904 if (dump_file && (dump_flags & TDF_DETAILS))
2905 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2906 iter_count);
2909 struct nesting_copy_body_data
2911 copy_body_data cb;
2912 struct nesting_info *root;
2915 /* A helper subroutine for debug_var_chain type remapping. */
2917 static tree
2918 nesting_copy_decl (tree decl, copy_body_data *id)
2920 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2921 tree *slot = nid->root->var_map->get (decl);
2923 if (slot)
2924 return (tree) *slot;
2926 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2928 tree new_decl = copy_decl_no_change (decl, id);
2929 DECL_ORIGINAL_TYPE (new_decl)
2930 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2931 return new_decl;
2934 if (VAR_P (decl)
2935 || TREE_CODE (decl) == PARM_DECL
2936 || TREE_CODE (decl) == RESULT_DECL)
2937 return decl;
2939 return copy_decl_no_change (decl, id);
2942 /* A helper function for remap_vla_decls. See if *TP contains
2943 some remapped variables. */
2945 static tree
2946 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2948 struct nesting_info *root = (struct nesting_info *) data;
2949 tree t = *tp;
2951 if (DECL_P (t))
2953 *walk_subtrees = 0;
2954 tree *slot = root->var_map->get (t);
2956 if (slot)
2957 return *slot;
2959 return NULL;
2962 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2963 involved. */
2965 static void
2966 remap_vla_decls (tree block, struct nesting_info *root)
2968 tree var, subblock, val, type;
2969 struct nesting_copy_body_data id;
2971 for (subblock = BLOCK_SUBBLOCKS (block);
2972 subblock;
2973 subblock = BLOCK_CHAIN (subblock))
2974 remap_vla_decls (subblock, root);
2976 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2977 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
2979 val = DECL_VALUE_EXPR (var);
2980 type = TREE_TYPE (var);
2982 if (!(TREE_CODE (val) == INDIRECT_REF
2983 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2984 && variably_modified_type_p (type, NULL)))
2985 continue;
2987 if (root->var_map->get (TREE_OPERAND (val, 0))
2988 || walk_tree (&type, contains_remapped_vars, root, NULL))
2989 break;
2992 if (var == NULL_TREE)
2993 return;
2995 memset (&id, 0, sizeof (id));
2996 id.cb.copy_decl = nesting_copy_decl;
2997 id.cb.decl_map = new hash_map<tree, tree>;
2998 id.root = root;
3000 for (; var; var = DECL_CHAIN (var))
3001 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3003 struct nesting_info *i;
3004 tree newt, context;
3006 val = DECL_VALUE_EXPR (var);
3007 type = TREE_TYPE (var);
3009 if (!(TREE_CODE (val) == INDIRECT_REF
3010 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3011 && variably_modified_type_p (type, NULL)))
3012 continue;
3014 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
3015 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
3016 continue;
3018 context = decl_function_context (var);
3019 for (i = root; i; i = i->outer)
3020 if (i->context == context)
3021 break;
3023 if (i == NULL)
3024 continue;
3026 /* Fully expand value expressions. This avoids having debug variables
3027 only referenced from them and that can be swept during GC. */
3028 if (slot)
3030 tree t = (tree) *slot;
3031 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
3032 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
3035 id.cb.src_fn = i->context;
3036 id.cb.dst_fn = i->context;
3037 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3039 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3040 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3042 newt = TREE_TYPE (newt);
3043 type = TREE_TYPE (type);
3045 if (TYPE_NAME (newt)
3046 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3047 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3048 && newt != type
3049 && TYPE_NAME (newt) == TYPE_NAME (type))
3050 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3052 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3053 if (val != DECL_VALUE_EXPR (var))
3054 SET_DECL_VALUE_EXPR (var, val);
3057 delete id.cb.decl_map;
3060 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3061 involved. */
3063 static void
3064 fixup_vla_decls (tree block)
3066 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3067 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3069 tree val = DECL_VALUE_EXPR (var);
3071 if (!(TREE_CODE (val) == INDIRECT_REF
3072 && VAR_P (TREE_OPERAND (val, 0))
3073 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3074 continue;
3076 /* Fully expand value expressions. This avoids having debug variables
3077 only referenced from them and that can be swept during GC. */
3078 val = build1 (INDIRECT_REF, TREE_TYPE (val),
3079 DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3080 SET_DECL_VALUE_EXPR (var, val);
3083 for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3084 fixup_vla_decls (sub);
3087 /* Fold the MEM_REF *E. */
3088 bool
3089 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3091 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
3092 *ref_p = fold (*ref_p);
3093 return true;
3096 /* Given DECL, a nested function, build an initialization call for FIELD,
3097 the trampoline or descriptor for DECL, using FUNC as the function. */
3099 static gcall *
3100 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3101 tree func)
3103 tree arg1, arg2, arg3, x;
3105 gcc_assert (DECL_STATIC_CHAIN (decl));
3106 arg3 = build_addr (info->frame_decl);
3108 arg2 = build_addr (decl);
3110 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3111 info->frame_decl, field, NULL_TREE);
3112 arg1 = build_addr (x);
3114 return gimple_build_call (func, 3, arg1, arg2, arg3);
3117 /* Do "everything else" to clean up or complete state collected by the various
3118 walking passes -- create a field to hold the frame base address, lay out the
3119 types and decls, generate code to initialize the frame decl, store critical
3120 expressions in the struct function for rtl to find. */
3122 static void
3123 finalize_nesting_tree_1 (struct nesting_info *root)
3125 gimple_seq stmt_list = NULL;
3126 gimple *stmt;
3127 tree context = root->context;
3128 struct function *sf;
3130 if (root->thunk_p)
3131 return;
3133 /* If we created a non-local frame type or decl, we need to lay them
3134 out at this time. */
3135 if (root->frame_type)
3137 /* Debugging information needs to compute the frame base address of the
3138 parent frame out of the static chain from the nested frame.
3140 The static chain is the address of the FRAME record, so one could
3141 imagine it would be possible to compute the frame base address just
3142 adding a constant offset to this address. Unfortunately, this is not
3143 possible: if the FRAME object has alignment constraints that are
3144 stronger than the stack, then the offset between the frame base and
3145 the FRAME object will be dynamic.
3147 What we do instead is to append a field to the FRAME object that holds
3148 the frame base address: then debug info just has to fetch this
3149 field. */
3151 /* Debugging information will refer to the CFA as the frame base
3152 address: we will do the same here. */
3153 const tree frame_addr_fndecl
3154 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3156 /* Create a field in the FRAME record to hold the frame base address for
3157 this stack frame. Since it will be used only by the debugger, put it
3158 at the end of the record in order not to shift all other offsets. */
3159 tree fb_decl = make_node (FIELD_DECL);
3161 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3162 TREE_TYPE (fb_decl) = ptr_type_node;
3163 TREE_ADDRESSABLE (fb_decl) = 1;
3164 DECL_CONTEXT (fb_decl) = root->frame_type;
3165 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3166 fb_decl);
3168 /* In some cases the frame type will trigger the -Wpadded warning.
3169 This is not helpful; suppress it. */
3170 int save_warn_padded = warn_padded;
3171 warn_padded = 0;
3172 layout_type (root->frame_type);
3173 warn_padded = save_warn_padded;
3174 layout_decl (root->frame_decl, 0);
3176 /* Initialize the frame base address field. If the builtin we need is
3177 not available, set it to NULL so that debugging information does not
3178 reference junk. */
3179 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3180 root->frame_decl, fb_decl, NULL_TREE);
3181 tree fb_tmp;
3183 if (frame_addr_fndecl != NULL_TREE)
3185 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3186 integer_zero_node);
3187 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3189 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3191 else
3192 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3193 gimple_seq_add_stmt (&stmt_list,
3194 gimple_build_assign (fb_ref, fb_tmp));
3196 declare_vars (root->frame_decl,
3197 gimple_seq_first_stmt (gimple_body (context)), true);
3200 /* If any parameters were referenced non-locally, then we need to insert
3201 a copy or a pointer. */
3202 if (root->any_parm_remapped)
3204 tree p;
3205 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3207 tree field, x, y;
3209 field = lookup_field_for_decl (root, p, NO_INSERT);
3210 if (!field)
3211 continue;
3213 if (use_pointer_in_frame (p))
3214 x = build_addr (p);
3215 else
3216 x = p;
3218 /* If the assignment is from a non-register the stmt is
3219 not valid gimple. Make it so by using a temporary instead. */
3220 if (!is_gimple_reg (x)
3221 && is_gimple_reg_type (TREE_TYPE (x)))
3223 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3224 x = init_tmp_var (root, x, &gsi);
3227 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3228 root->frame_decl, field, NULL_TREE);
3229 stmt = gimple_build_assign (y, x);
3230 gimple_seq_add_stmt (&stmt_list, stmt);
3234 /* If a chain_field was created, then it needs to be initialized
3235 from chain_decl. */
3236 if (root->chain_field)
3238 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3239 root->frame_decl, root->chain_field, NULL_TREE);
3240 stmt = gimple_build_assign (x, get_chain_decl (root));
3241 gimple_seq_add_stmt (&stmt_list, stmt);
3244 /* If trampolines were created, then we need to initialize them. */
3245 if (root->any_tramp_created)
3247 struct nesting_info *i;
3248 for (i = root->inner; i ; i = i->next)
3250 tree field, x;
3252 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3253 if (!field)
3254 continue;
3256 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3257 stmt = build_init_call_stmt (root, i->context, field, x);
3258 gimple_seq_add_stmt (&stmt_list, stmt);
3262 /* If descriptors were created, then we need to initialize them. */
3263 if (root->any_descr_created)
3265 struct nesting_info *i;
3266 for (i = root->inner; i ; i = i->next)
3268 tree field, x;
3270 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3271 if (!field)
3272 continue;
3274 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3275 stmt = build_init_call_stmt (root, i->context, field, x);
3276 gimple_seq_add_stmt (&stmt_list, stmt);
3280 /* If we created initialization statements, insert them. */
3281 if (stmt_list)
3283 gbind *bind;
3284 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3285 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3286 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3287 gimple_bind_set_body (bind, stmt_list);
3290 /* If a chain_decl was created, then it needs to be registered with
3291 struct function so that it gets initialized from the static chain
3292 register at the beginning of the function. */
3293 sf = DECL_STRUCT_FUNCTION (root->context);
3294 sf->static_chain_decl = root->chain_decl;
3296 /* Similarly for the non-local goto save area. */
3297 if (root->nl_goto_field)
3299 sf->nonlocal_goto_save_area
3300 = get_frame_field (root, context, root->nl_goto_field, NULL);
3301 sf->has_nonlocal_label = 1;
3304 /* Make sure all new local variables get inserted into the
3305 proper BIND_EXPR. */
3306 if (root->new_local_var_chain)
3307 declare_vars (root->new_local_var_chain,
3308 gimple_seq_first_stmt (gimple_body (root->context)),
3309 false);
3311 if (root->debug_var_chain)
3313 tree debug_var;
3314 gbind *scope;
3316 remap_vla_decls (DECL_INITIAL (root->context), root);
3318 for (debug_var = root->debug_var_chain; debug_var;
3319 debug_var = DECL_CHAIN (debug_var))
3320 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3321 break;
3323 /* If there are any debug decls with variable length types,
3324 remap those types using other debug_var_chain variables. */
3325 if (debug_var)
3327 struct nesting_copy_body_data id;
3329 memset (&id, 0, sizeof (id));
3330 id.cb.copy_decl = nesting_copy_decl;
3331 id.cb.decl_map = new hash_map<tree, tree>;
3332 id.root = root;
3334 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3335 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3337 tree type = TREE_TYPE (debug_var);
3338 tree newt, t = type;
3339 struct nesting_info *i;
3341 for (i = root; i; i = i->outer)
3342 if (variably_modified_type_p (type, i->context))
3343 break;
3345 if (i == NULL)
3346 continue;
3348 id.cb.src_fn = i->context;
3349 id.cb.dst_fn = i->context;
3350 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3352 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3353 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3355 newt = TREE_TYPE (newt);
3356 t = TREE_TYPE (t);
3358 if (TYPE_NAME (newt)
3359 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3360 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3361 && newt != t
3362 && TYPE_NAME (newt) == TYPE_NAME (t))
3363 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3366 delete id.cb.decl_map;
3369 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3370 if (gimple_bind_block (scope))
3371 declare_vars (root->debug_var_chain, scope, true);
3372 else
3373 BLOCK_VARS (DECL_INITIAL (root->context))
3374 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3375 root->debug_var_chain);
3377 else
3378 fixup_vla_decls (DECL_INITIAL (root->context));
3380 /* Fold the rewritten MEM_REF trees. */
3381 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3383 /* Dump the translated tree function. */
3384 if (dump_file)
3386 fputs ("\n\n", dump_file);
3387 dump_function_to_file (root->context, dump_file, dump_flags);
3391 static void
3392 finalize_nesting_tree (struct nesting_info *root)
3394 struct nesting_info *n;
3395 FOR_EACH_NEST_INFO (n, root)
3396 finalize_nesting_tree_1 (n);
3399 /* Unnest the nodes and pass them to cgraph. */
3401 static void
3402 unnest_nesting_tree_1 (struct nesting_info *root)
3404 struct cgraph_node *node = cgraph_node::get (root->context);
3406 /* For nested functions update the cgraph to reflect unnesting.
3407 We also delay finalizing of these functions up to this point. */
3408 if (node->origin)
3410 node->unnest ();
3411 if (!root->thunk_p)
3412 cgraph_node::finalize_function (root->context, true);
3416 static void
3417 unnest_nesting_tree (struct nesting_info *root)
3419 struct nesting_info *n;
3420 FOR_EACH_NEST_INFO (n, root)
3421 unnest_nesting_tree_1 (n);
3424 /* Free the data structures allocated during this pass. */
3426 static void
3427 free_nesting_tree (struct nesting_info *root)
3429 struct nesting_info *node, *next;
3431 node = iter_nestinfo_start (root);
3434 next = iter_nestinfo_next (node);
3435 delete node->var_map;
3436 delete node->field_map;
3437 delete node->mem_refs;
3438 free (node);
3439 node = next;
3441 while (node);
3444 /* Gimplify a function and all its nested functions. */
3445 static void
3446 gimplify_all_functions (struct cgraph_node *root)
3448 struct cgraph_node *iter;
3449 if (!gimple_body (root->decl))
3450 gimplify_function_tree (root->decl);
3451 for (iter = root->nested; iter; iter = iter->next_nested)
3452 if (!iter->thunk.thunk_p)
3453 gimplify_all_functions (iter);
3456 /* Main entry point for this pass. Process FNDECL and all of its nested
3457 subroutines and turn them into something less tightly bound. */
3459 void
3460 lower_nested_functions (tree fndecl)
3462 struct cgraph_node *cgn;
3463 struct nesting_info *root;
3465 /* If there are no nested functions, there's nothing to do. */
3466 cgn = cgraph_node::get (fndecl);
3467 if (!cgn->nested)
3468 return;
3470 gimplify_all_functions (cgn);
3472 set_dump_file (dump_begin (TDI_nested, &dump_flags));
3473 if (dump_file)
3474 fprintf (dump_file, "\n;; Function %s\n\n",
3475 lang_hooks.decl_printable_name (fndecl, 2));
3477 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3478 root = create_nesting_tree (cgn);
3480 walk_all_functions (convert_nonlocal_reference_stmt,
3481 convert_nonlocal_reference_op,
3482 root);
3483 walk_all_functions (convert_local_reference_stmt,
3484 convert_local_reference_op,
3485 root);
3486 walk_all_functions (convert_nl_goto_reference, NULL, root);
3487 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3489 convert_all_function_calls (root);
3490 finalize_nesting_tree (root);
3491 unnest_nesting_tree (root);
3493 free_nesting_tree (root);
3494 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3496 if (dump_file)
3498 dump_end (TDI_nested, dump_file);
3499 set_dump_file (NULL);
3503 #include "gt-tree-nested.h"