]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-nested.c
re PR debug/53927 (wrong value for DW_AT_static_link)
[thirdparty/gcc.git] / gcc / tree-nested.c
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stringpool.h"
26 #include "stor-layout.h"
27 #include "tm_p.h"
28 #include "function.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "pointer-set.h"
32 #include "basic-block.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "cgraph.h"
44 #include "tree-cfg.h"
45 #include "expr.h" /* FIXME: For STACK_SAVEAREA_MODE and SAVE_NONLOCAL. */
46 #include "langhooks.h"
47 #include "gimple-low.h"
48
49
50 /* The object of this pass is to lower the representation of a set of nested
51 functions in order to expose all of the gory details of the various
52 nonlocal references. We want to do this sooner rather than later, in
53 order to give us more freedom in emitting all of the functions in question.
54
55 Back in olden times, when gcc was young, we developed an insanely
56 complicated scheme whereby variables which were referenced nonlocally
57 were forced to live in the stack of the declaring function, and then
58 the nested functions magically discovered where these variables were
59 placed. In order for this scheme to function properly, it required
60 that the outer function be partially expanded, then we switch to
61 compiling the inner function, and once done with those we switch back
62 to compiling the outer function. Such delicate ordering requirements
63 makes it difficult to do whole translation unit optimizations
64 involving such functions.
65
66 The implementation here is much more direct. Everything that can be
67 referenced by an inner function is a member of an explicitly created
68 structure herein called the "nonlocal frame struct". The incoming
69 static chain for a nested function is a pointer to this struct in
70 the parent. In this way, we settle on known offsets from a known
71 base, and so are decoupled from the logic that places objects in the
72 function's stack frame. More importantly, we don't have to wait for
73 that to happen -- since the compilation of the inner function is no
74 longer tied to a real stack frame, the nonlocal frame struct can be
75 allocated anywhere. Which means that the outer function is now
76 inlinable.
77
78 Theory of operation here is very simple. Iterate over all the
79 statements in all the functions (depth first) several times,
80 allocating structures and fields on demand. In general we want to
81 examine inner functions first, so that we can avoid making changes
82 to outer functions which are unnecessary.
83
84 The order of the passes matters a bit, in that later passes will be
85 skipped if it is discovered that the functions don't actually interact
86 at all. That is, they're nested in the lexical sense but could have
87 been written as independent functions without change. */
88
89
90 struct nesting_info
91 {
92 struct nesting_info *outer;
93 struct nesting_info *inner;
94 struct nesting_info *next;
95
96 struct pointer_map_t *field_map;
97 struct pointer_map_t *var_map;
98 struct pointer_set_t *mem_refs;
99 bitmap suppress_expansion;
100
101 tree context;
102 tree new_local_var_chain;
103 tree debug_var_chain;
104 tree frame_type;
105 tree frame_decl;
106 tree chain_field;
107 tree chain_decl;
108 tree nl_goto_field;
109
110 bool any_parm_remapped;
111 bool any_tramp_created;
112 char static_chain_added;
113 };
114
115
116 /* Iterate over the nesting tree, starting with ROOT, depth first. */
117
118 static inline struct nesting_info *
119 iter_nestinfo_start (struct nesting_info *root)
120 {
121 while (root->inner)
122 root = root->inner;
123 return root;
124 }
125
126 static inline struct nesting_info *
127 iter_nestinfo_next (struct nesting_info *node)
128 {
129 if (node->next)
130 return iter_nestinfo_start (node->next);
131 return node->outer;
132 }
133
134 #define FOR_EACH_NEST_INFO(I, ROOT) \
135 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
136
137 /* Obstack used for the bitmaps in the struct above. */
138 static struct bitmap_obstack nesting_info_bitmap_obstack;
139
140
141 /* We're working in so many different function contexts simultaneously,
142 that create_tmp_var is dangerous. Prevent mishap. */
143 #define create_tmp_var cant_use_create_tmp_var_here_dummy
144
145 /* Like create_tmp_var, except record the variable for registration at
146 the given nesting level. */
147
148 static tree
149 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
150 {
151 tree tmp_var;
152
153 /* If the type is of variable size or a type which must be created by the
154 frontend, something is wrong. Note that we explicitly allow
155 incomplete types here, since we create them ourselves here. */
156 gcc_assert (!TREE_ADDRESSABLE (type));
157 gcc_assert (!TYPE_SIZE_UNIT (type)
158 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
159
160 tmp_var = create_tmp_var_raw (type, prefix);
161 DECL_CONTEXT (tmp_var) = info->context;
162 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
163 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
164 if (TREE_CODE (type) == COMPLEX_TYPE
165 || TREE_CODE (type) == VECTOR_TYPE)
166 DECL_GIMPLE_REG_P (tmp_var) = 1;
167
168 info->new_local_var_chain = tmp_var;
169
170 return tmp_var;
171 }
172
173 /* Take the address of EXP to be used within function CONTEXT.
174 Mark it for addressability as necessary. */
175
176 tree
177 build_addr (tree exp, tree context)
178 {
179 tree base = exp;
180 tree save_context;
181 tree retval;
182
183 while (handled_component_p (base))
184 base = TREE_OPERAND (base, 0);
185
186 if (DECL_P (base))
187 TREE_ADDRESSABLE (base) = 1;
188
189 /* Building the ADDR_EXPR will compute a set of properties for
190 that ADDR_EXPR. Those properties are unfortunately context
191 specific, i.e., they are dependent on CURRENT_FUNCTION_DECL.
192
193 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
194 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
195 way the properties are for the ADDR_EXPR are computed properly. */
196 save_context = current_function_decl;
197 current_function_decl = context;
198 retval = build_fold_addr_expr (exp);
199 current_function_decl = save_context;
200 return retval;
201 }
202
203 /* Insert FIELD into TYPE, sorted by alignment requirements. */
204
205 void
206 insert_field_into_struct (tree type, tree field)
207 {
208 tree *p;
209
210 DECL_CONTEXT (field) = type;
211
212 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
213 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
214 break;
215
216 DECL_CHAIN (field) = *p;
217 *p = field;
218
219 /* Set correct alignment for frame struct type. */
220 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
221 TYPE_ALIGN (type) = DECL_ALIGN (field);
222 }
223
224 /* Build or return the RECORD_TYPE that describes the frame state that is
225 shared between INFO->CONTEXT and its nested functions. This record will
226 not be complete until finalize_nesting_tree; up until that point we'll
227 be adding fields as necessary.
228
229 We also build the DECL that represents this frame in the function. */
230
231 static tree
232 get_frame_type (struct nesting_info *info)
233 {
234 tree type = info->frame_type;
235 if (!type)
236 {
237 char *name;
238
239 type = make_node (RECORD_TYPE);
240
241 name = concat ("FRAME.",
242 IDENTIFIER_POINTER (DECL_NAME (info->context)),
243 NULL);
244 TYPE_NAME (type) = get_identifier (name);
245 free (name);
246
247 info->frame_type = type;
248 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
249 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
250
251 /* ??? Always make it addressable for now, since it is meant to
252 be pointed to by the static chain pointer. This pessimizes
253 when it turns out that no static chains are needed because
254 the nested functions referencing non-local variables are not
255 reachable, but the true pessimization is to create the non-
256 local frame structure in the first place. */
257 TREE_ADDRESSABLE (info->frame_decl) = 1;
258 }
259 return type;
260 }
261
262 /* Return true if DECL should be referenced by pointer in the non-local
263 frame structure. */
264
265 static bool
266 use_pointer_in_frame (tree decl)
267 {
268 if (TREE_CODE (decl) == PARM_DECL)
269 {
270 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
271 sized decls, and inefficient to copy large aggregates. Don't bother
272 moving anything but scalar variables. */
273 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
274 }
275 else
276 {
277 /* Variable sized types make things "interesting" in the frame. */
278 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
279 }
280 }
281
282 /* Given DECL, a non-locally accessed variable, find or create a field
283 in the non-local frame structure for the given nesting context. */
284
285 static tree
286 lookup_field_for_decl (struct nesting_info *info, tree decl,
287 enum insert_option insert)
288 {
289 void **slot;
290
291 if (insert == NO_INSERT)
292 {
293 slot = pointer_map_contains (info->field_map, decl);
294 return slot ? (tree) *slot : NULL_TREE;
295 }
296
297 slot = pointer_map_insert (info->field_map, decl);
298 if (!*slot)
299 {
300 tree field = make_node (FIELD_DECL);
301 DECL_NAME (field) = DECL_NAME (decl);
302
303 if (use_pointer_in_frame (decl))
304 {
305 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
306 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
307 DECL_NONADDRESSABLE_P (field) = 1;
308 }
309 else
310 {
311 TREE_TYPE (field) = TREE_TYPE (decl);
312 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
313 DECL_ALIGN (field) = DECL_ALIGN (decl);
314 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
315 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
316 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
317 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
318 }
319
320 insert_field_into_struct (get_frame_type (info), field);
321 *slot = field;
322
323 if (TREE_CODE (decl) == PARM_DECL)
324 info->any_parm_remapped = true;
325 }
326
327 return (tree) *slot;
328 }
329
330 /* Build or return the variable that holds the static chain within
331 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
332
333 static tree
334 get_chain_decl (struct nesting_info *info)
335 {
336 tree decl = info->chain_decl;
337
338 if (!decl)
339 {
340 tree type;
341
342 type = get_frame_type (info->outer);
343 type = build_pointer_type (type);
344
345 /* Note that this variable is *not* entered into any BIND_EXPR;
346 the construction of this variable is handled specially in
347 expand_function_start and initialize_inlined_parameters.
348 Note also that it's represented as a parameter. This is more
349 close to the truth, since the initial value does come from
350 the caller. */
351 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
352 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
353 DECL_ARTIFICIAL (decl) = 1;
354 DECL_IGNORED_P (decl) = 1;
355 TREE_USED (decl) = 1;
356 DECL_CONTEXT (decl) = info->context;
357 DECL_ARG_TYPE (decl) = type;
358
359 /* Tell tree-inline.c that we never write to this variable, so
360 it can copy-prop the replacement value immediately. */
361 TREE_READONLY (decl) = 1;
362
363 info->chain_decl = decl;
364
365 if (dump_file
366 && (dump_flags & TDF_DETAILS)
367 && !DECL_STATIC_CHAIN (info->context))
368 fprintf (dump_file, "Setting static-chain for %s\n",
369 lang_hooks.decl_printable_name (info->context, 2));
370
371 DECL_STATIC_CHAIN (info->context) = 1;
372 }
373 return decl;
374 }
375
376 /* Build or return the field within the non-local frame state that holds
377 the static chain for INFO->CONTEXT. This is the way to walk back up
378 multiple nesting levels. */
379
380 static tree
381 get_chain_field (struct nesting_info *info)
382 {
383 tree field = info->chain_field;
384
385 if (!field)
386 {
387 tree type = build_pointer_type (get_frame_type (info->outer));
388
389 field = make_node (FIELD_DECL);
390 DECL_NAME (field) = get_identifier ("__chain");
391 TREE_TYPE (field) = type;
392 DECL_ALIGN (field) = TYPE_ALIGN (type);
393 DECL_NONADDRESSABLE_P (field) = 1;
394
395 insert_field_into_struct (get_frame_type (info), field);
396
397 info->chain_field = field;
398
399 if (dump_file
400 && (dump_flags & TDF_DETAILS)
401 && !DECL_STATIC_CHAIN (info->context))
402 fprintf (dump_file, "Setting static-chain for %s\n",
403 lang_hooks.decl_printable_name (info->context, 2));
404
405 DECL_STATIC_CHAIN (info->context) = 1;
406 }
407 return field;
408 }
409
410 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
411
412 static tree
413 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
414 gimple call)
415 {
416 tree t;
417
418 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
419 gimple_call_set_lhs (call, t);
420 if (! gsi_end_p (*gsi))
421 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
422 gsi_insert_before (gsi, call, GSI_SAME_STMT);
423
424 return t;
425 }
426
427
428 /* Copy EXP into a temporary. Allocate the temporary in the context of
429 INFO and insert the initialization statement before GSI. */
430
431 static tree
432 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
433 {
434 tree t;
435 gimple stmt;
436
437 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
438 stmt = gimple_build_assign (t, exp);
439 if (! gsi_end_p (*gsi))
440 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
441 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
442
443 return t;
444 }
445
446
447 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
448
449 static tree
450 gsi_gimplify_val (struct nesting_info *info, tree exp,
451 gimple_stmt_iterator *gsi)
452 {
453 if (is_gimple_val (exp))
454 return exp;
455 else
456 return init_tmp_var (info, exp, gsi);
457 }
458
459 /* Similarly, but copy from the temporary and insert the statement
460 after the iterator. */
461
462 static tree
463 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
464 {
465 tree t;
466 gimple stmt;
467
468 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
469 stmt = gimple_build_assign (exp, t);
470 if (! gsi_end_p (*gsi))
471 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
472 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
473
474 return t;
475 }
476
477 /* Build or return the type used to represent a nested function trampoline. */
478
479 static GTY(()) tree trampoline_type;
480
481 static tree
482 get_trampoline_type (struct nesting_info *info)
483 {
484 unsigned align, size;
485 tree t;
486
487 if (trampoline_type)
488 return trampoline_type;
489
490 align = TRAMPOLINE_ALIGNMENT;
491 size = TRAMPOLINE_SIZE;
492
493 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
494 then allocate extra space so that we can do dynamic alignment. */
495 if (align > STACK_BOUNDARY)
496 {
497 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
498 align = STACK_BOUNDARY;
499 }
500
501 t = build_index_type (size_int (size - 1));
502 t = build_array_type (char_type_node, t);
503 t = build_decl (DECL_SOURCE_LOCATION (info->context),
504 FIELD_DECL, get_identifier ("__data"), t);
505 DECL_ALIGN (t) = align;
506 DECL_USER_ALIGN (t) = 1;
507
508 trampoline_type = make_node (RECORD_TYPE);
509 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
510 TYPE_FIELDS (trampoline_type) = t;
511 layout_type (trampoline_type);
512 DECL_CONTEXT (t) = trampoline_type;
513
514 return trampoline_type;
515 }
516
517 /* Given DECL, a nested function, find or create a field in the non-local
518 frame structure for a trampoline for this function. */
519
520 static tree
521 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
522 enum insert_option insert)
523 {
524 void **slot;
525
526 if (insert == NO_INSERT)
527 {
528 slot = pointer_map_contains (info->var_map, decl);
529 return slot ? (tree) *slot : NULL_TREE;
530 }
531
532 slot = pointer_map_insert (info->var_map, decl);
533 if (!*slot)
534 {
535 tree field = make_node (FIELD_DECL);
536 DECL_NAME (field) = DECL_NAME (decl);
537 TREE_TYPE (field) = get_trampoline_type (info);
538 TREE_ADDRESSABLE (field) = 1;
539
540 insert_field_into_struct (get_frame_type (info), field);
541 *slot = field;
542
543 info->any_tramp_created = true;
544 }
545
546 return (tree) *slot;
547 }
548
549 /* Build or return the field within the non-local frame state that holds
550 the non-local goto "jmp_buf". The buffer itself is maintained by the
551 rtl middle-end as dynamic stack space is allocated. */
552
553 static tree
554 get_nl_goto_field (struct nesting_info *info)
555 {
556 tree field = info->nl_goto_field;
557 if (!field)
558 {
559 unsigned size;
560 tree type;
561
562 /* For __builtin_nonlocal_goto, we need N words. The first is the
563 frame pointer, the rest is for the target's stack pointer save
564 area. The number of words is controlled by STACK_SAVEAREA_MODE;
565 not the best interface, but it'll do for now. */
566 if (Pmode == ptr_mode)
567 type = ptr_type_node;
568 else
569 type = lang_hooks.types.type_for_mode (Pmode, 1);
570
571 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
572 size = size / GET_MODE_SIZE (Pmode);
573 size = size + 1;
574
575 type = build_array_type
576 (type, build_index_type (size_int (size)));
577
578 field = make_node (FIELD_DECL);
579 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
580 TREE_TYPE (field) = type;
581 DECL_ALIGN (field) = TYPE_ALIGN (type);
582 TREE_ADDRESSABLE (field) = 1;
583
584 insert_field_into_struct (get_frame_type (info), field);
585
586 info->nl_goto_field = field;
587 }
588
589 return field;
590 }
591
592 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
593
594 static void
595 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
596 struct nesting_info *info, gimple_seq *pseq)
597 {
598 struct walk_stmt_info wi;
599
600 memset (&wi, 0, sizeof (wi));
601 wi.info = info;
602 wi.val_only = true;
603 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
604 }
605
606
607 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
608
609 static inline void
610 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
611 struct nesting_info *info)
612 {
613 gimple_seq body = gimple_body (info->context);
614 walk_body (callback_stmt, callback_op, info, &body);
615 gimple_set_body (info->context, body);
616 }
617
618 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
619
620 static void
621 walk_gimple_omp_for (gimple for_stmt,
622 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
623 struct nesting_info *info)
624 {
625 struct walk_stmt_info wi;
626 gimple_seq seq;
627 tree t;
628 size_t i;
629
630 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
631
632 seq = NULL;
633 memset (&wi, 0, sizeof (wi));
634 wi.info = info;
635 wi.gsi = gsi_last (seq);
636
637 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
638 {
639 wi.val_only = false;
640 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
641 &wi, NULL);
642 wi.val_only = true;
643 wi.is_lhs = false;
644 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
645 &wi, NULL);
646
647 wi.val_only = true;
648 wi.is_lhs = false;
649 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
650 &wi, NULL);
651
652 t = gimple_omp_for_incr (for_stmt, i);
653 gcc_assert (BINARY_CLASS_P (t));
654 wi.val_only = false;
655 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
656 wi.val_only = true;
657 wi.is_lhs = false;
658 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
659 }
660
661 seq = gsi_seq (wi.gsi);
662 if (!gimple_seq_empty_p (seq))
663 {
664 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
665 annotate_all_with_location (seq, gimple_location (for_stmt));
666 gimple_seq_add_seq (&pre_body, seq);
667 gimple_omp_for_set_pre_body (for_stmt, pre_body);
668 }
669 }
670
671 /* Similarly for ROOT and all functions nested underneath, depth first. */
672
673 static void
674 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
675 struct nesting_info *root)
676 {
677 struct nesting_info *n;
678 FOR_EACH_NEST_INFO (n, root)
679 walk_function (callback_stmt, callback_op, n);
680 }
681
682
683 /* We have to check for a fairly pathological case. The operands of function
684 nested function are to be interpreted in the context of the enclosing
685 function. So if any are variably-sized, they will get remapped when the
686 enclosing function is inlined. But that remapping would also have to be
687 done in the types of the PARM_DECLs of the nested function, meaning the
688 argument types of that function will disagree with the arguments in the
689 calls to that function. So we'd either have to make a copy of the nested
690 function corresponding to each time the enclosing function was inlined or
691 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
692 function. The former is not practical. The latter would still require
693 detecting this case to know when to add the conversions. So, for now at
694 least, we don't inline such an enclosing function.
695
696 We have to do that check recursively, so here return indicating whether
697 FNDECL has such a nested function. ORIG_FN is the function we were
698 trying to inline to use for checking whether any argument is variably
699 modified by anything in it.
700
701 It would be better to do this in tree-inline.c so that we could give
702 the appropriate warning for why a function can't be inlined, but that's
703 too late since the nesting structure has already been flattened and
704 adding a flag just to record this fact seems a waste of a flag. */
705
706 static bool
707 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
708 {
709 struct cgraph_node *cgn = cgraph_get_node (fndecl);
710 tree arg;
711
712 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
713 {
714 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
715 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
716 return true;
717
718 if (check_for_nested_with_variably_modified (cgn->decl,
719 orig_fndecl))
720 return true;
721 }
722
723 return false;
724 }
725
726 /* Construct our local datastructure describing the function nesting
727 tree rooted by CGN. */
728
729 static struct nesting_info *
730 create_nesting_tree (struct cgraph_node *cgn)
731 {
732 struct nesting_info *info = XCNEW (struct nesting_info);
733 info->field_map = pointer_map_create ();
734 info->var_map = pointer_map_create ();
735 info->mem_refs = pointer_set_create ();
736 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
737 info->context = cgn->decl;
738
739 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
740 {
741 struct nesting_info *sub = create_nesting_tree (cgn);
742 sub->outer = info;
743 sub->next = info->inner;
744 info->inner = sub;
745 }
746
747 /* See discussion at check_for_nested_with_variably_modified for a
748 discussion of why this has to be here. */
749 if (check_for_nested_with_variably_modified (info->context, info->context))
750 DECL_UNINLINABLE (info->context) = true;
751
752 return info;
753 }
754
755 /* Return an expression computing the static chain for TARGET_CONTEXT
756 from INFO->CONTEXT. Insert any necessary computations before TSI. */
757
758 static tree
759 get_static_chain (struct nesting_info *info, tree target_context,
760 gimple_stmt_iterator *gsi)
761 {
762 struct nesting_info *i;
763 tree x;
764
765 if (info->context == target_context)
766 {
767 x = build_addr (info->frame_decl, target_context);
768 }
769 else
770 {
771 x = get_chain_decl (info);
772
773 for (i = info->outer; i->context != target_context; i = i->outer)
774 {
775 tree field = get_chain_field (i);
776
777 x = build_simple_mem_ref (x);
778 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
779 x = init_tmp_var (info, x, gsi);
780 }
781 }
782
783 return x;
784 }
785
786
787 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
788 frame as seen from INFO->CONTEXT. Insert any necessary computations
789 before GSI. */
790
791 static tree
792 get_frame_field (struct nesting_info *info, tree target_context,
793 tree field, gimple_stmt_iterator *gsi)
794 {
795 struct nesting_info *i;
796 tree x;
797
798 if (info->context == target_context)
799 {
800 /* Make sure frame_decl gets created. */
801 (void) get_frame_type (info);
802 x = info->frame_decl;
803 }
804 else
805 {
806 x = get_chain_decl (info);
807
808 for (i = info->outer; i->context != target_context; i = i->outer)
809 {
810 tree field = get_chain_field (i);
811
812 x = build_simple_mem_ref (x);
813 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
814 x = init_tmp_var (info, x, gsi);
815 }
816
817 x = build_simple_mem_ref (x);
818 }
819
820 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
821 return x;
822 }
823
824 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
825
826 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
827 in the nested function with DECL_VALUE_EXPR set to reference the true
828 variable in the parent function. This is used both for debug info
829 and in OpenMP lowering. */
830
831 static tree
832 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
833 {
834 tree target_context;
835 struct nesting_info *i;
836 tree x, field, new_decl;
837 void **slot;
838
839 slot = pointer_map_insert (info->var_map, decl);
840
841 if (*slot)
842 return (tree) *slot;
843
844 target_context = decl_function_context (decl);
845
846 /* A copy of the code in get_frame_field, but without the temporaries. */
847 if (info->context == target_context)
848 {
849 /* Make sure frame_decl gets created. */
850 (void) get_frame_type (info);
851 x = info->frame_decl;
852 i = info;
853 }
854 else
855 {
856 x = get_chain_decl (info);
857 for (i = info->outer; i->context != target_context; i = i->outer)
858 {
859 field = get_chain_field (i);
860 x = build_simple_mem_ref (x);
861 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
862 }
863 x = build_simple_mem_ref (x);
864 }
865
866 field = lookup_field_for_decl (i, decl, INSERT);
867 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
868 if (use_pointer_in_frame (decl))
869 x = build_simple_mem_ref (x);
870
871 /* ??? We should be remapping types as well, surely. */
872 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
873 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
874 DECL_CONTEXT (new_decl) = info->context;
875 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
876 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
877 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
878 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
879 TREE_READONLY (new_decl) = TREE_READONLY (decl);
880 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
881 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
882 if ((TREE_CODE (decl) == PARM_DECL
883 || TREE_CODE (decl) == RESULT_DECL
884 || TREE_CODE (decl) == VAR_DECL)
885 && DECL_BY_REFERENCE (decl))
886 DECL_BY_REFERENCE (new_decl) = 1;
887
888 SET_DECL_VALUE_EXPR (new_decl, x);
889 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
890
891 *slot = new_decl;
892 DECL_CHAIN (new_decl) = info->debug_var_chain;
893 info->debug_var_chain = new_decl;
894
895 if (!optimize
896 && info->context != target_context
897 && variably_modified_type_p (TREE_TYPE (decl), NULL))
898 note_nonlocal_vla_type (info, TREE_TYPE (decl));
899
900 return new_decl;
901 }
902
903
904 /* Callback for walk_gimple_stmt, rewrite all references to VAR
905 and PARM_DECLs that belong to outer functions.
906
907 The rewrite will involve some number of structure accesses back up
908 the static chain. E.g. for a variable FOO up one nesting level it'll
909 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
910 indirections apply to decls for which use_pointer_in_frame is true. */
911
912 static tree
913 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
914 {
915 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
916 struct nesting_info *const info = (struct nesting_info *) wi->info;
917 tree t = *tp;
918
919 *walk_subtrees = 0;
920 switch (TREE_CODE (t))
921 {
922 case VAR_DECL:
923 /* Non-automatic variables are never processed. */
924 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
925 break;
926 /* FALLTHRU */
927
928 case PARM_DECL:
929 if (decl_function_context (t) != info->context)
930 {
931 tree x;
932 wi->changed = true;
933
934 x = get_nonlocal_debug_decl (info, t);
935 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
936 {
937 tree target_context = decl_function_context (t);
938 struct nesting_info *i;
939 for (i = info->outer; i->context != target_context; i = i->outer)
940 continue;
941 x = lookup_field_for_decl (i, t, INSERT);
942 x = get_frame_field (info, target_context, x, &wi->gsi);
943 if (use_pointer_in_frame (t))
944 {
945 x = init_tmp_var (info, x, &wi->gsi);
946 x = build_simple_mem_ref (x);
947 }
948 }
949
950 if (wi->val_only)
951 {
952 if (wi->is_lhs)
953 x = save_tmp_var (info, x, &wi->gsi);
954 else
955 x = init_tmp_var (info, x, &wi->gsi);
956 }
957
958 *tp = x;
959 }
960 break;
961
962 case LABEL_DECL:
963 /* We're taking the address of a label from a parent function, but
964 this is not itself a non-local goto. Mark the label such that it
965 will not be deleted, much as we would with a label address in
966 static storage. */
967 if (decl_function_context (t) != info->context)
968 FORCED_LABEL (t) = 1;
969 break;
970
971 case ADDR_EXPR:
972 {
973 bool save_val_only = wi->val_only;
974
975 wi->val_only = false;
976 wi->is_lhs = false;
977 wi->changed = false;
978 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
979 wi->val_only = true;
980
981 if (wi->changed)
982 {
983 tree save_context;
984
985 /* If we changed anything, we might no longer be directly
986 referencing a decl. */
987 save_context = current_function_decl;
988 current_function_decl = info->context;
989 recompute_tree_invariant_for_addr_expr (t);
990 current_function_decl = save_context;
991
992 /* If the callback converted the address argument in a context
993 where we only accept variables (and min_invariant, presumably),
994 then compute the address into a temporary. */
995 if (save_val_only)
996 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
997 t, &wi->gsi);
998 }
999 }
1000 break;
1001
1002 case REALPART_EXPR:
1003 case IMAGPART_EXPR:
1004 case COMPONENT_REF:
1005 case ARRAY_REF:
1006 case ARRAY_RANGE_REF:
1007 case BIT_FIELD_REF:
1008 /* Go down this entire nest and just look at the final prefix and
1009 anything that describes the references. Otherwise, we lose track
1010 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1011 wi->val_only = true;
1012 wi->is_lhs = false;
1013 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1014 {
1015 if (TREE_CODE (t) == COMPONENT_REF)
1016 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1017 NULL);
1018 else if (TREE_CODE (t) == ARRAY_REF
1019 || TREE_CODE (t) == ARRAY_RANGE_REF)
1020 {
1021 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1022 wi, NULL);
1023 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1024 wi, NULL);
1025 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1026 wi, NULL);
1027 }
1028 }
1029 wi->val_only = false;
1030 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1031 break;
1032
1033 case VIEW_CONVERT_EXPR:
1034 /* Just request to look at the subtrees, leaving val_only and lhs
1035 untouched. This might actually be for !val_only + lhs, in which
1036 case we don't want to force a replacement by a temporary. */
1037 *walk_subtrees = 1;
1038 break;
1039
1040 default:
1041 if (!IS_TYPE_OR_DECL_P (t))
1042 {
1043 *walk_subtrees = 1;
1044 wi->val_only = true;
1045 wi->is_lhs = false;
1046 }
1047 break;
1048 }
1049
1050 return NULL_TREE;
1051 }
1052
1053 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1054 struct walk_stmt_info *);
1055
1056 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1057 and PARM_DECLs that belong to outer functions. */
1058
1059 static bool
1060 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1061 {
1062 struct nesting_info *const info = (struct nesting_info *) wi->info;
1063 bool need_chain = false, need_stmts = false;
1064 tree clause, decl;
1065 int dummy;
1066 bitmap new_suppress;
1067
1068 new_suppress = BITMAP_GGC_ALLOC ();
1069 bitmap_copy (new_suppress, info->suppress_expansion);
1070
1071 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1072 {
1073 switch (OMP_CLAUSE_CODE (clause))
1074 {
1075 case OMP_CLAUSE_REDUCTION:
1076 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1077 need_stmts = true;
1078 goto do_decl_clause;
1079
1080 case OMP_CLAUSE_LASTPRIVATE:
1081 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1082 need_stmts = true;
1083 goto do_decl_clause;
1084
1085 case OMP_CLAUSE_LINEAR:
1086 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1087 need_stmts = true;
1088 goto do_decl_clause;
1089
1090 case OMP_CLAUSE_PRIVATE:
1091 case OMP_CLAUSE_FIRSTPRIVATE:
1092 case OMP_CLAUSE_COPYPRIVATE:
1093 case OMP_CLAUSE_SHARED:
1094 do_decl_clause:
1095 decl = OMP_CLAUSE_DECL (clause);
1096 if (TREE_CODE (decl) == VAR_DECL
1097 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1098 break;
1099 if (decl_function_context (decl) != info->context)
1100 {
1101 bitmap_set_bit (new_suppress, DECL_UID (decl));
1102 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1103 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1104 need_chain = true;
1105 }
1106 break;
1107
1108 case OMP_CLAUSE_SCHEDULE:
1109 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1110 break;
1111 /* FALLTHRU */
1112 case OMP_CLAUSE_FINAL:
1113 case OMP_CLAUSE_IF:
1114 case OMP_CLAUSE_NUM_THREADS:
1115 case OMP_CLAUSE_DEPEND:
1116 wi->val_only = true;
1117 wi->is_lhs = false;
1118 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1119 &dummy, wi);
1120 break;
1121
1122 case OMP_CLAUSE_NOWAIT:
1123 case OMP_CLAUSE_ORDERED:
1124 case OMP_CLAUSE_DEFAULT:
1125 case OMP_CLAUSE_COPYIN:
1126 case OMP_CLAUSE_COLLAPSE:
1127 case OMP_CLAUSE_UNTIED:
1128 case OMP_CLAUSE_MERGEABLE:
1129 break;
1130
1131 default:
1132 gcc_unreachable ();
1133 }
1134 }
1135
1136 info->suppress_expansion = new_suppress;
1137
1138 if (need_stmts)
1139 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1140 switch (OMP_CLAUSE_CODE (clause))
1141 {
1142 case OMP_CLAUSE_REDUCTION:
1143 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1144 {
1145 tree old_context
1146 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1147 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1148 = info->context;
1149 walk_body (convert_nonlocal_reference_stmt,
1150 convert_nonlocal_reference_op, info,
1151 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1152 walk_body (convert_nonlocal_reference_stmt,
1153 convert_nonlocal_reference_op, info,
1154 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1155 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1156 = old_context;
1157 }
1158 break;
1159
1160 case OMP_CLAUSE_LASTPRIVATE:
1161 walk_body (convert_nonlocal_reference_stmt,
1162 convert_nonlocal_reference_op, info,
1163 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1164 break;
1165
1166 case OMP_CLAUSE_LINEAR:
1167 walk_body (convert_nonlocal_reference_stmt,
1168 convert_nonlocal_reference_op, info,
1169 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1170 break;
1171
1172 default:
1173 break;
1174 }
1175
1176 return need_chain;
1177 }
1178
1179 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1180
1181 static void
1182 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1183 {
1184 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1185 type = TREE_TYPE (type);
1186
1187 if (TYPE_NAME (type)
1188 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1189 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1190 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1191
1192 while (POINTER_TYPE_P (type)
1193 || TREE_CODE (type) == VECTOR_TYPE
1194 || TREE_CODE (type) == FUNCTION_TYPE
1195 || TREE_CODE (type) == METHOD_TYPE)
1196 type = TREE_TYPE (type);
1197
1198 if (TREE_CODE (type) == ARRAY_TYPE)
1199 {
1200 tree domain, t;
1201
1202 note_nonlocal_vla_type (info, TREE_TYPE (type));
1203 domain = TYPE_DOMAIN (type);
1204 if (domain)
1205 {
1206 t = TYPE_MIN_VALUE (domain);
1207 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1208 && decl_function_context (t) != info->context)
1209 get_nonlocal_debug_decl (info, t);
1210 t = TYPE_MAX_VALUE (domain);
1211 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1212 && decl_function_context (t) != info->context)
1213 get_nonlocal_debug_decl (info, t);
1214 }
1215 }
1216 }
1217
1218 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1219 in BLOCK. */
1220
1221 static void
1222 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1223 {
1224 tree var;
1225
1226 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1227 if (TREE_CODE (var) == VAR_DECL
1228 && variably_modified_type_p (TREE_TYPE (var), NULL)
1229 && DECL_HAS_VALUE_EXPR_P (var)
1230 && decl_function_context (var) != info->context)
1231 note_nonlocal_vla_type (info, TREE_TYPE (var));
1232 }
1233
1234 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1235 PARM_DECLs that belong to outer functions. This handles statements
1236 that are not handled via the standard recursion done in
1237 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1238 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1239 operands of STMT have been handled by this function. */
1240
1241 static tree
1242 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1243 struct walk_stmt_info *wi)
1244 {
1245 struct nesting_info *info = (struct nesting_info *) wi->info;
1246 tree save_local_var_chain;
1247 bitmap save_suppress;
1248 gimple stmt = gsi_stmt (*gsi);
1249
1250 switch (gimple_code (stmt))
1251 {
1252 case GIMPLE_GOTO:
1253 /* Don't walk non-local gotos for now. */
1254 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1255 {
1256 wi->val_only = true;
1257 wi->is_lhs = false;
1258 *handled_ops_p = true;
1259 return NULL_TREE;
1260 }
1261 break;
1262
1263 case GIMPLE_OMP_PARALLEL:
1264 case GIMPLE_OMP_TASK:
1265 save_suppress = info->suppress_expansion;
1266 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1267 wi))
1268 {
1269 tree c, decl;
1270 decl = get_chain_decl (info);
1271 c = build_omp_clause (gimple_location (stmt),
1272 OMP_CLAUSE_FIRSTPRIVATE);
1273 OMP_CLAUSE_DECL (c) = decl;
1274 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1275 gimple_omp_taskreg_set_clauses (stmt, c);
1276 }
1277
1278 save_local_var_chain = info->new_local_var_chain;
1279 info->new_local_var_chain = NULL;
1280
1281 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1282 info, gimple_omp_body_ptr (stmt));
1283
1284 if (info->new_local_var_chain)
1285 declare_vars (info->new_local_var_chain,
1286 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1287 false);
1288 info->new_local_var_chain = save_local_var_chain;
1289 info->suppress_expansion = save_suppress;
1290 break;
1291
1292 case GIMPLE_OMP_FOR:
1293 save_suppress = info->suppress_expansion;
1294 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1295 walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1296 convert_nonlocal_reference_op, info);
1297 walk_body (convert_nonlocal_reference_stmt,
1298 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1299 info->suppress_expansion = save_suppress;
1300 break;
1301
1302 case GIMPLE_OMP_SECTIONS:
1303 save_suppress = info->suppress_expansion;
1304 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1305 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1306 info, gimple_omp_body_ptr (stmt));
1307 info->suppress_expansion = save_suppress;
1308 break;
1309
1310 case GIMPLE_OMP_SINGLE:
1311 save_suppress = info->suppress_expansion;
1312 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1313 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1314 info, gimple_omp_body_ptr (stmt));
1315 info->suppress_expansion = save_suppress;
1316 break;
1317
1318 case GIMPLE_OMP_TARGET:
1319 save_suppress = info->suppress_expansion;
1320 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1321 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1322 info, gimple_omp_body_ptr (stmt));
1323 info->suppress_expansion = save_suppress;
1324 break;
1325
1326 case GIMPLE_OMP_TEAMS:
1327 save_suppress = info->suppress_expansion;
1328 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1329 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1330 info, gimple_omp_body_ptr (stmt));
1331 info->suppress_expansion = save_suppress;
1332 break;
1333
1334 case GIMPLE_OMP_SECTION:
1335 case GIMPLE_OMP_MASTER:
1336 case GIMPLE_OMP_TASKGROUP:
1337 case GIMPLE_OMP_ORDERED:
1338 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1339 info, gimple_omp_body_ptr (stmt));
1340 break;
1341
1342 case GIMPLE_BIND:
1343 if (!optimize && gimple_bind_block (stmt))
1344 note_nonlocal_block_vlas (info, gimple_bind_block (stmt));
1345
1346 for (tree var = gimple_bind_vars (stmt); var; var = DECL_CHAIN (var))
1347 if (TREE_CODE (var) == NAMELIST_DECL)
1348 {
1349 /* Adjust decls mentioned in NAMELIST_DECL. */
1350 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1351 tree decl;
1352 unsigned int i;
1353
1354 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1355 {
1356 if (TREE_CODE (decl) == VAR_DECL
1357 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1358 continue;
1359 if (decl_function_context (decl) != info->context)
1360 CONSTRUCTOR_ELT (decls, i)->value
1361 = get_nonlocal_debug_decl (info, decl);
1362 }
1363 }
1364
1365 *handled_ops_p = false;
1366 return NULL_TREE;
1367
1368 case GIMPLE_COND:
1369 wi->val_only = true;
1370 wi->is_lhs = false;
1371 *handled_ops_p = false;
1372 return NULL_TREE;
1373
1374 default:
1375 /* For every other statement that we are not interested in
1376 handling here, let the walker traverse the operands. */
1377 *handled_ops_p = false;
1378 return NULL_TREE;
1379 }
1380
1381 /* We have handled all of STMT operands, no need to traverse the operands. */
1382 *handled_ops_p = true;
1383 return NULL_TREE;
1384 }
1385
1386
1387 /* A subroutine of convert_local_reference. Create a local variable
1388 in the parent function with DECL_VALUE_EXPR set to reference the
1389 field in FRAME. This is used both for debug info and in OpenMP
1390 lowering. */
1391
1392 static tree
1393 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1394 {
1395 tree x, new_decl;
1396 void **slot;
1397
1398 slot = pointer_map_insert (info->var_map, decl);
1399 if (*slot)
1400 return (tree) *slot;
1401
1402 /* Make sure frame_decl gets created. */
1403 (void) get_frame_type (info);
1404 x = info->frame_decl;
1405 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1406
1407 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1408 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1409 DECL_CONTEXT (new_decl) = info->context;
1410 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1411 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1412 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1413 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1414 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1415 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1416 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1417 if ((TREE_CODE (decl) == PARM_DECL
1418 || TREE_CODE (decl) == RESULT_DECL
1419 || TREE_CODE (decl) == VAR_DECL)
1420 && DECL_BY_REFERENCE (decl))
1421 DECL_BY_REFERENCE (new_decl) = 1;
1422
1423 SET_DECL_VALUE_EXPR (new_decl, x);
1424 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1425 *slot = new_decl;
1426
1427 DECL_CHAIN (new_decl) = info->debug_var_chain;
1428 info->debug_var_chain = new_decl;
1429
1430 /* Do not emit debug info twice. */
1431 DECL_IGNORED_P (decl) = 1;
1432
1433 return new_decl;
1434 }
1435
1436
1437 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1438 and PARM_DECLs that were referenced by inner nested functions.
1439 The rewrite will be a structure reference to the local frame variable. */
1440
1441 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1442
1443 static tree
1444 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1445 {
1446 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1447 struct nesting_info *const info = (struct nesting_info *) wi->info;
1448 tree t = *tp, field, x;
1449 bool save_val_only;
1450
1451 *walk_subtrees = 0;
1452 switch (TREE_CODE (t))
1453 {
1454 case VAR_DECL:
1455 /* Non-automatic variables are never processed. */
1456 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1457 break;
1458 /* FALLTHRU */
1459
1460 case PARM_DECL:
1461 if (decl_function_context (t) == info->context)
1462 {
1463 /* If we copied a pointer to the frame, then the original decl
1464 is used unchanged in the parent function. */
1465 if (use_pointer_in_frame (t))
1466 break;
1467
1468 /* No need to transform anything if no child references the
1469 variable. */
1470 field = lookup_field_for_decl (info, t, NO_INSERT);
1471 if (!field)
1472 break;
1473 wi->changed = true;
1474
1475 x = get_local_debug_decl (info, t, field);
1476 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1477 x = get_frame_field (info, info->context, field, &wi->gsi);
1478
1479 if (wi->val_only)
1480 {
1481 if (wi->is_lhs)
1482 x = save_tmp_var (info, x, &wi->gsi);
1483 else
1484 x = init_tmp_var (info, x, &wi->gsi);
1485 }
1486
1487 *tp = x;
1488 }
1489 break;
1490
1491 case ADDR_EXPR:
1492 save_val_only = wi->val_only;
1493 wi->val_only = false;
1494 wi->is_lhs = false;
1495 wi->changed = false;
1496 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1497 wi->val_only = save_val_only;
1498
1499 /* If we converted anything ... */
1500 if (wi->changed)
1501 {
1502 tree save_context;
1503
1504 /* Then the frame decl is now addressable. */
1505 TREE_ADDRESSABLE (info->frame_decl) = 1;
1506
1507 save_context = current_function_decl;
1508 current_function_decl = info->context;
1509 recompute_tree_invariant_for_addr_expr (t);
1510 current_function_decl = save_context;
1511
1512 /* If we are in a context where we only accept values, then
1513 compute the address into a temporary. */
1514 if (save_val_only)
1515 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1516 t, &wi->gsi);
1517 }
1518 break;
1519
1520 case REALPART_EXPR:
1521 case IMAGPART_EXPR:
1522 case COMPONENT_REF:
1523 case ARRAY_REF:
1524 case ARRAY_RANGE_REF:
1525 case BIT_FIELD_REF:
1526 /* Go down this entire nest and just look at the final prefix and
1527 anything that describes the references. Otherwise, we lose track
1528 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1529 save_val_only = wi->val_only;
1530 wi->val_only = true;
1531 wi->is_lhs = false;
1532 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1533 {
1534 if (TREE_CODE (t) == COMPONENT_REF)
1535 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1536 NULL);
1537 else if (TREE_CODE (t) == ARRAY_REF
1538 || TREE_CODE (t) == ARRAY_RANGE_REF)
1539 {
1540 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1541 NULL);
1542 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1543 NULL);
1544 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1545 NULL);
1546 }
1547 }
1548 wi->val_only = false;
1549 walk_tree (tp, convert_local_reference_op, wi, NULL);
1550 wi->val_only = save_val_only;
1551 break;
1552
1553 case MEM_REF:
1554 save_val_only = wi->val_only;
1555 wi->val_only = true;
1556 wi->is_lhs = false;
1557 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1558 wi, NULL);
1559 /* We need to re-fold the MEM_REF as component references as
1560 part of a ADDR_EXPR address are not allowed. But we cannot
1561 fold here, as the chain record type is not yet finalized. */
1562 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1563 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1564 pointer_set_insert (info->mem_refs, tp);
1565 wi->val_only = save_val_only;
1566 break;
1567
1568 case VIEW_CONVERT_EXPR:
1569 /* Just request to look at the subtrees, leaving val_only and lhs
1570 untouched. This might actually be for !val_only + lhs, in which
1571 case we don't want to force a replacement by a temporary. */
1572 *walk_subtrees = 1;
1573 break;
1574
1575 default:
1576 if (!IS_TYPE_OR_DECL_P (t))
1577 {
1578 *walk_subtrees = 1;
1579 wi->val_only = true;
1580 wi->is_lhs = false;
1581 }
1582 break;
1583 }
1584
1585 return NULL_TREE;
1586 }
1587
1588 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1589 struct walk_stmt_info *);
1590
1591 /* Helper for convert_local_reference. Convert all the references in
1592 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1593
1594 static bool
1595 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1596 {
1597 struct nesting_info *const info = (struct nesting_info *) wi->info;
1598 bool need_frame = false, need_stmts = false;
1599 tree clause, decl;
1600 int dummy;
1601 bitmap new_suppress;
1602
1603 new_suppress = BITMAP_GGC_ALLOC ();
1604 bitmap_copy (new_suppress, info->suppress_expansion);
1605
1606 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1607 {
1608 switch (OMP_CLAUSE_CODE (clause))
1609 {
1610 case OMP_CLAUSE_REDUCTION:
1611 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1612 need_stmts = true;
1613 goto do_decl_clause;
1614
1615 case OMP_CLAUSE_LASTPRIVATE:
1616 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1617 need_stmts = true;
1618 goto do_decl_clause;
1619
1620 case OMP_CLAUSE_LINEAR:
1621 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1622 need_stmts = true;
1623 goto do_decl_clause;
1624
1625 case OMP_CLAUSE_PRIVATE:
1626 case OMP_CLAUSE_FIRSTPRIVATE:
1627 case OMP_CLAUSE_COPYPRIVATE:
1628 case OMP_CLAUSE_SHARED:
1629 do_decl_clause:
1630 decl = OMP_CLAUSE_DECL (clause);
1631 if (TREE_CODE (decl) == VAR_DECL
1632 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1633 break;
1634 if (decl_function_context (decl) == info->context
1635 && !use_pointer_in_frame (decl))
1636 {
1637 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1638 if (field)
1639 {
1640 bitmap_set_bit (new_suppress, DECL_UID (decl));
1641 OMP_CLAUSE_DECL (clause)
1642 = get_local_debug_decl (info, decl, field);
1643 need_frame = true;
1644 }
1645 }
1646 break;
1647
1648 case OMP_CLAUSE_SCHEDULE:
1649 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1650 break;
1651 /* FALLTHRU */
1652 case OMP_CLAUSE_FINAL:
1653 case OMP_CLAUSE_IF:
1654 case OMP_CLAUSE_NUM_THREADS:
1655 case OMP_CLAUSE_DEPEND:
1656 wi->val_only = true;
1657 wi->is_lhs = false;
1658 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1659 wi);
1660 break;
1661
1662 case OMP_CLAUSE_NOWAIT:
1663 case OMP_CLAUSE_ORDERED:
1664 case OMP_CLAUSE_DEFAULT:
1665 case OMP_CLAUSE_COPYIN:
1666 case OMP_CLAUSE_COLLAPSE:
1667 case OMP_CLAUSE_UNTIED:
1668 case OMP_CLAUSE_MERGEABLE:
1669 break;
1670
1671 default:
1672 gcc_unreachable ();
1673 }
1674 }
1675
1676 info->suppress_expansion = new_suppress;
1677
1678 if (need_stmts)
1679 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1680 switch (OMP_CLAUSE_CODE (clause))
1681 {
1682 case OMP_CLAUSE_REDUCTION:
1683 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1684 {
1685 tree old_context
1686 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1687 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1688 = info->context;
1689 walk_body (convert_local_reference_stmt,
1690 convert_local_reference_op, info,
1691 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1692 walk_body (convert_local_reference_stmt,
1693 convert_local_reference_op, info,
1694 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1695 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1696 = old_context;
1697 }
1698 break;
1699
1700 case OMP_CLAUSE_LASTPRIVATE:
1701 walk_body (convert_local_reference_stmt,
1702 convert_local_reference_op, info,
1703 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1704 break;
1705
1706 case OMP_CLAUSE_LINEAR:
1707 walk_body (convert_local_reference_stmt,
1708 convert_local_reference_op, info,
1709 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1710 break;
1711
1712 default:
1713 break;
1714 }
1715
1716 return need_frame;
1717 }
1718
1719
1720 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1721 and PARM_DECLs that were referenced by inner nested functions.
1722 The rewrite will be a structure reference to the local frame variable. */
1723
1724 static tree
1725 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1726 struct walk_stmt_info *wi)
1727 {
1728 struct nesting_info *info = (struct nesting_info *) wi->info;
1729 tree save_local_var_chain;
1730 bitmap save_suppress;
1731 gimple stmt = gsi_stmt (*gsi);
1732
1733 switch (gimple_code (stmt))
1734 {
1735 case GIMPLE_OMP_PARALLEL:
1736 case GIMPLE_OMP_TASK:
1737 save_suppress = info->suppress_expansion;
1738 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1739 wi))
1740 {
1741 tree c;
1742 (void) get_frame_type (info);
1743 c = build_omp_clause (gimple_location (stmt),
1744 OMP_CLAUSE_SHARED);
1745 OMP_CLAUSE_DECL (c) = info->frame_decl;
1746 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1747 gimple_omp_taskreg_set_clauses (stmt, c);
1748 }
1749
1750 save_local_var_chain = info->new_local_var_chain;
1751 info->new_local_var_chain = NULL;
1752
1753 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1754 gimple_omp_body_ptr (stmt));
1755
1756 if (info->new_local_var_chain)
1757 declare_vars (info->new_local_var_chain,
1758 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1759 info->new_local_var_chain = save_local_var_chain;
1760 info->suppress_expansion = save_suppress;
1761 break;
1762
1763 case GIMPLE_OMP_FOR:
1764 save_suppress = info->suppress_expansion;
1765 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1766 walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1767 convert_local_reference_op, info);
1768 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1769 info, gimple_omp_body_ptr (stmt));
1770 info->suppress_expansion = save_suppress;
1771 break;
1772
1773 case GIMPLE_OMP_SECTIONS:
1774 save_suppress = info->suppress_expansion;
1775 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1776 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1777 info, gimple_omp_body_ptr (stmt));
1778 info->suppress_expansion = save_suppress;
1779 break;
1780
1781 case GIMPLE_OMP_SINGLE:
1782 save_suppress = info->suppress_expansion;
1783 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1784 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1785 info, gimple_omp_body_ptr (stmt));
1786 info->suppress_expansion = save_suppress;
1787 break;
1788
1789 case GIMPLE_OMP_TARGET:
1790 save_suppress = info->suppress_expansion;
1791 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
1792 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1793 info, gimple_omp_body_ptr (stmt));
1794 info->suppress_expansion = save_suppress;
1795 break;
1796
1797 case GIMPLE_OMP_TEAMS:
1798 save_suppress = info->suppress_expansion;
1799 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1800 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1801 info, gimple_omp_body_ptr (stmt));
1802 info->suppress_expansion = save_suppress;
1803 break;
1804
1805 case GIMPLE_OMP_SECTION:
1806 case GIMPLE_OMP_MASTER:
1807 case GIMPLE_OMP_TASKGROUP:
1808 case GIMPLE_OMP_ORDERED:
1809 walk_body (convert_local_reference_stmt, convert_local_reference_op,
1810 info, gimple_omp_body_ptr (stmt));
1811 break;
1812
1813 case GIMPLE_COND:
1814 wi->val_only = true;
1815 wi->is_lhs = false;
1816 *handled_ops_p = false;
1817 return NULL_TREE;
1818
1819 case GIMPLE_ASSIGN:
1820 if (gimple_clobber_p (stmt))
1821 {
1822 tree lhs = gimple_assign_lhs (stmt);
1823 if (!use_pointer_in_frame (lhs)
1824 && lookup_field_for_decl (info, lhs, NO_INSERT))
1825 {
1826 gsi_replace (gsi, gimple_build_nop (), true);
1827 break;
1828 }
1829 }
1830 *handled_ops_p = false;
1831 return NULL_TREE;
1832
1833 case GIMPLE_BIND:
1834 for (tree var = gimple_bind_vars (stmt); var; var = DECL_CHAIN (var))
1835 if (TREE_CODE (var) == NAMELIST_DECL)
1836 {
1837 /* Adjust decls mentioned in NAMELIST_DECL. */
1838 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1839 tree decl;
1840 unsigned int i;
1841
1842 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1843 {
1844 if (TREE_CODE (decl) == VAR_DECL
1845 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1846 continue;
1847 if (decl_function_context (decl) == info->context
1848 && !use_pointer_in_frame (decl))
1849 {
1850 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1851 if (field)
1852 {
1853 CONSTRUCTOR_ELT (decls, i)->value
1854 = get_local_debug_decl (info, decl, field);
1855 }
1856 }
1857 }
1858 }
1859
1860 *handled_ops_p = false;
1861 return NULL_TREE;
1862
1863 default:
1864 /* For every other statement that we are not interested in
1865 handling here, let the walker traverse the operands. */
1866 *handled_ops_p = false;
1867 return NULL_TREE;
1868 }
1869
1870 /* Indicate that we have handled all the operands ourselves. */
1871 *handled_ops_p = true;
1872 return NULL_TREE;
1873 }
1874
1875
1876 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1877 that reference labels from outer functions. The rewrite will be a
1878 call to __builtin_nonlocal_goto. */
1879
1880 static tree
1881 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1882 struct walk_stmt_info *wi)
1883 {
1884 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1885 tree label, new_label, target_context, x, field;
1886 void **slot;
1887 gimple call;
1888 gimple stmt = gsi_stmt (*gsi);
1889
1890 if (gimple_code (stmt) != GIMPLE_GOTO)
1891 {
1892 *handled_ops_p = false;
1893 return NULL_TREE;
1894 }
1895
1896 label = gimple_goto_dest (stmt);
1897 if (TREE_CODE (label) != LABEL_DECL)
1898 {
1899 *handled_ops_p = false;
1900 return NULL_TREE;
1901 }
1902
1903 target_context = decl_function_context (label);
1904 if (target_context == info->context)
1905 {
1906 *handled_ops_p = false;
1907 return NULL_TREE;
1908 }
1909
1910 for (i = info->outer; target_context != i->context; i = i->outer)
1911 continue;
1912
1913 /* The original user label may also be use for a normal goto, therefore
1914 we must create a new label that will actually receive the abnormal
1915 control transfer. This new label will be marked LABEL_NONLOCAL; this
1916 mark will trigger proper behavior in the cfg, as well as cause the
1917 (hairy target-specific) non-local goto receiver code to be generated
1918 when we expand rtl. Enter this association into var_map so that we
1919 can insert the new label into the IL during a second pass. */
1920 slot = pointer_map_insert (i->var_map, label);
1921 if (*slot == NULL)
1922 {
1923 new_label = create_artificial_label (UNKNOWN_LOCATION);
1924 DECL_NONLOCAL (new_label) = 1;
1925 *slot = new_label;
1926 }
1927 else
1928 new_label = (tree) *slot;
1929
1930 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1931 field = get_nl_goto_field (i);
1932 x = get_frame_field (info, target_context, field, gsi);
1933 x = build_addr (x, target_context);
1934 x = gsi_gimplify_val (info, x, gsi);
1935 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
1936 2, build_addr (new_label, target_context), x);
1937 gsi_replace (gsi, call, false);
1938
1939 /* We have handled all of STMT's operands, no need to keep going. */
1940 *handled_ops_p = true;
1941 return NULL_TREE;
1942 }
1943
1944
1945 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1946 are referenced via nonlocal goto from a nested function. The rewrite
1947 will involve installing a newly generated DECL_NONLOCAL label, and
1948 (potentially) a branch around the rtl gunk that is assumed to be
1949 attached to such a label. */
1950
1951 static tree
1952 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1953 struct walk_stmt_info *wi)
1954 {
1955 struct nesting_info *const info = (struct nesting_info *) wi->info;
1956 tree label, new_label;
1957 gimple_stmt_iterator tmp_gsi;
1958 void **slot;
1959 gimple stmt = gsi_stmt (*gsi);
1960
1961 if (gimple_code (stmt) != GIMPLE_LABEL)
1962 {
1963 *handled_ops_p = false;
1964 return NULL_TREE;
1965 }
1966
1967 label = gimple_label_label (stmt);
1968
1969 slot = pointer_map_contains (info->var_map, label);
1970 if (!slot)
1971 {
1972 *handled_ops_p = false;
1973 return NULL_TREE;
1974 }
1975
1976 /* If there's any possibility that the previous statement falls through,
1977 then we must branch around the new non-local label. */
1978 tmp_gsi = wi->gsi;
1979 gsi_prev (&tmp_gsi);
1980 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
1981 {
1982 gimple stmt = gimple_build_goto (label);
1983 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1984 }
1985
1986 new_label = (tree) *slot;
1987 stmt = gimple_build_label (new_label);
1988 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1989
1990 *handled_ops_p = true;
1991 return NULL_TREE;
1992 }
1993
1994
1995 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1996 of nested functions that require the use of trampolines. The rewrite
1997 will involve a reference a trampoline generated for the occasion. */
1998
1999 static tree
2000 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2001 {
2002 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2003 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2004 tree t = *tp, decl, target_context, x, builtin;
2005 gimple call;
2006
2007 *walk_subtrees = 0;
2008 switch (TREE_CODE (t))
2009 {
2010 case ADDR_EXPR:
2011 /* Build
2012 T.1 = &CHAIN->tramp;
2013 T.2 = __builtin_adjust_trampoline (T.1);
2014 T.3 = (func_type)T.2;
2015 */
2016
2017 decl = TREE_OPERAND (t, 0);
2018 if (TREE_CODE (decl) != FUNCTION_DECL)
2019 break;
2020
2021 /* Only need to process nested functions. */
2022 target_context = decl_function_context (decl);
2023 if (!target_context)
2024 break;
2025
2026 /* If the nested function doesn't use a static chain, then
2027 it doesn't need a trampoline. */
2028 if (!DECL_STATIC_CHAIN (decl))
2029 break;
2030
2031 /* If we don't want a trampoline, then don't build one. */
2032 if (TREE_NO_TRAMPOLINE (t))
2033 break;
2034
2035 /* Lookup the immediate parent of the callee, as that's where
2036 we need to insert the trampoline. */
2037 for (i = info; i->context != target_context; i = i->outer)
2038 continue;
2039 x = lookup_tramp_for_decl (i, decl, INSERT);
2040
2041 /* Compute the address of the field holding the trampoline. */
2042 x = get_frame_field (info, target_context, x, &wi->gsi);
2043 x = build_addr (x, target_context);
2044 x = gsi_gimplify_val (info, x, &wi->gsi);
2045
2046 /* Do machine-specific ugliness. Normally this will involve
2047 computing extra alignment, but it can really be anything. */
2048 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2049 call = gimple_build_call (builtin, 1, x);
2050 x = init_tmp_var_with_call (info, &wi->gsi, call);
2051
2052 /* Cast back to the proper function type. */
2053 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2054 x = init_tmp_var (info, x, &wi->gsi);
2055
2056 *tp = x;
2057 break;
2058
2059 default:
2060 if (!IS_TYPE_OR_DECL_P (t))
2061 *walk_subtrees = 1;
2062 break;
2063 }
2064
2065 return NULL_TREE;
2066 }
2067
2068
2069 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2070 to addresses of nested functions that require the use of
2071 trampolines. The rewrite will involve a reference a trampoline
2072 generated for the occasion. */
2073
2074 static tree
2075 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2076 struct walk_stmt_info *wi)
2077 {
2078 struct nesting_info *info = (struct nesting_info *) wi->info;
2079 gimple stmt = gsi_stmt (*gsi);
2080
2081 switch (gimple_code (stmt))
2082 {
2083 case GIMPLE_CALL:
2084 {
2085 /* Only walk call arguments, lest we generate trampolines for
2086 direct calls. */
2087 unsigned long i, nargs = gimple_call_num_args (stmt);
2088 for (i = 0; i < nargs; i++)
2089 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2090 wi, NULL);
2091 break;
2092 }
2093
2094 case GIMPLE_OMP_PARALLEL:
2095 case GIMPLE_OMP_TASK:
2096 {
2097 tree save_local_var_chain;
2098 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2099 save_local_var_chain = info->new_local_var_chain;
2100 info->new_local_var_chain = NULL;
2101 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2102 info, gimple_omp_body_ptr (stmt));
2103 if (info->new_local_var_chain)
2104 declare_vars (info->new_local_var_chain,
2105 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2106 false);
2107 info->new_local_var_chain = save_local_var_chain;
2108 }
2109 break;
2110
2111 default:
2112 *handled_ops_p = false;
2113 return NULL_TREE;
2114 break;
2115 }
2116
2117 *handled_ops_p = true;
2118 return NULL_TREE;
2119 }
2120
2121
2122
2123 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2124 that reference nested functions to make sure that the static chain
2125 is set up properly for the call. */
2126
2127 static tree
2128 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2129 struct walk_stmt_info *wi)
2130 {
2131 struct nesting_info *const info = (struct nesting_info *) wi->info;
2132 tree decl, target_context;
2133 char save_static_chain_added;
2134 int i;
2135 gimple stmt = gsi_stmt (*gsi);
2136
2137 switch (gimple_code (stmt))
2138 {
2139 case GIMPLE_CALL:
2140 if (gimple_call_chain (stmt))
2141 break;
2142 decl = gimple_call_fndecl (stmt);
2143 if (!decl)
2144 break;
2145 target_context = decl_function_context (decl);
2146 if (target_context && DECL_STATIC_CHAIN (decl))
2147 {
2148 gimple_call_set_chain (stmt, get_static_chain (info, target_context,
2149 &wi->gsi));
2150 info->static_chain_added |= (1 << (info->context != target_context));
2151 }
2152 break;
2153
2154 case GIMPLE_OMP_PARALLEL:
2155 case GIMPLE_OMP_TASK:
2156 save_static_chain_added = info->static_chain_added;
2157 info->static_chain_added = 0;
2158 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2159 for (i = 0; i < 2; i++)
2160 {
2161 tree c, decl;
2162 if ((info->static_chain_added & (1 << i)) == 0)
2163 continue;
2164 decl = i ? get_chain_decl (info) : info->frame_decl;
2165 /* Don't add CHAIN.* or FRAME.* twice. */
2166 for (c = gimple_omp_taskreg_clauses (stmt);
2167 c;
2168 c = OMP_CLAUSE_CHAIN (c))
2169 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2170 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2171 && OMP_CLAUSE_DECL (c) == decl)
2172 break;
2173 if (c == NULL)
2174 {
2175 c = build_omp_clause (gimple_location (stmt),
2176 i ? OMP_CLAUSE_FIRSTPRIVATE
2177 : OMP_CLAUSE_SHARED);
2178 OMP_CLAUSE_DECL (c) = decl;
2179 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2180 gimple_omp_taskreg_set_clauses (stmt, c);
2181 }
2182 }
2183 info->static_chain_added |= save_static_chain_added;
2184 break;
2185
2186 case GIMPLE_OMP_FOR:
2187 walk_body (convert_gimple_call, NULL, info,
2188 gimple_omp_for_pre_body_ptr (stmt));
2189 /* FALLTHRU */
2190 case GIMPLE_OMP_SECTIONS:
2191 case GIMPLE_OMP_SECTION:
2192 case GIMPLE_OMP_SINGLE:
2193 case GIMPLE_OMP_TARGET:
2194 case GIMPLE_OMP_TEAMS:
2195 case GIMPLE_OMP_MASTER:
2196 case GIMPLE_OMP_TASKGROUP:
2197 case GIMPLE_OMP_ORDERED:
2198 case GIMPLE_OMP_CRITICAL:
2199 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2200 break;
2201
2202 default:
2203 /* Keep looking for other operands. */
2204 *handled_ops_p = false;
2205 return NULL_TREE;
2206 }
2207
2208 *handled_ops_p = true;
2209 return NULL_TREE;
2210 }
2211
2212 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2213 call expressions. At the same time, determine if a nested function
2214 actually uses its static chain; if not, remember that. */
2215
2216 static void
2217 convert_all_function_calls (struct nesting_info *root)
2218 {
2219 unsigned int chain_count = 0, old_chain_count, iter_count;
2220 struct nesting_info *n;
2221
2222 /* First, optimistically clear static_chain for all decls that haven't
2223 used the static chain already for variable access. But always create
2224 it if not optimizing. This makes it possible to reconstruct the static
2225 nesting tree at run time and thus to resolve up-level references from
2226 within the debugger. */
2227 FOR_EACH_NEST_INFO (n, root)
2228 {
2229 tree decl = n->context;
2230 if (!optimize)
2231 {
2232 if (n->inner)
2233 (void) get_frame_type (n);
2234 if (n->outer)
2235 (void) get_chain_decl (n);
2236 }
2237 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2238 {
2239 DECL_STATIC_CHAIN (decl) = 0;
2240 if (dump_file && (dump_flags & TDF_DETAILS))
2241 fprintf (dump_file, "Guessing no static-chain for %s\n",
2242 lang_hooks.decl_printable_name (decl, 2));
2243 }
2244 else
2245 DECL_STATIC_CHAIN (decl) = 1;
2246 chain_count += DECL_STATIC_CHAIN (decl);
2247 }
2248
2249 /* Walk the functions and perform transformations. Note that these
2250 transformations can induce new uses of the static chain, which in turn
2251 require re-examining all users of the decl. */
2252 /* ??? It would make sense to try to use the call graph to speed this up,
2253 but the call graph hasn't really been built yet. Even if it did, we
2254 would still need to iterate in this loop since address-of references
2255 wouldn't show up in the callgraph anyway. */
2256 iter_count = 0;
2257 do
2258 {
2259 old_chain_count = chain_count;
2260 chain_count = 0;
2261 iter_count++;
2262
2263 if (dump_file && (dump_flags & TDF_DETAILS))
2264 fputc ('\n', dump_file);
2265
2266 FOR_EACH_NEST_INFO (n, root)
2267 {
2268 tree decl = n->context;
2269 walk_function (convert_tramp_reference_stmt,
2270 convert_tramp_reference_op, n);
2271 walk_function (convert_gimple_call, NULL, n);
2272 chain_count += DECL_STATIC_CHAIN (decl);
2273 }
2274 }
2275 while (chain_count != old_chain_count);
2276
2277 if (dump_file && (dump_flags & TDF_DETAILS))
2278 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2279 iter_count);
2280 }
2281
2282 struct nesting_copy_body_data
2283 {
2284 copy_body_data cb;
2285 struct nesting_info *root;
2286 };
2287
2288 /* A helper subroutine for debug_var_chain type remapping. */
2289
2290 static tree
2291 nesting_copy_decl (tree decl, copy_body_data *id)
2292 {
2293 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2294 void **slot = pointer_map_contains (nid->root->var_map, decl);
2295
2296 if (slot)
2297 return (tree) *slot;
2298
2299 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2300 {
2301 tree new_decl = copy_decl_no_change (decl, id);
2302 DECL_ORIGINAL_TYPE (new_decl)
2303 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2304 return new_decl;
2305 }
2306
2307 if (TREE_CODE (decl) == VAR_DECL
2308 || TREE_CODE (decl) == PARM_DECL
2309 || TREE_CODE (decl) == RESULT_DECL)
2310 return decl;
2311
2312 return copy_decl_no_change (decl, id);
2313 }
2314
2315 /* A helper function for remap_vla_decls. See if *TP contains
2316 some remapped variables. */
2317
2318 static tree
2319 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2320 {
2321 struct nesting_info *root = (struct nesting_info *) data;
2322 tree t = *tp;
2323 void **slot;
2324
2325 if (DECL_P (t))
2326 {
2327 *walk_subtrees = 0;
2328 slot = pointer_map_contains (root->var_map, t);
2329
2330 if (slot)
2331 return (tree) *slot;
2332 }
2333 return NULL;
2334 }
2335
2336 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2337 involved. */
2338
2339 static void
2340 remap_vla_decls (tree block, struct nesting_info *root)
2341 {
2342 tree var, subblock, val, type;
2343 struct nesting_copy_body_data id;
2344
2345 for (subblock = BLOCK_SUBBLOCKS (block);
2346 subblock;
2347 subblock = BLOCK_CHAIN (subblock))
2348 remap_vla_decls (subblock, root);
2349
2350 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2351 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2352 {
2353 val = DECL_VALUE_EXPR (var);
2354 type = TREE_TYPE (var);
2355
2356 if (!(TREE_CODE (val) == INDIRECT_REF
2357 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2358 && variably_modified_type_p (type, NULL)))
2359 continue;
2360
2361 if (pointer_map_contains (root->var_map, TREE_OPERAND (val, 0))
2362 || walk_tree (&type, contains_remapped_vars, root, NULL))
2363 break;
2364 }
2365
2366 if (var == NULL_TREE)
2367 return;
2368
2369 memset (&id, 0, sizeof (id));
2370 id.cb.copy_decl = nesting_copy_decl;
2371 id.cb.decl_map = pointer_map_create ();
2372 id.root = root;
2373
2374 for (; var; var = DECL_CHAIN (var))
2375 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2376 {
2377 struct nesting_info *i;
2378 tree newt, context;
2379 void **slot;
2380
2381 val = DECL_VALUE_EXPR (var);
2382 type = TREE_TYPE (var);
2383
2384 if (!(TREE_CODE (val) == INDIRECT_REF
2385 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2386 && variably_modified_type_p (type, NULL)))
2387 continue;
2388
2389 slot = pointer_map_contains (root->var_map, TREE_OPERAND (val, 0));
2390 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2391 continue;
2392
2393 context = decl_function_context (var);
2394 for (i = root; i; i = i->outer)
2395 if (i->context == context)
2396 break;
2397
2398 if (i == NULL)
2399 continue;
2400
2401 /* Fully expand value expressions. This avoids having debug variables
2402 only referenced from them and that can be swept during GC. */
2403 if (slot)
2404 {
2405 tree t = (tree) *slot;
2406 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2407 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2408 }
2409
2410 id.cb.src_fn = i->context;
2411 id.cb.dst_fn = i->context;
2412 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2413
2414 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2415 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2416 {
2417 newt = TREE_TYPE (newt);
2418 type = TREE_TYPE (type);
2419 }
2420 if (TYPE_NAME (newt)
2421 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2422 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2423 && newt != type
2424 && TYPE_NAME (newt) == TYPE_NAME (type))
2425 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2426
2427 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2428 if (val != DECL_VALUE_EXPR (var))
2429 SET_DECL_VALUE_EXPR (var, val);
2430 }
2431
2432 pointer_map_destroy (id.cb.decl_map);
2433 }
2434
2435 /* Fold the MEM_REF *E. */
2436 static bool
2437 fold_mem_refs (const void *e, void *data ATTRIBUTE_UNUSED)
2438 {
2439 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2440 *ref_p = fold (*ref_p);
2441 return true;
2442 }
2443
2444 /* Do "everything else" to clean up or complete state collected by the
2445 various walking passes -- lay out the types and decls, generate code
2446 to initialize the frame decl, store critical expressions in the
2447 struct function for rtl to find. */
2448
2449 static void
2450 finalize_nesting_tree_1 (struct nesting_info *root)
2451 {
2452 gimple_seq stmt_list;
2453 gimple stmt;
2454 tree context = root->context;
2455 struct function *sf;
2456
2457 stmt_list = NULL;
2458
2459 /* If we created a non-local frame type or decl, we need to lay them
2460 out at this time. */
2461 if (root->frame_type)
2462 {
2463 /* In some cases the frame type will trigger the -Wpadded warning.
2464 This is not helpful; suppress it. */
2465 int save_warn_padded = warn_padded;
2466 tree *adjust;
2467
2468 warn_padded = 0;
2469 layout_type (root->frame_type);
2470 warn_padded = save_warn_padded;
2471 layout_decl (root->frame_decl, 0);
2472
2473 /* Remove root->frame_decl from root->new_local_var_chain, so
2474 that we can declare it also in the lexical blocks, which
2475 helps ensure virtual regs that end up appearing in its RTL
2476 expression get substituted in instantiate_virtual_regs(). */
2477 for (adjust = &root->new_local_var_chain;
2478 *adjust != root->frame_decl;
2479 adjust = &DECL_CHAIN (*adjust))
2480 gcc_assert (DECL_CHAIN (*adjust));
2481 *adjust = DECL_CHAIN (*adjust);
2482
2483 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2484 declare_vars (root->frame_decl,
2485 gimple_seq_first_stmt (gimple_body (context)), true);
2486 }
2487
2488 /* If any parameters were referenced non-locally, then we need to
2489 insert a copy. Likewise, if any variables were referenced by
2490 pointer, we need to initialize the address. */
2491 if (root->any_parm_remapped)
2492 {
2493 tree p;
2494 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2495 {
2496 tree field, x, y;
2497
2498 field = lookup_field_for_decl (root, p, NO_INSERT);
2499 if (!field)
2500 continue;
2501
2502 if (use_pointer_in_frame (p))
2503 x = build_addr (p, context);
2504 else
2505 x = p;
2506
2507 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2508 root->frame_decl, field, NULL_TREE);
2509 stmt = gimple_build_assign (y, x);
2510 gimple_seq_add_stmt (&stmt_list, stmt);
2511 /* If the assignment is from a non-register the stmt is
2512 not valid gimple. Make it so by using a temporary instead. */
2513 if (!is_gimple_reg (x)
2514 && is_gimple_reg_type (TREE_TYPE (x)))
2515 {
2516 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2517 x = init_tmp_var (root, x, &gsi);
2518 gimple_assign_set_rhs1 (stmt, x);
2519 }
2520 }
2521 }
2522
2523 /* If a chain_field was created, then it needs to be initialized
2524 from chain_decl. */
2525 if (root->chain_field)
2526 {
2527 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2528 root->frame_decl, root->chain_field, NULL_TREE);
2529 stmt = gimple_build_assign (x, get_chain_decl (root));
2530 gimple_seq_add_stmt (&stmt_list, stmt);
2531 }
2532
2533 /* If trampolines were created, then we need to initialize them. */
2534 if (root->any_tramp_created)
2535 {
2536 struct nesting_info *i;
2537 for (i = root->inner; i ; i = i->next)
2538 {
2539 tree arg1, arg2, arg3, x, field;
2540
2541 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2542 if (!field)
2543 continue;
2544
2545 gcc_assert (DECL_STATIC_CHAIN (i->context));
2546 arg3 = build_addr (root->frame_decl, context);
2547
2548 arg2 = build_addr (i->context, context);
2549
2550 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2551 root->frame_decl, field, NULL_TREE);
2552 arg1 = build_addr (x, context);
2553
2554 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2555 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2556 gimple_seq_add_stmt (&stmt_list, stmt);
2557 }
2558 }
2559
2560 /* If we created initialization statements, insert them. */
2561 if (stmt_list)
2562 {
2563 gimple bind;
2564 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2565 bind = gimple_seq_first_stmt (gimple_body (context));
2566 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2567 gimple_bind_set_body (bind, stmt_list);
2568 }
2569
2570 /* If a chain_decl was created, then it needs to be registered with
2571 struct function so that it gets initialized from the static chain
2572 register at the beginning of the function. */
2573 sf = DECL_STRUCT_FUNCTION (root->context);
2574 sf->static_chain_decl = root->chain_decl;
2575
2576 /* Similarly for the non-local goto save area. */
2577 if (root->nl_goto_field)
2578 {
2579 sf->nonlocal_goto_save_area
2580 = get_frame_field (root, context, root->nl_goto_field, NULL);
2581 sf->has_nonlocal_label = 1;
2582 }
2583
2584 /* Make sure all new local variables get inserted into the
2585 proper BIND_EXPR. */
2586 if (root->new_local_var_chain)
2587 declare_vars (root->new_local_var_chain,
2588 gimple_seq_first_stmt (gimple_body (root->context)),
2589 false);
2590
2591 if (root->debug_var_chain)
2592 {
2593 tree debug_var;
2594 gimple scope;
2595
2596 remap_vla_decls (DECL_INITIAL (root->context), root);
2597
2598 for (debug_var = root->debug_var_chain; debug_var;
2599 debug_var = DECL_CHAIN (debug_var))
2600 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2601 break;
2602
2603 /* If there are any debug decls with variable length types,
2604 remap those types using other debug_var_chain variables. */
2605 if (debug_var)
2606 {
2607 struct nesting_copy_body_data id;
2608
2609 memset (&id, 0, sizeof (id));
2610 id.cb.copy_decl = nesting_copy_decl;
2611 id.cb.decl_map = pointer_map_create ();
2612 id.root = root;
2613
2614 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
2615 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2616 {
2617 tree type = TREE_TYPE (debug_var);
2618 tree newt, t = type;
2619 struct nesting_info *i;
2620
2621 for (i = root; i; i = i->outer)
2622 if (variably_modified_type_p (type, i->context))
2623 break;
2624
2625 if (i == NULL)
2626 continue;
2627
2628 id.cb.src_fn = i->context;
2629 id.cb.dst_fn = i->context;
2630 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2631
2632 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2633 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2634 {
2635 newt = TREE_TYPE (newt);
2636 t = TREE_TYPE (t);
2637 }
2638 if (TYPE_NAME (newt)
2639 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2640 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2641 && newt != t
2642 && TYPE_NAME (newt) == TYPE_NAME (t))
2643 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2644 }
2645
2646 pointer_map_destroy (id.cb.decl_map);
2647 }
2648
2649 scope = gimple_seq_first_stmt (gimple_body (root->context));
2650 if (gimple_bind_block (scope))
2651 declare_vars (root->debug_var_chain, scope, true);
2652 else
2653 BLOCK_VARS (DECL_INITIAL (root->context))
2654 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
2655 root->debug_var_chain);
2656 }
2657
2658 /* Fold the rewritten MEM_REF trees. */
2659 pointer_set_traverse (root->mem_refs, fold_mem_refs, NULL);
2660
2661 /* Dump the translated tree function. */
2662 if (dump_file)
2663 {
2664 fputs ("\n\n", dump_file);
2665 dump_function_to_file (root->context, dump_file, dump_flags);
2666 }
2667 }
2668
2669 static void
2670 finalize_nesting_tree (struct nesting_info *root)
2671 {
2672 struct nesting_info *n;
2673 FOR_EACH_NEST_INFO (n, root)
2674 finalize_nesting_tree_1 (n);
2675 }
2676
2677 /* Unnest the nodes and pass them to cgraph. */
2678
2679 static void
2680 unnest_nesting_tree_1 (struct nesting_info *root)
2681 {
2682 struct cgraph_node *node = cgraph_get_node (root->context);
2683
2684 /* For nested functions update the cgraph to reflect unnesting.
2685 We also delay finalizing of these functions up to this point. */
2686 if (node->origin)
2687 {
2688 cgraph_unnest_node (node);
2689 cgraph_finalize_function (root->context, true);
2690 }
2691 }
2692
2693 static void
2694 unnest_nesting_tree (struct nesting_info *root)
2695 {
2696 struct nesting_info *n;
2697 FOR_EACH_NEST_INFO (n, root)
2698 unnest_nesting_tree_1 (n);
2699 }
2700
2701 /* Free the data structures allocated during this pass. */
2702
2703 static void
2704 free_nesting_tree (struct nesting_info *root)
2705 {
2706 struct nesting_info *node, *next;
2707
2708 node = iter_nestinfo_start (root);
2709 do
2710 {
2711 next = iter_nestinfo_next (node);
2712 pointer_map_destroy (node->var_map);
2713 pointer_map_destroy (node->field_map);
2714 pointer_set_destroy (node->mem_refs);
2715 free (node);
2716 node = next;
2717 }
2718 while (node);
2719 }
2720
2721 /* Gimplify a function and all its nested functions. */
2722 static void
2723 gimplify_all_functions (struct cgraph_node *root)
2724 {
2725 struct cgraph_node *iter;
2726 if (!gimple_body (root->decl))
2727 gimplify_function_tree (root->decl);
2728 for (iter = root->nested; iter; iter = iter->next_nested)
2729 gimplify_all_functions (iter);
2730 }
2731
2732 /* Main entry point for this pass. Process FNDECL and all of its nested
2733 subroutines and turn them into something less tightly bound. */
2734
2735 void
2736 lower_nested_functions (tree fndecl)
2737 {
2738 struct cgraph_node *cgn;
2739 struct nesting_info *root;
2740
2741 /* If there are no nested functions, there's nothing to do. */
2742 cgn = cgraph_get_node (fndecl);
2743 if (!cgn->nested)
2744 return;
2745
2746 gimplify_all_functions (cgn);
2747
2748 dump_file = dump_begin (TDI_nested, &dump_flags);
2749 if (dump_file)
2750 fprintf (dump_file, "\n;; Function %s\n\n",
2751 lang_hooks.decl_printable_name (fndecl, 2));
2752
2753 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2754 root = create_nesting_tree (cgn);
2755
2756 walk_all_functions (convert_nonlocal_reference_stmt,
2757 convert_nonlocal_reference_op,
2758 root);
2759 walk_all_functions (convert_local_reference_stmt,
2760 convert_local_reference_op,
2761 root);
2762 walk_all_functions (convert_nl_goto_reference, NULL, root);
2763 walk_all_functions (convert_nl_goto_receiver, NULL, root);
2764
2765 convert_all_function_calls (root);
2766 finalize_nesting_tree (root);
2767 unnest_nesting_tree (root);
2768
2769 free_nesting_tree (root);
2770 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2771
2772 if (dump_file)
2773 {
2774 dump_end (TDI_nested, dump_file);
2775 dump_file = NULL;
2776 }
2777 }
2778
2779 #include "gt-tree-nested.h"