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