OSDN Git Service

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