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