]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-nested.c
gimplify.c (enum gimplify_omp_var_data): Add GOVD_CONDTEMP.
[thirdparty/gcc.git] / gcc / tree-nested.c
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2019 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "dumpfile.h"
35 #include "tree-inline.h"
36 #include "gimplify.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "tree-cfg.h"
40 #include "explow.h"
41 #include "langhooks.h"
42 #include "gimple-low.h"
43 #include "gomp-constants.h"
44 #include "diagnostic.h"
45
46
47 /* The object of this pass is to lower the representation of a set of nested
48 functions in order to expose all of the gory details of the various
49 nonlocal references. We want to do this sooner rather than later, in
50 order to give us more freedom in emitting all of the functions in question.
51
52 Back in olden times, when gcc was young, we developed an insanely
53 complicated scheme whereby variables which were referenced nonlocally
54 were forced to live in the stack of the declaring function, and then
55 the nested functions magically discovered where these variables were
56 placed. In order for this scheme to function properly, it required
57 that the outer function be partially expanded, then we switch to
58 compiling the inner function, and once done with those we switch back
59 to compiling the outer function. Such delicate ordering requirements
60 makes it difficult to do whole translation unit optimizations
61 involving such functions.
62
63 The implementation here is much more direct. Everything that can be
64 referenced by an inner function is a member of an explicitly created
65 structure herein called the "nonlocal frame struct". The incoming
66 static chain for a nested function is a pointer to this struct in
67 the parent. In this way, we settle on known offsets from a known
68 base, and so are decoupled from the logic that places objects in the
69 function's stack frame. More importantly, we don't have to wait for
70 that to happen -- since the compilation of the inner function is no
71 longer tied to a real stack frame, the nonlocal frame struct can be
72 allocated anywhere. Which means that the outer function is now
73 inlinable.
74
75 Theory of operation here is very simple. Iterate over all the
76 statements in all the functions (depth first) several times,
77 allocating structures and fields on demand. In general we want to
78 examine inner functions first, so that we can avoid making changes
79 to outer functions which are unnecessary.
80
81 The order of the passes matters a bit, in that later passes will be
82 skipped if it is discovered that the functions don't actually interact
83 at all. That is, they're nested in the lexical sense but could have
84 been written as independent functions without change. */
85
86
87 struct nesting_info
88 {
89 struct nesting_info *outer;
90 struct nesting_info *inner;
91 struct nesting_info *next;
92
93 hash_map<tree, tree> *field_map;
94 hash_map<tree, tree> *var_map;
95 hash_set<tree *> *mem_refs;
96 bitmap suppress_expansion;
97
98 tree context;
99 tree new_local_var_chain;
100 tree debug_var_chain;
101 tree frame_type;
102 tree frame_decl;
103 tree chain_field;
104 tree chain_decl;
105 tree nl_goto_field;
106
107 bool thunk_p;
108 bool any_parm_remapped;
109 bool any_tramp_created;
110 bool any_descr_created;
111 char static_chain_added;
112 };
113
114
115 /* Iterate over the nesting tree, starting with ROOT, depth first. */
116
117 static inline struct nesting_info *
118 iter_nestinfo_start (struct nesting_info *root)
119 {
120 while (root->inner)
121 root = root->inner;
122 return root;
123 }
124
125 static inline struct nesting_info *
126 iter_nestinfo_next (struct nesting_info *node)
127 {
128 if (node->next)
129 return iter_nestinfo_start (node->next);
130 return node->outer;
131 }
132
133 #define FOR_EACH_NEST_INFO(I, ROOT) \
134 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
135
136 /* Obstack used for the bitmaps in the struct above. */
137 static struct bitmap_obstack nesting_info_bitmap_obstack;
138
139
140 /* We're working in so many different function contexts simultaneously,
141 that create_tmp_var is dangerous. Prevent mishap. */
142 #define create_tmp_var cant_use_create_tmp_var_here_dummy
143
144 /* Like create_tmp_var, except record the variable for registration at
145 the given nesting level. */
146
147 static tree
148 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
149 {
150 tree tmp_var;
151
152 /* If the type is of variable size or a type which must be created by the
153 frontend, something is wrong. Note that we explicitly allow
154 incomplete types here, since we create them ourselves here. */
155 gcc_assert (!TREE_ADDRESSABLE (type));
156 gcc_assert (!TYPE_SIZE_UNIT (type)
157 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
158
159 tmp_var = create_tmp_var_raw (type, prefix);
160 DECL_CONTEXT (tmp_var) = info->context;
161 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
162 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
163 if (TREE_CODE (type) == COMPLEX_TYPE
164 || TREE_CODE (type) == VECTOR_TYPE)
165 DECL_GIMPLE_REG_P (tmp_var) = 1;
166
167 info->new_local_var_chain = tmp_var;
168
169 return tmp_var;
170 }
171
172 /* Take the address of EXP to be used within function CONTEXT.
173 Mark it for addressability as necessary. */
174
175 tree
176 build_addr (tree exp)
177 {
178 mark_addressable (exp);
179 return build_fold_addr_expr (exp);
180 }
181
182 /* Insert FIELD into TYPE, sorted by alignment requirements. */
183
184 void
185 insert_field_into_struct (tree type, tree field)
186 {
187 tree *p;
188
189 DECL_CONTEXT (field) = type;
190
191 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
192 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
193 break;
194
195 DECL_CHAIN (field) = *p;
196 *p = field;
197
198 /* Set correct alignment for frame struct type. */
199 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
200 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
201 }
202
203 /* Build or return the RECORD_TYPE that describes the frame state that is
204 shared between INFO->CONTEXT and its nested functions. This record will
205 not be complete until finalize_nesting_tree; up until that point we'll
206 be adding fields as necessary.
207
208 We also build the DECL that represents this frame in the function. */
209
210 static tree
211 get_frame_type (struct nesting_info *info)
212 {
213 tree type = info->frame_type;
214 if (!type)
215 {
216 char *name;
217
218 type = make_node (RECORD_TYPE);
219
220 name = concat ("FRAME.",
221 IDENTIFIER_POINTER (DECL_NAME (info->context)),
222 NULL);
223 TYPE_NAME (type) = get_identifier (name);
224 free (name);
225
226 info->frame_type = type;
227
228 /* Do not put info->frame_decl on info->new_local_var_chain,
229 so that we can declare it in the lexical blocks, which
230 makes sure virtual regs that end up appearing in its RTL
231 expression get substituted in instantiate_virtual_regs. */
232 info->frame_decl = create_tmp_var_raw (type, "FRAME");
233 DECL_CONTEXT (info->frame_decl) = info->context;
234 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
235 DECL_SEEN_IN_BIND_EXPR_P (info->frame_decl) = 1;
236
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;
244 }
245
246 return type;
247 }
248
249 /* Return true if DECL should be referenced by pointer in the non-local frame
250 structure. */
251
252 static bool
253 use_pointer_in_frame (tree decl)
254 {
255 if (TREE_CODE (decl) == PARM_DECL)
256 {
257 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
258 sized DECLs, and inefficient to copy large aggregates. Don't bother
259 moving anything but scalar parameters. */
260 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
261 }
262 else
263 {
264 /* Variable-sized DECLs can only come from OMP clauses at this point
265 since the gimplifier has already turned the regular variables into
266 pointers. Do the same as the gimplifier. */
267 return !DECL_SIZE (decl) || TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST;
268 }
269 }
270
271 /* Given DECL, a non-locally accessed variable, find or create a field
272 in the non-local frame structure for the given nesting context. */
273
274 static tree
275 lookup_field_for_decl (struct nesting_info *info, tree decl,
276 enum insert_option insert)
277 {
278 gcc_checking_assert (decl_function_context (decl) == info->context);
279
280 if (insert == NO_INSERT)
281 {
282 tree *slot = info->field_map->get (decl);
283 return slot ? *slot : NULL_TREE;
284 }
285
286 tree *slot = &info->field_map->get_or_insert (decl);
287 if (!*slot)
288 {
289 tree type = get_frame_type (info);
290 tree field = make_node (FIELD_DECL);
291 DECL_NAME (field) = DECL_NAME (decl);
292
293 if (use_pointer_in_frame (decl))
294 {
295 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
296 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
297 DECL_NONADDRESSABLE_P (field) = 1;
298 }
299 else
300 {
301 TREE_TYPE (field) = TREE_TYPE (decl);
302 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
303 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
304 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
305 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
306 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
307 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
308
309 /* Declare the transformation and adjust the original DECL. For a
310 variable or for a parameter when not optimizing, we make it point
311 to the field in the frame directly. For a parameter, we don't do
312 it when optimizing because the variable tracking pass will already
313 do the job, */
314 if (VAR_P (decl) || !optimize)
315 {
316 tree x
317 = build3 (COMPONENT_REF, TREE_TYPE (field), info->frame_decl,
318 field, NULL_TREE);
319
320 /* If the next declaration is a PARM_DECL pointing to the DECL,
321 we need to adjust its VALUE_EXPR directly, since chains of
322 VALUE_EXPRs run afoul of garbage collection. This occurs
323 in Ada for Out parameters that aren't copied in. */
324 tree next = DECL_CHAIN (decl);
325 if (next
326 && TREE_CODE (next) == PARM_DECL
327 && DECL_HAS_VALUE_EXPR_P (next)
328 && DECL_VALUE_EXPR (next) == decl)
329 SET_DECL_VALUE_EXPR (next, x);
330
331 SET_DECL_VALUE_EXPR (decl, x);
332 DECL_HAS_VALUE_EXPR_P (decl) = 1;
333 }
334 }
335
336 insert_field_into_struct (type, field);
337 *slot = field;
338
339 if (TREE_CODE (decl) == PARM_DECL)
340 info->any_parm_remapped = true;
341 }
342
343 return *slot;
344 }
345
346 /* Build or return the variable that holds the static chain within
347 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
348
349 static tree
350 get_chain_decl (struct nesting_info *info)
351 {
352 tree decl = info->chain_decl;
353
354 if (!decl)
355 {
356 tree type;
357
358 type = get_frame_type (info->outer);
359 type = build_pointer_type (type);
360
361 /* Note that this variable is *not* entered into any BIND_EXPR;
362 the construction of this variable is handled specially in
363 expand_function_start and initialize_inlined_parameters.
364 Note also that it's represented as a parameter. This is more
365 close to the truth, since the initial value does come from
366 the caller. */
367 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
368 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
369 DECL_ARTIFICIAL (decl) = 1;
370 DECL_IGNORED_P (decl) = 1;
371 TREE_USED (decl) = 1;
372 DECL_CONTEXT (decl) = info->context;
373 DECL_ARG_TYPE (decl) = type;
374
375 /* Tell tree-inline.c that we never write to this variable, so
376 it can copy-prop the replacement value immediately. */
377 TREE_READONLY (decl) = 1;
378
379 info->chain_decl = decl;
380
381 if (dump_file
382 && (dump_flags & TDF_DETAILS)
383 && !DECL_STATIC_CHAIN (info->context))
384 fprintf (dump_file, "Setting static-chain for %s\n",
385 lang_hooks.decl_printable_name (info->context, 2));
386
387 DECL_STATIC_CHAIN (info->context) = 1;
388 }
389 return decl;
390 }
391
392 /* Build or return the field within the non-local frame state that holds
393 the static chain for INFO->CONTEXT. This is the way to walk back up
394 multiple nesting levels. */
395
396 static tree
397 get_chain_field (struct nesting_info *info)
398 {
399 tree field = info->chain_field;
400
401 if (!field)
402 {
403 tree type = build_pointer_type (get_frame_type (info->outer));
404
405 field = make_node (FIELD_DECL);
406 DECL_NAME (field) = get_identifier ("__chain");
407 TREE_TYPE (field) = type;
408 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
409 DECL_NONADDRESSABLE_P (field) = 1;
410
411 insert_field_into_struct (get_frame_type (info), field);
412
413 info->chain_field = field;
414
415 if (dump_file
416 && (dump_flags & TDF_DETAILS)
417 && !DECL_STATIC_CHAIN (info->context))
418 fprintf (dump_file, "Setting static-chain for %s\n",
419 lang_hooks.decl_printable_name (info->context, 2));
420
421 DECL_STATIC_CHAIN (info->context) = 1;
422 }
423 return field;
424 }
425
426 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
427
428 static tree
429 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
430 gcall *call)
431 {
432 tree t;
433
434 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
435 gimple_call_set_lhs (call, t);
436 if (! gsi_end_p (*gsi))
437 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
438 gsi_insert_before (gsi, call, GSI_SAME_STMT);
439
440 return t;
441 }
442
443
444 /* Copy EXP into a temporary. Allocate the temporary in the context of
445 INFO and insert the initialization statement before GSI. */
446
447 static tree
448 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
449 {
450 tree t;
451 gimple *stmt;
452
453 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
454 stmt = gimple_build_assign (t, exp);
455 if (! gsi_end_p (*gsi))
456 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
457 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
458
459 return t;
460 }
461
462
463 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
464
465 static tree
466 gsi_gimplify_val (struct nesting_info *info, tree exp,
467 gimple_stmt_iterator *gsi)
468 {
469 if (is_gimple_val (exp))
470 return exp;
471 else
472 return init_tmp_var (info, exp, gsi);
473 }
474
475 /* Similarly, but copy from the temporary and insert the statement
476 after the iterator. */
477
478 static tree
479 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
480 {
481 tree t;
482 gimple *stmt;
483
484 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
485 stmt = gimple_build_assign (exp, t);
486 if (! gsi_end_p (*gsi))
487 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
488 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
489
490 return t;
491 }
492
493 /* Build or return the type used to represent a nested function trampoline. */
494
495 static GTY(()) tree trampoline_type;
496
497 static tree
498 get_trampoline_type (struct nesting_info *info)
499 {
500 unsigned align, size;
501 tree t;
502
503 if (trampoline_type)
504 return trampoline_type;
505
506 align = TRAMPOLINE_ALIGNMENT;
507 size = TRAMPOLINE_SIZE;
508
509 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
510 then allocate extra space so that we can do dynamic alignment. */
511 if (align > STACK_BOUNDARY)
512 {
513 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
514 align = STACK_BOUNDARY;
515 }
516
517 t = build_index_type (size_int (size - 1));
518 t = build_array_type (char_type_node, t);
519 t = build_decl (DECL_SOURCE_LOCATION (info->context),
520 FIELD_DECL, get_identifier ("__data"), t);
521 SET_DECL_ALIGN (t, align);
522 DECL_USER_ALIGN (t) = 1;
523
524 trampoline_type = make_node (RECORD_TYPE);
525 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
526 TYPE_FIELDS (trampoline_type) = t;
527 layout_type (trampoline_type);
528 DECL_CONTEXT (t) = trampoline_type;
529
530 return trampoline_type;
531 }
532
533 /* Build or return the type used to represent a nested function descriptor. */
534
535 static GTY(()) tree descriptor_type;
536
537 static tree
538 get_descriptor_type (struct nesting_info *info)
539 {
540 /* The base alignment is that of a function. */
541 const unsigned align = FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY);
542 tree t;
543
544 if (descriptor_type)
545 return descriptor_type;
546
547 t = build_index_type (integer_one_node);
548 t = build_array_type (ptr_type_node, t);
549 t = build_decl (DECL_SOURCE_LOCATION (info->context),
550 FIELD_DECL, get_identifier ("__data"), t);
551 SET_DECL_ALIGN (t, MAX (TYPE_ALIGN (ptr_type_node), align));
552 DECL_USER_ALIGN (t) = 1;
553
554 descriptor_type = make_node (RECORD_TYPE);
555 TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
556 TYPE_FIELDS (descriptor_type) = t;
557 layout_type (descriptor_type);
558 DECL_CONTEXT (t) = descriptor_type;
559
560 return descriptor_type;
561 }
562
563 /* Given DECL, a nested function, find or create an element in the
564 var map for this function. */
565
566 static tree
567 lookup_element_for_decl (struct nesting_info *info, tree decl,
568 enum insert_option insert)
569 {
570 if (insert == NO_INSERT)
571 {
572 tree *slot = info->var_map->get (decl);
573 return slot ? *slot : NULL_TREE;
574 }
575
576 tree *slot = &info->var_map->get_or_insert (decl);
577 if (!*slot)
578 *slot = build_tree_list (NULL_TREE, NULL_TREE);
579
580 return (tree) *slot;
581 }
582
583 /* Given DECL, a nested function, create a field in the non-local
584 frame structure for this function. */
585
586 static tree
587 create_field_for_decl (struct nesting_info *info, tree decl, tree type)
588 {
589 tree field = make_node (FIELD_DECL);
590 DECL_NAME (field) = DECL_NAME (decl);
591 TREE_TYPE (field) = type;
592 TREE_ADDRESSABLE (field) = 1;
593 insert_field_into_struct (get_frame_type (info), field);
594 return field;
595 }
596
597 /* Given DECL, a nested function, find or create a field in the non-local
598 frame structure for a trampoline for this function. */
599
600 static tree
601 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
602 enum insert_option insert)
603 {
604 tree elt, field;
605
606 elt = lookup_element_for_decl (info, decl, insert);
607 if (!elt)
608 return NULL_TREE;
609
610 field = TREE_PURPOSE (elt);
611
612 if (!field && insert == INSERT)
613 {
614 field = create_field_for_decl (info, decl, get_trampoline_type (info));
615 TREE_PURPOSE (elt) = field;
616 info->any_tramp_created = true;
617 }
618
619 return field;
620 }
621
622 /* Given DECL, a nested function, find or create a field in the non-local
623 frame structure for a descriptor for this function. */
624
625 static tree
626 lookup_descr_for_decl (struct nesting_info *info, tree decl,
627 enum insert_option insert)
628 {
629 tree elt, field;
630
631 elt = lookup_element_for_decl (info, decl, insert);
632 if (!elt)
633 return NULL_TREE;
634
635 field = TREE_VALUE (elt);
636
637 if (!field && insert == INSERT)
638 {
639 field = create_field_for_decl (info, decl, get_descriptor_type (info));
640 TREE_VALUE (elt) = field;
641 info->any_descr_created = true;
642 }
643
644 return field;
645 }
646
647 /* Build or return the field within the non-local frame state that holds
648 the non-local goto "jmp_buf". The buffer itself is maintained by the
649 rtl middle-end as dynamic stack space is allocated. */
650
651 static tree
652 get_nl_goto_field (struct nesting_info *info)
653 {
654 tree field = info->nl_goto_field;
655 if (!field)
656 {
657 unsigned size;
658 tree type;
659
660 /* For __builtin_nonlocal_goto, we need N words. The first is the
661 frame pointer, the rest is for the target's stack pointer save
662 area. The number of words is controlled by STACK_SAVEAREA_MODE;
663 not the best interface, but it'll do for now. */
664 if (Pmode == ptr_mode)
665 type = ptr_type_node;
666 else
667 type = lang_hooks.types.type_for_mode (Pmode, 1);
668
669 scalar_int_mode mode
670 = as_a <scalar_int_mode> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
671 size = GET_MODE_SIZE (mode);
672 size = size / GET_MODE_SIZE (Pmode);
673 size = size + 1;
674
675 type = build_array_type
676 (type, build_index_type (size_int (size)));
677
678 field = make_node (FIELD_DECL);
679 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
680 TREE_TYPE (field) = type;
681 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
682 TREE_ADDRESSABLE (field) = 1;
683
684 insert_field_into_struct (get_frame_type (info), field);
685
686 info->nl_goto_field = field;
687 }
688
689 return field;
690 }
691
692 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
693
694 static void
695 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
696 struct nesting_info *info, gimple_seq *pseq)
697 {
698 struct walk_stmt_info wi;
699
700 memset (&wi, 0, sizeof (wi));
701 wi.info = info;
702 wi.val_only = true;
703 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
704 }
705
706
707 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
708
709 static inline void
710 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
711 struct nesting_info *info)
712 {
713 gimple_seq body = gimple_body (info->context);
714 walk_body (callback_stmt, callback_op, info, &body);
715 gimple_set_body (info->context, body);
716 }
717
718 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
719
720 static void
721 walk_gimple_omp_for (gomp_for *for_stmt,
722 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
723 struct nesting_info *info)
724 {
725 struct walk_stmt_info wi;
726 gimple_seq seq;
727 tree t;
728 size_t i;
729
730 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
731
732 seq = NULL;
733 memset (&wi, 0, sizeof (wi));
734 wi.info = info;
735 wi.gsi = gsi_last (seq);
736
737 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
738 {
739 wi.val_only = false;
740 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
741 &wi, NULL);
742 wi.val_only = true;
743 wi.is_lhs = false;
744 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
745 &wi, NULL);
746
747 wi.val_only = true;
748 wi.is_lhs = false;
749 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
750 &wi, NULL);
751
752 t = gimple_omp_for_incr (for_stmt, i);
753 gcc_assert (BINARY_CLASS_P (t));
754 wi.val_only = false;
755 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
756 wi.val_only = true;
757 wi.is_lhs = false;
758 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
759 }
760
761 seq = gsi_seq (wi.gsi);
762 if (!gimple_seq_empty_p (seq))
763 {
764 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
765 annotate_all_with_location (seq, gimple_location (for_stmt));
766 gimple_seq_add_seq (&pre_body, seq);
767 gimple_omp_for_set_pre_body (for_stmt, pre_body);
768 }
769 }
770
771 /* Similarly for ROOT and all functions nested underneath, depth first. */
772
773 static void
774 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
775 struct nesting_info *root)
776 {
777 struct nesting_info *n;
778 FOR_EACH_NEST_INFO (n, root)
779 walk_function (callback_stmt, callback_op, n);
780 }
781
782
783 /* We have to check for a fairly pathological case. The operands of function
784 nested function are to be interpreted in the context of the enclosing
785 function. So if any are variably-sized, they will get remapped when the
786 enclosing function is inlined. But that remapping would also have to be
787 done in the types of the PARM_DECLs of the nested function, meaning the
788 argument types of that function will disagree with the arguments in the
789 calls to that function. So we'd either have to make a copy of the nested
790 function corresponding to each time the enclosing function was inlined or
791 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
792 function. The former is not practical. The latter would still require
793 detecting this case to know when to add the conversions. So, for now at
794 least, we don't inline such an enclosing function.
795
796 We have to do that check recursively, so here return indicating whether
797 FNDECL has such a nested function. ORIG_FN is the function we were
798 trying to inline to use for checking whether any argument is variably
799 modified by anything in it.
800
801 It would be better to do this in tree-inline.c so that we could give
802 the appropriate warning for why a function can't be inlined, but that's
803 too late since the nesting structure has already been flattened and
804 adding a flag just to record this fact seems a waste of a flag. */
805
806 static bool
807 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
808 {
809 struct cgraph_node *cgn = cgraph_node::get (fndecl);
810 tree arg;
811
812 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
813 {
814 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
815 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
816 return true;
817
818 if (check_for_nested_with_variably_modified (cgn->decl,
819 orig_fndecl))
820 return true;
821 }
822
823 return false;
824 }
825
826 /* Construct our local datastructure describing the function nesting
827 tree rooted by CGN. */
828
829 static struct nesting_info *
830 create_nesting_tree (struct cgraph_node *cgn)
831 {
832 struct nesting_info *info = XCNEW (struct nesting_info);
833 info->field_map = new hash_map<tree, tree>;
834 info->var_map = new hash_map<tree, tree>;
835 info->mem_refs = new hash_set<tree *>;
836 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
837 info->context = cgn->decl;
838 info->thunk_p = cgn->thunk.thunk_p;
839
840 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
841 {
842 struct nesting_info *sub = create_nesting_tree (cgn);
843 sub->outer = info;
844 sub->next = info->inner;
845 info->inner = sub;
846 }
847
848 /* See discussion at check_for_nested_with_variably_modified for a
849 discussion of why this has to be here. */
850 if (check_for_nested_with_variably_modified (info->context, info->context))
851 DECL_UNINLINABLE (info->context) = true;
852
853 return info;
854 }
855
856 /* Return an expression computing the static chain for TARGET_CONTEXT
857 from INFO->CONTEXT. Insert any necessary computations before TSI. */
858
859 static tree
860 get_static_chain (struct nesting_info *info, tree target_context,
861 gimple_stmt_iterator *gsi)
862 {
863 struct nesting_info *i;
864 tree x;
865
866 if (info->context == target_context)
867 {
868 x = build_addr (info->frame_decl);
869 info->static_chain_added |= 1;
870 }
871 else
872 {
873 x = get_chain_decl (info);
874 info->static_chain_added |= 2;
875
876 for (i = info->outer; i->context != target_context; i = i->outer)
877 {
878 tree field = get_chain_field (i);
879
880 x = build_simple_mem_ref (x);
881 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
882 x = init_tmp_var (info, x, gsi);
883 }
884 }
885
886 return x;
887 }
888
889
890 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
891 frame as seen from INFO->CONTEXT. Insert any necessary computations
892 before GSI. */
893
894 static tree
895 get_frame_field (struct nesting_info *info, tree target_context,
896 tree field, gimple_stmt_iterator *gsi)
897 {
898 struct nesting_info *i;
899 tree x;
900
901 if (info->context == target_context)
902 {
903 /* Make sure frame_decl gets created. */
904 (void) get_frame_type (info);
905 x = info->frame_decl;
906 info->static_chain_added |= 1;
907 }
908 else
909 {
910 x = get_chain_decl (info);
911 info->static_chain_added |= 2;
912
913 for (i = info->outer; i->context != target_context; i = i->outer)
914 {
915 tree field = get_chain_field (i);
916
917 x = build_simple_mem_ref (x);
918 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
919 x = init_tmp_var (info, x, gsi);
920 }
921
922 x = build_simple_mem_ref (x);
923 }
924
925 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
926 return x;
927 }
928
929 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
930
931 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
932 in the nested function with DECL_VALUE_EXPR set to reference the true
933 variable in the parent function. This is used both for debug info
934 and in OMP lowering. */
935
936 static tree
937 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
938 {
939 tree target_context;
940 struct nesting_info *i;
941 tree x, field, new_decl;
942
943 tree *slot = &info->var_map->get_or_insert (decl);
944
945 if (*slot)
946 return *slot;
947
948 target_context = decl_function_context (decl);
949
950 /* A copy of the code in get_frame_field, but without the temporaries. */
951 if (info->context == target_context)
952 {
953 /* Make sure frame_decl gets created. */
954 (void) get_frame_type (info);
955 x = info->frame_decl;
956 i = info;
957 info->static_chain_added |= 1;
958 }
959 else
960 {
961 x = get_chain_decl (info);
962 info->static_chain_added |= 2;
963 for (i = info->outer; i->context != target_context; i = i->outer)
964 {
965 field = get_chain_field (i);
966 x = build_simple_mem_ref (x);
967 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
968 }
969 x = build_simple_mem_ref (x);
970 }
971
972 field = lookup_field_for_decl (i, decl, INSERT);
973 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
974 if (use_pointer_in_frame (decl))
975 x = build_simple_mem_ref (x);
976
977 /* ??? We should be remapping types as well, surely. */
978 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
979 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
980 DECL_CONTEXT (new_decl) = info->context;
981 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
982 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
983 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
984 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
985 TREE_READONLY (new_decl) = TREE_READONLY (decl);
986 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
987 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
988 if ((TREE_CODE (decl) == PARM_DECL
989 || TREE_CODE (decl) == RESULT_DECL
990 || VAR_P (decl))
991 && DECL_BY_REFERENCE (decl))
992 DECL_BY_REFERENCE (new_decl) = 1;
993
994 SET_DECL_VALUE_EXPR (new_decl, x);
995 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
996
997 *slot = new_decl;
998 DECL_CHAIN (new_decl) = info->debug_var_chain;
999 info->debug_var_chain = new_decl;
1000
1001 if (!optimize
1002 && info->context != target_context
1003 && variably_modified_type_p (TREE_TYPE (decl), NULL))
1004 note_nonlocal_vla_type (info, TREE_TYPE (decl));
1005
1006 return new_decl;
1007 }
1008
1009
1010 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1011 and PARM_DECLs that belong to outer functions.
1012
1013 The rewrite will involve some number of structure accesses back up
1014 the static chain. E.g. for a variable FOO up one nesting level it'll
1015 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1016 indirections apply to decls for which use_pointer_in_frame is true. */
1017
1018 static tree
1019 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
1020 {
1021 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1022 struct nesting_info *const info = (struct nesting_info *) wi->info;
1023 tree t = *tp;
1024
1025 *walk_subtrees = 0;
1026 switch (TREE_CODE (t))
1027 {
1028 case VAR_DECL:
1029 /* Non-automatic variables are never processed. */
1030 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1031 break;
1032 /* FALLTHRU */
1033
1034 case PARM_DECL:
1035 {
1036 tree x, target_context = decl_function_context (t);
1037
1038 if (info->context == target_context)
1039 break;
1040
1041 wi->changed = true;
1042
1043 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1044 x = get_nonlocal_debug_decl (info, t);
1045 else
1046 {
1047 struct nesting_info *i = info;
1048 while (i && i->context != target_context)
1049 i = i->outer;
1050 /* If none of the outer contexts is the target context, this means
1051 that the VAR or PARM_DECL is referenced in a wrong context. */
1052 if (!i)
1053 internal_error ("%s from %s referenced in %s",
1054 IDENTIFIER_POINTER (DECL_NAME (t)),
1055 IDENTIFIER_POINTER (DECL_NAME (target_context)),
1056 IDENTIFIER_POINTER (DECL_NAME (info->context)));
1057
1058 x = lookup_field_for_decl (i, t, INSERT);
1059 x = get_frame_field (info, target_context, x, &wi->gsi);
1060 if (use_pointer_in_frame (t))
1061 {
1062 x = init_tmp_var (info, x, &wi->gsi);
1063 x = build_simple_mem_ref (x);
1064 }
1065 }
1066
1067 if (wi->val_only)
1068 {
1069 if (wi->is_lhs)
1070 x = save_tmp_var (info, x, &wi->gsi);
1071 else
1072 x = init_tmp_var (info, x, &wi->gsi);
1073 }
1074
1075 *tp = x;
1076 }
1077 break;
1078
1079 case LABEL_DECL:
1080 /* We're taking the address of a label from a parent function, but
1081 this is not itself a non-local goto. Mark the label such that it
1082 will not be deleted, much as we would with a label address in
1083 static storage. */
1084 if (decl_function_context (t) != info->context)
1085 FORCED_LABEL (t) = 1;
1086 break;
1087
1088 case ADDR_EXPR:
1089 {
1090 bool save_val_only = wi->val_only;
1091
1092 wi->val_only = false;
1093 wi->is_lhs = false;
1094 wi->changed = false;
1095 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1096 wi->val_only = true;
1097
1098 if (wi->changed)
1099 {
1100 tree save_context;
1101
1102 /* If we changed anything, we might no longer be directly
1103 referencing a decl. */
1104 save_context = current_function_decl;
1105 current_function_decl = info->context;
1106 recompute_tree_invariant_for_addr_expr (t);
1107 current_function_decl = save_context;
1108
1109 /* If the callback converted the address argument in a context
1110 where we only accept variables (and min_invariant, presumably),
1111 then compute the address into a temporary. */
1112 if (save_val_only)
1113 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1114 t, &wi->gsi);
1115 }
1116 }
1117 break;
1118
1119 case REALPART_EXPR:
1120 case IMAGPART_EXPR:
1121 case COMPONENT_REF:
1122 case ARRAY_REF:
1123 case ARRAY_RANGE_REF:
1124 case BIT_FIELD_REF:
1125 /* Go down this entire nest and just look at the final prefix and
1126 anything that describes the references. Otherwise, we lose track
1127 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1128 wi->val_only = true;
1129 wi->is_lhs = false;
1130 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1131 {
1132 if (TREE_CODE (t) == COMPONENT_REF)
1133 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1134 NULL);
1135 else if (TREE_CODE (t) == ARRAY_REF
1136 || TREE_CODE (t) == ARRAY_RANGE_REF)
1137 {
1138 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1139 wi, NULL);
1140 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1141 wi, NULL);
1142 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1143 wi, NULL);
1144 }
1145 }
1146 wi->val_only = false;
1147 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1148 break;
1149
1150 case VIEW_CONVERT_EXPR:
1151 /* Just request to look at the subtrees, leaving val_only and lhs
1152 untouched. This might actually be for !val_only + lhs, in which
1153 case we don't want to force a replacement by a temporary. */
1154 *walk_subtrees = 1;
1155 break;
1156
1157 default:
1158 if (!IS_TYPE_OR_DECL_P (t))
1159 {
1160 *walk_subtrees = 1;
1161 wi->val_only = true;
1162 wi->is_lhs = false;
1163 }
1164 break;
1165 }
1166
1167 return NULL_TREE;
1168 }
1169
1170 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1171 struct walk_stmt_info *);
1172
1173 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1174 and PARM_DECLs that belong to outer functions. */
1175
1176 static bool
1177 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1178 {
1179 struct nesting_info *const info = (struct nesting_info *) wi->info;
1180 bool need_chain = false, need_stmts = false;
1181 tree clause, decl;
1182 int dummy;
1183 bitmap new_suppress;
1184
1185 new_suppress = BITMAP_GGC_ALLOC ();
1186 bitmap_copy (new_suppress, info->suppress_expansion);
1187
1188 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1189 {
1190 switch (OMP_CLAUSE_CODE (clause))
1191 {
1192 case OMP_CLAUSE_REDUCTION:
1193 case OMP_CLAUSE_IN_REDUCTION:
1194 case OMP_CLAUSE_TASK_REDUCTION:
1195 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1196 need_stmts = true;
1197 goto do_decl_clause;
1198
1199 case OMP_CLAUSE_LASTPRIVATE:
1200 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1201 need_stmts = true;
1202 goto do_decl_clause;
1203
1204 case OMP_CLAUSE_LINEAR:
1205 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1206 need_stmts = true;
1207 wi->val_only = true;
1208 wi->is_lhs = false;
1209 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1210 &dummy, wi);
1211 goto do_decl_clause;
1212
1213 case OMP_CLAUSE_PRIVATE:
1214 case OMP_CLAUSE_FIRSTPRIVATE:
1215 case OMP_CLAUSE_COPYPRIVATE:
1216 case OMP_CLAUSE_SHARED:
1217 case OMP_CLAUSE_TO_DECLARE:
1218 case OMP_CLAUSE_LINK:
1219 case OMP_CLAUSE_USE_DEVICE_PTR:
1220 case OMP_CLAUSE_IS_DEVICE_PTR:
1221 do_decl_clause:
1222 decl = OMP_CLAUSE_DECL (clause);
1223 if (VAR_P (decl)
1224 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1225 break;
1226 if (decl_function_context (decl) != info->context)
1227 {
1228 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1229 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1230 bitmap_set_bit (new_suppress, DECL_UID (decl));
1231 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1232 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1233 need_chain = true;
1234 }
1235 break;
1236
1237 case OMP_CLAUSE_SCHEDULE:
1238 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1239 break;
1240 /* FALLTHRU */
1241 case OMP_CLAUSE_FINAL:
1242 case OMP_CLAUSE_IF:
1243 case OMP_CLAUSE_NUM_THREADS:
1244 case OMP_CLAUSE_DEPEND:
1245 case OMP_CLAUSE_DEVICE:
1246 case OMP_CLAUSE_NUM_TEAMS:
1247 case OMP_CLAUSE_THREAD_LIMIT:
1248 case OMP_CLAUSE_SAFELEN:
1249 case OMP_CLAUSE_SIMDLEN:
1250 case OMP_CLAUSE_PRIORITY:
1251 case OMP_CLAUSE_GRAINSIZE:
1252 case OMP_CLAUSE_NUM_TASKS:
1253 case OMP_CLAUSE_HINT:
1254 case OMP_CLAUSE_NUM_GANGS:
1255 case OMP_CLAUSE_NUM_WORKERS:
1256 case OMP_CLAUSE_VECTOR_LENGTH:
1257 case OMP_CLAUSE_GANG:
1258 case OMP_CLAUSE_WORKER:
1259 case OMP_CLAUSE_VECTOR:
1260 case OMP_CLAUSE_ASYNC:
1261 case OMP_CLAUSE_WAIT:
1262 /* Several OpenACC clauses have optional arguments. Check if they
1263 are present. */
1264 if (OMP_CLAUSE_OPERAND (clause, 0))
1265 {
1266 wi->val_only = true;
1267 wi->is_lhs = false;
1268 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1269 &dummy, wi);
1270 }
1271
1272 /* The gang clause accepts two arguments. */
1273 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1274 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1275 {
1276 wi->val_only = true;
1277 wi->is_lhs = false;
1278 convert_nonlocal_reference_op
1279 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1280 }
1281 break;
1282
1283 case OMP_CLAUSE_DIST_SCHEDULE:
1284 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1285 {
1286 wi->val_only = true;
1287 wi->is_lhs = false;
1288 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1289 &dummy, wi);
1290 }
1291 break;
1292
1293 case OMP_CLAUSE_MAP:
1294 case OMP_CLAUSE_TO:
1295 case OMP_CLAUSE_FROM:
1296 if (OMP_CLAUSE_SIZE (clause))
1297 {
1298 wi->val_only = true;
1299 wi->is_lhs = false;
1300 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1301 &dummy, wi);
1302 }
1303 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1304 goto do_decl_clause;
1305 wi->val_only = true;
1306 wi->is_lhs = false;
1307 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1308 wi, NULL);
1309 break;
1310
1311 case OMP_CLAUSE_ALIGNED:
1312 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1313 {
1314 wi->val_only = true;
1315 wi->is_lhs = false;
1316 convert_nonlocal_reference_op
1317 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1318 }
1319 /* FALLTHRU */
1320 case OMP_CLAUSE_NONTEMPORAL:
1321 /* Like do_decl_clause, but don't add any suppression. */
1322 decl = OMP_CLAUSE_DECL (clause);
1323 if (VAR_P (decl)
1324 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1325 break;
1326 if (decl_function_context (decl) != info->context)
1327 {
1328 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1329 need_chain = true;
1330 }
1331 break;
1332
1333 case OMP_CLAUSE_NOWAIT:
1334 case OMP_CLAUSE_ORDERED:
1335 case OMP_CLAUSE_DEFAULT:
1336 case OMP_CLAUSE_COPYIN:
1337 case OMP_CLAUSE_COLLAPSE:
1338 case OMP_CLAUSE_TILE:
1339 case OMP_CLAUSE_UNTIED:
1340 case OMP_CLAUSE_MERGEABLE:
1341 case OMP_CLAUSE_PROC_BIND:
1342 case OMP_CLAUSE_NOGROUP:
1343 case OMP_CLAUSE_THREADS:
1344 case OMP_CLAUSE_SIMD:
1345 case OMP_CLAUSE_DEFAULTMAP:
1346 case OMP_CLAUSE_SEQ:
1347 case OMP_CLAUSE_INDEPENDENT:
1348 case OMP_CLAUSE_AUTO:
1349 case OMP_CLAUSE_IF_PRESENT:
1350 case OMP_CLAUSE_FINALIZE:
1351 case OMP_CLAUSE__CONDTEMP_:
1352 break;
1353
1354 /* The following clause belongs to the OpenACC cache directive, which
1355 is discarded during gimplification. */
1356 case OMP_CLAUSE__CACHE_:
1357 /* The following clauses are only allowed in the OpenMP declare simd
1358 directive, so not seen here. */
1359 case OMP_CLAUSE_UNIFORM:
1360 case OMP_CLAUSE_INBRANCH:
1361 case OMP_CLAUSE_NOTINBRANCH:
1362 /* The following clauses are only allowed on OpenMP cancel and
1363 cancellation point directives, which at this point have already
1364 been lowered into a function call. */
1365 case OMP_CLAUSE_FOR:
1366 case OMP_CLAUSE_PARALLEL:
1367 case OMP_CLAUSE_SECTIONS:
1368 case OMP_CLAUSE_TASKGROUP:
1369 /* The following clauses are only added during OMP lowering; nested
1370 function decomposition happens before that. */
1371 case OMP_CLAUSE__LOOPTEMP_:
1372 case OMP_CLAUSE__REDUCTEMP_:
1373 case OMP_CLAUSE__SIMDUID_:
1374 case OMP_CLAUSE__GRIDDIM_:
1375 case OMP_CLAUSE__SIMT_:
1376 /* Anything else. */
1377 default:
1378 gcc_unreachable ();
1379 }
1380 }
1381
1382 info->suppress_expansion = new_suppress;
1383
1384 if (need_stmts)
1385 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1386 switch (OMP_CLAUSE_CODE (clause))
1387 {
1388 case OMP_CLAUSE_REDUCTION:
1389 case OMP_CLAUSE_IN_REDUCTION:
1390 case OMP_CLAUSE_TASK_REDUCTION:
1391 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1392 {
1393 tree old_context
1394 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1395 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1396 = info->context;
1397 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1398 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1399 = info->context;
1400 walk_body (convert_nonlocal_reference_stmt,
1401 convert_nonlocal_reference_op, info,
1402 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1403 walk_body (convert_nonlocal_reference_stmt,
1404 convert_nonlocal_reference_op, info,
1405 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1406 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1407 = old_context;
1408 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1409 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1410 = old_context;
1411 }
1412 break;
1413
1414 case OMP_CLAUSE_LASTPRIVATE:
1415 walk_body (convert_nonlocal_reference_stmt,
1416 convert_nonlocal_reference_op, info,
1417 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1418 break;
1419
1420 case OMP_CLAUSE_LINEAR:
1421 walk_body (convert_nonlocal_reference_stmt,
1422 convert_nonlocal_reference_op, info,
1423 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1424 break;
1425
1426 default:
1427 break;
1428 }
1429
1430 return need_chain;
1431 }
1432
1433 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1434
1435 static void
1436 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1437 {
1438 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1439 type = TREE_TYPE (type);
1440
1441 if (TYPE_NAME (type)
1442 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1443 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1444 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1445
1446 while (POINTER_TYPE_P (type)
1447 || TREE_CODE (type) == VECTOR_TYPE
1448 || TREE_CODE (type) == FUNCTION_TYPE
1449 || TREE_CODE (type) == METHOD_TYPE)
1450 type = TREE_TYPE (type);
1451
1452 if (TREE_CODE (type) == ARRAY_TYPE)
1453 {
1454 tree domain, t;
1455
1456 note_nonlocal_vla_type (info, TREE_TYPE (type));
1457 domain = TYPE_DOMAIN (type);
1458 if (domain)
1459 {
1460 t = TYPE_MIN_VALUE (domain);
1461 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1462 && decl_function_context (t) != info->context)
1463 get_nonlocal_debug_decl (info, t);
1464 t = TYPE_MAX_VALUE (domain);
1465 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1466 && decl_function_context (t) != info->context)
1467 get_nonlocal_debug_decl (info, t);
1468 }
1469 }
1470 }
1471
1472 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1473 PARM_DECLs that belong to outer functions. This handles statements
1474 that are not handled via the standard recursion done in
1475 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1476 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1477 operands of STMT have been handled by this function. */
1478
1479 static tree
1480 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1481 struct walk_stmt_info *wi)
1482 {
1483 struct nesting_info *info = (struct nesting_info *) wi->info;
1484 tree save_local_var_chain;
1485 bitmap save_suppress;
1486 gimple *stmt = gsi_stmt (*gsi);
1487
1488 switch (gimple_code (stmt))
1489 {
1490 case GIMPLE_GOTO:
1491 /* Don't walk non-local gotos for now. */
1492 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1493 {
1494 wi->val_only = true;
1495 wi->is_lhs = false;
1496 *handled_ops_p = false;
1497 return NULL_TREE;
1498 }
1499 break;
1500
1501 case GIMPLE_OMP_TEAMS:
1502 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
1503 {
1504 save_suppress = info->suppress_expansion;
1505 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt),
1506 wi);
1507 walk_body (convert_nonlocal_reference_stmt,
1508 convert_nonlocal_reference_op, info,
1509 gimple_omp_body_ptr (stmt));
1510 info->suppress_expansion = save_suppress;
1511 break;
1512 }
1513 /* FALLTHRU */
1514
1515 case GIMPLE_OMP_PARALLEL:
1516 case GIMPLE_OMP_TASK:
1517 save_suppress = info->suppress_expansion;
1518 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1519 wi))
1520 {
1521 tree c, decl;
1522 decl = get_chain_decl (info);
1523 c = build_omp_clause (gimple_location (stmt),
1524 OMP_CLAUSE_FIRSTPRIVATE);
1525 OMP_CLAUSE_DECL (c) = decl;
1526 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1527 gimple_omp_taskreg_set_clauses (stmt, c);
1528 }
1529
1530 save_local_var_chain = info->new_local_var_chain;
1531 info->new_local_var_chain = NULL;
1532
1533 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1534 info, gimple_omp_body_ptr (stmt));
1535
1536 if (info->new_local_var_chain)
1537 declare_vars (info->new_local_var_chain,
1538 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1539 false);
1540 info->new_local_var_chain = save_local_var_chain;
1541 info->suppress_expansion = save_suppress;
1542 break;
1543
1544 case GIMPLE_OMP_FOR:
1545 save_suppress = info->suppress_expansion;
1546 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1547 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1548 convert_nonlocal_reference_stmt,
1549 convert_nonlocal_reference_op, info);
1550 walk_body (convert_nonlocal_reference_stmt,
1551 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1552 info->suppress_expansion = save_suppress;
1553 break;
1554
1555 case GIMPLE_OMP_SECTIONS:
1556 save_suppress = info->suppress_expansion;
1557 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1558 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1559 info, gimple_omp_body_ptr (stmt));
1560 info->suppress_expansion = save_suppress;
1561 break;
1562
1563 case GIMPLE_OMP_SINGLE:
1564 save_suppress = info->suppress_expansion;
1565 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1566 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1567 info, gimple_omp_body_ptr (stmt));
1568 info->suppress_expansion = save_suppress;
1569 break;
1570
1571 case GIMPLE_OMP_TASKGROUP:
1572 save_suppress = info->suppress_expansion;
1573 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
1574 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1575 info, gimple_omp_body_ptr (stmt));
1576 info->suppress_expansion = save_suppress;
1577 break;
1578
1579 case GIMPLE_OMP_TARGET:
1580 if (!is_gimple_omp_offloaded (stmt))
1581 {
1582 save_suppress = info->suppress_expansion;
1583 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1584 wi);
1585 info->suppress_expansion = save_suppress;
1586 walk_body (convert_nonlocal_reference_stmt,
1587 convert_nonlocal_reference_op, info,
1588 gimple_omp_body_ptr (stmt));
1589 break;
1590 }
1591 save_suppress = info->suppress_expansion;
1592 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1593 wi))
1594 {
1595 tree c, decl;
1596 decl = get_chain_decl (info);
1597 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1598 OMP_CLAUSE_DECL (c) = decl;
1599 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1600 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1601 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1602 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1603 }
1604
1605 save_local_var_chain = info->new_local_var_chain;
1606 info->new_local_var_chain = NULL;
1607
1608 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1609 info, gimple_omp_body_ptr (stmt));
1610
1611 if (info->new_local_var_chain)
1612 declare_vars (info->new_local_var_chain,
1613 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1614 false);
1615 info->new_local_var_chain = save_local_var_chain;
1616 info->suppress_expansion = save_suppress;
1617 break;
1618
1619 case GIMPLE_OMP_SECTION:
1620 case GIMPLE_OMP_MASTER:
1621 case GIMPLE_OMP_ORDERED:
1622 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1623 info, gimple_omp_body_ptr (stmt));
1624 break;
1625
1626 case GIMPLE_BIND:
1627 {
1628 gbind *bind_stmt = as_a <gbind *> (stmt);
1629
1630 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1631 if (TREE_CODE (var) == NAMELIST_DECL)
1632 {
1633 /* Adjust decls mentioned in NAMELIST_DECL. */
1634 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1635 tree decl;
1636 unsigned int i;
1637
1638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1639 {
1640 if (VAR_P (decl)
1641 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1642 continue;
1643 if (decl_function_context (decl) != info->context)
1644 CONSTRUCTOR_ELT (decls, i)->value
1645 = get_nonlocal_debug_decl (info, decl);
1646 }
1647 }
1648
1649 *handled_ops_p = false;
1650 return NULL_TREE;
1651 }
1652 case GIMPLE_COND:
1653 wi->val_only = true;
1654 wi->is_lhs = false;
1655 *handled_ops_p = false;
1656 return NULL_TREE;
1657
1658 case GIMPLE_ASSIGN:
1659 if (gimple_clobber_p (stmt))
1660 {
1661 tree lhs = gimple_assign_lhs (stmt);
1662 if (DECL_P (lhs)
1663 && !(TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
1664 && decl_function_context (lhs) != info->context)
1665 {
1666 gsi_replace (gsi, gimple_build_nop (), true);
1667 break;
1668 }
1669 }
1670 *handled_ops_p = false;
1671 return NULL_TREE;
1672
1673 default:
1674 /* For every other statement that we are not interested in
1675 handling here, let the walker traverse the operands. */
1676 *handled_ops_p = false;
1677 return NULL_TREE;
1678 }
1679
1680 /* We have handled all of STMT operands, no need to traverse the operands. */
1681 *handled_ops_p = true;
1682 return NULL_TREE;
1683 }
1684
1685
1686 /* A subroutine of convert_local_reference. Create a local variable
1687 in the parent function with DECL_VALUE_EXPR set to reference the
1688 field in FRAME. This is used both for debug info and in OMP
1689 lowering. */
1690
1691 static tree
1692 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1693 {
1694 tree x, new_decl;
1695
1696 tree *slot = &info->var_map->get_or_insert (decl);
1697 if (*slot)
1698 return *slot;
1699
1700 /* Make sure frame_decl gets created. */
1701 (void) get_frame_type (info);
1702 x = info->frame_decl;
1703 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1704
1705 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1706 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1707 DECL_CONTEXT (new_decl) = info->context;
1708 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1709 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1710 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1711 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1712 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1713 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1714 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1715 if ((TREE_CODE (decl) == PARM_DECL
1716 || TREE_CODE (decl) == RESULT_DECL
1717 || VAR_P (decl))
1718 && DECL_BY_REFERENCE (decl))
1719 DECL_BY_REFERENCE (new_decl) = 1;
1720
1721 SET_DECL_VALUE_EXPR (new_decl, x);
1722 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1723 *slot = new_decl;
1724
1725 DECL_CHAIN (new_decl) = info->debug_var_chain;
1726 info->debug_var_chain = new_decl;
1727
1728 /* Do not emit debug info twice. */
1729 DECL_IGNORED_P (decl) = 1;
1730
1731 return new_decl;
1732 }
1733
1734
1735 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1736 and PARM_DECLs that were referenced by inner nested functions.
1737 The rewrite will be a structure reference to the local frame variable. */
1738
1739 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1740
1741 static tree
1742 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1743 {
1744 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1745 struct nesting_info *const info = (struct nesting_info *) wi->info;
1746 tree t = *tp, field, x;
1747 bool save_val_only;
1748
1749 *walk_subtrees = 0;
1750 switch (TREE_CODE (t))
1751 {
1752 case VAR_DECL:
1753 /* Non-automatic variables are never processed. */
1754 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1755 break;
1756 /* FALLTHRU */
1757
1758 case PARM_DECL:
1759 if (t != info->frame_decl && decl_function_context (t) == info->context)
1760 {
1761 /* If we copied a pointer to the frame, then the original decl
1762 is used unchanged in the parent function. */
1763 if (use_pointer_in_frame (t))
1764 break;
1765
1766 /* No need to transform anything if no child references the
1767 variable. */
1768 field = lookup_field_for_decl (info, t, NO_INSERT);
1769 if (!field)
1770 break;
1771 wi->changed = true;
1772
1773 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1774 x = get_local_debug_decl (info, t, field);
1775 else
1776 x = get_frame_field (info, info->context, field, &wi->gsi);
1777
1778 if (wi->val_only)
1779 {
1780 if (wi->is_lhs)
1781 x = save_tmp_var (info, x, &wi->gsi);
1782 else
1783 x = init_tmp_var (info, x, &wi->gsi);
1784 }
1785
1786 *tp = x;
1787 }
1788 break;
1789
1790 case ADDR_EXPR:
1791 save_val_only = wi->val_only;
1792 wi->val_only = false;
1793 wi->is_lhs = false;
1794 wi->changed = false;
1795 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1796 wi->val_only = save_val_only;
1797
1798 /* If we converted anything ... */
1799 if (wi->changed)
1800 {
1801 tree save_context;
1802
1803 /* Then the frame decl is now addressable. */
1804 TREE_ADDRESSABLE (info->frame_decl) = 1;
1805
1806 save_context = current_function_decl;
1807 current_function_decl = info->context;
1808 recompute_tree_invariant_for_addr_expr (t);
1809 current_function_decl = save_context;
1810
1811 /* If we are in a context where we only accept values, then
1812 compute the address into a temporary. */
1813 if (save_val_only)
1814 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1815 t, &wi->gsi);
1816 }
1817 break;
1818
1819 case REALPART_EXPR:
1820 case IMAGPART_EXPR:
1821 case COMPONENT_REF:
1822 case ARRAY_REF:
1823 case ARRAY_RANGE_REF:
1824 case BIT_FIELD_REF:
1825 /* Go down this entire nest and just look at the final prefix and
1826 anything that describes the references. Otherwise, we lose track
1827 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1828 save_val_only = wi->val_only;
1829 wi->val_only = true;
1830 wi->is_lhs = false;
1831 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1832 {
1833 if (TREE_CODE (t) == COMPONENT_REF)
1834 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1835 NULL);
1836 else if (TREE_CODE (t) == ARRAY_REF
1837 || TREE_CODE (t) == ARRAY_RANGE_REF)
1838 {
1839 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1840 NULL);
1841 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1842 NULL);
1843 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1844 NULL);
1845 }
1846 }
1847 wi->val_only = false;
1848 walk_tree (tp, convert_local_reference_op, wi, NULL);
1849 wi->val_only = save_val_only;
1850 break;
1851
1852 case MEM_REF:
1853 save_val_only = wi->val_only;
1854 wi->val_only = true;
1855 wi->is_lhs = false;
1856 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1857 wi, NULL);
1858 /* We need to re-fold the MEM_REF as component references as
1859 part of a ADDR_EXPR address are not allowed. But we cannot
1860 fold here, as the chain record type is not yet finalized. */
1861 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1862 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1863 info->mem_refs->add (tp);
1864 wi->val_only = save_val_only;
1865 break;
1866
1867 case VIEW_CONVERT_EXPR:
1868 /* Just request to look at the subtrees, leaving val_only and lhs
1869 untouched. This might actually be for !val_only + lhs, in which
1870 case we don't want to force a replacement by a temporary. */
1871 *walk_subtrees = 1;
1872 break;
1873
1874 default:
1875 if (!IS_TYPE_OR_DECL_P (t))
1876 {
1877 *walk_subtrees = 1;
1878 wi->val_only = true;
1879 wi->is_lhs = false;
1880 }
1881 break;
1882 }
1883
1884 return NULL_TREE;
1885 }
1886
1887 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1888 struct walk_stmt_info *);
1889
1890 /* Helper for convert_local_reference. Convert all the references in
1891 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1892
1893 static bool
1894 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1895 {
1896 struct nesting_info *const info = (struct nesting_info *) wi->info;
1897 bool need_frame = false, need_stmts = false;
1898 tree clause, decl;
1899 int dummy;
1900 bitmap new_suppress;
1901
1902 new_suppress = BITMAP_GGC_ALLOC ();
1903 bitmap_copy (new_suppress, info->suppress_expansion);
1904
1905 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1906 {
1907 switch (OMP_CLAUSE_CODE (clause))
1908 {
1909 case OMP_CLAUSE_REDUCTION:
1910 case OMP_CLAUSE_IN_REDUCTION:
1911 case OMP_CLAUSE_TASK_REDUCTION:
1912 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1913 need_stmts = true;
1914 goto do_decl_clause;
1915
1916 case OMP_CLAUSE_LASTPRIVATE:
1917 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1918 need_stmts = true;
1919 goto do_decl_clause;
1920
1921 case OMP_CLAUSE_LINEAR:
1922 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1923 need_stmts = true;
1924 wi->val_only = true;
1925 wi->is_lhs = false;
1926 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1927 wi);
1928 goto do_decl_clause;
1929
1930 case OMP_CLAUSE_PRIVATE:
1931 case OMP_CLAUSE_FIRSTPRIVATE:
1932 case OMP_CLAUSE_COPYPRIVATE:
1933 case OMP_CLAUSE_SHARED:
1934 case OMP_CLAUSE_TO_DECLARE:
1935 case OMP_CLAUSE_LINK:
1936 case OMP_CLAUSE_USE_DEVICE_PTR:
1937 case OMP_CLAUSE_IS_DEVICE_PTR:
1938 do_decl_clause:
1939 decl = OMP_CLAUSE_DECL (clause);
1940 if (VAR_P (decl)
1941 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1942 break;
1943 if (decl_function_context (decl) == info->context
1944 && !use_pointer_in_frame (decl))
1945 {
1946 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1947 if (field)
1948 {
1949 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1950 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1951 bitmap_set_bit (new_suppress, DECL_UID (decl));
1952 OMP_CLAUSE_DECL (clause)
1953 = get_local_debug_decl (info, decl, field);
1954 need_frame = true;
1955 }
1956 }
1957 break;
1958
1959 case OMP_CLAUSE_SCHEDULE:
1960 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1961 break;
1962 /* FALLTHRU */
1963 case OMP_CLAUSE_FINAL:
1964 case OMP_CLAUSE_IF:
1965 case OMP_CLAUSE_NUM_THREADS:
1966 case OMP_CLAUSE_DEPEND:
1967 case OMP_CLAUSE_DEVICE:
1968 case OMP_CLAUSE_NUM_TEAMS:
1969 case OMP_CLAUSE_THREAD_LIMIT:
1970 case OMP_CLAUSE_SAFELEN:
1971 case OMP_CLAUSE_SIMDLEN:
1972 case OMP_CLAUSE_PRIORITY:
1973 case OMP_CLAUSE_GRAINSIZE:
1974 case OMP_CLAUSE_NUM_TASKS:
1975 case OMP_CLAUSE_HINT:
1976 case OMP_CLAUSE_NUM_GANGS:
1977 case OMP_CLAUSE_NUM_WORKERS:
1978 case OMP_CLAUSE_VECTOR_LENGTH:
1979 case OMP_CLAUSE_GANG:
1980 case OMP_CLAUSE_WORKER:
1981 case OMP_CLAUSE_VECTOR:
1982 case OMP_CLAUSE_ASYNC:
1983 case OMP_CLAUSE_WAIT:
1984 /* Several OpenACC clauses have optional arguments. Check if they
1985 are present. */
1986 if (OMP_CLAUSE_OPERAND (clause, 0))
1987 {
1988 wi->val_only = true;
1989 wi->is_lhs = false;
1990 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1991 &dummy, wi);
1992 }
1993
1994 /* The gang clause accepts two arguments. */
1995 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1996 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1997 {
1998 wi->val_only = true;
1999 wi->is_lhs = false;
2000 convert_nonlocal_reference_op
2001 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
2002 }
2003 break;
2004
2005 case OMP_CLAUSE_DIST_SCHEDULE:
2006 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
2007 {
2008 wi->val_only = true;
2009 wi->is_lhs = false;
2010 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2011 &dummy, wi);
2012 }
2013 break;
2014
2015 case OMP_CLAUSE_MAP:
2016 case OMP_CLAUSE_TO:
2017 case OMP_CLAUSE_FROM:
2018 if (OMP_CLAUSE_SIZE (clause))
2019 {
2020 wi->val_only = true;
2021 wi->is_lhs = false;
2022 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
2023 &dummy, wi);
2024 }
2025 if (DECL_P (OMP_CLAUSE_DECL (clause)))
2026 goto do_decl_clause;
2027 wi->val_only = true;
2028 wi->is_lhs = false;
2029 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
2030 wi, NULL);
2031 break;
2032
2033 case OMP_CLAUSE_ALIGNED:
2034 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2035 {
2036 wi->val_only = true;
2037 wi->is_lhs = false;
2038 convert_local_reference_op
2039 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2040 }
2041 /* FALLTHRU */
2042 case OMP_CLAUSE_NONTEMPORAL:
2043 /* Like do_decl_clause, but don't add any suppression. */
2044 decl = OMP_CLAUSE_DECL (clause);
2045 if (VAR_P (decl)
2046 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2047 break;
2048 if (decl_function_context (decl) == info->context
2049 && !use_pointer_in_frame (decl))
2050 {
2051 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2052 if (field)
2053 {
2054 OMP_CLAUSE_DECL (clause)
2055 = get_local_debug_decl (info, decl, field);
2056 need_frame = true;
2057 }
2058 }
2059 break;
2060
2061 case OMP_CLAUSE_NOWAIT:
2062 case OMP_CLAUSE_ORDERED:
2063 case OMP_CLAUSE_DEFAULT:
2064 case OMP_CLAUSE_COPYIN:
2065 case OMP_CLAUSE_COLLAPSE:
2066 case OMP_CLAUSE_TILE:
2067 case OMP_CLAUSE_UNTIED:
2068 case OMP_CLAUSE_MERGEABLE:
2069 case OMP_CLAUSE_PROC_BIND:
2070 case OMP_CLAUSE_NOGROUP:
2071 case OMP_CLAUSE_THREADS:
2072 case OMP_CLAUSE_SIMD:
2073 case OMP_CLAUSE_DEFAULTMAP:
2074 case OMP_CLAUSE_SEQ:
2075 case OMP_CLAUSE_INDEPENDENT:
2076 case OMP_CLAUSE_AUTO:
2077 case OMP_CLAUSE_IF_PRESENT:
2078 case OMP_CLAUSE_FINALIZE:
2079 case OMP_CLAUSE__CONDTEMP_:
2080 break;
2081
2082 /* The following clause belongs to the OpenACC cache directive, which
2083 is discarded during gimplification. */
2084 case OMP_CLAUSE__CACHE_:
2085 /* The following clauses are only allowed in the OpenMP declare simd
2086 directive, so not seen here. */
2087 case OMP_CLAUSE_UNIFORM:
2088 case OMP_CLAUSE_INBRANCH:
2089 case OMP_CLAUSE_NOTINBRANCH:
2090 /* The following clauses are only allowed on OpenMP cancel and
2091 cancellation point directives, which at this point have already
2092 been lowered into a function call. */
2093 case OMP_CLAUSE_FOR:
2094 case OMP_CLAUSE_PARALLEL:
2095 case OMP_CLAUSE_SECTIONS:
2096 case OMP_CLAUSE_TASKGROUP:
2097 /* The following clauses are only added during OMP lowering; nested
2098 function decomposition happens before that. */
2099 case OMP_CLAUSE__LOOPTEMP_:
2100 case OMP_CLAUSE__REDUCTEMP_:
2101 case OMP_CLAUSE__SIMDUID_:
2102 case OMP_CLAUSE__GRIDDIM_:
2103 case OMP_CLAUSE__SIMT_:
2104 /* Anything else. */
2105 default:
2106 gcc_unreachable ();
2107 }
2108 }
2109
2110 info->suppress_expansion = new_suppress;
2111
2112 if (need_stmts)
2113 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2114 switch (OMP_CLAUSE_CODE (clause))
2115 {
2116 case OMP_CLAUSE_REDUCTION:
2117 case OMP_CLAUSE_IN_REDUCTION:
2118 case OMP_CLAUSE_TASK_REDUCTION:
2119 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2120 {
2121 tree old_context
2122 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2123 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2124 = info->context;
2125 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2126 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2127 = info->context;
2128 walk_body (convert_local_reference_stmt,
2129 convert_local_reference_op, info,
2130 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2131 walk_body (convert_local_reference_stmt,
2132 convert_local_reference_op, info,
2133 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2134 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2135 = old_context;
2136 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2137 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2138 = old_context;
2139 }
2140 break;
2141
2142 case OMP_CLAUSE_LASTPRIVATE:
2143 walk_body (convert_local_reference_stmt,
2144 convert_local_reference_op, info,
2145 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2146 break;
2147
2148 case OMP_CLAUSE_LINEAR:
2149 walk_body (convert_local_reference_stmt,
2150 convert_local_reference_op, info,
2151 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2152 break;
2153
2154 default:
2155 break;
2156 }
2157
2158 return need_frame;
2159 }
2160
2161
2162 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2163 and PARM_DECLs that were referenced by inner nested functions.
2164 The rewrite will be a structure reference to the local frame variable. */
2165
2166 static tree
2167 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2168 struct walk_stmt_info *wi)
2169 {
2170 struct nesting_info *info = (struct nesting_info *) wi->info;
2171 tree save_local_var_chain;
2172 bitmap save_suppress;
2173 char save_static_chain_added;
2174 bool frame_decl_added;
2175 gimple *stmt = gsi_stmt (*gsi);
2176
2177 switch (gimple_code (stmt))
2178 {
2179 case GIMPLE_OMP_TEAMS:
2180 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2181 {
2182 save_suppress = info->suppress_expansion;
2183 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2184 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2185 info, gimple_omp_body_ptr (stmt));
2186 info->suppress_expansion = save_suppress;
2187 break;
2188 }
2189 /* FALLTHRU */
2190
2191 case GIMPLE_OMP_PARALLEL:
2192 case GIMPLE_OMP_TASK:
2193 save_suppress = info->suppress_expansion;
2194 frame_decl_added = false;
2195 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2196 wi))
2197 {
2198 tree c = build_omp_clause (gimple_location (stmt),
2199 OMP_CLAUSE_SHARED);
2200 (void) get_frame_type (info);
2201 OMP_CLAUSE_DECL (c) = info->frame_decl;
2202 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2203 gimple_omp_taskreg_set_clauses (stmt, c);
2204 info->static_chain_added |= 4;
2205 frame_decl_added = true;
2206 }
2207
2208 save_local_var_chain = info->new_local_var_chain;
2209 save_static_chain_added = info->static_chain_added;
2210 info->new_local_var_chain = NULL;
2211 info->static_chain_added = 0;
2212
2213 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2214 gimple_omp_body_ptr (stmt));
2215
2216 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2217 {
2218 tree c = build_omp_clause (gimple_location (stmt),
2219 OMP_CLAUSE_SHARED);
2220 (void) get_frame_type (info);
2221 OMP_CLAUSE_DECL (c) = info->frame_decl;
2222 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2223 info->static_chain_added |= 4;
2224 gimple_omp_taskreg_set_clauses (stmt, c);
2225 }
2226 if (info->new_local_var_chain)
2227 declare_vars (info->new_local_var_chain,
2228 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2229 info->new_local_var_chain = save_local_var_chain;
2230 info->suppress_expansion = save_suppress;
2231 info->static_chain_added |= save_static_chain_added;
2232 break;
2233
2234 case GIMPLE_OMP_FOR:
2235 save_suppress = info->suppress_expansion;
2236 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2237 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2238 convert_local_reference_stmt,
2239 convert_local_reference_op, info);
2240 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2241 info, gimple_omp_body_ptr (stmt));
2242 info->suppress_expansion = save_suppress;
2243 break;
2244
2245 case GIMPLE_OMP_SECTIONS:
2246 save_suppress = info->suppress_expansion;
2247 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2248 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2249 info, gimple_omp_body_ptr (stmt));
2250 info->suppress_expansion = save_suppress;
2251 break;
2252
2253 case GIMPLE_OMP_SINGLE:
2254 save_suppress = info->suppress_expansion;
2255 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2256 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2257 info, gimple_omp_body_ptr (stmt));
2258 info->suppress_expansion = save_suppress;
2259 break;
2260
2261 case GIMPLE_OMP_TASKGROUP:
2262 save_suppress = info->suppress_expansion;
2263 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
2264 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2265 info, gimple_omp_body_ptr (stmt));
2266 info->suppress_expansion = save_suppress;
2267 break;
2268
2269 case GIMPLE_OMP_TARGET:
2270 if (!is_gimple_omp_offloaded (stmt))
2271 {
2272 save_suppress = info->suppress_expansion;
2273 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2274 info->suppress_expansion = save_suppress;
2275 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2276 info, gimple_omp_body_ptr (stmt));
2277 break;
2278 }
2279 save_suppress = info->suppress_expansion;
2280 frame_decl_added = false;
2281 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2282 {
2283 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2284 (void) get_frame_type (info);
2285 OMP_CLAUSE_DECL (c) = info->frame_decl;
2286 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2287 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2288 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2289 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2290 info->static_chain_added |= 4;
2291 frame_decl_added = true;
2292 }
2293
2294 save_local_var_chain = info->new_local_var_chain;
2295 save_static_chain_added = info->static_chain_added;
2296 info->new_local_var_chain = NULL;
2297 info->static_chain_added = 0;
2298
2299 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2300 gimple_omp_body_ptr (stmt));
2301
2302 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2303 {
2304 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2305 (void) get_frame_type (info);
2306 OMP_CLAUSE_DECL (c) = info->frame_decl;
2307 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2308 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2309 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2310 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2311 info->static_chain_added |= 4;
2312 }
2313
2314 if (info->new_local_var_chain)
2315 declare_vars (info->new_local_var_chain,
2316 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2317 info->new_local_var_chain = save_local_var_chain;
2318 info->suppress_expansion = save_suppress;
2319 info->static_chain_added |= save_static_chain_added;
2320 break;
2321
2322 case GIMPLE_OMP_SECTION:
2323 case GIMPLE_OMP_MASTER:
2324 case GIMPLE_OMP_ORDERED:
2325 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2326 info, gimple_omp_body_ptr (stmt));
2327 break;
2328
2329 case GIMPLE_COND:
2330 wi->val_only = true;
2331 wi->is_lhs = false;
2332 *handled_ops_p = false;
2333 return NULL_TREE;
2334
2335 case GIMPLE_ASSIGN:
2336 if (gimple_clobber_p (stmt))
2337 {
2338 tree lhs = gimple_assign_lhs (stmt);
2339 if (DECL_P (lhs)
2340 && !use_pointer_in_frame (lhs)
2341 && lookup_field_for_decl (info, lhs, NO_INSERT))
2342 {
2343 gsi_replace (gsi, gimple_build_nop (), true);
2344 break;
2345 }
2346 }
2347 *handled_ops_p = false;
2348 return NULL_TREE;
2349
2350 case GIMPLE_BIND:
2351 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2352 var;
2353 var = DECL_CHAIN (var))
2354 if (TREE_CODE (var) == NAMELIST_DECL)
2355 {
2356 /* Adjust decls mentioned in NAMELIST_DECL. */
2357 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2358 tree decl;
2359 unsigned int i;
2360
2361 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2362 {
2363 if (VAR_P (decl)
2364 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2365 continue;
2366 if (decl_function_context (decl) == info->context
2367 && !use_pointer_in_frame (decl))
2368 {
2369 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2370 if (field)
2371 {
2372 CONSTRUCTOR_ELT (decls, i)->value
2373 = get_local_debug_decl (info, decl, field);
2374 }
2375 }
2376 }
2377 }
2378
2379 *handled_ops_p = false;
2380 return NULL_TREE;
2381
2382 default:
2383 /* For every other statement that we are not interested in
2384 handling here, let the walker traverse the operands. */
2385 *handled_ops_p = false;
2386 return NULL_TREE;
2387 }
2388
2389 /* Indicate that we have handled all the operands ourselves. */
2390 *handled_ops_p = true;
2391 return NULL_TREE;
2392 }
2393
2394
2395 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2396 that reference labels from outer functions. The rewrite will be a
2397 call to __builtin_nonlocal_goto. */
2398
2399 static tree
2400 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2401 struct walk_stmt_info *wi)
2402 {
2403 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2404 tree label, new_label, target_context, x, field;
2405 gcall *call;
2406 gimple *stmt = gsi_stmt (*gsi);
2407
2408 if (gimple_code (stmt) != GIMPLE_GOTO)
2409 {
2410 *handled_ops_p = false;
2411 return NULL_TREE;
2412 }
2413
2414 label = gimple_goto_dest (stmt);
2415 if (TREE_CODE (label) != LABEL_DECL)
2416 {
2417 *handled_ops_p = false;
2418 return NULL_TREE;
2419 }
2420
2421 target_context = decl_function_context (label);
2422 if (target_context == info->context)
2423 {
2424 *handled_ops_p = false;
2425 return NULL_TREE;
2426 }
2427
2428 for (i = info->outer; target_context != i->context; i = i->outer)
2429 continue;
2430
2431 /* The original user label may also be use for a normal goto, therefore
2432 we must create a new label that will actually receive the abnormal
2433 control transfer. This new label will be marked LABEL_NONLOCAL; this
2434 mark will trigger proper behavior in the cfg, as well as cause the
2435 (hairy target-specific) non-local goto receiver code to be generated
2436 when we expand rtl. Enter this association into var_map so that we
2437 can insert the new label into the IL during a second pass. */
2438 tree *slot = &i->var_map->get_or_insert (label);
2439 if (*slot == NULL)
2440 {
2441 new_label = create_artificial_label (UNKNOWN_LOCATION);
2442 DECL_NONLOCAL (new_label) = 1;
2443 *slot = new_label;
2444 }
2445 else
2446 new_label = *slot;
2447
2448 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2449 field = get_nl_goto_field (i);
2450 x = get_frame_field (info, target_context, field, gsi);
2451 x = build_addr (x);
2452 x = gsi_gimplify_val (info, x, gsi);
2453 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2454 2, build_addr (new_label), x);
2455 gsi_replace (gsi, call, false);
2456
2457 /* We have handled all of STMT's operands, no need to keep going. */
2458 *handled_ops_p = true;
2459 return NULL_TREE;
2460 }
2461
2462
2463 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2464 are referenced via nonlocal goto from a nested function. The rewrite
2465 will involve installing a newly generated DECL_NONLOCAL label, and
2466 (potentially) a branch around the rtl gunk that is assumed to be
2467 attached to such a label. */
2468
2469 static tree
2470 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2471 struct walk_stmt_info *wi)
2472 {
2473 struct nesting_info *const info = (struct nesting_info *) wi->info;
2474 tree label, new_label;
2475 gimple_stmt_iterator tmp_gsi;
2476 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2477
2478 if (!stmt)
2479 {
2480 *handled_ops_p = false;
2481 return NULL_TREE;
2482 }
2483
2484 label = gimple_label_label (stmt);
2485
2486 tree *slot = info->var_map->get (label);
2487 if (!slot)
2488 {
2489 *handled_ops_p = false;
2490 return NULL_TREE;
2491 }
2492
2493 /* If there's any possibility that the previous statement falls through,
2494 then we must branch around the new non-local label. */
2495 tmp_gsi = wi->gsi;
2496 gsi_prev (&tmp_gsi);
2497 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2498 {
2499 gimple *stmt = gimple_build_goto (label);
2500 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2501 }
2502
2503 new_label = (tree) *slot;
2504 stmt = gimple_build_label (new_label);
2505 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2506
2507 *handled_ops_p = true;
2508 return NULL_TREE;
2509 }
2510
2511
2512 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2513 of nested functions that require the use of trampolines. The rewrite
2514 will involve a reference a trampoline generated for the occasion. */
2515
2516 static tree
2517 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2518 {
2519 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2520 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2521 tree t = *tp, decl, target_context, x, builtin;
2522 bool descr;
2523 gcall *call;
2524
2525 *walk_subtrees = 0;
2526 switch (TREE_CODE (t))
2527 {
2528 case ADDR_EXPR:
2529 /* Build
2530 T.1 = &CHAIN->tramp;
2531 T.2 = __builtin_adjust_trampoline (T.1);
2532 T.3 = (func_type)T.2;
2533 */
2534
2535 decl = TREE_OPERAND (t, 0);
2536 if (TREE_CODE (decl) != FUNCTION_DECL)
2537 break;
2538
2539 /* Only need to process nested functions. */
2540 target_context = decl_function_context (decl);
2541 if (!target_context)
2542 break;
2543
2544 /* If the nested function doesn't use a static chain, then
2545 it doesn't need a trampoline. */
2546 if (!DECL_STATIC_CHAIN (decl))
2547 break;
2548
2549 /* If we don't want a trampoline, then don't build one. */
2550 if (TREE_NO_TRAMPOLINE (t))
2551 break;
2552
2553 /* Lookup the immediate parent of the callee, as that's where
2554 we need to insert the trampoline. */
2555 for (i = info; i->context != target_context; i = i->outer)
2556 continue;
2557
2558 /* Decide whether to generate a descriptor or a trampoline. */
2559 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2560
2561 if (descr)
2562 x = lookup_descr_for_decl (i, decl, INSERT);
2563 else
2564 x = lookup_tramp_for_decl (i, decl, INSERT);
2565
2566 /* Compute the address of the field holding the trampoline. */
2567 x = get_frame_field (info, target_context, x, &wi->gsi);
2568 x = build_addr (x);
2569 x = gsi_gimplify_val (info, x, &wi->gsi);
2570
2571 /* Do machine-specific ugliness. Normally this will involve
2572 computing extra alignment, but it can really be anything. */
2573 if (descr)
2574 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2575 else
2576 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2577 call = gimple_build_call (builtin, 1, x);
2578 x = init_tmp_var_with_call (info, &wi->gsi, call);
2579
2580 /* Cast back to the proper function type. */
2581 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2582 x = init_tmp_var (info, x, &wi->gsi);
2583
2584 *tp = x;
2585 break;
2586
2587 default:
2588 if (!IS_TYPE_OR_DECL_P (t))
2589 *walk_subtrees = 1;
2590 break;
2591 }
2592
2593 return NULL_TREE;
2594 }
2595
2596
2597 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2598 to addresses of nested functions that require the use of
2599 trampolines. The rewrite will involve a reference a trampoline
2600 generated for the occasion. */
2601
2602 static tree
2603 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2604 struct walk_stmt_info *wi)
2605 {
2606 struct nesting_info *info = (struct nesting_info *) wi->info;
2607 gimple *stmt = gsi_stmt (*gsi);
2608
2609 switch (gimple_code (stmt))
2610 {
2611 case GIMPLE_CALL:
2612 {
2613 /* Only walk call arguments, lest we generate trampolines for
2614 direct calls. */
2615 unsigned long i, nargs = gimple_call_num_args (stmt);
2616 for (i = 0; i < nargs; i++)
2617 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2618 wi, NULL);
2619 break;
2620 }
2621
2622 case GIMPLE_OMP_TEAMS:
2623 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2624 {
2625 *handled_ops_p = false;
2626 return NULL_TREE;
2627 }
2628 goto do_parallel;
2629
2630 case GIMPLE_OMP_TARGET:
2631 if (!is_gimple_omp_offloaded (stmt))
2632 {
2633 *handled_ops_p = false;
2634 return NULL_TREE;
2635 }
2636 /* FALLTHRU */
2637 case GIMPLE_OMP_PARALLEL:
2638 case GIMPLE_OMP_TASK:
2639 do_parallel:
2640 {
2641 tree save_local_var_chain = info->new_local_var_chain;
2642 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2643 info->new_local_var_chain = NULL;
2644 char save_static_chain_added = info->static_chain_added;
2645 info->static_chain_added = 0;
2646 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2647 info, gimple_omp_body_ptr (stmt));
2648 if (info->new_local_var_chain)
2649 declare_vars (info->new_local_var_chain,
2650 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2651 false);
2652 for (int i = 0; i < 2; i++)
2653 {
2654 tree c, decl;
2655 if ((info->static_chain_added & (1 << i)) == 0)
2656 continue;
2657 decl = i ? get_chain_decl (info) : info->frame_decl;
2658 /* Don't add CHAIN.* or FRAME.* twice. */
2659 for (c = gimple_omp_taskreg_clauses (stmt);
2660 c;
2661 c = OMP_CLAUSE_CHAIN (c))
2662 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2663 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2664 && OMP_CLAUSE_DECL (c) == decl)
2665 break;
2666 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2667 {
2668 c = build_omp_clause (gimple_location (stmt),
2669 i ? OMP_CLAUSE_FIRSTPRIVATE
2670 : OMP_CLAUSE_SHARED);
2671 OMP_CLAUSE_DECL (c) = decl;
2672 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2673 gimple_omp_taskreg_set_clauses (stmt, c);
2674 }
2675 else if (c == NULL)
2676 {
2677 c = build_omp_clause (gimple_location (stmt),
2678 OMP_CLAUSE_MAP);
2679 OMP_CLAUSE_DECL (c) = decl;
2680 OMP_CLAUSE_SET_MAP_KIND (c,
2681 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2682 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2683 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2684 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2685 c);
2686 }
2687 }
2688 info->new_local_var_chain = save_local_var_chain;
2689 info->static_chain_added |= save_static_chain_added;
2690 }
2691 break;
2692
2693 default:
2694 *handled_ops_p = false;
2695 return NULL_TREE;
2696 }
2697
2698 *handled_ops_p = true;
2699 return NULL_TREE;
2700 }
2701
2702
2703
2704 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2705 that reference nested functions to make sure that the static chain
2706 is set up properly for the call. */
2707
2708 static tree
2709 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2710 struct walk_stmt_info *wi)
2711 {
2712 struct nesting_info *const info = (struct nesting_info *) wi->info;
2713 tree decl, target_context;
2714 char save_static_chain_added;
2715 int i;
2716 gimple *stmt = gsi_stmt (*gsi);
2717
2718 switch (gimple_code (stmt))
2719 {
2720 case GIMPLE_CALL:
2721 if (gimple_call_chain (stmt))
2722 break;
2723 decl = gimple_call_fndecl (stmt);
2724 if (!decl)
2725 break;
2726 target_context = decl_function_context (decl);
2727 if (target_context && DECL_STATIC_CHAIN (decl))
2728 {
2729 struct nesting_info *i = info;
2730 while (i && i->context != target_context)
2731 i = i->outer;
2732 /* If none of the outer contexts is the target context, this means
2733 that the function is called in a wrong context. */
2734 if (!i)
2735 internal_error ("%s from %s called in %s",
2736 IDENTIFIER_POINTER (DECL_NAME (decl)),
2737 IDENTIFIER_POINTER (DECL_NAME (target_context)),
2738 IDENTIFIER_POINTER (DECL_NAME (info->context)));
2739
2740 gimple_call_set_chain (as_a <gcall *> (stmt),
2741 get_static_chain (info, target_context,
2742 &wi->gsi));
2743 info->static_chain_added |= (1 << (info->context != target_context));
2744 }
2745 break;
2746
2747 case GIMPLE_OMP_TEAMS:
2748 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2749 {
2750 walk_body (convert_gimple_call, NULL, info,
2751 gimple_omp_body_ptr (stmt));
2752 break;
2753 }
2754 /* FALLTHRU */
2755
2756 case GIMPLE_OMP_PARALLEL:
2757 case GIMPLE_OMP_TASK:
2758 save_static_chain_added = info->static_chain_added;
2759 info->static_chain_added = 0;
2760 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2761 for (i = 0; i < 2; i++)
2762 {
2763 tree c, decl;
2764 if ((info->static_chain_added & (1 << i)) == 0)
2765 continue;
2766 decl = i ? get_chain_decl (info) : info->frame_decl;
2767 /* Don't add CHAIN.* or FRAME.* twice. */
2768 for (c = gimple_omp_taskreg_clauses (stmt);
2769 c;
2770 c = OMP_CLAUSE_CHAIN (c))
2771 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2772 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2773 && OMP_CLAUSE_DECL (c) == decl)
2774 break;
2775 if (c == NULL)
2776 {
2777 c = build_omp_clause (gimple_location (stmt),
2778 i ? OMP_CLAUSE_FIRSTPRIVATE
2779 : OMP_CLAUSE_SHARED);
2780 OMP_CLAUSE_DECL (c) = decl;
2781 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2782 gimple_omp_taskreg_set_clauses (stmt, c);
2783 }
2784 }
2785 info->static_chain_added |= save_static_chain_added;
2786 break;
2787
2788 case GIMPLE_OMP_TARGET:
2789 if (!is_gimple_omp_offloaded (stmt))
2790 {
2791 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2792 break;
2793 }
2794 save_static_chain_added = info->static_chain_added;
2795 info->static_chain_added = 0;
2796 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2797 for (i = 0; i < 2; i++)
2798 {
2799 tree c, decl;
2800 if ((info->static_chain_added & (1 << i)) == 0)
2801 continue;
2802 decl = i ? get_chain_decl (info) : info->frame_decl;
2803 /* Don't add CHAIN.* or FRAME.* twice. */
2804 for (c = gimple_omp_target_clauses (stmt);
2805 c;
2806 c = OMP_CLAUSE_CHAIN (c))
2807 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2808 && OMP_CLAUSE_DECL (c) == decl)
2809 break;
2810 if (c == NULL)
2811 {
2812 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2813 OMP_CLAUSE_DECL (c) = decl;
2814 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2815 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2816 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2817 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2818 c);
2819 }
2820 }
2821 info->static_chain_added |= save_static_chain_added;
2822 break;
2823
2824 case GIMPLE_OMP_FOR:
2825 walk_body (convert_gimple_call, NULL, info,
2826 gimple_omp_for_pre_body_ptr (stmt));
2827 /* FALLTHRU */
2828 case GIMPLE_OMP_SECTIONS:
2829 case GIMPLE_OMP_SECTION:
2830 case GIMPLE_OMP_SINGLE:
2831 case GIMPLE_OMP_MASTER:
2832 case GIMPLE_OMP_TASKGROUP:
2833 case GIMPLE_OMP_ORDERED:
2834 case GIMPLE_OMP_CRITICAL:
2835 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2836 break;
2837
2838 default:
2839 /* Keep looking for other operands. */
2840 *handled_ops_p = false;
2841 return NULL_TREE;
2842 }
2843
2844 *handled_ops_p = true;
2845 return NULL_TREE;
2846 }
2847
2848 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2849 call expressions. At the same time, determine if a nested function
2850 actually uses its static chain; if not, remember that. */
2851
2852 static void
2853 convert_all_function_calls (struct nesting_info *root)
2854 {
2855 unsigned int chain_count = 0, old_chain_count, iter_count;
2856 struct nesting_info *n;
2857
2858 /* First, optimistically clear static_chain for all decls that haven't
2859 used the static chain already for variable access. But always create
2860 it if not optimizing. This makes it possible to reconstruct the static
2861 nesting tree at run time and thus to resolve up-level references from
2862 within the debugger. */
2863 FOR_EACH_NEST_INFO (n, root)
2864 {
2865 if (n->thunk_p)
2866 continue;
2867 tree decl = n->context;
2868 if (!optimize)
2869 {
2870 if (n->inner)
2871 (void) get_frame_type (n);
2872 if (n->outer)
2873 (void) get_chain_decl (n);
2874 }
2875 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2876 {
2877 DECL_STATIC_CHAIN (decl) = 0;
2878 if (dump_file && (dump_flags & TDF_DETAILS))
2879 fprintf (dump_file, "Guessing no static-chain for %s\n",
2880 lang_hooks.decl_printable_name (decl, 2));
2881 }
2882 else
2883 DECL_STATIC_CHAIN (decl) = 1;
2884 chain_count += DECL_STATIC_CHAIN (decl);
2885 }
2886
2887 FOR_EACH_NEST_INFO (n, root)
2888 if (n->thunk_p)
2889 {
2890 tree decl = n->context;
2891 tree alias = cgraph_node::get (decl)->thunk.alias;
2892 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2893 }
2894
2895 /* Walk the functions and perform transformations. Note that these
2896 transformations can induce new uses of the static chain, which in turn
2897 require re-examining all users of the decl. */
2898 /* ??? It would make sense to try to use the call graph to speed this up,
2899 but the call graph hasn't really been built yet. Even if it did, we
2900 would still need to iterate in this loop since address-of references
2901 wouldn't show up in the callgraph anyway. */
2902 iter_count = 0;
2903 do
2904 {
2905 old_chain_count = chain_count;
2906 chain_count = 0;
2907 iter_count++;
2908
2909 if (dump_file && (dump_flags & TDF_DETAILS))
2910 fputc ('\n', dump_file);
2911
2912 FOR_EACH_NEST_INFO (n, root)
2913 {
2914 if (n->thunk_p)
2915 continue;
2916 tree decl = n->context;
2917 walk_function (convert_tramp_reference_stmt,
2918 convert_tramp_reference_op, n);
2919 walk_function (convert_gimple_call, NULL, n);
2920 chain_count += DECL_STATIC_CHAIN (decl);
2921 }
2922
2923 FOR_EACH_NEST_INFO (n, root)
2924 if (n->thunk_p)
2925 {
2926 tree decl = n->context;
2927 tree alias = cgraph_node::get (decl)->thunk.alias;
2928 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2929 }
2930 }
2931 while (chain_count != old_chain_count);
2932
2933 if (dump_file && (dump_flags & TDF_DETAILS))
2934 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2935 iter_count);
2936 }
2937
2938 struct nesting_copy_body_data
2939 {
2940 copy_body_data cb;
2941 struct nesting_info *root;
2942 };
2943
2944 /* A helper subroutine for debug_var_chain type remapping. */
2945
2946 static tree
2947 nesting_copy_decl (tree decl, copy_body_data *id)
2948 {
2949 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2950 tree *slot = nid->root->var_map->get (decl);
2951
2952 if (slot)
2953 return (tree) *slot;
2954
2955 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2956 {
2957 tree new_decl = copy_decl_no_change (decl, id);
2958 DECL_ORIGINAL_TYPE (new_decl)
2959 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2960 return new_decl;
2961 }
2962
2963 if (VAR_P (decl)
2964 || TREE_CODE (decl) == PARM_DECL
2965 || TREE_CODE (decl) == RESULT_DECL)
2966 return decl;
2967
2968 return copy_decl_no_change (decl, id);
2969 }
2970
2971 /* A helper function for remap_vla_decls. See if *TP contains
2972 some remapped variables. */
2973
2974 static tree
2975 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2976 {
2977 struct nesting_info *root = (struct nesting_info *) data;
2978 tree t = *tp;
2979
2980 if (DECL_P (t))
2981 {
2982 *walk_subtrees = 0;
2983 tree *slot = root->var_map->get (t);
2984
2985 if (slot)
2986 return *slot;
2987 }
2988 return NULL;
2989 }
2990
2991 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2992 involved. */
2993
2994 static void
2995 remap_vla_decls (tree block, struct nesting_info *root)
2996 {
2997 tree var, subblock, val, type;
2998 struct nesting_copy_body_data id;
2999
3000 for (subblock = BLOCK_SUBBLOCKS (block);
3001 subblock;
3002 subblock = BLOCK_CHAIN (subblock))
3003 remap_vla_decls (subblock, root);
3004
3005 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3006 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3007 {
3008 val = DECL_VALUE_EXPR (var);
3009 type = TREE_TYPE (var);
3010
3011 if (!(TREE_CODE (val) == INDIRECT_REF
3012 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3013 && variably_modified_type_p (type, NULL)))
3014 continue;
3015
3016 if (root->var_map->get (TREE_OPERAND (val, 0))
3017 || walk_tree (&type, contains_remapped_vars, root, NULL))
3018 break;
3019 }
3020
3021 if (var == NULL_TREE)
3022 return;
3023
3024 memset (&id, 0, sizeof (id));
3025 id.cb.copy_decl = nesting_copy_decl;
3026 id.cb.decl_map = new hash_map<tree, tree>;
3027 id.root = root;
3028
3029 for (; var; var = DECL_CHAIN (var))
3030 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3031 {
3032 struct nesting_info *i;
3033 tree newt, context;
3034
3035 val = DECL_VALUE_EXPR (var);
3036 type = TREE_TYPE (var);
3037
3038 if (!(TREE_CODE (val) == INDIRECT_REF
3039 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3040 && variably_modified_type_p (type, NULL)))
3041 continue;
3042
3043 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
3044 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
3045 continue;
3046
3047 context = decl_function_context (var);
3048 for (i = root; i; i = i->outer)
3049 if (i->context == context)
3050 break;
3051
3052 if (i == NULL)
3053 continue;
3054
3055 /* Fully expand value expressions. This avoids having debug variables
3056 only referenced from them and that can be swept during GC. */
3057 if (slot)
3058 {
3059 tree t = (tree) *slot;
3060 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
3061 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
3062 }
3063
3064 id.cb.src_fn = i->context;
3065 id.cb.dst_fn = i->context;
3066 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3067
3068 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3069 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3070 {
3071 newt = TREE_TYPE (newt);
3072 type = TREE_TYPE (type);
3073 }
3074 if (TYPE_NAME (newt)
3075 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3076 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3077 && newt != type
3078 && TYPE_NAME (newt) == TYPE_NAME (type))
3079 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3080
3081 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3082 if (val != DECL_VALUE_EXPR (var))
3083 SET_DECL_VALUE_EXPR (var, val);
3084 }
3085
3086 delete id.cb.decl_map;
3087 }
3088
3089 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3090 involved. */
3091
3092 static void
3093 fixup_vla_decls (tree block)
3094 {
3095 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3096 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3097 {
3098 tree val = DECL_VALUE_EXPR (var);
3099
3100 if (!(TREE_CODE (val) == INDIRECT_REF
3101 && VAR_P (TREE_OPERAND (val, 0))
3102 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3103 continue;
3104
3105 /* Fully expand value expressions. This avoids having debug variables
3106 only referenced from them and that can be swept during GC. */
3107 val = build1 (INDIRECT_REF, TREE_TYPE (val),
3108 DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3109 SET_DECL_VALUE_EXPR (var, val);
3110 }
3111
3112 for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3113 fixup_vla_decls (sub);
3114 }
3115
3116 /* Fold the MEM_REF *E. */
3117 bool
3118 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3119 {
3120 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
3121 *ref_p = fold (*ref_p);
3122 return true;
3123 }
3124
3125 /* Given DECL, a nested function, build an initialization call for FIELD,
3126 the trampoline or descriptor for DECL, using FUNC as the function. */
3127
3128 static gcall *
3129 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3130 tree func)
3131 {
3132 tree arg1, arg2, arg3, x;
3133
3134 gcc_assert (DECL_STATIC_CHAIN (decl));
3135 arg3 = build_addr (info->frame_decl);
3136
3137 arg2 = build_addr (decl);
3138
3139 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3140 info->frame_decl, field, NULL_TREE);
3141 arg1 = build_addr (x);
3142
3143 return gimple_build_call (func, 3, arg1, arg2, arg3);
3144 }
3145
3146 /* Do "everything else" to clean up or complete state collected by the various
3147 walking passes -- create a field to hold the frame base address, lay out the
3148 types and decls, generate code to initialize the frame decl, store critical
3149 expressions in the struct function for rtl to find. */
3150
3151 static void
3152 finalize_nesting_tree_1 (struct nesting_info *root)
3153 {
3154 gimple_seq stmt_list = NULL;
3155 gimple *stmt;
3156 tree context = root->context;
3157 struct function *sf;
3158
3159 if (root->thunk_p)
3160 return;
3161
3162 /* If we created a non-local frame type or decl, we need to lay them
3163 out at this time. */
3164 if (root->frame_type)
3165 {
3166 /* Debugging information needs to compute the frame base address of the
3167 parent frame out of the static chain from the nested frame.
3168
3169 The static chain is the address of the FRAME record, so one could
3170 imagine it would be possible to compute the frame base address just
3171 adding a constant offset to this address. Unfortunately, this is not
3172 possible: if the FRAME object has alignment constraints that are
3173 stronger than the stack, then the offset between the frame base and
3174 the FRAME object will be dynamic.
3175
3176 What we do instead is to append a field to the FRAME object that holds
3177 the frame base address: then debug info just has to fetch this
3178 field. */
3179
3180 /* Debugging information will refer to the CFA as the frame base
3181 address: we will do the same here. */
3182 const tree frame_addr_fndecl
3183 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3184
3185 /* Create a field in the FRAME record to hold the frame base address for
3186 this stack frame. Since it will be used only by the debugger, put it
3187 at the end of the record in order not to shift all other offsets. */
3188 tree fb_decl = make_node (FIELD_DECL);
3189
3190 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3191 TREE_TYPE (fb_decl) = ptr_type_node;
3192 TREE_ADDRESSABLE (fb_decl) = 1;
3193 DECL_CONTEXT (fb_decl) = root->frame_type;
3194 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3195 fb_decl);
3196
3197 /* In some cases the frame type will trigger the -Wpadded warning.
3198 This is not helpful; suppress it. */
3199 int save_warn_padded = warn_padded;
3200 warn_padded = 0;
3201 layout_type (root->frame_type);
3202 warn_padded = save_warn_padded;
3203 layout_decl (root->frame_decl, 0);
3204
3205 /* Initialize the frame base address field. If the builtin we need is
3206 not available, set it to NULL so that debugging information does not
3207 reference junk. */
3208 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3209 root->frame_decl, fb_decl, NULL_TREE);
3210 tree fb_tmp;
3211
3212 if (frame_addr_fndecl != NULL_TREE)
3213 {
3214 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3215 integer_zero_node);
3216 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3217
3218 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3219 }
3220 else
3221 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3222 gimple_seq_add_stmt (&stmt_list,
3223 gimple_build_assign (fb_ref, fb_tmp));
3224
3225 declare_vars (root->frame_decl,
3226 gimple_seq_first_stmt (gimple_body (context)), true);
3227 }
3228
3229 /* If any parameters were referenced non-locally, then we need to insert
3230 a copy or a pointer. */
3231 if (root->any_parm_remapped)
3232 {
3233 tree p;
3234 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3235 {
3236 tree field, x, y;
3237
3238 field = lookup_field_for_decl (root, p, NO_INSERT);
3239 if (!field)
3240 continue;
3241
3242 if (use_pointer_in_frame (p))
3243 x = build_addr (p);
3244 else
3245 x = p;
3246
3247 /* If the assignment is from a non-register the stmt is
3248 not valid gimple. Make it so by using a temporary instead. */
3249 if (!is_gimple_reg (x)
3250 && is_gimple_reg_type (TREE_TYPE (x)))
3251 {
3252 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3253 x = init_tmp_var (root, x, &gsi);
3254 }
3255
3256 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3257 root->frame_decl, field, NULL_TREE);
3258 stmt = gimple_build_assign (y, x);
3259 gimple_seq_add_stmt (&stmt_list, stmt);
3260 }
3261 }
3262
3263 /* If a chain_field was created, then it needs to be initialized
3264 from chain_decl. */
3265 if (root->chain_field)
3266 {
3267 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3268 root->frame_decl, root->chain_field, NULL_TREE);
3269 stmt = gimple_build_assign (x, get_chain_decl (root));
3270 gimple_seq_add_stmt (&stmt_list, stmt);
3271 }
3272
3273 /* If trampolines were created, then we need to initialize them. */
3274 if (root->any_tramp_created)
3275 {
3276 struct nesting_info *i;
3277 for (i = root->inner; i ; i = i->next)
3278 {
3279 tree field, x;
3280
3281 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3282 if (!field)
3283 continue;
3284
3285 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3286 stmt = build_init_call_stmt (root, i->context, field, x);
3287 gimple_seq_add_stmt (&stmt_list, stmt);
3288 }
3289 }
3290
3291 /* If descriptors were created, then we need to initialize them. */
3292 if (root->any_descr_created)
3293 {
3294 struct nesting_info *i;
3295 for (i = root->inner; i ; i = i->next)
3296 {
3297 tree field, x;
3298
3299 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3300 if (!field)
3301 continue;
3302
3303 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3304 stmt = build_init_call_stmt (root, i->context, field, x);
3305 gimple_seq_add_stmt (&stmt_list, stmt);
3306 }
3307 }
3308
3309 /* If we created initialization statements, insert them. */
3310 if (stmt_list)
3311 {
3312 gbind *bind;
3313 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3314 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3315 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3316 gimple_bind_set_body (bind, stmt_list);
3317 }
3318
3319 /* If a chain_decl was created, then it needs to be registered with
3320 struct function so that it gets initialized from the static chain
3321 register at the beginning of the function. */
3322 sf = DECL_STRUCT_FUNCTION (root->context);
3323 sf->static_chain_decl = root->chain_decl;
3324
3325 /* Similarly for the non-local goto save area. */
3326 if (root->nl_goto_field)
3327 {
3328 sf->nonlocal_goto_save_area
3329 = get_frame_field (root, context, root->nl_goto_field, NULL);
3330 sf->has_nonlocal_label = 1;
3331 }
3332
3333 /* Make sure all new local variables get inserted into the
3334 proper BIND_EXPR. */
3335 if (root->new_local_var_chain)
3336 declare_vars (root->new_local_var_chain,
3337 gimple_seq_first_stmt (gimple_body (root->context)),
3338 false);
3339
3340 if (root->debug_var_chain)
3341 {
3342 tree debug_var;
3343 gbind *scope;
3344
3345 remap_vla_decls (DECL_INITIAL (root->context), root);
3346
3347 for (debug_var = root->debug_var_chain; debug_var;
3348 debug_var = DECL_CHAIN (debug_var))
3349 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3350 break;
3351
3352 /* If there are any debug decls with variable length types,
3353 remap those types using other debug_var_chain variables. */
3354 if (debug_var)
3355 {
3356 struct nesting_copy_body_data id;
3357
3358 memset (&id, 0, sizeof (id));
3359 id.cb.copy_decl = nesting_copy_decl;
3360 id.cb.decl_map = new hash_map<tree, tree>;
3361 id.root = root;
3362
3363 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3364 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3365 {
3366 tree type = TREE_TYPE (debug_var);
3367 tree newt, t = type;
3368 struct nesting_info *i;
3369
3370 for (i = root; i; i = i->outer)
3371 if (variably_modified_type_p (type, i->context))
3372 break;
3373
3374 if (i == NULL)
3375 continue;
3376
3377 id.cb.src_fn = i->context;
3378 id.cb.dst_fn = i->context;
3379 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3380
3381 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3382 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3383 {
3384 newt = TREE_TYPE (newt);
3385 t = TREE_TYPE (t);
3386 }
3387 if (TYPE_NAME (newt)
3388 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3389 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3390 && newt != t
3391 && TYPE_NAME (newt) == TYPE_NAME (t))
3392 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3393 }
3394
3395 delete id.cb.decl_map;
3396 }
3397
3398 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3399 if (gimple_bind_block (scope))
3400 declare_vars (root->debug_var_chain, scope, true);
3401 else
3402 BLOCK_VARS (DECL_INITIAL (root->context))
3403 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3404 root->debug_var_chain);
3405 }
3406 else
3407 fixup_vla_decls (DECL_INITIAL (root->context));
3408
3409 /* Fold the rewritten MEM_REF trees. */
3410 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3411
3412 /* Dump the translated tree function. */
3413 if (dump_file)
3414 {
3415 fputs ("\n\n", dump_file);
3416 dump_function_to_file (root->context, dump_file, dump_flags);
3417 }
3418 }
3419
3420 static void
3421 finalize_nesting_tree (struct nesting_info *root)
3422 {
3423 struct nesting_info *n;
3424 FOR_EACH_NEST_INFO (n, root)
3425 finalize_nesting_tree_1 (n);
3426 }
3427
3428 /* Unnest the nodes and pass them to cgraph. */
3429
3430 static void
3431 unnest_nesting_tree_1 (struct nesting_info *root)
3432 {
3433 struct cgraph_node *node = cgraph_node::get (root->context);
3434
3435 /* For nested functions update the cgraph to reflect unnesting.
3436 We also delay finalizing of these functions up to this point. */
3437 if (node->origin)
3438 {
3439 node->unnest ();
3440 if (!root->thunk_p)
3441 cgraph_node::finalize_function (root->context, true);
3442 }
3443 }
3444
3445 static void
3446 unnest_nesting_tree (struct nesting_info *root)
3447 {
3448 struct nesting_info *n;
3449 FOR_EACH_NEST_INFO (n, root)
3450 unnest_nesting_tree_1 (n);
3451 }
3452
3453 /* Free the data structures allocated during this pass. */
3454
3455 static void
3456 free_nesting_tree (struct nesting_info *root)
3457 {
3458 struct nesting_info *node, *next;
3459
3460 node = iter_nestinfo_start (root);
3461 do
3462 {
3463 next = iter_nestinfo_next (node);
3464 delete node->var_map;
3465 delete node->field_map;
3466 delete node->mem_refs;
3467 free (node);
3468 node = next;
3469 }
3470 while (node);
3471 }
3472
3473 /* Gimplify a function and all its nested functions. */
3474 static void
3475 gimplify_all_functions (struct cgraph_node *root)
3476 {
3477 struct cgraph_node *iter;
3478 if (!gimple_body (root->decl))
3479 gimplify_function_tree (root->decl);
3480 for (iter = root->nested; iter; iter = iter->next_nested)
3481 if (!iter->thunk.thunk_p)
3482 gimplify_all_functions (iter);
3483 }
3484
3485 /* Main entry point for this pass. Process FNDECL and all of its nested
3486 subroutines and turn them into something less tightly bound. */
3487
3488 void
3489 lower_nested_functions (tree fndecl)
3490 {
3491 struct cgraph_node *cgn;
3492 struct nesting_info *root;
3493
3494 /* If there are no nested functions, there's nothing to do. */
3495 cgn = cgraph_node::get (fndecl);
3496 if (!cgn->nested)
3497 return;
3498
3499 gimplify_all_functions (cgn);
3500
3501 set_dump_file (dump_begin (TDI_nested, &dump_flags));
3502 if (dump_file)
3503 fprintf (dump_file, "\n;; Function %s\n\n",
3504 lang_hooks.decl_printable_name (fndecl, 2));
3505
3506 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3507 root = create_nesting_tree (cgn);
3508
3509 walk_all_functions (convert_nonlocal_reference_stmt,
3510 convert_nonlocal_reference_op,
3511 root);
3512 walk_all_functions (convert_local_reference_stmt,
3513 convert_local_reference_op,
3514 root);
3515 walk_all_functions (convert_nl_goto_reference, NULL, root);
3516 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3517
3518 convert_all_function_calls (root);
3519 finalize_nesting_tree (root);
3520 unnest_nesting_tree (root);
3521
3522 free_nesting_tree (root);
3523 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3524
3525 if (dump_file)
3526 {
3527 dump_end (TDI_nested, dump_file);
3528 set_dump_file (NULL);
3529 }
3530 }
3531
3532 #include "gt-tree-nested.h"