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