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