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