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