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