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