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