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