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