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