* omp-low.c (MASK_GANG, MASK_WORKER, MASK_VECTOR): Delete.
[official-gcc.git] / gcc / tree-nested.c
blobc04c42956cabf8d8e8a593e92c969e93ee20004a
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)
185 mark_addressable (exp);
186 return build_fold_addr_expr (exp);
189 /* Insert FIELD into TYPE, sorted by alignment requirements. */
191 void
192 insert_field_into_struct (tree type, tree field)
194 tree *p;
196 DECL_CONTEXT (field) = type;
198 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
199 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
200 break;
202 DECL_CHAIN (field) = *p;
203 *p = field;
205 /* Set correct alignment for frame struct type. */
206 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
207 TYPE_ALIGN (type) = DECL_ALIGN (field);
210 /* Build or return the RECORD_TYPE that describes the frame state that is
211 shared between INFO->CONTEXT and its nested functions. This record will
212 not be complete until finalize_nesting_tree; up until that point we'll
213 be adding fields as necessary.
215 We also build the DECL that represents this frame in the function. */
217 static tree
218 get_frame_type (struct nesting_info *info)
220 tree type = info->frame_type;
221 if (!type)
223 char *name;
225 type = make_node (RECORD_TYPE);
227 name = concat ("FRAME.",
228 IDENTIFIER_POINTER (DECL_NAME (info->context)),
229 NULL);
230 TYPE_NAME (type) = get_identifier (name);
231 free (name);
233 info->frame_type = type;
234 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
235 DECL_NONLOCAL_FRAME (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;
245 return type;
248 /* Return true if DECL should be referenced by pointer in the non-local
249 frame structure. */
251 static bool
252 use_pointer_in_frame (tree decl)
254 if (TREE_CODE (decl) == PARM_DECL)
256 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
257 sized decls, and inefficient to copy large aggregates. Don't bother
258 moving anything but scalar variables. */
259 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
261 else
263 /* Variable sized types make things "interesting" in the frame. */
264 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
268 /* Given DECL, a non-locally accessed variable, find or create a field
269 in the non-local frame structure for the given nesting context. */
271 static tree
272 lookup_field_for_decl (struct nesting_info *info, tree decl,
273 enum insert_option insert)
275 if (insert == NO_INSERT)
277 tree *slot = info->field_map->get (decl);
278 return slot ? *slot : NULL_TREE;
281 tree *slot = &info->field_map->get_or_insert (decl);
282 if (!*slot)
284 tree field = make_node (FIELD_DECL);
285 DECL_NAME (field) = DECL_NAME (decl);
287 if (use_pointer_in_frame (decl))
289 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
290 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
291 DECL_NONADDRESSABLE_P (field) = 1;
293 else
295 TREE_TYPE (field) = TREE_TYPE (decl);
296 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
297 DECL_ALIGN (field) = DECL_ALIGN (decl);
298 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
299 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
300 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
301 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
304 insert_field_into_struct (get_frame_type (info), field);
305 *slot = field;
307 if (TREE_CODE (decl) == PARM_DECL)
308 info->any_parm_remapped = true;
311 return *slot;
314 /* Build or return the variable that holds the static chain within
315 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
317 static tree
318 get_chain_decl (struct nesting_info *info)
320 tree decl = info->chain_decl;
322 if (!decl)
324 tree type;
326 type = get_frame_type (info->outer);
327 type = build_pointer_type (type);
329 /* Note that this variable is *not* entered into any BIND_EXPR;
330 the construction of this variable is handled specially in
331 expand_function_start and initialize_inlined_parameters.
332 Note also that it's represented as a parameter. This is more
333 close to the truth, since the initial value does come from
334 the caller. */
335 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
336 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
337 DECL_ARTIFICIAL (decl) = 1;
338 DECL_IGNORED_P (decl) = 1;
339 TREE_USED (decl) = 1;
340 DECL_CONTEXT (decl) = info->context;
341 DECL_ARG_TYPE (decl) = type;
343 /* Tell tree-inline.c that we never write to this variable, so
344 it can copy-prop the replacement value immediately. */
345 TREE_READONLY (decl) = 1;
347 info->chain_decl = decl;
349 if (dump_file
350 && (dump_flags & TDF_DETAILS)
351 && !DECL_STATIC_CHAIN (info->context))
352 fprintf (dump_file, "Setting static-chain for %s\n",
353 lang_hooks.decl_printable_name (info->context, 2));
355 DECL_STATIC_CHAIN (info->context) = 1;
357 return decl;
360 /* Build or return the field within the non-local frame state that holds
361 the static chain for INFO->CONTEXT. This is the way to walk back up
362 multiple nesting levels. */
364 static tree
365 get_chain_field (struct nesting_info *info)
367 tree field = info->chain_field;
369 if (!field)
371 tree type = build_pointer_type (get_frame_type (info->outer));
373 field = make_node (FIELD_DECL);
374 DECL_NAME (field) = get_identifier ("__chain");
375 TREE_TYPE (field) = type;
376 DECL_ALIGN (field) = TYPE_ALIGN (type);
377 DECL_NONADDRESSABLE_P (field) = 1;
379 insert_field_into_struct (get_frame_type (info), field);
381 info->chain_field = field;
383 if (dump_file
384 && (dump_flags & TDF_DETAILS)
385 && !DECL_STATIC_CHAIN (info->context))
386 fprintf (dump_file, "Setting static-chain for %s\n",
387 lang_hooks.decl_printable_name (info->context, 2));
389 DECL_STATIC_CHAIN (info->context) = 1;
391 return field;
394 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
396 static tree
397 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
398 gcall *call)
400 tree t;
402 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
403 gimple_call_set_lhs (call, t);
404 if (! gsi_end_p (*gsi))
405 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
406 gsi_insert_before (gsi, call, GSI_SAME_STMT);
408 return t;
412 /* Copy EXP into a temporary. Allocate the temporary in the context of
413 INFO and insert the initialization statement before GSI. */
415 static tree
416 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
418 tree t;
419 gimple *stmt;
421 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
422 stmt = gimple_build_assign (t, exp);
423 if (! gsi_end_p (*gsi))
424 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
425 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
427 return t;
431 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
433 static tree
434 gsi_gimplify_val (struct nesting_info *info, tree exp,
435 gimple_stmt_iterator *gsi)
437 if (is_gimple_val (exp))
438 return exp;
439 else
440 return init_tmp_var (info, exp, gsi);
443 /* Similarly, but copy from the temporary and insert the statement
444 after the iterator. */
446 static tree
447 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
449 tree t;
450 gimple *stmt;
452 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
453 stmt = gimple_build_assign (exp, t);
454 if (! gsi_end_p (*gsi))
455 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
456 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
458 return t;
461 /* Build or return the type used to represent a nested function trampoline. */
463 static GTY(()) tree trampoline_type;
465 static tree
466 get_trampoline_type (struct nesting_info *info)
468 unsigned align, size;
469 tree t;
471 if (trampoline_type)
472 return trampoline_type;
474 align = TRAMPOLINE_ALIGNMENT;
475 size = TRAMPOLINE_SIZE;
477 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
478 then allocate extra space so that we can do dynamic alignment. */
479 if (align > STACK_BOUNDARY)
481 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
482 align = STACK_BOUNDARY;
485 t = build_index_type (size_int (size - 1));
486 t = build_array_type (char_type_node, t);
487 t = build_decl (DECL_SOURCE_LOCATION (info->context),
488 FIELD_DECL, get_identifier ("__data"), t);
489 DECL_ALIGN (t) = align;
490 DECL_USER_ALIGN (t) = 1;
492 trampoline_type = make_node (RECORD_TYPE);
493 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
494 TYPE_FIELDS (trampoline_type) = t;
495 layout_type (trampoline_type);
496 DECL_CONTEXT (t) = trampoline_type;
498 return trampoline_type;
501 /* Given DECL, a nested function, find or create a field in the non-local
502 frame structure for a trampoline for this function. */
504 static tree
505 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
506 enum insert_option insert)
508 if (insert == NO_INSERT)
510 tree *slot = info->var_map->get (decl);
511 return slot ? *slot : NULL_TREE;
514 tree *slot = &info->var_map->get_or_insert (decl);
515 if (!*slot)
517 tree field = make_node (FIELD_DECL);
518 DECL_NAME (field) = DECL_NAME (decl);
519 TREE_TYPE (field) = get_trampoline_type (info);
520 TREE_ADDRESSABLE (field) = 1;
522 insert_field_into_struct (get_frame_type (info), field);
523 *slot = field;
525 info->any_tramp_created = true;
528 return *slot;
531 /* Build or return the field within the non-local frame state that holds
532 the non-local goto "jmp_buf". The buffer itself is maintained by the
533 rtl middle-end as dynamic stack space is allocated. */
535 static tree
536 get_nl_goto_field (struct nesting_info *info)
538 tree field = info->nl_goto_field;
539 if (!field)
541 unsigned size;
542 tree type;
544 /* For __builtin_nonlocal_goto, we need N words. The first is the
545 frame pointer, the rest is for the target's stack pointer save
546 area. The number of words is controlled by STACK_SAVEAREA_MODE;
547 not the best interface, but it'll do for now. */
548 if (Pmode == ptr_mode)
549 type = ptr_type_node;
550 else
551 type = lang_hooks.types.type_for_mode (Pmode, 1);
553 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
554 size = size / GET_MODE_SIZE (Pmode);
555 size = size + 1;
557 type = build_array_type
558 (type, build_index_type (size_int (size)));
560 field = make_node (FIELD_DECL);
561 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
562 TREE_TYPE (field) = type;
563 DECL_ALIGN (field) = TYPE_ALIGN (type);
564 TREE_ADDRESSABLE (field) = 1;
566 insert_field_into_struct (get_frame_type (info), field);
568 info->nl_goto_field = field;
571 return field;
574 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
576 static void
577 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
578 struct nesting_info *info, gimple_seq *pseq)
580 struct walk_stmt_info wi;
582 memset (&wi, 0, sizeof (wi));
583 wi.info = info;
584 wi.val_only = true;
585 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
589 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
591 static inline void
592 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
593 struct nesting_info *info)
595 gimple_seq body = gimple_body (info->context);
596 walk_body (callback_stmt, callback_op, info, &body);
597 gimple_set_body (info->context, body);
600 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
602 static void
603 walk_gimple_omp_for (gomp_for *for_stmt,
604 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
605 struct nesting_info *info)
607 struct walk_stmt_info wi;
608 gimple_seq seq;
609 tree t;
610 size_t i;
612 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
614 seq = NULL;
615 memset (&wi, 0, sizeof (wi));
616 wi.info = info;
617 wi.gsi = gsi_last (seq);
619 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
621 wi.val_only = false;
622 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
623 &wi, NULL);
624 wi.val_only = true;
625 wi.is_lhs = false;
626 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
627 &wi, NULL);
629 wi.val_only = true;
630 wi.is_lhs = false;
631 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
632 &wi, NULL);
634 t = gimple_omp_for_incr (for_stmt, i);
635 gcc_assert (BINARY_CLASS_P (t));
636 wi.val_only = false;
637 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
638 wi.val_only = true;
639 wi.is_lhs = false;
640 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
643 seq = gsi_seq (wi.gsi);
644 if (!gimple_seq_empty_p (seq))
646 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
647 annotate_all_with_location (seq, gimple_location (for_stmt));
648 gimple_seq_add_seq (&pre_body, seq);
649 gimple_omp_for_set_pre_body (for_stmt, pre_body);
653 /* Similarly for ROOT and all functions nested underneath, depth first. */
655 static void
656 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
657 struct nesting_info *root)
659 struct nesting_info *n;
660 FOR_EACH_NEST_INFO (n, root)
661 walk_function (callback_stmt, callback_op, n);
665 /* We have to check for a fairly pathological case. The operands of function
666 nested function are to be interpreted in the context of the enclosing
667 function. So if any are variably-sized, they will get remapped when the
668 enclosing function is inlined. But that remapping would also have to be
669 done in the types of the PARM_DECLs of the nested function, meaning the
670 argument types of that function will disagree with the arguments in the
671 calls to that function. So we'd either have to make a copy of the nested
672 function corresponding to each time the enclosing function was inlined or
673 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
674 function. The former is not practical. The latter would still require
675 detecting this case to know when to add the conversions. So, for now at
676 least, we don't inline such an enclosing function.
678 We have to do that check recursively, so here return indicating whether
679 FNDECL has such a nested function. ORIG_FN is the function we were
680 trying to inline to use for checking whether any argument is variably
681 modified by anything in it.
683 It would be better to do this in tree-inline.c so that we could give
684 the appropriate warning for why a function can't be inlined, but that's
685 too late since the nesting structure has already been flattened and
686 adding a flag just to record this fact seems a waste of a flag. */
688 static bool
689 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
691 struct cgraph_node *cgn = cgraph_node::get (fndecl);
692 tree arg;
694 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
696 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
697 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
698 return true;
700 if (check_for_nested_with_variably_modified (cgn->decl,
701 orig_fndecl))
702 return true;
705 return false;
708 /* Construct our local datastructure describing the function nesting
709 tree rooted by CGN. */
711 static struct nesting_info *
712 create_nesting_tree (struct cgraph_node *cgn)
714 struct nesting_info *info = XCNEW (struct nesting_info);
715 info->field_map = new hash_map<tree, tree>;
716 info->var_map = new hash_map<tree, tree>;
717 info->mem_refs = new hash_set<tree *>;
718 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
719 info->context = cgn->decl;
721 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
723 struct nesting_info *sub = create_nesting_tree (cgn);
724 sub->outer = info;
725 sub->next = info->inner;
726 info->inner = sub;
729 /* See discussion at check_for_nested_with_variably_modified for a
730 discussion of why this has to be here. */
731 if (check_for_nested_with_variably_modified (info->context, info->context))
732 DECL_UNINLINABLE (info->context) = true;
734 return info;
737 /* Return an expression computing the static chain for TARGET_CONTEXT
738 from INFO->CONTEXT. Insert any necessary computations before TSI. */
740 static tree
741 get_static_chain (struct nesting_info *info, tree target_context,
742 gimple_stmt_iterator *gsi)
744 struct nesting_info *i;
745 tree x;
747 if (info->context == target_context)
749 x = build_addr (info->frame_decl);
750 info->static_chain_added |= 1;
752 else
754 x = get_chain_decl (info);
755 info->static_chain_added |= 2;
757 for (i = info->outer; i->context != target_context; i = i->outer)
759 tree field = get_chain_field (i);
761 x = build_simple_mem_ref (x);
762 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
763 x = init_tmp_var (info, x, gsi);
767 return x;
771 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
772 frame as seen from INFO->CONTEXT. Insert any necessary computations
773 before GSI. */
775 static tree
776 get_frame_field (struct nesting_info *info, tree target_context,
777 tree field, gimple_stmt_iterator *gsi)
779 struct nesting_info *i;
780 tree x;
782 if (info->context == target_context)
784 /* Make sure frame_decl gets created. */
785 (void) get_frame_type (info);
786 x = info->frame_decl;
787 info->static_chain_added |= 1;
789 else
791 x = get_chain_decl (info);
792 info->static_chain_added |= 2;
794 for (i = info->outer; i->context != target_context; i = i->outer)
796 tree field = get_chain_field (i);
798 x = build_simple_mem_ref (x);
799 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
800 x = init_tmp_var (info, x, gsi);
803 x = build_simple_mem_ref (x);
806 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
807 return x;
810 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
812 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
813 in the nested function with DECL_VALUE_EXPR set to reference the true
814 variable in the parent function. This is used both for debug info
815 and in OMP lowering. */
817 static tree
818 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
820 tree target_context;
821 struct nesting_info *i;
822 tree x, field, new_decl;
824 tree *slot = &info->var_map->get_or_insert (decl);
826 if (*slot)
827 return *slot;
829 target_context = decl_function_context (decl);
831 /* A copy of the code in get_frame_field, but without the temporaries. */
832 if (info->context == target_context)
834 /* Make sure frame_decl gets created. */
835 (void) get_frame_type (info);
836 x = info->frame_decl;
837 i = info;
838 info->static_chain_added |= 1;
840 else
842 x = get_chain_decl (info);
843 info->static_chain_added |= 2;
844 for (i = info->outer; i->context != target_context; i = i->outer)
846 field = get_chain_field (i);
847 x = build_simple_mem_ref (x);
848 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
850 x = build_simple_mem_ref (x);
853 field = lookup_field_for_decl (i, decl, INSERT);
854 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
855 if (use_pointer_in_frame (decl))
856 x = build_simple_mem_ref (x);
858 /* ??? We should be remapping types as well, surely. */
859 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
860 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
861 DECL_CONTEXT (new_decl) = info->context;
862 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
863 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
864 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
865 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
866 TREE_READONLY (new_decl) = TREE_READONLY (decl);
867 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
868 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
869 if ((TREE_CODE (decl) == PARM_DECL
870 || TREE_CODE (decl) == RESULT_DECL
871 || TREE_CODE (decl) == VAR_DECL)
872 && DECL_BY_REFERENCE (decl))
873 DECL_BY_REFERENCE (new_decl) = 1;
875 SET_DECL_VALUE_EXPR (new_decl, x);
876 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
878 *slot = new_decl;
879 DECL_CHAIN (new_decl) = info->debug_var_chain;
880 info->debug_var_chain = new_decl;
882 if (!optimize
883 && info->context != target_context
884 && variably_modified_type_p (TREE_TYPE (decl), NULL))
885 note_nonlocal_vla_type (info, TREE_TYPE (decl));
887 return new_decl;
891 /* Callback for walk_gimple_stmt, rewrite all references to VAR
892 and PARM_DECLs that belong to outer functions.
894 The rewrite will involve some number of structure accesses back up
895 the static chain. E.g. for a variable FOO up one nesting level it'll
896 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
897 indirections apply to decls for which use_pointer_in_frame is true. */
899 static tree
900 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
902 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
903 struct nesting_info *const info = (struct nesting_info *) wi->info;
904 tree t = *tp;
906 *walk_subtrees = 0;
907 switch (TREE_CODE (t))
909 case VAR_DECL:
910 /* Non-automatic variables are never processed. */
911 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
912 break;
913 /* FALLTHRU */
915 case PARM_DECL:
916 if (decl_function_context (t) != info->context)
918 tree x;
919 wi->changed = true;
921 x = get_nonlocal_debug_decl (info, t);
922 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
924 tree target_context = decl_function_context (t);
925 struct nesting_info *i;
926 for (i = info->outer; i->context != target_context; i = i->outer)
927 continue;
928 x = lookup_field_for_decl (i, t, INSERT);
929 x = get_frame_field (info, target_context, x, &wi->gsi);
930 if (use_pointer_in_frame (t))
932 x = init_tmp_var (info, x, &wi->gsi);
933 x = build_simple_mem_ref (x);
937 if (wi->val_only)
939 if (wi->is_lhs)
940 x = save_tmp_var (info, x, &wi->gsi);
941 else
942 x = init_tmp_var (info, x, &wi->gsi);
945 *tp = x;
947 break;
949 case LABEL_DECL:
950 /* We're taking the address of a label from a parent function, but
951 this is not itself a non-local goto. Mark the label such that it
952 will not be deleted, much as we would with a label address in
953 static storage. */
954 if (decl_function_context (t) != info->context)
955 FORCED_LABEL (t) = 1;
956 break;
958 case ADDR_EXPR:
960 bool save_val_only = wi->val_only;
962 wi->val_only = false;
963 wi->is_lhs = false;
964 wi->changed = false;
965 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
966 wi->val_only = true;
968 if (wi->changed)
970 tree save_context;
972 /* If we changed anything, we might no longer be directly
973 referencing a decl. */
974 save_context = current_function_decl;
975 current_function_decl = info->context;
976 recompute_tree_invariant_for_addr_expr (t);
977 current_function_decl = save_context;
979 /* If the callback converted the address argument in a context
980 where we only accept variables (and min_invariant, presumably),
981 then compute the address into a temporary. */
982 if (save_val_only)
983 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
984 t, &wi->gsi);
987 break;
989 case REALPART_EXPR:
990 case IMAGPART_EXPR:
991 case COMPONENT_REF:
992 case ARRAY_REF:
993 case ARRAY_RANGE_REF:
994 case BIT_FIELD_REF:
995 /* Go down this entire nest and just look at the final prefix and
996 anything that describes the references. Otherwise, we lose track
997 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
998 wi->val_only = true;
999 wi->is_lhs = false;
1000 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1002 if (TREE_CODE (t) == COMPONENT_REF)
1003 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1004 NULL);
1005 else if (TREE_CODE (t) == ARRAY_REF
1006 || TREE_CODE (t) == ARRAY_RANGE_REF)
1008 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1009 wi, NULL);
1010 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1011 wi, NULL);
1012 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1013 wi, NULL);
1016 wi->val_only = false;
1017 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1018 break;
1020 case VIEW_CONVERT_EXPR:
1021 /* Just request to look at the subtrees, leaving val_only and lhs
1022 untouched. This might actually be for !val_only + lhs, in which
1023 case we don't want to force a replacement by a temporary. */
1024 *walk_subtrees = 1;
1025 break;
1027 default:
1028 if (!IS_TYPE_OR_DECL_P (t))
1030 *walk_subtrees = 1;
1031 wi->val_only = true;
1032 wi->is_lhs = false;
1034 break;
1037 return NULL_TREE;
1040 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1041 struct walk_stmt_info *);
1043 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1044 and PARM_DECLs that belong to outer functions. */
1046 static bool
1047 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1049 struct nesting_info *const info = (struct nesting_info *) wi->info;
1050 bool need_chain = false, need_stmts = false;
1051 tree clause, decl;
1052 int dummy;
1053 bitmap new_suppress;
1055 new_suppress = BITMAP_GGC_ALLOC ();
1056 bitmap_copy (new_suppress, info->suppress_expansion);
1058 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1060 switch (OMP_CLAUSE_CODE (clause))
1062 case OMP_CLAUSE_REDUCTION:
1063 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1064 need_stmts = true;
1065 goto do_decl_clause;
1067 case OMP_CLAUSE_LASTPRIVATE:
1068 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1069 need_stmts = true;
1070 goto do_decl_clause;
1072 case OMP_CLAUSE_LINEAR:
1073 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1074 need_stmts = true;
1075 wi->val_only = true;
1076 wi->is_lhs = false;
1077 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1078 &dummy, wi);
1079 goto do_decl_clause;
1081 case OMP_CLAUSE_PRIVATE:
1082 case OMP_CLAUSE_FIRSTPRIVATE:
1083 case OMP_CLAUSE_COPYPRIVATE:
1084 case OMP_CLAUSE_SHARED:
1085 case OMP_CLAUSE_TO_DECLARE:
1086 case OMP_CLAUSE_LINK:
1087 case OMP_CLAUSE_USE_DEVICE_PTR:
1088 case OMP_CLAUSE_IS_DEVICE_PTR:
1089 do_decl_clause:
1090 decl = OMP_CLAUSE_DECL (clause);
1091 if (TREE_CODE (decl) == VAR_DECL
1092 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1093 break;
1094 if (decl_function_context (decl) != info->context)
1096 bitmap_set_bit (new_suppress, DECL_UID (decl));
1097 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1098 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1099 need_chain = true;
1101 break;
1103 case OMP_CLAUSE_SCHEDULE:
1104 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1105 break;
1106 /* FALLTHRU */
1107 case OMP_CLAUSE_FINAL:
1108 case OMP_CLAUSE_IF:
1109 case OMP_CLAUSE_NUM_THREADS:
1110 case OMP_CLAUSE_DEPEND:
1111 case OMP_CLAUSE_DEVICE:
1112 case OMP_CLAUSE_NUM_TEAMS:
1113 case OMP_CLAUSE_THREAD_LIMIT:
1114 case OMP_CLAUSE_SAFELEN:
1115 case OMP_CLAUSE_SIMDLEN:
1116 case OMP_CLAUSE_PRIORITY:
1117 case OMP_CLAUSE_GRAINSIZE:
1118 case OMP_CLAUSE_NUM_TASKS:
1119 case OMP_CLAUSE_HINT:
1120 case OMP_CLAUSE__CILK_FOR_COUNT_:
1121 wi->val_only = true;
1122 wi->is_lhs = false;
1123 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1124 &dummy, wi);
1125 break;
1127 case OMP_CLAUSE_DIST_SCHEDULE:
1128 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1130 wi->val_only = true;
1131 wi->is_lhs = false;
1132 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1133 &dummy, wi);
1135 break;
1137 case OMP_CLAUSE_MAP:
1138 case OMP_CLAUSE_TO:
1139 case OMP_CLAUSE_FROM:
1140 if (OMP_CLAUSE_SIZE (clause))
1142 wi->val_only = true;
1143 wi->is_lhs = false;
1144 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1145 &dummy, wi);
1147 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1148 goto do_decl_clause;
1149 wi->val_only = true;
1150 wi->is_lhs = false;
1151 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1152 wi, NULL);
1153 break;
1155 case OMP_CLAUSE_ALIGNED:
1156 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1158 wi->val_only = true;
1159 wi->is_lhs = false;
1160 convert_nonlocal_reference_op
1161 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1163 /* Like do_decl_clause, but don't add any suppression. */
1164 decl = OMP_CLAUSE_DECL (clause);
1165 if (TREE_CODE (decl) == VAR_DECL
1166 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1167 break;
1168 if (decl_function_context (decl) != info->context)
1170 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1171 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1172 need_chain = true;
1174 break;
1176 case OMP_CLAUSE_NOWAIT:
1177 case OMP_CLAUSE_ORDERED:
1178 case OMP_CLAUSE_DEFAULT:
1179 case OMP_CLAUSE_COPYIN:
1180 case OMP_CLAUSE_COLLAPSE:
1181 case OMP_CLAUSE_UNTIED:
1182 case OMP_CLAUSE_MERGEABLE:
1183 case OMP_CLAUSE_PROC_BIND:
1184 case OMP_CLAUSE_NOGROUP:
1185 case OMP_CLAUSE_THREADS:
1186 case OMP_CLAUSE_SIMD:
1187 case OMP_CLAUSE_DEFAULTMAP:
1188 break;
1190 default:
1191 gcc_unreachable ();
1195 info->suppress_expansion = new_suppress;
1197 if (need_stmts)
1198 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1199 switch (OMP_CLAUSE_CODE (clause))
1201 case OMP_CLAUSE_REDUCTION:
1202 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1204 tree old_context
1205 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1206 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1207 = info->context;
1208 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1209 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1210 = info->context;
1211 walk_body (convert_nonlocal_reference_stmt,
1212 convert_nonlocal_reference_op, info,
1213 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1214 walk_body (convert_nonlocal_reference_stmt,
1215 convert_nonlocal_reference_op, info,
1216 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1217 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1218 = old_context;
1219 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1220 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1221 = old_context;
1223 break;
1225 case OMP_CLAUSE_LASTPRIVATE:
1226 walk_body (convert_nonlocal_reference_stmt,
1227 convert_nonlocal_reference_op, info,
1228 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1229 break;
1231 case OMP_CLAUSE_LINEAR:
1232 walk_body (convert_nonlocal_reference_stmt,
1233 convert_nonlocal_reference_op, info,
1234 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1235 break;
1237 default:
1238 break;
1241 return need_chain;
1244 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1246 static void
1247 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1249 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1250 type = TREE_TYPE (type);
1252 if (TYPE_NAME (type)
1253 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1254 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1255 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1257 while (POINTER_TYPE_P (type)
1258 || TREE_CODE (type) == VECTOR_TYPE
1259 || TREE_CODE (type) == FUNCTION_TYPE
1260 || TREE_CODE (type) == METHOD_TYPE)
1261 type = TREE_TYPE (type);
1263 if (TREE_CODE (type) == ARRAY_TYPE)
1265 tree domain, t;
1267 note_nonlocal_vla_type (info, TREE_TYPE (type));
1268 domain = TYPE_DOMAIN (type);
1269 if (domain)
1271 t = TYPE_MIN_VALUE (domain);
1272 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1273 && decl_function_context (t) != info->context)
1274 get_nonlocal_debug_decl (info, t);
1275 t = TYPE_MAX_VALUE (domain);
1276 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1277 && decl_function_context (t) != info->context)
1278 get_nonlocal_debug_decl (info, t);
1283 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1284 in BLOCK. */
1286 static void
1287 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1289 tree var;
1291 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1292 if (TREE_CODE (var) == VAR_DECL
1293 && variably_modified_type_p (TREE_TYPE (var), NULL)
1294 && DECL_HAS_VALUE_EXPR_P (var)
1295 && decl_function_context (var) != info->context)
1296 note_nonlocal_vla_type (info, TREE_TYPE (var));
1299 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1300 PARM_DECLs that belong to outer functions. This handles statements
1301 that are not handled via the standard recursion done in
1302 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1303 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1304 operands of STMT have been handled by this function. */
1306 static tree
1307 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1308 struct walk_stmt_info *wi)
1310 struct nesting_info *info = (struct nesting_info *) wi->info;
1311 tree save_local_var_chain;
1312 bitmap save_suppress;
1313 gimple *stmt = gsi_stmt (*gsi);
1315 switch (gimple_code (stmt))
1317 case GIMPLE_GOTO:
1318 /* Don't walk non-local gotos for now. */
1319 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1321 wi->val_only = true;
1322 wi->is_lhs = false;
1323 *handled_ops_p = true;
1324 return NULL_TREE;
1326 break;
1328 case GIMPLE_OMP_PARALLEL:
1329 case GIMPLE_OMP_TASK:
1330 save_suppress = info->suppress_expansion;
1331 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1332 wi))
1334 tree c, decl;
1335 decl = get_chain_decl (info);
1336 c = build_omp_clause (gimple_location (stmt),
1337 OMP_CLAUSE_FIRSTPRIVATE);
1338 OMP_CLAUSE_DECL (c) = decl;
1339 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1340 gimple_omp_taskreg_set_clauses (stmt, c);
1343 save_local_var_chain = info->new_local_var_chain;
1344 info->new_local_var_chain = NULL;
1346 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1347 info, gimple_omp_body_ptr (stmt));
1349 if (info->new_local_var_chain)
1350 declare_vars (info->new_local_var_chain,
1351 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1352 false);
1353 info->new_local_var_chain = save_local_var_chain;
1354 info->suppress_expansion = save_suppress;
1355 break;
1357 case GIMPLE_OMP_FOR:
1358 save_suppress = info->suppress_expansion;
1359 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1360 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1361 convert_nonlocal_reference_stmt,
1362 convert_nonlocal_reference_op, info);
1363 walk_body (convert_nonlocal_reference_stmt,
1364 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1365 info->suppress_expansion = save_suppress;
1366 break;
1368 case GIMPLE_OMP_SECTIONS:
1369 save_suppress = info->suppress_expansion;
1370 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1371 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1372 info, gimple_omp_body_ptr (stmt));
1373 info->suppress_expansion = save_suppress;
1374 break;
1376 case GIMPLE_OMP_SINGLE:
1377 save_suppress = info->suppress_expansion;
1378 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1379 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1380 info, gimple_omp_body_ptr (stmt));
1381 info->suppress_expansion = save_suppress;
1382 break;
1384 case GIMPLE_OMP_TARGET:
1385 if (!is_gimple_omp_offloaded (stmt))
1387 save_suppress = info->suppress_expansion;
1388 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1389 wi);
1390 info->suppress_expansion = save_suppress;
1391 walk_body (convert_nonlocal_reference_stmt,
1392 convert_nonlocal_reference_op, info,
1393 gimple_omp_body_ptr (stmt));
1394 break;
1396 save_suppress = info->suppress_expansion;
1397 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1398 wi))
1400 tree c, decl;
1401 decl = get_chain_decl (info);
1402 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1403 OMP_CLAUSE_DECL (c) = decl;
1404 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1405 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1406 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1407 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1410 save_local_var_chain = info->new_local_var_chain;
1411 info->new_local_var_chain = NULL;
1413 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1414 info, gimple_omp_body_ptr (stmt));
1416 if (info->new_local_var_chain)
1417 declare_vars (info->new_local_var_chain,
1418 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1419 false);
1420 info->new_local_var_chain = save_local_var_chain;
1421 info->suppress_expansion = save_suppress;
1422 break;
1424 case GIMPLE_OMP_TEAMS:
1425 save_suppress = info->suppress_expansion;
1426 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1427 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1428 info, gimple_omp_body_ptr (stmt));
1429 info->suppress_expansion = save_suppress;
1430 break;
1432 case GIMPLE_OMP_SECTION:
1433 case GIMPLE_OMP_MASTER:
1434 case GIMPLE_OMP_TASKGROUP:
1435 case GIMPLE_OMP_ORDERED:
1436 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1437 info, gimple_omp_body_ptr (stmt));
1438 break;
1440 case GIMPLE_BIND:
1442 gbind *bind_stmt = as_a <gbind *> (stmt);
1443 if (!optimize && gimple_bind_block (bind_stmt))
1444 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1446 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1447 if (TREE_CODE (var) == NAMELIST_DECL)
1449 /* Adjust decls mentioned in NAMELIST_DECL. */
1450 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1451 tree decl;
1452 unsigned int i;
1454 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1456 if (TREE_CODE (decl) == VAR_DECL
1457 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1458 continue;
1459 if (decl_function_context (decl) != info->context)
1460 CONSTRUCTOR_ELT (decls, i)->value
1461 = get_nonlocal_debug_decl (info, decl);
1465 *handled_ops_p = false;
1466 return NULL_TREE;
1468 case GIMPLE_COND:
1469 wi->val_only = true;
1470 wi->is_lhs = false;
1471 *handled_ops_p = false;
1472 return NULL_TREE;
1474 default:
1475 /* For every other statement that we are not interested in
1476 handling here, let the walker traverse the operands. */
1477 *handled_ops_p = false;
1478 return NULL_TREE;
1481 /* We have handled all of STMT operands, no need to traverse the operands. */
1482 *handled_ops_p = true;
1483 return NULL_TREE;
1487 /* A subroutine of convert_local_reference. Create a local variable
1488 in the parent function with DECL_VALUE_EXPR set to reference the
1489 field in FRAME. This is used both for debug info and in OMP
1490 lowering. */
1492 static tree
1493 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1495 tree x, new_decl;
1497 tree *slot = &info->var_map->get_or_insert (decl);
1498 if (*slot)
1499 return *slot;
1501 /* Make sure frame_decl gets created. */
1502 (void) get_frame_type (info);
1503 x = info->frame_decl;
1504 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1506 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1507 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1508 DECL_CONTEXT (new_decl) = info->context;
1509 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1510 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1511 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1512 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1513 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1514 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1515 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1516 if ((TREE_CODE (decl) == PARM_DECL
1517 || TREE_CODE (decl) == RESULT_DECL
1518 || TREE_CODE (decl) == VAR_DECL)
1519 && DECL_BY_REFERENCE (decl))
1520 DECL_BY_REFERENCE (new_decl) = 1;
1522 SET_DECL_VALUE_EXPR (new_decl, x);
1523 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1524 *slot = new_decl;
1526 DECL_CHAIN (new_decl) = info->debug_var_chain;
1527 info->debug_var_chain = new_decl;
1529 /* Do not emit debug info twice. */
1530 DECL_IGNORED_P (decl) = 1;
1532 return new_decl;
1536 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1537 and PARM_DECLs that were referenced by inner nested functions.
1538 The rewrite will be a structure reference to the local frame variable. */
1540 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1542 static tree
1543 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1545 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1546 struct nesting_info *const info = (struct nesting_info *) wi->info;
1547 tree t = *tp, field, x;
1548 bool save_val_only;
1550 *walk_subtrees = 0;
1551 switch (TREE_CODE (t))
1553 case VAR_DECL:
1554 /* Non-automatic variables are never processed. */
1555 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1556 break;
1557 /* FALLTHRU */
1559 case PARM_DECL:
1560 if (decl_function_context (t) == info->context)
1562 /* If we copied a pointer to the frame, then the original decl
1563 is used unchanged in the parent function. */
1564 if (use_pointer_in_frame (t))
1565 break;
1567 /* No need to transform anything if no child references the
1568 variable. */
1569 field = lookup_field_for_decl (info, t, NO_INSERT);
1570 if (!field)
1571 break;
1572 wi->changed = true;
1574 x = get_local_debug_decl (info, t, field);
1575 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1576 x = get_frame_field (info, info->context, field, &wi->gsi);
1578 if (wi->val_only)
1580 if (wi->is_lhs)
1581 x = save_tmp_var (info, x, &wi->gsi);
1582 else
1583 x = init_tmp_var (info, x, &wi->gsi);
1586 *tp = x;
1588 break;
1590 case ADDR_EXPR:
1591 save_val_only = wi->val_only;
1592 wi->val_only = false;
1593 wi->is_lhs = false;
1594 wi->changed = false;
1595 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1596 wi->val_only = save_val_only;
1598 /* If we converted anything ... */
1599 if (wi->changed)
1601 tree save_context;
1603 /* Then the frame decl is now addressable. */
1604 TREE_ADDRESSABLE (info->frame_decl) = 1;
1606 save_context = current_function_decl;
1607 current_function_decl = info->context;
1608 recompute_tree_invariant_for_addr_expr (t);
1609 current_function_decl = save_context;
1611 /* If we are in a context where we only accept values, then
1612 compute the address into a temporary. */
1613 if (save_val_only)
1614 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1615 t, &wi->gsi);
1617 break;
1619 case REALPART_EXPR:
1620 case IMAGPART_EXPR:
1621 case COMPONENT_REF:
1622 case ARRAY_REF:
1623 case ARRAY_RANGE_REF:
1624 case BIT_FIELD_REF:
1625 /* Go down this entire nest and just look at the final prefix and
1626 anything that describes the references. Otherwise, we lose track
1627 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1628 save_val_only = wi->val_only;
1629 wi->val_only = true;
1630 wi->is_lhs = false;
1631 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1633 if (TREE_CODE (t) == COMPONENT_REF)
1634 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1635 NULL);
1636 else if (TREE_CODE (t) == ARRAY_REF
1637 || TREE_CODE (t) == ARRAY_RANGE_REF)
1639 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1640 NULL);
1641 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1642 NULL);
1643 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1644 NULL);
1647 wi->val_only = false;
1648 walk_tree (tp, convert_local_reference_op, wi, NULL);
1649 wi->val_only = save_val_only;
1650 break;
1652 case MEM_REF:
1653 save_val_only = wi->val_only;
1654 wi->val_only = true;
1655 wi->is_lhs = false;
1656 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1657 wi, NULL);
1658 /* We need to re-fold the MEM_REF as component references as
1659 part of a ADDR_EXPR address are not allowed. But we cannot
1660 fold here, as the chain record type is not yet finalized. */
1661 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1662 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1663 info->mem_refs->add (tp);
1664 wi->val_only = save_val_only;
1665 break;
1667 case VIEW_CONVERT_EXPR:
1668 /* Just request to look at the subtrees, leaving val_only and lhs
1669 untouched. This might actually be for !val_only + lhs, in which
1670 case we don't want to force a replacement by a temporary. */
1671 *walk_subtrees = 1;
1672 break;
1674 default:
1675 if (!IS_TYPE_OR_DECL_P (t))
1677 *walk_subtrees = 1;
1678 wi->val_only = true;
1679 wi->is_lhs = false;
1681 break;
1684 return NULL_TREE;
1687 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1688 struct walk_stmt_info *);
1690 /* Helper for convert_local_reference. Convert all the references in
1691 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1693 static bool
1694 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1696 struct nesting_info *const info = (struct nesting_info *) wi->info;
1697 bool need_frame = false, need_stmts = false;
1698 tree clause, decl;
1699 int dummy;
1700 bitmap new_suppress;
1702 new_suppress = BITMAP_GGC_ALLOC ();
1703 bitmap_copy (new_suppress, info->suppress_expansion);
1705 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1707 switch (OMP_CLAUSE_CODE (clause))
1709 case OMP_CLAUSE_REDUCTION:
1710 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1711 need_stmts = true;
1712 goto do_decl_clause;
1714 case OMP_CLAUSE_LASTPRIVATE:
1715 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1716 need_stmts = true;
1717 goto do_decl_clause;
1719 case OMP_CLAUSE_LINEAR:
1720 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1721 need_stmts = true;
1722 wi->val_only = true;
1723 wi->is_lhs = false;
1724 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1725 wi);
1726 goto do_decl_clause;
1728 case OMP_CLAUSE_PRIVATE:
1729 case OMP_CLAUSE_FIRSTPRIVATE:
1730 case OMP_CLAUSE_COPYPRIVATE:
1731 case OMP_CLAUSE_SHARED:
1732 case OMP_CLAUSE_TO_DECLARE:
1733 case OMP_CLAUSE_LINK:
1734 case OMP_CLAUSE_USE_DEVICE_PTR:
1735 case OMP_CLAUSE_IS_DEVICE_PTR:
1736 do_decl_clause:
1737 decl = OMP_CLAUSE_DECL (clause);
1738 if (TREE_CODE (decl) == VAR_DECL
1739 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1740 break;
1741 if (decl_function_context (decl) == info->context
1742 && !use_pointer_in_frame (decl))
1744 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1745 if (field)
1747 bitmap_set_bit (new_suppress, DECL_UID (decl));
1748 OMP_CLAUSE_DECL (clause)
1749 = get_local_debug_decl (info, decl, field);
1750 need_frame = true;
1753 break;
1755 case OMP_CLAUSE_SCHEDULE:
1756 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1757 break;
1758 /* FALLTHRU */
1759 case OMP_CLAUSE_FINAL:
1760 case OMP_CLAUSE_IF:
1761 case OMP_CLAUSE_NUM_THREADS:
1762 case OMP_CLAUSE_DEPEND:
1763 case OMP_CLAUSE_DEVICE:
1764 case OMP_CLAUSE_NUM_TEAMS:
1765 case OMP_CLAUSE_THREAD_LIMIT:
1766 case OMP_CLAUSE_SAFELEN:
1767 case OMP_CLAUSE_SIMDLEN:
1768 case OMP_CLAUSE_PRIORITY:
1769 case OMP_CLAUSE_GRAINSIZE:
1770 case OMP_CLAUSE_NUM_TASKS:
1771 case OMP_CLAUSE_HINT:
1772 case OMP_CLAUSE__CILK_FOR_COUNT_:
1773 wi->val_only = true;
1774 wi->is_lhs = false;
1775 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1776 wi);
1777 break;
1779 case OMP_CLAUSE_DIST_SCHEDULE:
1780 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1782 wi->val_only = true;
1783 wi->is_lhs = false;
1784 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1785 &dummy, wi);
1787 break;
1789 case OMP_CLAUSE_MAP:
1790 case OMP_CLAUSE_TO:
1791 case OMP_CLAUSE_FROM:
1792 if (OMP_CLAUSE_SIZE (clause))
1794 wi->val_only = true;
1795 wi->is_lhs = false;
1796 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1797 &dummy, wi);
1799 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1800 goto do_decl_clause;
1801 wi->val_only = true;
1802 wi->is_lhs = false;
1803 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1804 wi, NULL);
1805 break;
1807 case OMP_CLAUSE_ALIGNED:
1808 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1810 wi->val_only = true;
1811 wi->is_lhs = false;
1812 convert_local_reference_op
1813 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1815 /* Like do_decl_clause, but don't add any suppression. */
1816 decl = OMP_CLAUSE_DECL (clause);
1817 if (TREE_CODE (decl) == VAR_DECL
1818 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1819 break;
1820 if (decl_function_context (decl) == info->context
1821 && !use_pointer_in_frame (decl))
1823 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1824 if (field)
1826 OMP_CLAUSE_DECL (clause)
1827 = get_local_debug_decl (info, decl, field);
1828 need_frame = true;
1831 break;
1833 case OMP_CLAUSE_NOWAIT:
1834 case OMP_CLAUSE_ORDERED:
1835 case OMP_CLAUSE_DEFAULT:
1836 case OMP_CLAUSE_COPYIN:
1837 case OMP_CLAUSE_COLLAPSE:
1838 case OMP_CLAUSE_UNTIED:
1839 case OMP_CLAUSE_MERGEABLE:
1840 case OMP_CLAUSE_PROC_BIND:
1841 case OMP_CLAUSE_NOGROUP:
1842 case OMP_CLAUSE_THREADS:
1843 case OMP_CLAUSE_SIMD:
1844 case OMP_CLAUSE_DEFAULTMAP:
1845 break;
1847 default:
1848 gcc_unreachable ();
1852 info->suppress_expansion = new_suppress;
1854 if (need_stmts)
1855 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1856 switch (OMP_CLAUSE_CODE (clause))
1858 case OMP_CLAUSE_REDUCTION:
1859 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1861 tree old_context
1862 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1863 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1864 = info->context;
1865 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1866 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1867 = info->context;
1868 walk_body (convert_local_reference_stmt,
1869 convert_local_reference_op, info,
1870 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1871 walk_body (convert_local_reference_stmt,
1872 convert_local_reference_op, info,
1873 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1874 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1875 = old_context;
1876 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1877 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1878 = old_context;
1880 break;
1882 case OMP_CLAUSE_LASTPRIVATE:
1883 walk_body (convert_local_reference_stmt,
1884 convert_local_reference_op, info,
1885 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1886 break;
1888 case OMP_CLAUSE_LINEAR:
1889 walk_body (convert_local_reference_stmt,
1890 convert_local_reference_op, info,
1891 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1892 break;
1894 default:
1895 break;
1898 return need_frame;
1902 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1903 and PARM_DECLs that were referenced by inner nested functions.
1904 The rewrite will be a structure reference to the local frame variable. */
1906 static tree
1907 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1908 struct walk_stmt_info *wi)
1910 struct nesting_info *info = (struct nesting_info *) wi->info;
1911 tree save_local_var_chain;
1912 bitmap save_suppress;
1913 gimple *stmt = gsi_stmt (*gsi);
1915 switch (gimple_code (stmt))
1917 case GIMPLE_OMP_PARALLEL:
1918 case GIMPLE_OMP_TASK:
1919 save_suppress = info->suppress_expansion;
1920 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1921 wi))
1923 tree c;
1924 (void) get_frame_type (info);
1925 c = build_omp_clause (gimple_location (stmt),
1926 OMP_CLAUSE_SHARED);
1927 OMP_CLAUSE_DECL (c) = info->frame_decl;
1928 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1929 gimple_omp_taskreg_set_clauses (stmt, c);
1932 save_local_var_chain = info->new_local_var_chain;
1933 info->new_local_var_chain = NULL;
1935 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1936 gimple_omp_body_ptr (stmt));
1938 if (info->new_local_var_chain)
1939 declare_vars (info->new_local_var_chain,
1940 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1941 info->new_local_var_chain = save_local_var_chain;
1942 info->suppress_expansion = save_suppress;
1943 break;
1945 case GIMPLE_OMP_FOR:
1946 save_suppress = info->suppress_expansion;
1947 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1948 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1949 convert_local_reference_stmt,
1950 convert_local_reference_op, info);
1951 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1952 info, gimple_omp_body_ptr (stmt));
1953 info->suppress_expansion = save_suppress;
1954 break;
1956 case GIMPLE_OMP_SECTIONS:
1957 save_suppress = info->suppress_expansion;
1958 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1959 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1960 info, gimple_omp_body_ptr (stmt));
1961 info->suppress_expansion = save_suppress;
1962 break;
1964 case GIMPLE_OMP_SINGLE:
1965 save_suppress = info->suppress_expansion;
1966 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1967 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1968 info, gimple_omp_body_ptr (stmt));
1969 info->suppress_expansion = save_suppress;
1970 break;
1972 case GIMPLE_OMP_TARGET:
1973 if (!is_gimple_omp_offloaded (stmt))
1975 save_suppress = info->suppress_expansion;
1976 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1977 info->suppress_expansion = save_suppress;
1978 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1979 info, gimple_omp_body_ptr (stmt));
1980 break;
1982 save_suppress = info->suppress_expansion;
1983 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
1985 tree c;
1986 (void) get_frame_type (info);
1987 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1988 OMP_CLAUSE_DECL (c) = info->frame_decl;
1989 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
1990 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
1991 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1992 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1995 save_local_var_chain = info->new_local_var_chain;
1996 info->new_local_var_chain = NULL;
1998 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1999 gimple_omp_body_ptr (stmt));
2001 if (info->new_local_var_chain)
2002 declare_vars (info->new_local_var_chain,
2003 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2004 info->new_local_var_chain = save_local_var_chain;
2005 info->suppress_expansion = save_suppress;
2006 break;
2008 case GIMPLE_OMP_TEAMS:
2009 save_suppress = info->suppress_expansion;
2010 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2011 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2012 info, gimple_omp_body_ptr (stmt));
2013 info->suppress_expansion = save_suppress;
2014 break;
2016 case GIMPLE_OMP_SECTION:
2017 case GIMPLE_OMP_MASTER:
2018 case GIMPLE_OMP_TASKGROUP:
2019 case GIMPLE_OMP_ORDERED:
2020 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2021 info, gimple_omp_body_ptr (stmt));
2022 break;
2024 case GIMPLE_COND:
2025 wi->val_only = true;
2026 wi->is_lhs = false;
2027 *handled_ops_p = false;
2028 return NULL_TREE;
2030 case GIMPLE_ASSIGN:
2031 if (gimple_clobber_p (stmt))
2033 tree lhs = gimple_assign_lhs (stmt);
2034 if (!use_pointer_in_frame (lhs)
2035 && lookup_field_for_decl (info, lhs, NO_INSERT))
2037 gsi_replace (gsi, gimple_build_nop (), true);
2038 break;
2041 *handled_ops_p = false;
2042 return NULL_TREE;
2044 case GIMPLE_BIND:
2045 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2046 var;
2047 var = DECL_CHAIN (var))
2048 if (TREE_CODE (var) == NAMELIST_DECL)
2050 /* Adjust decls mentioned in NAMELIST_DECL. */
2051 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2052 tree decl;
2053 unsigned int i;
2055 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2057 if (TREE_CODE (decl) == VAR_DECL
2058 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2059 continue;
2060 if (decl_function_context (decl) == info->context
2061 && !use_pointer_in_frame (decl))
2063 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2064 if (field)
2066 CONSTRUCTOR_ELT (decls, i)->value
2067 = get_local_debug_decl (info, decl, field);
2073 *handled_ops_p = false;
2074 return NULL_TREE;
2076 default:
2077 /* For every other statement that we are not interested in
2078 handling here, let the walker traverse the operands. */
2079 *handled_ops_p = false;
2080 return NULL_TREE;
2083 /* Indicate that we have handled all the operands ourselves. */
2084 *handled_ops_p = true;
2085 return NULL_TREE;
2089 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2090 that reference labels from outer functions. The rewrite will be a
2091 call to __builtin_nonlocal_goto. */
2093 static tree
2094 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2095 struct walk_stmt_info *wi)
2097 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2098 tree label, new_label, target_context, x, field;
2099 gcall *call;
2100 gimple *stmt = gsi_stmt (*gsi);
2102 if (gimple_code (stmt) != GIMPLE_GOTO)
2104 *handled_ops_p = false;
2105 return NULL_TREE;
2108 label = gimple_goto_dest (stmt);
2109 if (TREE_CODE (label) != LABEL_DECL)
2111 *handled_ops_p = false;
2112 return NULL_TREE;
2115 target_context = decl_function_context (label);
2116 if (target_context == info->context)
2118 *handled_ops_p = false;
2119 return NULL_TREE;
2122 for (i = info->outer; target_context != i->context; i = i->outer)
2123 continue;
2125 /* The original user label may also be use for a normal goto, therefore
2126 we must create a new label that will actually receive the abnormal
2127 control transfer. This new label will be marked LABEL_NONLOCAL; this
2128 mark will trigger proper behavior in the cfg, as well as cause the
2129 (hairy target-specific) non-local goto receiver code to be generated
2130 when we expand rtl. Enter this association into var_map so that we
2131 can insert the new label into the IL during a second pass. */
2132 tree *slot = &i->var_map->get_or_insert (label);
2133 if (*slot == NULL)
2135 new_label = create_artificial_label (UNKNOWN_LOCATION);
2136 DECL_NONLOCAL (new_label) = 1;
2137 *slot = new_label;
2139 else
2140 new_label = *slot;
2142 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2143 field = get_nl_goto_field (i);
2144 x = get_frame_field (info, target_context, field, gsi);
2145 x = build_addr (x);
2146 x = gsi_gimplify_val (info, x, gsi);
2147 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2148 2, build_addr (new_label), x);
2149 gsi_replace (gsi, call, false);
2151 /* We have handled all of STMT's operands, no need to keep going. */
2152 *handled_ops_p = true;
2153 return NULL_TREE;
2157 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2158 are referenced via nonlocal goto from a nested function. The rewrite
2159 will involve installing a newly generated DECL_NONLOCAL label, and
2160 (potentially) a branch around the rtl gunk that is assumed to be
2161 attached to such a label. */
2163 static tree
2164 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2165 struct walk_stmt_info *wi)
2167 struct nesting_info *const info = (struct nesting_info *) wi->info;
2168 tree label, new_label;
2169 gimple_stmt_iterator tmp_gsi;
2170 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2172 if (!stmt)
2174 *handled_ops_p = false;
2175 return NULL_TREE;
2178 label = gimple_label_label (stmt);
2180 tree *slot = info->var_map->get (label);
2181 if (!slot)
2183 *handled_ops_p = false;
2184 return NULL_TREE;
2187 /* If there's any possibility that the previous statement falls through,
2188 then we must branch around the new non-local label. */
2189 tmp_gsi = wi->gsi;
2190 gsi_prev (&tmp_gsi);
2191 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2193 gimple *stmt = gimple_build_goto (label);
2194 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2197 new_label = (tree) *slot;
2198 stmt = gimple_build_label (new_label);
2199 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2201 *handled_ops_p = true;
2202 return NULL_TREE;
2206 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2207 of nested functions that require the use of trampolines. The rewrite
2208 will involve a reference a trampoline generated for the occasion. */
2210 static tree
2211 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2213 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2214 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2215 tree t = *tp, decl, target_context, x, builtin;
2216 gcall *call;
2218 *walk_subtrees = 0;
2219 switch (TREE_CODE (t))
2221 case ADDR_EXPR:
2222 /* Build
2223 T.1 = &CHAIN->tramp;
2224 T.2 = __builtin_adjust_trampoline (T.1);
2225 T.3 = (func_type)T.2;
2228 decl = TREE_OPERAND (t, 0);
2229 if (TREE_CODE (decl) != FUNCTION_DECL)
2230 break;
2232 /* Only need to process nested functions. */
2233 target_context = decl_function_context (decl);
2234 if (!target_context)
2235 break;
2237 /* If the nested function doesn't use a static chain, then
2238 it doesn't need a trampoline. */
2239 if (!DECL_STATIC_CHAIN (decl))
2240 break;
2242 /* If we don't want a trampoline, then don't build one. */
2243 if (TREE_NO_TRAMPOLINE (t))
2244 break;
2246 /* Lookup the immediate parent of the callee, as that's where
2247 we need to insert the trampoline. */
2248 for (i = info; i->context != target_context; i = i->outer)
2249 continue;
2250 x = lookup_tramp_for_decl (i, decl, INSERT);
2252 /* Compute the address of the field holding the trampoline. */
2253 x = get_frame_field (info, target_context, x, &wi->gsi);
2254 x = build_addr (x);
2255 x = gsi_gimplify_val (info, x, &wi->gsi);
2257 /* Do machine-specific ugliness. Normally this will involve
2258 computing extra alignment, but it can really be anything. */
2259 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2260 call = gimple_build_call (builtin, 1, x);
2261 x = init_tmp_var_with_call (info, &wi->gsi, call);
2263 /* Cast back to the proper function type. */
2264 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2265 x = init_tmp_var (info, x, &wi->gsi);
2267 *tp = x;
2268 break;
2270 default:
2271 if (!IS_TYPE_OR_DECL_P (t))
2272 *walk_subtrees = 1;
2273 break;
2276 return NULL_TREE;
2280 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2281 to addresses of nested functions that require the use of
2282 trampolines. The rewrite will involve a reference a trampoline
2283 generated for the occasion. */
2285 static tree
2286 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2287 struct walk_stmt_info *wi)
2289 struct nesting_info *info = (struct nesting_info *) wi->info;
2290 gimple *stmt = gsi_stmt (*gsi);
2292 switch (gimple_code (stmt))
2294 case GIMPLE_CALL:
2296 /* Only walk call arguments, lest we generate trampolines for
2297 direct calls. */
2298 unsigned long i, nargs = gimple_call_num_args (stmt);
2299 for (i = 0; i < nargs; i++)
2300 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2301 wi, NULL);
2302 break;
2305 case GIMPLE_OMP_TARGET:
2306 if (!is_gimple_omp_offloaded (stmt))
2308 *handled_ops_p = false;
2309 return NULL_TREE;
2311 /* FALLTHRU */
2312 case GIMPLE_OMP_PARALLEL:
2313 case GIMPLE_OMP_TASK:
2315 tree save_local_var_chain = info->new_local_var_chain;
2316 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2317 info->new_local_var_chain = NULL;
2318 char save_static_chain_added = info->static_chain_added;
2319 info->static_chain_added = 0;
2320 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2321 info, gimple_omp_body_ptr (stmt));
2322 if (info->new_local_var_chain)
2323 declare_vars (info->new_local_var_chain,
2324 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2325 false);
2326 for (int i = 0; i < 2; i++)
2328 tree c, decl;
2329 if ((info->static_chain_added & (1 << i)) == 0)
2330 continue;
2331 decl = i ? get_chain_decl (info) : info->frame_decl;
2332 /* Don't add CHAIN.* or FRAME.* twice. */
2333 for (c = gimple_omp_taskreg_clauses (stmt);
2335 c = OMP_CLAUSE_CHAIN (c))
2336 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2338 && OMP_CLAUSE_DECL (c) == decl)
2339 break;
2340 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2342 c = build_omp_clause (gimple_location (stmt),
2343 i ? OMP_CLAUSE_FIRSTPRIVATE
2344 : OMP_CLAUSE_SHARED);
2345 OMP_CLAUSE_DECL (c) = decl;
2346 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2347 gimple_omp_taskreg_set_clauses (stmt, c);
2349 else if (c == NULL)
2351 c = build_omp_clause (gimple_location (stmt),
2352 OMP_CLAUSE_MAP);
2353 OMP_CLAUSE_DECL (c) = decl;
2354 OMP_CLAUSE_SET_MAP_KIND (c,
2355 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2356 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2357 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2358 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2362 info->new_local_var_chain = save_local_var_chain;
2363 info->static_chain_added |= save_static_chain_added;
2365 break;
2367 default:
2368 *handled_ops_p = false;
2369 return NULL_TREE;
2372 *handled_ops_p = true;
2373 return NULL_TREE;
2378 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2379 that reference nested functions to make sure that the static chain
2380 is set up properly for the call. */
2382 static tree
2383 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2384 struct walk_stmt_info *wi)
2386 struct nesting_info *const info = (struct nesting_info *) wi->info;
2387 tree decl, target_context;
2388 char save_static_chain_added;
2389 int i;
2390 gimple *stmt = gsi_stmt (*gsi);
2392 switch (gimple_code (stmt))
2394 case GIMPLE_CALL:
2395 if (gimple_call_chain (stmt))
2396 break;
2397 decl = gimple_call_fndecl (stmt);
2398 if (!decl)
2399 break;
2400 target_context = decl_function_context (decl);
2401 if (target_context && DECL_STATIC_CHAIN (decl))
2403 gimple_call_set_chain (as_a <gcall *> (stmt),
2404 get_static_chain (info, target_context,
2405 &wi->gsi));
2406 info->static_chain_added |= (1 << (info->context != target_context));
2408 break;
2410 case GIMPLE_OMP_PARALLEL:
2411 case GIMPLE_OMP_TASK:
2412 save_static_chain_added = info->static_chain_added;
2413 info->static_chain_added = 0;
2414 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2415 for (i = 0; i < 2; i++)
2417 tree c, decl;
2418 if ((info->static_chain_added & (1 << i)) == 0)
2419 continue;
2420 decl = i ? get_chain_decl (info) : info->frame_decl;
2421 /* Don't add CHAIN.* or FRAME.* twice. */
2422 for (c = gimple_omp_taskreg_clauses (stmt);
2424 c = OMP_CLAUSE_CHAIN (c))
2425 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2426 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2427 && OMP_CLAUSE_DECL (c) == decl)
2428 break;
2429 if (c == NULL)
2431 c = build_omp_clause (gimple_location (stmt),
2432 i ? OMP_CLAUSE_FIRSTPRIVATE
2433 : OMP_CLAUSE_SHARED);
2434 OMP_CLAUSE_DECL (c) = decl;
2435 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2436 gimple_omp_taskreg_set_clauses (stmt, c);
2439 info->static_chain_added |= save_static_chain_added;
2440 break;
2442 case GIMPLE_OMP_TARGET:
2443 if (!is_gimple_omp_offloaded (stmt))
2445 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2446 break;
2448 save_static_chain_added = info->static_chain_added;
2449 info->static_chain_added = 0;
2450 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2451 for (i = 0; i < 2; i++)
2453 tree c, decl;
2454 if ((info->static_chain_added & (1 << i)) == 0)
2455 continue;
2456 decl = i ? get_chain_decl (info) : info->frame_decl;
2457 /* Don't add CHAIN.* or FRAME.* twice. */
2458 for (c = gimple_omp_target_clauses (stmt);
2460 c = OMP_CLAUSE_CHAIN (c))
2461 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2462 && OMP_CLAUSE_DECL (c) == decl)
2463 break;
2464 if (c == NULL)
2466 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2467 OMP_CLAUSE_DECL (c) = decl;
2468 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2469 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2470 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2471 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2475 info->static_chain_added |= save_static_chain_added;
2476 break;
2478 case GIMPLE_OMP_FOR:
2479 walk_body (convert_gimple_call, NULL, info,
2480 gimple_omp_for_pre_body_ptr (stmt));
2481 /* FALLTHRU */
2482 case GIMPLE_OMP_SECTIONS:
2483 case GIMPLE_OMP_SECTION:
2484 case GIMPLE_OMP_SINGLE:
2485 case GIMPLE_OMP_TEAMS:
2486 case GIMPLE_OMP_MASTER:
2487 case GIMPLE_OMP_TASKGROUP:
2488 case GIMPLE_OMP_ORDERED:
2489 case GIMPLE_OMP_CRITICAL:
2490 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2491 break;
2493 default:
2494 /* Keep looking for other operands. */
2495 *handled_ops_p = false;
2496 return NULL_TREE;
2499 *handled_ops_p = true;
2500 return NULL_TREE;
2503 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2504 call expressions. At the same time, determine if a nested function
2505 actually uses its static chain; if not, remember that. */
2507 static void
2508 convert_all_function_calls (struct nesting_info *root)
2510 unsigned int chain_count = 0, old_chain_count, iter_count;
2511 struct nesting_info *n;
2513 /* First, optimistically clear static_chain for all decls that haven't
2514 used the static chain already for variable access. But always create
2515 it if not optimizing. This makes it possible to reconstruct the static
2516 nesting tree at run time and thus to resolve up-level references from
2517 within the debugger. */
2518 FOR_EACH_NEST_INFO (n, root)
2520 tree decl = n->context;
2521 if (!optimize)
2523 if (n->inner)
2524 (void) get_frame_type (n);
2525 if (n->outer)
2526 (void) get_chain_decl (n);
2528 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2530 DECL_STATIC_CHAIN (decl) = 0;
2531 if (dump_file && (dump_flags & TDF_DETAILS))
2532 fprintf (dump_file, "Guessing no static-chain for %s\n",
2533 lang_hooks.decl_printable_name (decl, 2));
2535 else
2536 DECL_STATIC_CHAIN (decl) = 1;
2537 chain_count += DECL_STATIC_CHAIN (decl);
2540 /* Walk the functions and perform transformations. Note that these
2541 transformations can induce new uses of the static chain, which in turn
2542 require re-examining all users of the decl. */
2543 /* ??? It would make sense to try to use the call graph to speed this up,
2544 but the call graph hasn't really been built yet. Even if it did, we
2545 would still need to iterate in this loop since address-of references
2546 wouldn't show up in the callgraph anyway. */
2547 iter_count = 0;
2550 old_chain_count = chain_count;
2551 chain_count = 0;
2552 iter_count++;
2554 if (dump_file && (dump_flags & TDF_DETAILS))
2555 fputc ('\n', dump_file);
2557 FOR_EACH_NEST_INFO (n, root)
2559 tree decl = n->context;
2560 walk_function (convert_tramp_reference_stmt,
2561 convert_tramp_reference_op, n);
2562 walk_function (convert_gimple_call, NULL, n);
2563 chain_count += DECL_STATIC_CHAIN (decl);
2566 while (chain_count != old_chain_count);
2568 if (dump_file && (dump_flags & TDF_DETAILS))
2569 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2570 iter_count);
2573 struct nesting_copy_body_data
2575 copy_body_data cb;
2576 struct nesting_info *root;
2579 /* A helper subroutine for debug_var_chain type remapping. */
2581 static tree
2582 nesting_copy_decl (tree decl, copy_body_data *id)
2584 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2585 tree *slot = nid->root->var_map->get (decl);
2587 if (slot)
2588 return (tree) *slot;
2590 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2592 tree new_decl = copy_decl_no_change (decl, id);
2593 DECL_ORIGINAL_TYPE (new_decl)
2594 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2595 return new_decl;
2598 if (TREE_CODE (decl) == VAR_DECL
2599 || TREE_CODE (decl) == PARM_DECL
2600 || TREE_CODE (decl) == RESULT_DECL)
2601 return decl;
2603 return copy_decl_no_change (decl, id);
2606 /* A helper function for remap_vla_decls. See if *TP contains
2607 some remapped variables. */
2609 static tree
2610 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2612 struct nesting_info *root = (struct nesting_info *) data;
2613 tree t = *tp;
2615 if (DECL_P (t))
2617 *walk_subtrees = 0;
2618 tree *slot = root->var_map->get (t);
2620 if (slot)
2621 return *slot;
2623 return NULL;
2626 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2627 involved. */
2629 static void
2630 remap_vla_decls (tree block, struct nesting_info *root)
2632 tree var, subblock, val, type;
2633 struct nesting_copy_body_data id;
2635 for (subblock = BLOCK_SUBBLOCKS (block);
2636 subblock;
2637 subblock = BLOCK_CHAIN (subblock))
2638 remap_vla_decls (subblock, root);
2640 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2641 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2643 val = DECL_VALUE_EXPR (var);
2644 type = TREE_TYPE (var);
2646 if (!(TREE_CODE (val) == INDIRECT_REF
2647 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2648 && variably_modified_type_p (type, NULL)))
2649 continue;
2651 if (root->var_map->get (TREE_OPERAND (val, 0))
2652 || walk_tree (&type, contains_remapped_vars, root, NULL))
2653 break;
2656 if (var == NULL_TREE)
2657 return;
2659 memset (&id, 0, sizeof (id));
2660 id.cb.copy_decl = nesting_copy_decl;
2661 id.cb.decl_map = new hash_map<tree, tree>;
2662 id.root = root;
2664 for (; var; var = DECL_CHAIN (var))
2665 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2667 struct nesting_info *i;
2668 tree newt, context;
2670 val = DECL_VALUE_EXPR (var);
2671 type = TREE_TYPE (var);
2673 if (!(TREE_CODE (val) == INDIRECT_REF
2674 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2675 && variably_modified_type_p (type, NULL)))
2676 continue;
2678 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2679 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2680 continue;
2682 context = decl_function_context (var);
2683 for (i = root; i; i = i->outer)
2684 if (i->context == context)
2685 break;
2687 if (i == NULL)
2688 continue;
2690 /* Fully expand value expressions. This avoids having debug variables
2691 only referenced from them and that can be swept during GC. */
2692 if (slot)
2694 tree t = (tree) *slot;
2695 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2696 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2699 id.cb.src_fn = i->context;
2700 id.cb.dst_fn = i->context;
2701 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2703 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2704 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2706 newt = TREE_TYPE (newt);
2707 type = TREE_TYPE (type);
2709 if (TYPE_NAME (newt)
2710 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2711 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2712 && newt != type
2713 && TYPE_NAME (newt) == TYPE_NAME (type))
2714 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2716 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2717 if (val != DECL_VALUE_EXPR (var))
2718 SET_DECL_VALUE_EXPR (var, val);
2721 delete id.cb.decl_map;
2724 /* Fold the MEM_REF *E. */
2725 bool
2726 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2728 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2729 *ref_p = fold (*ref_p);
2730 return true;
2733 /* Do "everything else" to clean up or complete state collected by the
2734 various walking passes -- lay out the types and decls, generate code
2735 to initialize the frame decl, store critical expressions in the
2736 struct function for rtl to find. */
2738 static void
2739 finalize_nesting_tree_1 (struct nesting_info *root)
2741 gimple_seq stmt_list;
2742 gimple *stmt;
2743 tree context = root->context;
2744 struct function *sf;
2746 stmt_list = NULL;
2748 /* If we created a non-local frame type or decl, we need to lay them
2749 out at this time. */
2750 if (root->frame_type)
2752 /* In some cases the frame type will trigger the -Wpadded warning.
2753 This is not helpful; suppress it. */
2754 int save_warn_padded = warn_padded;
2755 tree *adjust;
2757 warn_padded = 0;
2758 layout_type (root->frame_type);
2759 warn_padded = save_warn_padded;
2760 layout_decl (root->frame_decl, 0);
2762 /* Remove root->frame_decl from root->new_local_var_chain, so
2763 that we can declare it also in the lexical blocks, which
2764 helps ensure virtual regs that end up appearing in its RTL
2765 expression get substituted in instantiate_virtual_regs(). */
2766 for (adjust = &root->new_local_var_chain;
2767 *adjust != root->frame_decl;
2768 adjust = &DECL_CHAIN (*adjust))
2769 gcc_assert (DECL_CHAIN (*adjust));
2770 *adjust = DECL_CHAIN (*adjust);
2772 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2773 declare_vars (root->frame_decl,
2774 gimple_seq_first_stmt (gimple_body (context)), true);
2777 /* If any parameters were referenced non-locally, then we need to
2778 insert a copy. Likewise, if any variables were referenced by
2779 pointer, we need to initialize the address. */
2780 if (root->any_parm_remapped)
2782 tree p;
2783 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2785 tree field, x, y;
2787 field = lookup_field_for_decl (root, p, NO_INSERT);
2788 if (!field)
2789 continue;
2791 if (use_pointer_in_frame (p))
2792 x = build_addr (p);
2793 else
2794 x = p;
2796 /* If the assignment is from a non-register the stmt is
2797 not valid gimple. Make it so by using a temporary instead. */
2798 if (!is_gimple_reg (x)
2799 && is_gimple_reg_type (TREE_TYPE (x)))
2801 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2802 x = init_tmp_var (root, x, &gsi);
2805 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2806 root->frame_decl, field, NULL_TREE);
2807 stmt = gimple_build_assign (y, x);
2808 gimple_seq_add_stmt (&stmt_list, stmt);
2812 /* If a chain_field was created, then it needs to be initialized
2813 from chain_decl. */
2814 if (root->chain_field)
2816 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2817 root->frame_decl, root->chain_field, NULL_TREE);
2818 stmt = gimple_build_assign (x, get_chain_decl (root));
2819 gimple_seq_add_stmt (&stmt_list, stmt);
2822 /* If trampolines were created, then we need to initialize them. */
2823 if (root->any_tramp_created)
2825 struct nesting_info *i;
2826 for (i = root->inner; i ; i = i->next)
2828 tree arg1, arg2, arg3, x, field;
2830 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2831 if (!field)
2832 continue;
2834 gcc_assert (DECL_STATIC_CHAIN (i->context));
2835 arg3 = build_addr (root->frame_decl);
2837 arg2 = build_addr (i->context);
2839 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2840 root->frame_decl, field, NULL_TREE);
2841 arg1 = build_addr (x);
2843 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2844 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2845 gimple_seq_add_stmt (&stmt_list, stmt);
2849 /* If we created initialization statements, insert them. */
2850 if (stmt_list)
2852 gbind *bind;
2853 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2854 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2855 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2856 gimple_bind_set_body (bind, stmt_list);
2859 /* If a chain_decl was created, then it needs to be registered with
2860 struct function so that it gets initialized from the static chain
2861 register at the beginning of the function. */
2862 sf = DECL_STRUCT_FUNCTION (root->context);
2863 sf->static_chain_decl = root->chain_decl;
2865 /* Similarly for the non-local goto save area. */
2866 if (root->nl_goto_field)
2868 sf->nonlocal_goto_save_area
2869 = get_frame_field (root, context, root->nl_goto_field, NULL);
2870 sf->has_nonlocal_label = 1;
2873 /* Make sure all new local variables get inserted into the
2874 proper BIND_EXPR. */
2875 if (root->new_local_var_chain)
2876 declare_vars (root->new_local_var_chain,
2877 gimple_seq_first_stmt (gimple_body (root->context)),
2878 false);
2880 if (root->debug_var_chain)
2882 tree debug_var;
2883 gbind *scope;
2885 remap_vla_decls (DECL_INITIAL (root->context), root);
2887 for (debug_var = root->debug_var_chain; debug_var;
2888 debug_var = DECL_CHAIN (debug_var))
2889 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2890 break;
2892 /* If there are any debug decls with variable length types,
2893 remap those types using other debug_var_chain variables. */
2894 if (debug_var)
2896 struct nesting_copy_body_data id;
2898 memset (&id, 0, sizeof (id));
2899 id.cb.copy_decl = nesting_copy_decl;
2900 id.cb.decl_map = new hash_map<tree, tree>;
2901 id.root = root;
2903 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2904 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2906 tree type = TREE_TYPE (debug_var);
2907 tree newt, t = type;
2908 struct nesting_info *i;
2910 for (i = root; i; i = i->outer)
2911 if (variably_modified_type_p (type, i->context))
2912 break;
2914 if (i == NULL)
2915 continue;
2917 id.cb.src_fn = i->context;
2918 id.cb.dst_fn = i->context;
2919 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2921 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2922 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2924 newt = TREE_TYPE (newt);
2925 t = TREE_TYPE (t);
2927 if (TYPE_NAME (newt)
2928 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2929 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2930 && newt != t
2931 && TYPE_NAME (newt) == TYPE_NAME (t))
2932 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2935 delete id.cb.decl_map;
2938 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
2939 if (gimple_bind_block (scope))
2940 declare_vars (root->debug_var_chain, scope, true);
2941 else
2942 BLOCK_VARS (DECL_INITIAL (root->context))
2943 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2944 root->debug_var_chain);
2947 /* Fold the rewritten MEM_REF trees. */
2948 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
2950 /* Dump the translated tree function. */
2951 if (dump_file)
2953 fputs ("\n\n", dump_file);
2954 dump_function_to_file (root->context, dump_file, dump_flags);
2958 static void
2959 finalize_nesting_tree (struct nesting_info *root)
2961 struct nesting_info *n;
2962 FOR_EACH_NEST_INFO (n, root)
2963 finalize_nesting_tree_1 (n);
2966 /* Unnest the nodes and pass them to cgraph. */
2968 static void
2969 unnest_nesting_tree_1 (struct nesting_info *root)
2971 struct cgraph_node *node = cgraph_node::get (root->context);
2973 /* For nested functions update the cgraph to reflect unnesting.
2974 We also delay finalizing of these functions up to this point. */
2975 if (node->origin)
2977 node->unnest ();
2978 cgraph_node::finalize_function (root->context, true);
2982 static void
2983 unnest_nesting_tree (struct nesting_info *root)
2985 struct nesting_info *n;
2986 FOR_EACH_NEST_INFO (n, root)
2987 unnest_nesting_tree_1 (n);
2990 /* Free the data structures allocated during this pass. */
2992 static void
2993 free_nesting_tree (struct nesting_info *root)
2995 struct nesting_info *node, *next;
2997 node = iter_nestinfo_start (root);
3000 next = iter_nestinfo_next (node);
3001 delete node->var_map;
3002 delete node->field_map;
3003 delete node->mem_refs;
3004 free (node);
3005 node = next;
3007 while (node);
3010 /* Gimplify a function and all its nested functions. */
3011 static void
3012 gimplify_all_functions (struct cgraph_node *root)
3014 struct cgraph_node *iter;
3015 if (!gimple_body (root->decl))
3016 gimplify_function_tree (root->decl);
3017 for (iter = root->nested; iter; iter = iter->next_nested)
3018 gimplify_all_functions (iter);
3021 /* Main entry point for this pass. Process FNDECL and all of its nested
3022 subroutines and turn them into something less tightly bound. */
3024 void
3025 lower_nested_functions (tree fndecl)
3027 struct cgraph_node *cgn;
3028 struct nesting_info *root;
3030 /* If there are no nested functions, there's nothing to do. */
3031 cgn = cgraph_node::get (fndecl);
3032 if (!cgn->nested)
3033 return;
3035 gimplify_all_functions (cgn);
3037 dump_file = dump_begin (TDI_nested, &dump_flags);
3038 if (dump_file)
3039 fprintf (dump_file, "\n;; Function %s\n\n",
3040 lang_hooks.decl_printable_name (fndecl, 2));
3042 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3043 root = create_nesting_tree (cgn);
3045 walk_all_functions (convert_nonlocal_reference_stmt,
3046 convert_nonlocal_reference_op,
3047 root);
3048 walk_all_functions (convert_local_reference_stmt,
3049 convert_local_reference_op,
3050 root);
3051 walk_all_functions (convert_nl_goto_reference, NULL, root);
3052 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3054 convert_all_function_calls (root);
3055 finalize_nesting_tree (root);
3056 unnest_nesting_tree (root);
3058 free_nesting_tree (root);
3059 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3061 if (dump_file)
3063 dump_end (TDI_nested, dump_file);
3064 dump_file = NULL;
3068 #include "gt-tree-nested.h"