[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / tree-nested.c
blob4af70e9647511fd6d4d4ff158523a32124970439
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2015 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 "tree.h"
25 #include "gimple.h"
26 #include "rtl.h"
27 #include "alias.h"
28 #include "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "tm_p.h"
32 #include "tree-dump.h"
33 #include "tree-inline.h"
34 #include "internal-fn.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "gimple-walk.h"
38 #include "tree-iterator.h"
39 #include "cgraph.h"
40 #include "tree-cfg.h"
41 #include "flags.h"
42 #include "insn-config.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "calls.h"
47 #include "emit-rtl.h"
48 #include "varasm.h"
49 #include "stmt.h"
50 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
51 #include "langhooks.h"
52 #include "gimple-low.h"
53 #include "gomp-constants.h"
56 /* The object of this pass is to lower the representation of a set of nested
57 functions in order to expose all of the gory details of the various
58 nonlocal references. We want to do this sooner rather than later, in
59 order to give us more freedom in emitting all of the functions in question.
61 Back in olden times, when gcc was young, we developed an insanely
62 complicated scheme whereby variables which were referenced nonlocally
63 were forced to live in the stack of the declaring function, and then
64 the nested functions magically discovered where these variables were
65 placed. In order for this scheme to function properly, it required
66 that the outer function be partially expanded, then we switch to
67 compiling the inner function, and once done with those we switch back
68 to compiling the outer function. Such delicate ordering requirements
69 makes it difficult to do whole translation unit optimizations
70 involving such functions.
72 The implementation here is much more direct. Everything that can be
73 referenced by an inner function is a member of an explicitly created
74 structure herein called the "nonlocal frame struct". The incoming
75 static chain for a nested function is a pointer to this struct in
76 the parent. In this way, we settle on known offsets from a known
77 base, and so are decoupled from the logic that places objects in the
78 function's stack frame. More importantly, we don't have to wait for
79 that to happen -- since the compilation of the inner function is no
80 longer tied to a real stack frame, the nonlocal frame struct can be
81 allocated anywhere. Which means that the outer function is now
82 inlinable.
84 Theory of operation here is very simple. Iterate over all the
85 statements in all the functions (depth first) several times,
86 allocating structures and fields on demand. In general we want to
87 examine inner functions first, so that we can avoid making changes
88 to outer functions which are unnecessary.
90 The order of the passes matters a bit, in that later passes will be
91 skipped if it is discovered that the functions don't actually interact
92 at all. That is, they're nested in the lexical sense but could have
93 been written as independent functions without change. */
96 struct nesting_info
98 struct nesting_info *outer;
99 struct nesting_info *inner;
100 struct nesting_info *next;
102 hash_map<tree, tree> *field_map;
103 hash_map<tree, tree> *var_map;
104 hash_set<tree *> *mem_refs;
105 bitmap suppress_expansion;
107 tree context;
108 tree new_local_var_chain;
109 tree debug_var_chain;
110 tree frame_type;
111 tree frame_decl;
112 tree chain_field;
113 tree chain_decl;
114 tree nl_goto_field;
116 bool any_parm_remapped;
117 bool any_tramp_created;
118 char static_chain_added;
122 /* Iterate over the nesting tree, starting with ROOT, depth first. */
124 static inline struct nesting_info *
125 iter_nestinfo_start (struct nesting_info *root)
127 while (root->inner)
128 root = root->inner;
129 return root;
132 static inline struct nesting_info *
133 iter_nestinfo_next (struct nesting_info *node)
135 if (node->next)
136 return iter_nestinfo_start (node->next);
137 return node->outer;
140 #define FOR_EACH_NEST_INFO(I, ROOT) \
141 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
143 /* Obstack used for the bitmaps in the struct above. */
144 static struct bitmap_obstack nesting_info_bitmap_obstack;
147 /* We're working in so many different function contexts simultaneously,
148 that create_tmp_var is dangerous. Prevent mishap. */
149 #define create_tmp_var cant_use_create_tmp_var_here_dummy
151 /* Like create_tmp_var, except record the variable for registration at
152 the given nesting level. */
154 static tree
155 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
157 tree tmp_var;
159 /* If the type is of variable size or a type which must be created by the
160 frontend, something is wrong. Note that we explicitly allow
161 incomplete types here, since we create them ourselves here. */
162 gcc_assert (!TREE_ADDRESSABLE (type));
163 gcc_assert (!TYPE_SIZE_UNIT (type)
164 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
166 tmp_var = create_tmp_var_raw (type, prefix);
167 DECL_CONTEXT (tmp_var) = info->context;
168 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
169 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
170 if (TREE_CODE (type) == COMPLEX_TYPE
171 || TREE_CODE (type) == VECTOR_TYPE)
172 DECL_GIMPLE_REG_P (tmp_var) = 1;
174 info->new_local_var_chain = tmp_var;
176 return tmp_var;
179 /* Take the address of EXP to be used within function CONTEXT.
180 Mark it for addressability as necessary. */
182 tree
183 build_addr (tree exp, tree context)
185 tree base = exp;
186 tree save_context;
187 tree retval;
189 while (handled_component_p (base))
190 base = TREE_OPERAND (base, 0);
192 if (DECL_P (base))
193 TREE_ADDRESSABLE (base) = 1;
195 /* Building the ADDR_EXPR will compute a set of properties for
196 that ADDR_EXPR. Those properties are unfortunately context
197 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
199 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
200 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
201 way the properties are for the ADDR_EXPR are computed properly. */
202 save_context = current_function_decl;
203 current_function_decl = context;
204 retval = build_fold_addr_expr (exp);
205 current_function_decl = save_context;
206 return retval;
209 /* Insert FIELD into TYPE, sorted by alignment requirements. */
211 void
212 insert_field_into_struct (tree type, tree field)
214 tree *p;
216 DECL_CONTEXT (field) = type;
218 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
219 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
220 break;
222 DECL_CHAIN (field) = *p;
223 *p = field;
225 /* Set correct alignment for frame struct type. */
226 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
227 TYPE_ALIGN (type) = DECL_ALIGN (field);
230 /* Build or return the RECORD_TYPE that describes the frame state that is
231 shared between INFO->CONTEXT and its nested functions. This record will
232 not be complete until finalize_nesting_tree; up until that point we'll
233 be adding fields as necessary.
235 We also build the DECL that represents this frame in the function. */
237 static tree
238 get_frame_type (struct nesting_info *info)
240 tree type = info->frame_type;
241 if (!type)
243 char *name;
245 type = make_node (RECORD_TYPE);
247 name = concat ("FRAME.",
248 IDENTIFIER_POINTER (DECL_NAME (info->context)),
249 NULL);
250 TYPE_NAME (type) = get_identifier (name);
251 free (name);
253 info->frame_type = type;
254 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
255 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
257 /* ??? Always make it addressable for now, since it is meant to
258 be pointed to by the static chain pointer. This pessimizes
259 when it turns out that no static chains are needed because
260 the nested functions referencing non-local variables are not
261 reachable, but the true pessimization is to create the non-
262 local frame structure in the first place. */
263 TREE_ADDRESSABLE (info->frame_decl) = 1;
265 return type;
268 /* Return true if DECL should be referenced by pointer in the non-local
269 frame structure. */
271 static bool
272 use_pointer_in_frame (tree decl)
274 if (TREE_CODE (decl) == PARM_DECL)
276 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
277 sized decls, and inefficient to copy large aggregates. Don't bother
278 moving anything but scalar variables. */
279 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
281 else
283 /* Variable sized types make things "interesting" in the frame. */
284 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
288 /* Given DECL, a non-locally accessed variable, find or create a field
289 in the non-local frame structure for the given nesting context. */
291 static tree
292 lookup_field_for_decl (struct nesting_info *info, tree decl,
293 enum insert_option insert)
295 if (insert == NO_INSERT)
297 tree *slot = info->field_map->get (decl);
298 return slot ? *slot : NULL_TREE;
301 tree *slot = &info->field_map->get_or_insert (decl);
302 if (!*slot)
304 tree field = make_node (FIELD_DECL);
305 DECL_NAME (field) = DECL_NAME (decl);
307 if (use_pointer_in_frame (decl))
309 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
310 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
311 DECL_NONADDRESSABLE_P (field) = 1;
313 else
315 TREE_TYPE (field) = TREE_TYPE (decl);
316 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
317 DECL_ALIGN (field) = DECL_ALIGN (decl);
318 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
319 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
320 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
321 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
324 insert_field_into_struct (get_frame_type (info), field);
325 *slot = field;
327 if (TREE_CODE (decl) == PARM_DECL)
328 info->any_parm_remapped = true;
331 return *slot;
334 /* Build or return the variable that holds the static chain within
335 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
337 static tree
338 get_chain_decl (struct nesting_info *info)
340 tree decl = info->chain_decl;
342 if (!decl)
344 tree type;
346 type = get_frame_type (info->outer);
347 type = build_pointer_type (type);
349 /* Note that this variable is *not* entered into any BIND_EXPR;
350 the construction of this variable is handled specially in
351 expand_function_start and initialize_inlined_parameters.
352 Note also that it's represented as a parameter. This is more
353 close to the truth, since the initial value does come from
354 the caller. */
355 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
356 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
357 DECL_ARTIFICIAL (decl) = 1;
358 DECL_IGNORED_P (decl) = 1;
359 TREE_USED (decl) = 1;
360 DECL_CONTEXT (decl) = info->context;
361 DECL_ARG_TYPE (decl) = type;
363 /* Tell tree-inline.c that we never write to this variable, so
364 it can copy-prop the replacement value immediately. */
365 TREE_READONLY (decl) = 1;
367 info->chain_decl = decl;
369 if (dump_file
370 && (dump_flags & TDF_DETAILS)
371 && !DECL_STATIC_CHAIN (info->context))
372 fprintf (dump_file, "Setting static-chain for %s\n",
373 lang_hooks.decl_printable_name (info->context, 2));
375 DECL_STATIC_CHAIN (info->context) = 1;
377 return decl;
380 /* Build or return the field within the non-local frame state that holds
381 the static chain for INFO->CONTEXT. This is the way to walk back up
382 multiple nesting levels. */
384 static tree
385 get_chain_field (struct nesting_info *info)
387 tree field = info->chain_field;
389 if (!field)
391 tree type = build_pointer_type (get_frame_type (info->outer));
393 field = make_node (FIELD_DECL);
394 DECL_NAME (field) = get_identifier ("__chain");
395 TREE_TYPE (field) = type;
396 DECL_ALIGN (field) = TYPE_ALIGN (type);
397 DECL_NONADDRESSABLE_P (field) = 1;
399 insert_field_into_struct (get_frame_type (info), field);
401 info->chain_field = field;
403 if (dump_file
404 && (dump_flags & TDF_DETAILS)
405 && !DECL_STATIC_CHAIN (info->context))
406 fprintf (dump_file, "Setting static-chain for %s\n",
407 lang_hooks.decl_printable_name (info->context, 2));
409 DECL_STATIC_CHAIN (info->context) = 1;
411 return field;
414 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
416 static tree
417 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
418 gcall *call)
420 tree t;
422 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
423 gimple_call_set_lhs (call, t);
424 if (! gsi_end_p (*gsi))
425 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
426 gsi_insert_before (gsi, call, GSI_SAME_STMT);
428 return t;
432 /* Copy EXP into a temporary. Allocate the temporary in the context of
433 INFO and insert the initialization statement before GSI. */
435 static tree
436 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
438 tree t;
439 gimple *stmt;
441 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
442 stmt = gimple_build_assign (t, exp);
443 if (! gsi_end_p (*gsi))
444 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
445 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
447 return t;
451 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
453 static tree
454 gsi_gimplify_val (struct nesting_info *info, tree exp,
455 gimple_stmt_iterator *gsi)
457 if (is_gimple_val (exp))
458 return exp;
459 else
460 return init_tmp_var (info, exp, gsi);
463 /* Similarly, but copy from the temporary and insert the statement
464 after the iterator. */
466 static tree
467 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
469 tree t;
470 gimple *stmt;
472 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
473 stmt = gimple_build_assign (exp, t);
474 if (! gsi_end_p (*gsi))
475 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
476 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
478 return t;
481 /* Build or return the type used to represent a nested function trampoline. */
483 static GTY(()) tree trampoline_type;
485 static tree
486 get_trampoline_type (struct nesting_info *info)
488 unsigned align, size;
489 tree t;
491 if (trampoline_type)
492 return trampoline_type;
494 align = TRAMPOLINE_ALIGNMENT;
495 size = TRAMPOLINE_SIZE;
497 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
498 then allocate extra space so that we can do dynamic alignment. */
499 if (align > STACK_BOUNDARY)
501 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
502 align = STACK_BOUNDARY;
505 t = build_index_type (size_int (size - 1));
506 t = build_array_type (char_type_node, t);
507 t = build_decl (DECL_SOURCE_LOCATION (info->context),
508 FIELD_DECL, get_identifier ("__data"), t);
509 DECL_ALIGN (t) = align;
510 DECL_USER_ALIGN (t) = 1;
512 trampoline_type = make_node (RECORD_TYPE);
513 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
514 TYPE_FIELDS (trampoline_type) = t;
515 layout_type (trampoline_type);
516 DECL_CONTEXT (t) = trampoline_type;
518 return trampoline_type;
521 /* Given DECL, a nested function, find or create a field in the non-local
522 frame structure for a trampoline for this function. */
524 static tree
525 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
526 enum insert_option insert)
528 if (insert == NO_INSERT)
530 tree *slot = info->var_map->get (decl);
531 return slot ? *slot : NULL_TREE;
534 tree *slot = &info->var_map->get_or_insert (decl);
535 if (!*slot)
537 tree field = make_node (FIELD_DECL);
538 DECL_NAME (field) = DECL_NAME (decl);
539 TREE_TYPE (field) = get_trampoline_type (info);
540 TREE_ADDRESSABLE (field) = 1;
542 insert_field_into_struct (get_frame_type (info), field);
543 *slot = field;
545 info->any_tramp_created = true;
548 return *slot;
551 /* Build or return the field within the non-local frame state that holds
552 the non-local goto "jmp_buf". The buffer itself is maintained by the
553 rtl middle-end as dynamic stack space is allocated. */
555 static tree
556 get_nl_goto_field (struct nesting_info *info)
558 tree field = info->nl_goto_field;
559 if (!field)
561 unsigned size;
562 tree type;
564 /* For __builtin_nonlocal_goto, we need N words. The first is the
565 frame pointer, the rest is for the target's stack pointer save
566 area. The number of words is controlled by STACK_SAVEAREA_MODE;
567 not the best interface, but it'll do for now. */
568 if (Pmode == ptr_mode)
569 type = ptr_type_node;
570 else
571 type = lang_hooks.types.type_for_mode (Pmode, 1);
573 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
574 size = size / GET_MODE_SIZE (Pmode);
575 size = size + 1;
577 type = build_array_type
578 (type, build_index_type (size_int (size)));
580 field = make_node (FIELD_DECL);
581 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
582 TREE_TYPE (field) = type;
583 DECL_ALIGN (field) = TYPE_ALIGN (type);
584 TREE_ADDRESSABLE (field) = 1;
586 insert_field_into_struct (get_frame_type (info), field);
588 info->nl_goto_field = field;
591 return field;
594 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
596 static void
597 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
598 struct nesting_info *info, gimple_seq *pseq)
600 struct walk_stmt_info wi;
602 memset (&wi, 0, sizeof (wi));
603 wi.info = info;
604 wi.val_only = true;
605 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
609 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
611 static inline void
612 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
613 struct nesting_info *info)
615 gimple_seq body = gimple_body (info->context);
616 walk_body (callback_stmt, callback_op, info, &body);
617 gimple_set_body (info->context, body);
620 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
622 static void
623 walk_gimple_omp_for (gomp_for *for_stmt,
624 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
625 struct nesting_info *info)
627 struct walk_stmt_info wi;
628 gimple_seq seq;
629 tree t;
630 size_t i;
632 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
634 seq = NULL;
635 memset (&wi, 0, sizeof (wi));
636 wi.info = info;
637 wi.gsi = gsi_last (seq);
639 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
641 wi.val_only = false;
642 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
643 &wi, NULL);
644 wi.val_only = true;
645 wi.is_lhs = false;
646 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
647 &wi, NULL);
649 wi.val_only = true;
650 wi.is_lhs = false;
651 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
652 &wi, NULL);
654 t = gimple_omp_for_incr (for_stmt, i);
655 gcc_assert (BINARY_CLASS_P (t));
656 wi.val_only = false;
657 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
658 wi.val_only = true;
659 wi.is_lhs = false;
660 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
663 seq = gsi_seq (wi.gsi);
664 if (!gimple_seq_empty_p (seq))
666 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
667 annotate_all_with_location (seq, gimple_location (for_stmt));
668 gimple_seq_add_seq (&pre_body, seq);
669 gimple_omp_for_set_pre_body (for_stmt, pre_body);
673 /* Similarly for ROOT and all functions nested underneath, depth first. */
675 static void
676 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
677 struct nesting_info *root)
679 struct nesting_info *n;
680 FOR_EACH_NEST_INFO (n, root)
681 walk_function (callback_stmt, callback_op, n);
685 /* We have to check for a fairly pathological case. The operands of function
686 nested function are to be interpreted in the context of the enclosing
687 function. So if any are variably-sized, they will get remapped when the
688 enclosing function is inlined. But that remapping would also have to be
689 done in the types of the PARM_DECLs of the nested function, meaning the
690 argument types of that function will disagree with the arguments in the
691 calls to that function. So we'd either have to make a copy of the nested
692 function corresponding to each time the enclosing function was inlined or
693 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
694 function. The former is not practical. The latter would still require
695 detecting this case to know when to add the conversions. So, for now at
696 least, we don't inline such an enclosing function.
698 We have to do that check recursively, so here return indicating whether
699 FNDECL has such a nested function. ORIG_FN is the function we were
700 trying to inline to use for checking whether any argument is variably
701 modified by anything in it.
703 It would be better to do this in tree-inline.c so that we could give
704 the appropriate warning for why a function can't be inlined, but that's
705 too late since the nesting structure has already been flattened and
706 adding a flag just to record this fact seems a waste of a flag. */
708 static bool
709 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
711 struct cgraph_node *cgn = cgraph_node::get (fndecl);
712 tree arg;
714 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
716 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
717 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
718 return true;
720 if (check_for_nested_with_variably_modified (cgn->decl,
721 orig_fndecl))
722 return true;
725 return false;
728 /* Construct our local datastructure describing the function nesting
729 tree rooted by CGN. */
731 static struct nesting_info *
732 create_nesting_tree (struct cgraph_node *cgn)
734 struct nesting_info *info = XCNEW (struct nesting_info);
735 info->field_map = new hash_map<tree, tree>;
736 info->var_map = new hash_map<tree, tree>;
737 info->mem_refs = new hash_set<tree *>;
738 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
739 info->context = cgn->decl;
741 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
743 struct nesting_info *sub = create_nesting_tree (cgn);
744 sub->outer = info;
745 sub->next = info->inner;
746 info->inner = sub;
749 /* See discussion at check_for_nested_with_variably_modified for a
750 discussion of why this has to be here. */
751 if (check_for_nested_with_variably_modified (info->context, info->context))
752 DECL_UNINLINABLE (info->context) = true;
754 return info;
757 /* Return an expression computing the static chain for TARGET_CONTEXT
758 from INFO->CONTEXT. Insert any necessary computations before TSI. */
760 static tree
761 get_static_chain (struct nesting_info *info, tree target_context,
762 gimple_stmt_iterator *gsi)
764 struct nesting_info *i;
765 tree x;
767 if (info->context == target_context)
769 x = build_addr (info->frame_decl, target_context);
770 info->static_chain_added |= 1;
772 else
774 x = get_chain_decl (info);
775 info->static_chain_added |= 2;
777 for (i = info->outer; i->context != target_context; i = i->outer)
779 tree field = get_chain_field (i);
781 x = build_simple_mem_ref (x);
782 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
783 x = init_tmp_var (info, x, gsi);
787 return x;
791 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
792 frame as seen from INFO->CONTEXT. Insert any necessary computations
793 before GSI. */
795 static tree
796 get_frame_field (struct nesting_info *info, tree target_context,
797 tree field, gimple_stmt_iterator *gsi)
799 struct nesting_info *i;
800 tree x;
802 if (info->context == target_context)
804 /* Make sure frame_decl gets created. */
805 (void) get_frame_type (info);
806 x = info->frame_decl;
807 info->static_chain_added |= 1;
809 else
811 x = get_chain_decl (info);
812 info->static_chain_added |= 2;
814 for (i = info->outer; i->context != target_context; i = i->outer)
816 tree field = get_chain_field (i);
818 x = build_simple_mem_ref (x);
819 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
820 x = init_tmp_var (info, x, gsi);
823 x = build_simple_mem_ref (x);
826 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
827 return x;
830 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
832 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
833 in the nested function with DECL_VALUE_EXPR set to reference the true
834 variable in the parent function. This is used both for debug info
835 and in OMP lowering. */
837 static tree
838 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
840 tree target_context;
841 struct nesting_info *i;
842 tree x, field, new_decl;
844 tree *slot = &info->var_map->get_or_insert (decl);
846 if (*slot)
847 return *slot;
849 target_context = decl_function_context (decl);
851 /* A copy of the code in get_frame_field, but without the temporaries. */
852 if (info->context == target_context)
854 /* Make sure frame_decl gets created. */
855 (void) get_frame_type (info);
856 x = info->frame_decl;
857 i = info;
858 info->static_chain_added |= 1;
860 else
862 x = get_chain_decl (info);
863 info->static_chain_added |= 2;
864 for (i = info->outer; i->context != target_context; i = i->outer)
866 field = get_chain_field (i);
867 x = build_simple_mem_ref (x);
868 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
870 x = build_simple_mem_ref (x);
873 field = lookup_field_for_decl (i, decl, INSERT);
874 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
875 if (use_pointer_in_frame (decl))
876 x = build_simple_mem_ref (x);
878 /* ??? We should be remapping types as well, surely. */
879 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
880 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
881 DECL_CONTEXT (new_decl) = info->context;
882 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
883 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
884 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
885 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
886 TREE_READONLY (new_decl) = TREE_READONLY (decl);
887 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
888 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
889 if ((TREE_CODE (decl) == PARM_DECL
890 || TREE_CODE (decl) == RESULT_DECL
891 || TREE_CODE (decl) == VAR_DECL)
892 && DECL_BY_REFERENCE (decl))
893 DECL_BY_REFERENCE (new_decl) = 1;
895 SET_DECL_VALUE_EXPR (new_decl, x);
896 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
898 *slot = new_decl;
899 DECL_CHAIN (new_decl) = info->debug_var_chain;
900 info->debug_var_chain = new_decl;
902 if (!optimize
903 && info->context != target_context
904 && variably_modified_type_p (TREE_TYPE (decl), NULL))
905 note_nonlocal_vla_type (info, TREE_TYPE (decl));
907 return new_decl;
911 /* Callback for walk_gimple_stmt, rewrite all references to VAR
912 and PARM_DECLs that belong to outer functions.
914 The rewrite will involve some number of structure accesses back up
915 the static chain. E.g. for a variable FOO up one nesting level it'll
916 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
917 indirections apply to decls for which use_pointer_in_frame is true. */
919 static tree
920 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
922 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
923 struct nesting_info *const info = (struct nesting_info *) wi->info;
924 tree t = *tp;
926 *walk_subtrees = 0;
927 switch (TREE_CODE (t))
929 case VAR_DECL:
930 /* Non-automatic variables are never processed. */
931 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
932 break;
933 /* FALLTHRU */
935 case PARM_DECL:
936 if (decl_function_context (t) != info->context)
938 tree x;
939 wi->changed = true;
941 x = get_nonlocal_debug_decl (info, t);
942 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
944 tree target_context = decl_function_context (t);
945 struct nesting_info *i;
946 for (i = info->outer; i->context != target_context; i = i->outer)
947 continue;
948 x = lookup_field_for_decl (i, t, INSERT);
949 x = get_frame_field (info, target_context, x, &wi->gsi);
950 if (use_pointer_in_frame (t))
952 x = init_tmp_var (info, x, &wi->gsi);
953 x = build_simple_mem_ref (x);
957 if (wi->val_only)
959 if (wi->is_lhs)
960 x = save_tmp_var (info, x, &wi->gsi);
961 else
962 x = init_tmp_var (info, x, &wi->gsi);
965 *tp = x;
967 break;
969 case LABEL_DECL:
970 /* We're taking the address of a label from a parent function, but
971 this is not itself a non-local goto. Mark the label such that it
972 will not be deleted, much as we would with a label address in
973 static storage. */
974 if (decl_function_context (t) != info->context)
975 FORCED_LABEL (t) = 1;
976 break;
978 case ADDR_EXPR:
980 bool save_val_only = wi->val_only;
982 wi->val_only = false;
983 wi->is_lhs = false;
984 wi->changed = false;
985 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
986 wi->val_only = true;
988 if (wi->changed)
990 tree save_context;
992 /* If we changed anything, we might no longer be directly
993 referencing a decl. */
994 save_context = current_function_decl;
995 current_function_decl = info->context;
996 recompute_tree_invariant_for_addr_expr (t);
997 current_function_decl = save_context;
999 /* If the callback converted the address argument in a context
1000 where we only accept variables (and min_invariant, presumably),
1001 then compute the address into a temporary. */
1002 if (save_val_only)
1003 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1004 t, &wi->gsi);
1007 break;
1009 case REALPART_EXPR:
1010 case IMAGPART_EXPR:
1011 case COMPONENT_REF:
1012 case ARRAY_REF:
1013 case ARRAY_RANGE_REF:
1014 case BIT_FIELD_REF:
1015 /* Go down this entire nest and just look at the final prefix and
1016 anything that describes the references. Otherwise, we lose track
1017 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1018 wi->val_only = true;
1019 wi->is_lhs = false;
1020 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1022 if (TREE_CODE (t) == COMPONENT_REF)
1023 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1024 NULL);
1025 else if (TREE_CODE (t) == ARRAY_REF
1026 || TREE_CODE (t) == ARRAY_RANGE_REF)
1028 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1029 wi, NULL);
1030 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1031 wi, NULL);
1032 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1033 wi, NULL);
1036 wi->val_only = false;
1037 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1038 break;
1040 case VIEW_CONVERT_EXPR:
1041 /* Just request to look at the subtrees, leaving val_only and lhs
1042 untouched. This might actually be for !val_only + lhs, in which
1043 case we don't want to force a replacement by a temporary. */
1044 *walk_subtrees = 1;
1045 break;
1047 default:
1048 if (!IS_TYPE_OR_DECL_P (t))
1050 *walk_subtrees = 1;
1051 wi->val_only = true;
1052 wi->is_lhs = false;
1054 break;
1057 return NULL_TREE;
1060 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1061 struct walk_stmt_info *);
1063 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1064 and PARM_DECLs that belong to outer functions. */
1066 static bool
1067 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1069 struct nesting_info *const info = (struct nesting_info *) wi->info;
1070 bool need_chain = false, need_stmts = false;
1071 tree clause, decl;
1072 int dummy;
1073 bitmap new_suppress;
1075 new_suppress = BITMAP_GGC_ALLOC ();
1076 bitmap_copy (new_suppress, info->suppress_expansion);
1078 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1080 switch (OMP_CLAUSE_CODE (clause))
1082 case OMP_CLAUSE_REDUCTION:
1083 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1084 need_stmts = true;
1085 goto do_decl_clause;
1087 case OMP_CLAUSE_LASTPRIVATE:
1088 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1089 need_stmts = true;
1090 goto do_decl_clause;
1092 case OMP_CLAUSE_LINEAR:
1093 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1094 need_stmts = true;
1095 wi->val_only = true;
1096 wi->is_lhs = false;
1097 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1098 &dummy, wi);
1099 goto do_decl_clause;
1101 case OMP_CLAUSE_PRIVATE:
1102 case OMP_CLAUSE_FIRSTPRIVATE:
1103 case OMP_CLAUSE_COPYPRIVATE:
1104 case OMP_CLAUSE_SHARED:
1105 do_decl_clause:
1106 decl = OMP_CLAUSE_DECL (clause);
1107 if (TREE_CODE (decl) == VAR_DECL
1108 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1109 break;
1110 if (decl_function_context (decl) != info->context)
1112 bitmap_set_bit (new_suppress, DECL_UID (decl));
1113 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1114 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1115 need_chain = true;
1117 break;
1119 case OMP_CLAUSE_SCHEDULE:
1120 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1121 break;
1122 /* FALLTHRU */
1123 case OMP_CLAUSE_FINAL:
1124 case OMP_CLAUSE_IF:
1125 case OMP_CLAUSE_NUM_THREADS:
1126 case OMP_CLAUSE_DEPEND:
1127 case OMP_CLAUSE_DEVICE:
1128 case OMP_CLAUSE_NUM_TEAMS:
1129 case OMP_CLAUSE_THREAD_LIMIT:
1130 case OMP_CLAUSE_SAFELEN:
1131 case OMP_CLAUSE__CILK_FOR_COUNT_:
1132 wi->val_only = true;
1133 wi->is_lhs = false;
1134 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1135 &dummy, wi);
1136 break;
1138 case OMP_CLAUSE_DIST_SCHEDULE:
1139 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1141 wi->val_only = true;
1142 wi->is_lhs = false;
1143 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1144 &dummy, wi);
1146 break;
1148 case OMP_CLAUSE_MAP:
1149 case OMP_CLAUSE_TO:
1150 case OMP_CLAUSE_FROM:
1151 if (OMP_CLAUSE_SIZE (clause))
1153 wi->val_only = true;
1154 wi->is_lhs = false;
1155 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1156 &dummy, wi);
1158 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1159 goto do_decl_clause;
1160 wi->val_only = true;
1161 wi->is_lhs = false;
1162 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1163 wi, NULL);
1164 break;
1166 case OMP_CLAUSE_ALIGNED:
1167 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1169 wi->val_only = true;
1170 wi->is_lhs = false;
1171 convert_nonlocal_reference_op
1172 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1174 /* Like do_decl_clause, but don't add any suppression. */
1175 decl = OMP_CLAUSE_DECL (clause);
1176 if (TREE_CODE (decl) == VAR_DECL
1177 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1178 break;
1179 if (decl_function_context (decl) != info->context)
1181 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1182 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1183 need_chain = true;
1185 break;
1187 case OMP_CLAUSE_NOWAIT:
1188 case OMP_CLAUSE_ORDERED:
1189 case OMP_CLAUSE_DEFAULT:
1190 case OMP_CLAUSE_COPYIN:
1191 case OMP_CLAUSE_COLLAPSE:
1192 case OMP_CLAUSE_UNTIED:
1193 case OMP_CLAUSE_MERGEABLE:
1194 case OMP_CLAUSE_PROC_BIND:
1195 break;
1197 default:
1198 gcc_unreachable ();
1202 info->suppress_expansion = new_suppress;
1204 if (need_stmts)
1205 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1206 switch (OMP_CLAUSE_CODE (clause))
1208 case OMP_CLAUSE_REDUCTION:
1209 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1211 tree old_context
1212 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1213 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1214 = info->context;
1215 walk_body (convert_nonlocal_reference_stmt,
1216 convert_nonlocal_reference_op, info,
1217 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1218 walk_body (convert_nonlocal_reference_stmt,
1219 convert_nonlocal_reference_op, info,
1220 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1221 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1222 = old_context;
1224 break;
1226 case OMP_CLAUSE_LASTPRIVATE:
1227 walk_body (convert_nonlocal_reference_stmt,
1228 convert_nonlocal_reference_op, info,
1229 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1230 break;
1232 case OMP_CLAUSE_LINEAR:
1233 walk_body (convert_nonlocal_reference_stmt,
1234 convert_nonlocal_reference_op, info,
1235 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1236 break;
1238 default:
1239 break;
1242 return need_chain;
1245 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1247 static void
1248 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1250 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1251 type = TREE_TYPE (type);
1253 if (TYPE_NAME (type)
1254 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1255 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1256 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1258 while (POINTER_TYPE_P (type)
1259 || TREE_CODE (type) == VECTOR_TYPE
1260 || TREE_CODE (type) == FUNCTION_TYPE
1261 || TREE_CODE (type) == METHOD_TYPE)
1262 type = TREE_TYPE (type);
1264 if (TREE_CODE (type) == ARRAY_TYPE)
1266 tree domain, t;
1268 note_nonlocal_vla_type (info, TREE_TYPE (type));
1269 domain = TYPE_DOMAIN (type);
1270 if (domain)
1272 t = TYPE_MIN_VALUE (domain);
1273 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1274 && decl_function_context (t) != info->context)
1275 get_nonlocal_debug_decl (info, t);
1276 t = TYPE_MAX_VALUE (domain);
1277 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1278 && decl_function_context (t) != info->context)
1279 get_nonlocal_debug_decl (info, t);
1284 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1285 in BLOCK. */
1287 static void
1288 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1290 tree var;
1292 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1293 if (TREE_CODE (var) == VAR_DECL
1294 && variably_modified_type_p (TREE_TYPE (var), NULL)
1295 && DECL_HAS_VALUE_EXPR_P (var)
1296 && decl_function_context (var) != info->context)
1297 note_nonlocal_vla_type (info, TREE_TYPE (var));
1300 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1301 PARM_DECLs that belong to outer functions. This handles statements
1302 that are not handled via the standard recursion done in
1303 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1304 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1305 operands of STMT have been handled by this function. */
1307 static tree
1308 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1309 struct walk_stmt_info *wi)
1311 struct nesting_info *info = (struct nesting_info *) wi->info;
1312 tree save_local_var_chain;
1313 bitmap save_suppress;
1314 gimple *stmt = gsi_stmt (*gsi);
1316 switch (gimple_code (stmt))
1318 case GIMPLE_GOTO:
1319 /* Don't walk non-local gotos for now. */
1320 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1322 wi->val_only = true;
1323 wi->is_lhs = false;
1324 *handled_ops_p = true;
1325 return NULL_TREE;
1327 break;
1329 case GIMPLE_OMP_PARALLEL:
1330 case GIMPLE_OMP_TASK:
1331 save_suppress = info->suppress_expansion;
1332 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1333 wi))
1335 tree c, decl;
1336 decl = get_chain_decl (info);
1337 c = build_omp_clause (gimple_location (stmt),
1338 OMP_CLAUSE_FIRSTPRIVATE);
1339 OMP_CLAUSE_DECL (c) = decl;
1340 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1341 gimple_omp_taskreg_set_clauses (stmt, c);
1344 save_local_var_chain = info->new_local_var_chain;
1345 info->new_local_var_chain = NULL;
1347 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1348 info, gimple_omp_body_ptr (stmt));
1350 if (info->new_local_var_chain)
1351 declare_vars (info->new_local_var_chain,
1352 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1353 false);
1354 info->new_local_var_chain = save_local_var_chain;
1355 info->suppress_expansion = save_suppress;
1356 break;
1358 case GIMPLE_OMP_FOR:
1359 save_suppress = info->suppress_expansion;
1360 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1361 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1362 convert_nonlocal_reference_stmt,
1363 convert_nonlocal_reference_op, info);
1364 walk_body (convert_nonlocal_reference_stmt,
1365 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1366 info->suppress_expansion = save_suppress;
1367 break;
1369 case GIMPLE_OMP_SECTIONS:
1370 save_suppress = info->suppress_expansion;
1371 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1372 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1373 info, gimple_omp_body_ptr (stmt));
1374 info->suppress_expansion = save_suppress;
1375 break;
1377 case GIMPLE_OMP_SINGLE:
1378 save_suppress = info->suppress_expansion;
1379 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1380 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1381 info, gimple_omp_body_ptr (stmt));
1382 info->suppress_expansion = save_suppress;
1383 break;
1385 case GIMPLE_OMP_TARGET:
1386 if (!is_gimple_omp_offloaded (stmt))
1388 save_suppress = info->suppress_expansion;
1389 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1390 wi);
1391 info->suppress_expansion = save_suppress;
1392 walk_body (convert_nonlocal_reference_stmt,
1393 convert_nonlocal_reference_op, info,
1394 gimple_omp_body_ptr (stmt));
1395 break;
1397 save_suppress = info->suppress_expansion;
1398 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1399 wi))
1401 tree c, decl;
1402 decl = get_chain_decl (info);
1403 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1404 OMP_CLAUSE_DECL (c) = decl;
1405 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1406 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1407 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1408 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1411 save_local_var_chain = info->new_local_var_chain;
1412 info->new_local_var_chain = NULL;
1414 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1415 info, gimple_omp_body_ptr (stmt));
1417 if (info->new_local_var_chain)
1418 declare_vars (info->new_local_var_chain,
1419 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1420 false);
1421 info->new_local_var_chain = save_local_var_chain;
1422 info->suppress_expansion = save_suppress;
1423 break;
1425 case GIMPLE_OMP_TEAMS:
1426 save_suppress = info->suppress_expansion;
1427 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1428 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1429 info, gimple_omp_body_ptr (stmt));
1430 info->suppress_expansion = save_suppress;
1431 break;
1433 case GIMPLE_OMP_SECTION:
1434 case GIMPLE_OMP_MASTER:
1435 case GIMPLE_OMP_TASKGROUP:
1436 case GIMPLE_OMP_ORDERED:
1437 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1438 info, gimple_omp_body_ptr (stmt));
1439 break;
1441 case GIMPLE_BIND:
1443 gbind *bind_stmt = as_a <gbind *> (stmt);
1444 if (!optimize && gimple_bind_block (bind_stmt))
1445 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1447 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1448 if (TREE_CODE (var) == NAMELIST_DECL)
1450 /* Adjust decls mentioned in NAMELIST_DECL. */
1451 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1452 tree decl;
1453 unsigned int i;
1455 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1457 if (TREE_CODE (decl) == VAR_DECL
1458 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1459 continue;
1460 if (decl_function_context (decl) != info->context)
1461 CONSTRUCTOR_ELT (decls, i)->value
1462 = get_nonlocal_debug_decl (info, decl);
1466 *handled_ops_p = false;
1467 return NULL_TREE;
1469 case GIMPLE_COND:
1470 wi->val_only = true;
1471 wi->is_lhs = false;
1472 *handled_ops_p = false;
1473 return NULL_TREE;
1475 default:
1476 /* For every other statement that we are not interested in
1477 handling here, let the walker traverse the operands. */
1478 *handled_ops_p = false;
1479 return NULL_TREE;
1482 /* We have handled all of STMT operands, no need to traverse the operands. */
1483 *handled_ops_p = true;
1484 return NULL_TREE;
1488 /* A subroutine of convert_local_reference. Create a local variable
1489 in the parent function with DECL_VALUE_EXPR set to reference the
1490 field in FRAME. This is used both for debug info and in OMP
1491 lowering. */
1493 static tree
1494 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1496 tree x, new_decl;
1498 tree *slot = &info->var_map->get_or_insert (decl);
1499 if (*slot)
1500 return *slot;
1502 /* Make sure frame_decl gets created. */
1503 (void) get_frame_type (info);
1504 x = info->frame_decl;
1505 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1507 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1508 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1509 DECL_CONTEXT (new_decl) = info->context;
1510 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1511 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1512 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1513 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1514 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1515 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1516 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1517 if ((TREE_CODE (decl) == PARM_DECL
1518 || TREE_CODE (decl) == RESULT_DECL
1519 || TREE_CODE (decl) == VAR_DECL)
1520 && DECL_BY_REFERENCE (decl))
1521 DECL_BY_REFERENCE (new_decl) = 1;
1523 SET_DECL_VALUE_EXPR (new_decl, x);
1524 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1525 *slot = new_decl;
1527 DECL_CHAIN (new_decl) = info->debug_var_chain;
1528 info->debug_var_chain = new_decl;
1530 /* Do not emit debug info twice. */
1531 DECL_IGNORED_P (decl) = 1;
1533 return new_decl;
1537 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1538 and PARM_DECLs that were referenced by inner nested functions.
1539 The rewrite will be a structure reference to the local frame variable. */
1541 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1543 static tree
1544 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1546 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1547 struct nesting_info *const info = (struct nesting_info *) wi->info;
1548 tree t = *tp, field, x;
1549 bool save_val_only;
1551 *walk_subtrees = 0;
1552 switch (TREE_CODE (t))
1554 case VAR_DECL:
1555 /* Non-automatic variables are never processed. */
1556 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1557 break;
1558 /* FALLTHRU */
1560 case PARM_DECL:
1561 if (decl_function_context (t) == info->context)
1563 /* If we copied a pointer to the frame, then the original decl
1564 is used unchanged in the parent function. */
1565 if (use_pointer_in_frame (t))
1566 break;
1568 /* No need to transform anything if no child references the
1569 variable. */
1570 field = lookup_field_for_decl (info, t, NO_INSERT);
1571 if (!field)
1572 break;
1573 wi->changed = true;
1575 x = get_local_debug_decl (info, t, field);
1576 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1577 x = get_frame_field (info, info->context, field, &wi->gsi);
1579 if (wi->val_only)
1581 if (wi->is_lhs)
1582 x = save_tmp_var (info, x, &wi->gsi);
1583 else
1584 x = init_tmp_var (info, x, &wi->gsi);
1587 *tp = x;
1589 break;
1591 case ADDR_EXPR:
1592 save_val_only = wi->val_only;
1593 wi->val_only = false;
1594 wi->is_lhs = false;
1595 wi->changed = false;
1596 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1597 wi->val_only = save_val_only;
1599 /* If we converted anything ... */
1600 if (wi->changed)
1602 tree save_context;
1604 /* Then the frame decl is now addressable. */
1605 TREE_ADDRESSABLE (info->frame_decl) = 1;
1607 save_context = current_function_decl;
1608 current_function_decl = info->context;
1609 recompute_tree_invariant_for_addr_expr (t);
1610 current_function_decl = save_context;
1612 /* If we are in a context where we only accept values, then
1613 compute the address into a temporary. */
1614 if (save_val_only)
1615 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1616 t, &wi->gsi);
1618 break;
1620 case REALPART_EXPR:
1621 case IMAGPART_EXPR:
1622 case COMPONENT_REF:
1623 case ARRAY_REF:
1624 case ARRAY_RANGE_REF:
1625 case BIT_FIELD_REF:
1626 /* Go down this entire nest and just look at the final prefix and
1627 anything that describes the references. Otherwise, we lose track
1628 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1629 save_val_only = wi->val_only;
1630 wi->val_only = true;
1631 wi->is_lhs = false;
1632 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1634 if (TREE_CODE (t) == COMPONENT_REF)
1635 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1636 NULL);
1637 else if (TREE_CODE (t) == ARRAY_REF
1638 || TREE_CODE (t) == ARRAY_RANGE_REF)
1640 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1641 NULL);
1642 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1643 NULL);
1644 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1645 NULL);
1648 wi->val_only = false;
1649 walk_tree (tp, convert_local_reference_op, wi, NULL);
1650 wi->val_only = save_val_only;
1651 break;
1653 case MEM_REF:
1654 save_val_only = wi->val_only;
1655 wi->val_only = true;
1656 wi->is_lhs = false;
1657 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1658 wi, NULL);
1659 /* We need to re-fold the MEM_REF as component references as
1660 part of a ADDR_EXPR address are not allowed. But we cannot
1661 fold here, as the chain record type is not yet finalized. */
1662 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1663 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1664 info->mem_refs->add (tp);
1665 wi->val_only = save_val_only;
1666 break;
1668 case VIEW_CONVERT_EXPR:
1669 /* Just request to look at the subtrees, leaving val_only and lhs
1670 untouched. This might actually be for !val_only + lhs, in which
1671 case we don't want to force a replacement by a temporary. */
1672 *walk_subtrees = 1;
1673 break;
1675 default:
1676 if (!IS_TYPE_OR_DECL_P (t))
1678 *walk_subtrees = 1;
1679 wi->val_only = true;
1680 wi->is_lhs = false;
1682 break;
1685 return NULL_TREE;
1688 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1689 struct walk_stmt_info *);
1691 /* Helper for convert_local_reference. Convert all the references in
1692 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1694 static bool
1695 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1697 struct nesting_info *const info = (struct nesting_info *) wi->info;
1698 bool need_frame = false, need_stmts = false;
1699 tree clause, decl;
1700 int dummy;
1701 bitmap new_suppress;
1703 new_suppress = BITMAP_GGC_ALLOC ();
1704 bitmap_copy (new_suppress, info->suppress_expansion);
1706 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1708 switch (OMP_CLAUSE_CODE (clause))
1710 case OMP_CLAUSE_REDUCTION:
1711 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1712 need_stmts = true;
1713 goto do_decl_clause;
1715 case OMP_CLAUSE_LASTPRIVATE:
1716 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1717 need_stmts = true;
1718 goto do_decl_clause;
1720 case OMP_CLAUSE_LINEAR:
1721 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1722 need_stmts = true;
1723 wi->val_only = true;
1724 wi->is_lhs = false;
1725 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1726 wi);
1727 goto do_decl_clause;
1729 case OMP_CLAUSE_PRIVATE:
1730 case OMP_CLAUSE_FIRSTPRIVATE:
1731 case OMP_CLAUSE_COPYPRIVATE:
1732 case OMP_CLAUSE_SHARED:
1733 do_decl_clause:
1734 decl = OMP_CLAUSE_DECL (clause);
1735 if (TREE_CODE (decl) == VAR_DECL
1736 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1737 break;
1738 if (decl_function_context (decl) == info->context
1739 && !use_pointer_in_frame (decl))
1741 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1742 if (field)
1744 bitmap_set_bit (new_suppress, DECL_UID (decl));
1745 OMP_CLAUSE_DECL (clause)
1746 = get_local_debug_decl (info, decl, field);
1747 need_frame = true;
1750 break;
1752 case OMP_CLAUSE_SCHEDULE:
1753 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1754 break;
1755 /* FALLTHRU */
1756 case OMP_CLAUSE_FINAL:
1757 case OMP_CLAUSE_IF:
1758 case OMP_CLAUSE_NUM_THREADS:
1759 case OMP_CLAUSE_DEPEND:
1760 case OMP_CLAUSE_DEVICE:
1761 case OMP_CLAUSE_NUM_TEAMS:
1762 case OMP_CLAUSE_THREAD_LIMIT:
1763 case OMP_CLAUSE_SAFELEN:
1764 case OMP_CLAUSE__CILK_FOR_COUNT_:
1765 wi->val_only = true;
1766 wi->is_lhs = false;
1767 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1768 wi);
1769 break;
1771 case OMP_CLAUSE_DIST_SCHEDULE:
1772 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1774 wi->val_only = true;
1775 wi->is_lhs = false;
1776 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1777 &dummy, wi);
1779 break;
1781 case OMP_CLAUSE_MAP:
1782 case OMP_CLAUSE_TO:
1783 case OMP_CLAUSE_FROM:
1784 if (OMP_CLAUSE_SIZE (clause))
1786 wi->val_only = true;
1787 wi->is_lhs = false;
1788 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1789 &dummy, wi);
1791 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1792 goto do_decl_clause;
1793 wi->val_only = true;
1794 wi->is_lhs = false;
1795 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1796 wi, NULL);
1797 break;
1799 case OMP_CLAUSE_ALIGNED:
1800 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1802 wi->val_only = true;
1803 wi->is_lhs = false;
1804 convert_local_reference_op
1805 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1807 /* Like do_decl_clause, but don't add any suppression. */
1808 decl = OMP_CLAUSE_DECL (clause);
1809 if (TREE_CODE (decl) == VAR_DECL
1810 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1811 break;
1812 if (decl_function_context (decl) == info->context
1813 && !use_pointer_in_frame (decl))
1815 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1816 if (field)
1818 OMP_CLAUSE_DECL (clause)
1819 = get_local_debug_decl (info, decl, field);
1820 need_frame = true;
1823 break;
1825 case OMP_CLAUSE_NOWAIT:
1826 case OMP_CLAUSE_ORDERED:
1827 case OMP_CLAUSE_DEFAULT:
1828 case OMP_CLAUSE_COPYIN:
1829 case OMP_CLAUSE_COLLAPSE:
1830 case OMP_CLAUSE_UNTIED:
1831 case OMP_CLAUSE_MERGEABLE:
1832 case OMP_CLAUSE_PROC_BIND:
1833 break;
1835 default:
1836 gcc_unreachable ();
1840 info->suppress_expansion = new_suppress;
1842 if (need_stmts)
1843 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1844 switch (OMP_CLAUSE_CODE (clause))
1846 case OMP_CLAUSE_REDUCTION:
1847 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1849 tree old_context
1850 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1851 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1852 = info->context;
1853 walk_body (convert_local_reference_stmt,
1854 convert_local_reference_op, info,
1855 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1856 walk_body (convert_local_reference_stmt,
1857 convert_local_reference_op, info,
1858 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1859 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1860 = old_context;
1862 break;
1864 case OMP_CLAUSE_LASTPRIVATE:
1865 walk_body (convert_local_reference_stmt,
1866 convert_local_reference_op, info,
1867 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1868 break;
1870 case OMP_CLAUSE_LINEAR:
1871 walk_body (convert_local_reference_stmt,
1872 convert_local_reference_op, info,
1873 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1874 break;
1876 default:
1877 break;
1880 return need_frame;
1884 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1885 and PARM_DECLs that were referenced by inner nested functions.
1886 The rewrite will be a structure reference to the local frame variable. */
1888 static tree
1889 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1890 struct walk_stmt_info *wi)
1892 struct nesting_info *info = (struct nesting_info *) wi->info;
1893 tree save_local_var_chain;
1894 bitmap save_suppress;
1895 gimple *stmt = gsi_stmt (*gsi);
1897 switch (gimple_code (stmt))
1899 case GIMPLE_OMP_PARALLEL:
1900 case GIMPLE_OMP_TASK:
1901 save_suppress = info->suppress_expansion;
1902 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1903 wi))
1905 tree c;
1906 (void) get_frame_type (info);
1907 c = build_omp_clause (gimple_location (stmt),
1908 OMP_CLAUSE_SHARED);
1909 OMP_CLAUSE_DECL (c) = info->frame_decl;
1910 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1911 gimple_omp_taskreg_set_clauses (stmt, c);
1914 save_local_var_chain = info->new_local_var_chain;
1915 info->new_local_var_chain = NULL;
1917 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1918 gimple_omp_body_ptr (stmt));
1920 if (info->new_local_var_chain)
1921 declare_vars (info->new_local_var_chain,
1922 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1923 info->new_local_var_chain = save_local_var_chain;
1924 info->suppress_expansion = save_suppress;
1925 break;
1927 case GIMPLE_OMP_FOR:
1928 save_suppress = info->suppress_expansion;
1929 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1930 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1931 convert_local_reference_stmt,
1932 convert_local_reference_op, info);
1933 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1934 info, gimple_omp_body_ptr (stmt));
1935 info->suppress_expansion = save_suppress;
1936 break;
1938 case GIMPLE_OMP_SECTIONS:
1939 save_suppress = info->suppress_expansion;
1940 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1941 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1942 info, gimple_omp_body_ptr (stmt));
1943 info->suppress_expansion = save_suppress;
1944 break;
1946 case GIMPLE_OMP_SINGLE:
1947 save_suppress = info->suppress_expansion;
1948 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1949 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1950 info, gimple_omp_body_ptr (stmt));
1951 info->suppress_expansion = save_suppress;
1952 break;
1954 case GIMPLE_OMP_TARGET:
1955 if (!is_gimple_omp_offloaded (stmt))
1957 save_suppress = info->suppress_expansion;
1958 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1959 info->suppress_expansion = save_suppress;
1960 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1961 info, gimple_omp_body_ptr (stmt));
1962 break;
1964 save_suppress = info->suppress_expansion;
1965 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1967 tree c;
1968 (void) get_frame_type (info);
1969 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1970 OMP_CLAUSE_DECL (c) = info->frame_decl;
1971 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
1972 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1973 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1974 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1977 save_local_var_chain = info->new_local_var_chain;
1978 info->new_local_var_chain = NULL;
1980 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1981 gimple_omp_body_ptr (stmt));
1983 if (info->new_local_var_chain)
1984 declare_vars (info->new_local_var_chain,
1985 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1986 info->new_local_var_chain = save_local_var_chain;
1987 info->suppress_expansion = save_suppress;
1988 break;
1990 case GIMPLE_OMP_TEAMS:
1991 save_suppress = info->suppress_expansion;
1992 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1993 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1994 info, gimple_omp_body_ptr (stmt));
1995 info->suppress_expansion = save_suppress;
1996 break;
1998 case GIMPLE_OMP_SECTION:
1999 case GIMPLE_OMP_MASTER:
2000 case GIMPLE_OMP_TASKGROUP:
2001 case GIMPLE_OMP_ORDERED:
2002 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2003 info, gimple_omp_body_ptr (stmt));
2004 break;
2006 case GIMPLE_COND:
2007 wi->val_only = true;
2008 wi->is_lhs = false;
2009 *handled_ops_p = false;
2010 return NULL_TREE;
2012 case GIMPLE_ASSIGN:
2013 if (gimple_clobber_p (stmt))
2015 tree lhs = gimple_assign_lhs (stmt);
2016 if (!use_pointer_in_frame (lhs)
2017 && lookup_field_for_decl (info, lhs, NO_INSERT))
2019 gsi_replace (gsi, gimple_build_nop (), true);
2020 break;
2023 *handled_ops_p = false;
2024 return NULL_TREE;
2026 case GIMPLE_BIND:
2027 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2028 var;
2029 var = DECL_CHAIN (var))
2030 if (TREE_CODE (var) == NAMELIST_DECL)
2032 /* Adjust decls mentioned in NAMELIST_DECL. */
2033 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2034 tree decl;
2035 unsigned int i;
2037 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2039 if (TREE_CODE (decl) == VAR_DECL
2040 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2041 continue;
2042 if (decl_function_context (decl) == info->context
2043 && !use_pointer_in_frame (decl))
2045 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2046 if (field)
2048 CONSTRUCTOR_ELT (decls, i)->value
2049 = get_local_debug_decl (info, decl, field);
2055 *handled_ops_p = false;
2056 return NULL_TREE;
2058 default:
2059 /* For every other statement that we are not interested in
2060 handling here, let the walker traverse the operands. */
2061 *handled_ops_p = false;
2062 return NULL_TREE;
2065 /* Indicate that we have handled all the operands ourselves. */
2066 *handled_ops_p = true;
2067 return NULL_TREE;
2071 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2072 that reference labels from outer functions. The rewrite will be a
2073 call to __builtin_nonlocal_goto. */
2075 static tree
2076 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2077 struct walk_stmt_info *wi)
2079 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2080 tree label, new_label, target_context, x, field;
2081 gcall *call;
2082 gimple *stmt = gsi_stmt (*gsi);
2084 if (gimple_code (stmt) != GIMPLE_GOTO)
2086 *handled_ops_p = false;
2087 return NULL_TREE;
2090 label = gimple_goto_dest (stmt);
2091 if (TREE_CODE (label) != LABEL_DECL)
2093 *handled_ops_p = false;
2094 return NULL_TREE;
2097 target_context = decl_function_context (label);
2098 if (target_context == info->context)
2100 *handled_ops_p = false;
2101 return NULL_TREE;
2104 for (i = info->outer; target_context != i->context; i = i->outer)
2105 continue;
2107 /* The original user label may also be use for a normal goto, therefore
2108 we must create a new label that will actually receive the abnormal
2109 control transfer. This new label will be marked LABEL_NONLOCAL; this
2110 mark will trigger proper behavior in the cfg, as well as cause the
2111 (hairy target-specific) non-local goto receiver code to be generated
2112 when we expand rtl. Enter this association into var_map so that we
2113 can insert the new label into the IL during a second pass. */
2114 tree *slot = &i->var_map->get_or_insert (label);
2115 if (*slot == NULL)
2117 new_label = create_artificial_label (UNKNOWN_LOCATION);
2118 DECL_NONLOCAL (new_label) = 1;
2119 *slot = new_label;
2121 else
2122 new_label = *slot;
2124 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2125 field = get_nl_goto_field (i);
2126 x = get_frame_field (info, target_context, field, gsi);
2127 x = build_addr (x, target_context);
2128 x = gsi_gimplify_val (info, x, gsi);
2129 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2130 2, build_addr (new_label, target_context), x);
2131 gsi_replace (gsi, call, false);
2133 /* We have handled all of STMT's operands, no need to keep going. */
2134 *handled_ops_p = true;
2135 return NULL_TREE;
2139 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2140 are referenced via nonlocal goto from a nested function. The rewrite
2141 will involve installing a newly generated DECL_NONLOCAL label, and
2142 (potentially) a branch around the rtl gunk that is assumed to be
2143 attached to such a label. */
2145 static tree
2146 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2147 struct walk_stmt_info *wi)
2149 struct nesting_info *const info = (struct nesting_info *) wi->info;
2150 tree label, new_label;
2151 gimple_stmt_iterator tmp_gsi;
2152 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2154 if (!stmt)
2156 *handled_ops_p = false;
2157 return NULL_TREE;
2160 label = gimple_label_label (stmt);
2162 tree *slot = info->var_map->get (label);
2163 if (!slot)
2165 *handled_ops_p = false;
2166 return NULL_TREE;
2169 /* If there's any possibility that the previous statement falls through,
2170 then we must branch around the new non-local label. */
2171 tmp_gsi = wi->gsi;
2172 gsi_prev (&tmp_gsi);
2173 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2175 gimple *stmt = gimple_build_goto (label);
2176 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2179 new_label = (tree) *slot;
2180 stmt = gimple_build_label (new_label);
2181 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2183 *handled_ops_p = true;
2184 return NULL_TREE;
2188 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2189 of nested functions that require the use of trampolines. The rewrite
2190 will involve a reference a trampoline generated for the occasion. */
2192 static tree
2193 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2195 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2196 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2197 tree t = *tp, decl, target_context, x, builtin;
2198 gcall *call;
2200 *walk_subtrees = 0;
2201 switch (TREE_CODE (t))
2203 case ADDR_EXPR:
2204 /* Build
2205 T.1 = &CHAIN->tramp;
2206 T.2 = __builtin_adjust_trampoline (T.1);
2207 T.3 = (func_type)T.2;
2210 decl = TREE_OPERAND (t, 0);
2211 if (TREE_CODE (decl) != FUNCTION_DECL)
2212 break;
2214 /* Only need to process nested functions. */
2215 target_context = decl_function_context (decl);
2216 if (!target_context)
2217 break;
2219 /* If the nested function doesn't use a static chain, then
2220 it doesn't need a trampoline. */
2221 if (!DECL_STATIC_CHAIN (decl))
2222 break;
2224 /* If we don't want a trampoline, then don't build one. */
2225 if (TREE_NO_TRAMPOLINE (t))
2226 break;
2228 /* Lookup the immediate parent of the callee, as that's where
2229 we need to insert the trampoline. */
2230 for (i = info; i->context != target_context; i = i->outer)
2231 continue;
2232 x = lookup_tramp_for_decl (i, decl, INSERT);
2234 /* Compute the address of the field holding the trampoline. */
2235 x = get_frame_field (info, target_context, x, &wi->gsi);
2236 x = build_addr (x, target_context);
2237 x = gsi_gimplify_val (info, x, &wi->gsi);
2239 /* Do machine-specific ugliness. Normally this will involve
2240 computing extra alignment, but it can really be anything. */
2241 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2242 call = gimple_build_call (builtin, 1, x);
2243 x = init_tmp_var_with_call (info, &wi->gsi, call);
2245 /* Cast back to the proper function type. */
2246 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2247 x = init_tmp_var (info, x, &wi->gsi);
2249 *tp = x;
2250 break;
2252 default:
2253 if (!IS_TYPE_OR_DECL_P (t))
2254 *walk_subtrees = 1;
2255 break;
2258 return NULL_TREE;
2262 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2263 to addresses of nested functions that require the use of
2264 trampolines. The rewrite will involve a reference a trampoline
2265 generated for the occasion. */
2267 static tree
2268 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2269 struct walk_stmt_info *wi)
2271 struct nesting_info *info = (struct nesting_info *) wi->info;
2272 gimple *stmt = gsi_stmt (*gsi);
2274 switch (gimple_code (stmt))
2276 case GIMPLE_CALL:
2278 /* Only walk call arguments, lest we generate trampolines for
2279 direct calls. */
2280 unsigned long i, nargs = gimple_call_num_args (stmt);
2281 for (i = 0; i < nargs; i++)
2282 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2283 wi, NULL);
2284 break;
2287 case GIMPLE_OMP_TARGET:
2288 if (!is_gimple_omp_offloaded (stmt))
2290 *handled_ops_p = false;
2291 return NULL_TREE;
2293 /* FALLTHRU */
2294 case GIMPLE_OMP_PARALLEL:
2295 case GIMPLE_OMP_TASK:
2297 tree save_local_var_chain = info->new_local_var_chain;
2298 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2299 info->new_local_var_chain = NULL;
2300 char save_static_chain_added = info->static_chain_added;
2301 info->static_chain_added = 0;
2302 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2303 info, gimple_omp_body_ptr (stmt));
2304 if (info->new_local_var_chain)
2305 declare_vars (info->new_local_var_chain,
2306 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2307 false);
2308 for (int i = 0; i < 2; i++)
2310 tree c, decl;
2311 if ((info->static_chain_added & (1 << i)) == 0)
2312 continue;
2313 decl = i ? get_chain_decl (info) : info->frame_decl;
2314 /* Don't add CHAIN.* or FRAME.* twice. */
2315 for (c = gimple_omp_taskreg_clauses (stmt);
2317 c = OMP_CLAUSE_CHAIN (c))
2318 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2319 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2320 && OMP_CLAUSE_DECL (c) == decl)
2321 break;
2322 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2324 c = build_omp_clause (gimple_location (stmt),
2325 i ? OMP_CLAUSE_FIRSTPRIVATE
2326 : OMP_CLAUSE_SHARED);
2327 OMP_CLAUSE_DECL (c) = decl;
2328 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2329 gimple_omp_taskreg_set_clauses (stmt, c);
2331 else if (c == NULL)
2333 c = build_omp_clause (gimple_location (stmt),
2334 OMP_CLAUSE_MAP);
2335 OMP_CLAUSE_DECL (c) = decl;
2336 OMP_CLAUSE_SET_MAP_KIND (c,
2337 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2338 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2339 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2340 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2344 info->new_local_var_chain = save_local_var_chain;
2345 info->static_chain_added |= save_static_chain_added;
2347 break;
2349 default:
2350 *handled_ops_p = false;
2351 return NULL_TREE;
2354 *handled_ops_p = true;
2355 return NULL_TREE;
2360 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2361 that reference nested functions to make sure that the static chain
2362 is set up properly for the call. */
2364 static tree
2365 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2366 struct walk_stmt_info *wi)
2368 struct nesting_info *const info = (struct nesting_info *) wi->info;
2369 tree decl, target_context;
2370 char save_static_chain_added;
2371 int i;
2372 gimple *stmt = gsi_stmt (*gsi);
2374 switch (gimple_code (stmt))
2376 case GIMPLE_CALL:
2377 if (gimple_call_chain (stmt))
2378 break;
2379 decl = gimple_call_fndecl (stmt);
2380 if (!decl)
2381 break;
2382 target_context = decl_function_context (decl);
2383 if (target_context && DECL_STATIC_CHAIN (decl))
2385 gimple_call_set_chain (as_a <gcall *> (stmt),
2386 get_static_chain (info, target_context,
2387 &wi->gsi));
2388 info->static_chain_added |= (1 << (info->context != target_context));
2390 break;
2392 case GIMPLE_OMP_PARALLEL:
2393 case GIMPLE_OMP_TASK:
2394 save_static_chain_added = info->static_chain_added;
2395 info->static_chain_added = 0;
2396 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2397 for (i = 0; i < 2; i++)
2399 tree c, decl;
2400 if ((info->static_chain_added & (1 << i)) == 0)
2401 continue;
2402 decl = i ? get_chain_decl (info) : info->frame_decl;
2403 /* Don't add CHAIN.* or FRAME.* twice. */
2404 for (c = gimple_omp_taskreg_clauses (stmt);
2406 c = OMP_CLAUSE_CHAIN (c))
2407 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2408 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2409 && OMP_CLAUSE_DECL (c) == decl)
2410 break;
2411 if (c == NULL)
2413 c = build_omp_clause (gimple_location (stmt),
2414 i ? OMP_CLAUSE_FIRSTPRIVATE
2415 : OMP_CLAUSE_SHARED);
2416 OMP_CLAUSE_DECL (c) = decl;
2417 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2418 gimple_omp_taskreg_set_clauses (stmt, c);
2421 info->static_chain_added |= save_static_chain_added;
2422 break;
2424 case GIMPLE_OMP_TARGET:
2425 if (!is_gimple_omp_offloaded (stmt))
2427 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2428 break;
2430 save_static_chain_added = info->static_chain_added;
2431 info->static_chain_added = 0;
2432 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2433 for (i = 0; i < 2; i++)
2435 tree c, decl;
2436 if ((info->static_chain_added & (1 << i)) == 0)
2437 continue;
2438 decl = i ? get_chain_decl (info) : info->frame_decl;
2439 /* Don't add CHAIN.* or FRAME.* twice. */
2440 for (c = gimple_omp_target_clauses (stmt);
2442 c = OMP_CLAUSE_CHAIN (c))
2443 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2444 && OMP_CLAUSE_DECL (c) == decl)
2445 break;
2446 if (c == NULL)
2448 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2449 OMP_CLAUSE_DECL (c) = decl;
2450 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2451 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2452 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2453 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2457 info->static_chain_added |= save_static_chain_added;
2458 break;
2460 case GIMPLE_OMP_FOR:
2461 walk_body (convert_gimple_call, NULL, info,
2462 gimple_omp_for_pre_body_ptr (stmt));
2463 /* FALLTHRU */
2464 case GIMPLE_OMP_SECTIONS:
2465 case GIMPLE_OMP_SECTION:
2466 case GIMPLE_OMP_SINGLE:
2467 case GIMPLE_OMP_TEAMS:
2468 case GIMPLE_OMP_MASTER:
2469 case GIMPLE_OMP_TASKGROUP:
2470 case GIMPLE_OMP_ORDERED:
2471 case GIMPLE_OMP_CRITICAL:
2472 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2473 break;
2475 default:
2476 /* Keep looking for other operands. */
2477 *handled_ops_p = false;
2478 return NULL_TREE;
2481 *handled_ops_p = true;
2482 return NULL_TREE;
2485 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2486 call expressions. At the same time, determine if a nested function
2487 actually uses its static chain; if not, remember that. */
2489 static void
2490 convert_all_function_calls (struct nesting_info *root)
2492 unsigned int chain_count = 0, old_chain_count, iter_count;
2493 struct nesting_info *n;
2495 /* First, optimistically clear static_chain for all decls that haven't
2496 used the static chain already for variable access. But always create
2497 it if not optimizing. This makes it possible to reconstruct the static
2498 nesting tree at run time and thus to resolve up-level references from
2499 within the debugger. */
2500 FOR_EACH_NEST_INFO (n, root)
2502 tree decl = n->context;
2503 if (!optimize)
2505 if (n->inner)
2506 (void) get_frame_type (n);
2507 if (n->outer)
2508 (void) get_chain_decl (n);
2510 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2512 DECL_STATIC_CHAIN (decl) = 0;
2513 if (dump_file && (dump_flags & TDF_DETAILS))
2514 fprintf (dump_file, "Guessing no static-chain for %s\n",
2515 lang_hooks.decl_printable_name (decl, 2));
2517 else
2518 DECL_STATIC_CHAIN (decl) = 1;
2519 chain_count += DECL_STATIC_CHAIN (decl);
2522 /* Walk the functions and perform transformations. Note that these
2523 transformations can induce new uses of the static chain, which in turn
2524 require re-examining all users of the decl. */
2525 /* ??? It would make sense to try to use the call graph to speed this up,
2526 but the call graph hasn't really been built yet. Even if it did, we
2527 would still need to iterate in this loop since address-of references
2528 wouldn't show up in the callgraph anyway. */
2529 iter_count = 0;
2532 old_chain_count = chain_count;
2533 chain_count = 0;
2534 iter_count++;
2536 if (dump_file && (dump_flags & TDF_DETAILS))
2537 fputc ('\n', dump_file);
2539 FOR_EACH_NEST_INFO (n, root)
2541 tree decl = n->context;
2542 walk_function (convert_tramp_reference_stmt,
2543 convert_tramp_reference_op, n);
2544 walk_function (convert_gimple_call, NULL, n);
2545 chain_count += DECL_STATIC_CHAIN (decl);
2548 while (chain_count != old_chain_count);
2550 if (dump_file && (dump_flags & TDF_DETAILS))
2551 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2552 iter_count);
2555 struct nesting_copy_body_data
2557 copy_body_data cb;
2558 struct nesting_info *root;
2561 /* A helper subroutine for debug_var_chain type remapping. */
2563 static tree
2564 nesting_copy_decl (tree decl, copy_body_data *id)
2566 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2567 tree *slot = nid->root->var_map->get (decl);
2569 if (slot)
2570 return (tree) *slot;
2572 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2574 tree new_decl = copy_decl_no_change (decl, id);
2575 DECL_ORIGINAL_TYPE (new_decl)
2576 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2577 return new_decl;
2580 if (TREE_CODE (decl) == VAR_DECL
2581 || TREE_CODE (decl) == PARM_DECL
2582 || TREE_CODE (decl) == RESULT_DECL)
2583 return decl;
2585 return copy_decl_no_change (decl, id);
2588 /* A helper function for remap_vla_decls. See if *TP contains
2589 some remapped variables. */
2591 static tree
2592 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2594 struct nesting_info *root = (struct nesting_info *) data;
2595 tree t = *tp;
2597 if (DECL_P (t))
2599 *walk_subtrees = 0;
2600 tree *slot = root->var_map->get (t);
2602 if (slot)
2603 return *slot;
2605 return NULL;
2608 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2609 involved. */
2611 static void
2612 remap_vla_decls (tree block, struct nesting_info *root)
2614 tree var, subblock, val, type;
2615 struct nesting_copy_body_data id;
2617 for (subblock = BLOCK_SUBBLOCKS (block);
2618 subblock;
2619 subblock = BLOCK_CHAIN (subblock))
2620 remap_vla_decls (subblock, root);
2622 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2623 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2625 val = DECL_VALUE_EXPR (var);
2626 type = TREE_TYPE (var);
2628 if (!(TREE_CODE (val) == INDIRECT_REF
2629 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2630 && variably_modified_type_p (type, NULL)))
2631 continue;
2633 if (root->var_map->get (TREE_OPERAND (val, 0))
2634 || walk_tree (&type, contains_remapped_vars, root, NULL))
2635 break;
2638 if (var == NULL_TREE)
2639 return;
2641 memset (&id, 0, sizeof (id));
2642 id.cb.copy_decl = nesting_copy_decl;
2643 id.cb.decl_map = new hash_map<tree, tree>;
2644 id.root = root;
2646 for (; var; var = DECL_CHAIN (var))
2647 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2649 struct nesting_info *i;
2650 tree newt, context;
2652 val = DECL_VALUE_EXPR (var);
2653 type = TREE_TYPE (var);
2655 if (!(TREE_CODE (val) == INDIRECT_REF
2656 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2657 && variably_modified_type_p (type, NULL)))
2658 continue;
2660 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2661 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2662 continue;
2664 context = decl_function_context (var);
2665 for (i = root; i; i = i->outer)
2666 if (i->context == context)
2667 break;
2669 if (i == NULL)
2670 continue;
2672 /* Fully expand value expressions. This avoids having debug variables
2673 only referenced from them and that can be swept during GC. */
2674 if (slot)
2676 tree t = (tree) *slot;
2677 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2678 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2681 id.cb.src_fn = i->context;
2682 id.cb.dst_fn = i->context;
2683 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2685 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2686 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2688 newt = TREE_TYPE (newt);
2689 type = TREE_TYPE (type);
2691 if (TYPE_NAME (newt)
2692 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2693 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2694 && newt != type
2695 && TYPE_NAME (newt) == TYPE_NAME (type))
2696 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2698 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2699 if (val != DECL_VALUE_EXPR (var))
2700 SET_DECL_VALUE_EXPR (var, val);
2703 delete id.cb.decl_map;
2706 /* Fold the MEM_REF *E. */
2707 bool
2708 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2710 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2711 *ref_p = fold (*ref_p);
2712 return true;
2715 /* Do "everything else" to clean up or complete state collected by the
2716 various walking passes -- lay out the types and decls, generate code
2717 to initialize the frame decl, store critical expressions in the
2718 struct function for rtl to find. */
2720 static void
2721 finalize_nesting_tree_1 (struct nesting_info *root)
2723 gimple_seq stmt_list;
2724 gimple *stmt;
2725 tree context = root->context;
2726 struct function *sf;
2728 stmt_list = NULL;
2730 /* If we created a non-local frame type or decl, we need to lay them
2731 out at this time. */
2732 if (root->frame_type)
2734 /* In some cases the frame type will trigger the -Wpadded warning.
2735 This is not helpful; suppress it. */
2736 int save_warn_padded = warn_padded;
2737 tree *adjust;
2739 warn_padded = 0;
2740 layout_type (root->frame_type);
2741 warn_padded = save_warn_padded;
2742 layout_decl (root->frame_decl, 0);
2744 /* Remove root->frame_decl from root->new_local_var_chain, so
2745 that we can declare it also in the lexical blocks, which
2746 helps ensure virtual regs that end up appearing in its RTL
2747 expression get substituted in instantiate_virtual_regs(). */
2748 for (adjust = &root->new_local_var_chain;
2749 *adjust != root->frame_decl;
2750 adjust = &DECL_CHAIN (*adjust))
2751 gcc_assert (DECL_CHAIN (*adjust));
2752 *adjust = DECL_CHAIN (*adjust);
2754 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2755 declare_vars (root->frame_decl,
2756 gimple_seq_first_stmt (gimple_body (context)), true);
2759 /* If any parameters were referenced non-locally, then we need to
2760 insert a copy. Likewise, if any variables were referenced by
2761 pointer, we need to initialize the address. */
2762 if (root->any_parm_remapped)
2764 tree p;
2765 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2767 tree field, x, y;
2769 field = lookup_field_for_decl (root, p, NO_INSERT);
2770 if (!field)
2771 continue;
2773 if (use_pointer_in_frame (p))
2774 x = build_addr (p, context);
2775 else
2776 x = p;
2778 /* If the assignment is from a non-register the stmt is
2779 not valid gimple. Make it so by using a temporary instead. */
2780 if (!is_gimple_reg (x)
2781 && is_gimple_reg_type (TREE_TYPE (x)))
2783 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2784 x = init_tmp_var (root, x, &gsi);
2787 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2788 root->frame_decl, field, NULL_TREE);
2789 stmt = gimple_build_assign (y, x);
2790 gimple_seq_add_stmt (&stmt_list, stmt);
2794 /* If a chain_field was created, then it needs to be initialized
2795 from chain_decl. */
2796 if (root->chain_field)
2798 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2799 root->frame_decl, root->chain_field, NULL_TREE);
2800 stmt = gimple_build_assign (x, get_chain_decl (root));
2801 gimple_seq_add_stmt (&stmt_list, stmt);
2804 /* If trampolines were created, then we need to initialize them. */
2805 if (root->any_tramp_created)
2807 struct nesting_info *i;
2808 for (i = root->inner; i ; i = i->next)
2810 tree arg1, arg2, arg3, x, field;
2812 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2813 if (!field)
2814 continue;
2816 gcc_assert (DECL_STATIC_CHAIN (i->context));
2817 arg3 = build_addr (root->frame_decl, context);
2819 arg2 = build_addr (i->context, context);
2821 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2822 root->frame_decl, field, NULL_TREE);
2823 arg1 = build_addr (x, context);
2825 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2826 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2827 gimple_seq_add_stmt (&stmt_list, stmt);
2831 /* If we created initialization statements, insert them. */
2832 if (stmt_list)
2834 gbind *bind;
2835 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2836 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2837 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2838 gimple_bind_set_body (bind, stmt_list);
2841 /* If a chain_decl was created, then it needs to be registered with
2842 struct function so that it gets initialized from the static chain
2843 register at the beginning of the function. */
2844 sf = DECL_STRUCT_FUNCTION (root->context);
2845 sf->static_chain_decl = root->chain_decl;
2847 /* Similarly for the non-local goto save area. */
2848 if (root->nl_goto_field)
2850 sf->nonlocal_goto_save_area
2851 = get_frame_field (root, context, root->nl_goto_field, NULL);
2852 sf->has_nonlocal_label = 1;
2855 /* Make sure all new local variables get inserted into the
2856 proper BIND_EXPR. */
2857 if (root->new_local_var_chain)
2858 declare_vars (root->new_local_var_chain,
2859 gimple_seq_first_stmt (gimple_body (root->context)),
2860 false);
2862 if (root->debug_var_chain)
2864 tree debug_var;
2865 gbind *scope;
2867 remap_vla_decls (DECL_INITIAL (root->context), root);
2869 for (debug_var = root->debug_var_chain; debug_var;
2870 debug_var = DECL_CHAIN (debug_var))
2871 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2872 break;
2874 /* If there are any debug decls with variable length types,
2875 remap those types using other debug_var_chain variables. */
2876 if (debug_var)
2878 struct nesting_copy_body_data id;
2880 memset (&id, 0, sizeof (id));
2881 id.cb.copy_decl = nesting_copy_decl;
2882 id.cb.decl_map = new hash_map<tree, tree>;
2883 id.root = root;
2885 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2886 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2888 tree type = TREE_TYPE (debug_var);
2889 tree newt, t = type;
2890 struct nesting_info *i;
2892 for (i = root; i; i = i->outer)
2893 if (variably_modified_type_p (type, i->context))
2894 break;
2896 if (i == NULL)
2897 continue;
2899 id.cb.src_fn = i->context;
2900 id.cb.dst_fn = i->context;
2901 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2903 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2904 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2906 newt = TREE_TYPE (newt);
2907 t = TREE_TYPE (t);
2909 if (TYPE_NAME (newt)
2910 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2911 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2912 && newt != t
2913 && TYPE_NAME (newt) == TYPE_NAME (t))
2914 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2917 delete id.cb.decl_map;
2920 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
2921 if (gimple_bind_block (scope))
2922 declare_vars (root->debug_var_chain, scope, true);
2923 else
2924 BLOCK_VARS (DECL_INITIAL (root->context))
2925 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2926 root->debug_var_chain);
2929 /* Fold the rewritten MEM_REF trees. */
2930 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2932 /* Dump the translated tree function. */
2933 if (dump_file)
2935 fputs ("\n\n", dump_file);
2936 dump_function_to_file (root->context, dump_file, dump_flags);
2940 static void
2941 finalize_nesting_tree (struct nesting_info *root)
2943 struct nesting_info *n;
2944 FOR_EACH_NEST_INFO (n, root)
2945 finalize_nesting_tree_1 (n);
2948 /* Unnest the nodes and pass them to cgraph. */
2950 static void
2951 unnest_nesting_tree_1 (struct nesting_info *root)
2953 struct cgraph_node *node = cgraph_node::get (root->context);
2955 /* For nested functions update the cgraph to reflect unnesting.
2956 We also delay finalizing of these functions up to this point. */
2957 if (node->origin)
2959 node->unnest ();
2960 cgraph_node::finalize_function (root->context, true);
2964 static void
2965 unnest_nesting_tree (struct nesting_info *root)
2967 struct nesting_info *n;
2968 FOR_EACH_NEST_INFO (n, root)
2969 unnest_nesting_tree_1 (n);
2972 /* Free the data structures allocated during this pass. */
2974 static void
2975 free_nesting_tree (struct nesting_info *root)
2977 struct nesting_info *node, *next;
2979 node = iter_nestinfo_start (root);
2982 next = iter_nestinfo_next (node);
2983 delete node->var_map;
2984 delete node->field_map;
2985 delete node->mem_refs;
2986 free (node);
2987 node = next;
2989 while (node);
2992 /* Gimplify a function and all its nested functions. */
2993 static void
2994 gimplify_all_functions (struct cgraph_node *root)
2996 struct cgraph_node *iter;
2997 if (!gimple_body (root->decl))
2998 gimplify_function_tree (root->decl);
2999 for (iter = root->nested; iter; iter = iter->next_nested)
3000 gimplify_all_functions (iter);
3003 /* Main entry point for this pass. Process FNDECL and all of its nested
3004 subroutines and turn them into something less tightly bound. */
3006 void
3007 lower_nested_functions (tree fndecl)
3009 struct cgraph_node *cgn;
3010 struct nesting_info *root;
3012 /* If there are no nested functions, there's nothing to do. */
3013 cgn = cgraph_node::get (fndecl);
3014 if (!cgn->nested)
3015 return;
3017 gimplify_all_functions (cgn);
3019 dump_file = dump_begin (TDI_nested, &dump_flags);
3020 if (dump_file)
3021 fprintf (dump_file, "\n;; Function %s\n\n",
3022 lang_hooks.decl_printable_name (fndecl, 2));
3024 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3025 root = create_nesting_tree (cgn);
3027 walk_all_functions (convert_nonlocal_reference_stmt,
3028 convert_nonlocal_reference_op,
3029 root);
3030 walk_all_functions (convert_local_reference_stmt,
3031 convert_local_reference_op,
3032 root);
3033 walk_all_functions (convert_nl_goto_reference, NULL, root);
3034 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3036 convert_all_function_calls (root);
3037 finalize_nesting_tree (root);
3038 unnest_nesting_tree (root);
3040 free_nesting_tree (root);
3041 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3043 if (dump_file)
3045 dump_end (TDI_nested, dump_file);
3046 dump_file = NULL;
3050 #include "gt-tree-nested.h"