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