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