OSDN Git Service

* tree.h (DECL_BY_REFERENCE): Note that it is also valid for
[pf3gnuchains/gcc-fork.git] / gcc / tree-nested.c
1 /* Nested function decomposition for GIMPLE.
2    Copyright (C) 2004, 2005, 2006, 2007, 2008 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
774 /* A subroutine of convert_nonlocal_reference_op.  Create a local variable
775    in the nested function with DECL_VALUE_EXPR set to reference the true
776    variable in the parent function.  This is used both for debug info 
777    and in OpenMP lowering.  */
778
779 static tree
780 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
781 {
782   tree target_context;
783   struct nesting_info *i;
784   tree x, field, new_decl;
785   void **slot;
786
787   slot = pointer_map_insert (info->var_map, decl);
788
789   if (*slot)
790     return (tree) *slot;
791
792   target_context = decl_function_context (decl);
793
794   /* A copy of the code in get_frame_field, but without the temporaries.  */
795   if (info->context == target_context)
796     {
797       /* Make sure frame_decl gets created.  */
798       (void) get_frame_type (info);
799       x = info->frame_decl;
800       i = info;
801     }
802   else
803     {
804       x = get_chain_decl (info);
805       for (i = info->outer; i->context != target_context; i = i->outer)
806         {
807           field = get_chain_field (i);
808           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
809           x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
810         }
811       x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
812     }
813
814   field = lookup_field_for_decl (i, decl, INSERT);
815   x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
816   if (use_pointer_in_frame (decl))
817     x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
818
819   /* ??? We should be remapping types as well, surely.  */
820   new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
821   DECL_CONTEXT (new_decl) = info->context;
822   DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
823   DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
824   DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
825   TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
826   TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
827   TREE_READONLY (new_decl) = TREE_READONLY (decl);
828   TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
829   DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
830   if ((TREE_CODE (decl) == PARM_DECL
831        || TREE_CODE (decl) == RESULT_DECL
832        || TREE_CODE (decl) == VAR_DECL)
833       && DECL_BY_REFERENCE (decl))
834     DECL_BY_REFERENCE (new_decl) = 1;
835
836   SET_DECL_VALUE_EXPR (new_decl, x);
837   DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
838
839   *slot = new_decl;
840   TREE_CHAIN (new_decl) = info->debug_var_chain;
841   info->debug_var_chain = new_decl;
842
843   return new_decl;
844 }
845
846
847 /* Callback for walk_gimple_stmt, rewrite all references to VAR
848    and PARM_DECLs that belong to outer functions.
849
850    The rewrite will involve some number of structure accesses back up
851    the static chain.  E.g. for a variable FOO up one nesting level it'll
852    be CHAIN->FOO.  For two levels it'll be CHAIN->__chain->FOO.  Further
853    indirections apply to decls for which use_pointer_in_frame is true.  */
854
855 static tree
856 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
857 {
858   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
859   struct nesting_info *const info = (struct nesting_info *) wi->info;
860   tree t = *tp;
861
862   *walk_subtrees = 0;
863   switch (TREE_CODE (t))
864     {
865     case VAR_DECL:
866       /* Non-automatic variables are never processed.  */
867       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
868         break;
869       /* FALLTHRU */
870
871     case PARM_DECL:
872       if (decl_function_context (t) != info->context)
873         {
874           tree x;
875           wi->changed = true;
876
877           x = get_nonlocal_debug_decl (info, t);
878           if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
879             {
880               tree target_context = decl_function_context (t);
881               struct nesting_info *i;
882               for (i = info->outer; i->context != target_context; i = i->outer)
883                 continue;
884               x = lookup_field_for_decl (i, t, INSERT);
885               x = get_frame_field (info, target_context, x, &wi->gsi);
886               if (use_pointer_in_frame (t))
887                 {
888                   x = init_tmp_var (info, x, &wi->gsi);
889                   x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
890                 }
891             }
892
893           if (wi->val_only)
894             {
895               if (wi->is_lhs)
896                 x = save_tmp_var (info, x, &wi->gsi);
897               else
898                 x = init_tmp_var (info, x, &wi->gsi);
899             }
900
901           *tp = x;
902         }
903       break;
904
905     case LABEL_DECL:
906       /* We're taking the address of a label from a parent function, but
907          this is not itself a non-local goto.  Mark the label such that it
908          will not be deleted, much as we would with a label address in
909          static storage.  */
910       if (decl_function_context (t) != info->context)
911         FORCED_LABEL (t) = 1;
912       break;
913
914     case ADDR_EXPR:
915       {
916         bool save_val_only = wi->val_only;
917
918         wi->val_only = false;
919         wi->is_lhs = false;
920         wi->changed = false;
921         walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
922         wi->val_only = true;
923
924         if (wi->changed)
925           {
926             tree save_context;
927
928             /* If we changed anything, we might no longer be directly
929                referencing a decl.  */
930             save_context = current_function_decl;
931             current_function_decl = info->context;
932             recompute_tree_invariant_for_addr_expr (t);
933             current_function_decl = save_context;
934
935             /* If the callback converted the address argument in a context
936                where we only accept variables (and min_invariant, presumably),
937                then compute the address into a temporary.  */
938             if (save_val_only)
939               *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
940                                       t, &wi->gsi);
941           }
942       }
943       break;
944
945     case REALPART_EXPR:
946     case IMAGPART_EXPR:
947     case COMPONENT_REF:
948     case ARRAY_REF:
949     case ARRAY_RANGE_REF:
950     case BIT_FIELD_REF:
951       /* Go down this entire nest and just look at the final prefix and
952          anything that describes the references.  Otherwise, we lose track
953          of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
954       wi->val_only = true;
955       wi->is_lhs = false;
956       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
957         {
958           if (TREE_CODE (t) == COMPONENT_REF)
959             walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
960                        NULL);
961           else if (TREE_CODE (t) == ARRAY_REF
962                    || TREE_CODE (t) == ARRAY_RANGE_REF)
963             {
964               walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
965                          wi, NULL);
966               walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
967                          wi, NULL);
968               walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
969                          wi, NULL);
970             }
971           else if (TREE_CODE (t) == BIT_FIELD_REF)
972             {
973               walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
974                          wi, NULL);
975               walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
976                          wi, NULL);
977             }
978         }
979       wi->val_only = false;
980       walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
981       break;
982
983     case VIEW_CONVERT_EXPR:
984       /* Just request to look at the subtrees, leaving val_only and lhs
985          untouched.  This might actually be for !val_only + lhs, in which
986          case we don't want to force a replacement by a temporary.  */
987       *walk_subtrees = 1;
988       break;
989
990     default:
991       if (!IS_TYPE_OR_DECL_P (t))
992         {
993           *walk_subtrees = 1;
994           wi->val_only = true;
995           wi->is_lhs = false;
996         }
997       break;
998     }
999
1000   return NULL_TREE;
1001 }
1002
1003 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1004                                              struct walk_stmt_info *);
1005
1006 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1007    and PARM_DECLs that belong to outer functions.  */
1008
1009 static bool
1010 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1011 {
1012   struct nesting_info *const info = (struct nesting_info *) wi->info;
1013   bool need_chain = false, need_stmts = false;
1014   tree clause, decl;
1015   int dummy;
1016   bitmap new_suppress;
1017
1018   new_suppress = BITMAP_GGC_ALLOC ();
1019   bitmap_copy (new_suppress, info->suppress_expansion);
1020
1021   for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1022     {
1023       switch (OMP_CLAUSE_CODE (clause))
1024         {
1025         case OMP_CLAUSE_REDUCTION:
1026           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1027             need_stmts = true;
1028           goto do_decl_clause;
1029
1030         case OMP_CLAUSE_LASTPRIVATE:
1031           if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1032             need_stmts = true;
1033           goto do_decl_clause;
1034
1035         case OMP_CLAUSE_PRIVATE:
1036         case OMP_CLAUSE_FIRSTPRIVATE:
1037         case OMP_CLAUSE_COPYPRIVATE:
1038         case OMP_CLAUSE_SHARED:
1039         do_decl_clause:
1040           decl = OMP_CLAUSE_DECL (clause);
1041           if (TREE_CODE (decl) == VAR_DECL
1042               && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1043             break;
1044           if (decl_function_context (decl) != info->context)
1045             {
1046               bitmap_set_bit (new_suppress, DECL_UID (decl));
1047               OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1048               need_chain = true;
1049             }
1050           break;
1051
1052         case OMP_CLAUSE_SCHEDULE:
1053           if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1054             break;
1055           /* FALLTHRU */
1056         case OMP_CLAUSE_IF:
1057         case OMP_CLAUSE_NUM_THREADS:
1058           wi->val_only = true;
1059           wi->is_lhs = false;
1060           convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1061                                          &dummy, wi);
1062           break;
1063
1064         case OMP_CLAUSE_NOWAIT:
1065         case OMP_CLAUSE_ORDERED:
1066         case OMP_CLAUSE_DEFAULT:
1067         case OMP_CLAUSE_COPYIN:
1068         case OMP_CLAUSE_COLLAPSE:
1069         case OMP_CLAUSE_UNTIED:
1070           break;
1071
1072         default:
1073           gcc_unreachable ();
1074         }
1075     }
1076
1077   info->suppress_expansion = new_suppress;
1078
1079   if (need_stmts)
1080     for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1081       switch (OMP_CLAUSE_CODE (clause))
1082         {
1083         case OMP_CLAUSE_REDUCTION:
1084           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1085             {
1086               tree old_context
1087                 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1088               DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1089                 = info->context;
1090               walk_body (convert_nonlocal_reference_stmt,
1091                          convert_nonlocal_reference_op, info,
1092                          OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1093               walk_body (convert_nonlocal_reference_stmt,
1094                          convert_nonlocal_reference_op, info,
1095                          OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1096               DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1097                 = old_context;
1098             }
1099           break;
1100
1101         case OMP_CLAUSE_LASTPRIVATE:
1102           walk_body (convert_nonlocal_reference_stmt,
1103                      convert_nonlocal_reference_op, info,
1104                      OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1105           break;
1106
1107         default:
1108           break;
1109         }
1110
1111   return need_chain;
1112 }
1113
1114
1115 /* Callback for walk_gimple_stmt.  Rewrite all references to VAR and
1116    PARM_DECLs that belong to outer functions.  This handles statements
1117    that are not handled via the standard recursion done in
1118    walk_gimple_stmt.  STMT is the statement to examine, DATA is as in
1119    convert_nonlocal_reference_op.  Set *HANDLED_OPS_P to true if all the
1120    operands of STMT have been handled by this function.  */
1121
1122 static tree
1123 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1124                                  struct walk_stmt_info *wi)
1125 {
1126   struct nesting_info *info = (struct nesting_info *) wi->info;
1127   tree save_local_var_chain;
1128   bitmap save_suppress;
1129   gimple stmt = gsi_stmt (*gsi);
1130
1131   switch (gimple_code (stmt))
1132     {
1133     case GIMPLE_GOTO:
1134       /* Don't walk non-local gotos for now.  */
1135       if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1136         {
1137           wi->val_only = true;
1138           wi->is_lhs = false;
1139           *handled_ops_p = true;
1140           return NULL_TREE;
1141         }
1142       break;
1143
1144     case GIMPLE_OMP_PARALLEL:
1145     case GIMPLE_OMP_TASK:
1146       save_suppress = info->suppress_expansion;
1147       if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1148                                         wi))
1149         {
1150           tree c, decl;
1151           decl = get_chain_decl (info);
1152           c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1153           OMP_CLAUSE_DECL (c) = decl;
1154           OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1155           gimple_omp_taskreg_set_clauses (stmt, c);
1156         }
1157
1158       save_local_var_chain = info->new_local_var_chain;
1159       info->new_local_var_chain = NULL;
1160
1161       walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1162                  info, gimple_omp_body (stmt));
1163
1164       if (info->new_local_var_chain)
1165         declare_vars (info->new_local_var_chain,
1166                       gimple_seq_first_stmt (gimple_omp_body (stmt)),
1167                       false);
1168       info->new_local_var_chain = save_local_var_chain;
1169       info->suppress_expansion = save_suppress;
1170       break;
1171
1172     case GIMPLE_OMP_FOR:
1173       save_suppress = info->suppress_expansion;
1174       convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1175       walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
1176                            convert_nonlocal_reference_op, info);
1177       walk_body (convert_nonlocal_reference_stmt,
1178                  convert_nonlocal_reference_op, info, gimple_omp_body (stmt));
1179       info->suppress_expansion = save_suppress;
1180       break;
1181
1182     case GIMPLE_OMP_SECTIONS:
1183       save_suppress = info->suppress_expansion;
1184       convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1185       walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1186                  info, gimple_omp_body (stmt));
1187       info->suppress_expansion = save_suppress;
1188       break;
1189
1190     case GIMPLE_OMP_SINGLE:
1191       save_suppress = info->suppress_expansion;
1192       convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1193       walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1194                  info, gimple_omp_body (stmt));
1195       info->suppress_expansion = save_suppress;
1196       break;
1197
1198     case GIMPLE_OMP_SECTION:
1199     case GIMPLE_OMP_MASTER:
1200     case GIMPLE_OMP_ORDERED:
1201       walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1202                  info, gimple_omp_body (stmt));
1203       break;
1204
1205     default:
1206       /* For every other statement that we are not interested in
1207          handling here, let the walker traverse the operands.  */
1208       *handled_ops_p = false;
1209       return NULL_TREE;
1210     }
1211
1212   /* We have handled all of STMT operands, no need to traverse the operands.  */
1213   *handled_ops_p = true;
1214   return NULL_TREE;
1215 }
1216
1217
1218 /* A subroutine of convert_local_reference.  Create a local variable
1219    in the parent function with DECL_VALUE_EXPR set to reference the
1220    field in FRAME.  This is used both for debug info and in OpenMP
1221    lowering.  */
1222
1223 static tree
1224 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1225 {
1226   tree x, new_decl;
1227   void **slot;
1228
1229   slot = pointer_map_insert (info->var_map, decl);
1230   if (*slot)
1231     return (tree) *slot;
1232
1233   /* Make sure frame_decl gets created.  */
1234   (void) get_frame_type (info);
1235   x = info->frame_decl;
1236   x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1237
1238   new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1239   DECL_CONTEXT (new_decl) = info->context;
1240   DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1241   DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1242   DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1243   TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1244   TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1245   TREE_READONLY (new_decl) = TREE_READONLY (decl);
1246   TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1247   DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1248   if ((TREE_CODE (decl) == PARM_DECL
1249        || TREE_CODE (decl) == RESULT_DECL
1250        || TREE_CODE (decl) == VAR_DECL)
1251       && DECL_BY_REFERENCE (decl))
1252     DECL_BY_REFERENCE (new_decl) = 1;
1253
1254   SET_DECL_VALUE_EXPR (new_decl, x);
1255   DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1256   *slot = new_decl;
1257
1258   TREE_CHAIN (new_decl) = info->debug_var_chain;
1259   info->debug_var_chain = new_decl;
1260
1261   /* Do not emit debug info twice.  */
1262   DECL_IGNORED_P (decl) = 1;
1263
1264   return new_decl;
1265 }
1266
1267
1268 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1269    and PARM_DECLs that were referenced by inner nested functions.
1270    The rewrite will be a structure reference to the local frame variable.  */
1271
1272 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1273
1274 static tree
1275 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1276 {
1277   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1278   struct nesting_info *const info = (struct nesting_info *) wi->info;
1279   tree t = *tp, field, x;
1280   bool save_val_only;
1281
1282   *walk_subtrees = 0;
1283   switch (TREE_CODE (t))
1284     {
1285     case VAR_DECL:
1286       /* Non-automatic variables are never processed.  */
1287       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1288         break;
1289       /* FALLTHRU */
1290
1291     case PARM_DECL:
1292       if (decl_function_context (t) == info->context)
1293         {
1294           /* If we copied a pointer to the frame, then the original decl
1295              is used unchanged in the parent function.  */
1296           if (use_pointer_in_frame (t))
1297             break;
1298
1299           /* No need to transform anything if no child references the
1300              variable.  */
1301           field = lookup_field_for_decl (info, t, NO_INSERT);
1302           if (!field)
1303             break;
1304           wi->changed = true;
1305
1306           x = get_local_debug_decl (info, t, field);
1307           if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1308             x = get_frame_field (info, info->context, field, &wi->gsi);
1309
1310           if (wi->val_only)
1311             {
1312               if (wi->is_lhs)
1313                 x = save_tmp_var (info, x, &wi->gsi);
1314               else
1315                 x = init_tmp_var (info, x, &wi->gsi);
1316             }
1317
1318           *tp = x;
1319         }
1320       break;
1321
1322     case ADDR_EXPR:
1323       save_val_only = wi->val_only;
1324       wi->val_only = false;
1325       wi->is_lhs = false;
1326       wi->changed = false;
1327       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1328       wi->val_only = save_val_only;
1329
1330       /* If we converted anything ... */
1331       if (wi->changed)
1332         {
1333           tree save_context;
1334
1335           /* Then the frame decl is now addressable.  */
1336           TREE_ADDRESSABLE (info->frame_decl) = 1;
1337             
1338           save_context = current_function_decl;
1339           current_function_decl = info->context;
1340           recompute_tree_invariant_for_addr_expr (t);
1341           current_function_decl = save_context;
1342
1343           /* If we are in a context where we only accept values, then
1344              compute the address into a temporary.  */
1345           if (save_val_only)
1346             *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1347                                     t, &wi->gsi);
1348         }
1349       break;
1350
1351     case REALPART_EXPR:
1352     case IMAGPART_EXPR:
1353     case COMPONENT_REF:
1354     case ARRAY_REF:
1355     case ARRAY_RANGE_REF:
1356     case BIT_FIELD_REF:
1357       /* Go down this entire nest and just look at the final prefix and
1358          anything that describes the references.  Otherwise, we lose track
1359          of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
1360       save_val_only = wi->val_only;
1361       wi->val_only = true;
1362       wi->is_lhs = false;
1363       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1364         {
1365           if (TREE_CODE (t) == COMPONENT_REF)
1366             walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1367                        NULL);
1368           else if (TREE_CODE (t) == ARRAY_REF
1369                    || TREE_CODE (t) == ARRAY_RANGE_REF)
1370             {
1371               walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1372                          NULL);
1373               walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1374                          NULL);
1375               walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1376                          NULL);
1377             }
1378           else if (TREE_CODE (t) == BIT_FIELD_REF)
1379             {
1380               walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1381                          NULL);
1382               walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1383                          NULL);
1384             }
1385         }
1386       wi->val_only = false;
1387       walk_tree (tp, convert_local_reference_op, wi, NULL);
1388       wi->val_only = save_val_only;
1389       break;
1390
1391     case VIEW_CONVERT_EXPR:
1392       /* Just request to look at the subtrees, leaving val_only and lhs
1393          untouched.  This might actually be for !val_only + lhs, in which
1394          case we don't want to force a replacement by a temporary.  */
1395       *walk_subtrees = 1;
1396       break;
1397
1398     default:
1399       if (!IS_TYPE_OR_DECL_P (t))
1400         {
1401           *walk_subtrees = 1;
1402           wi->val_only = true;
1403           wi->is_lhs = false;
1404         }
1405       break;
1406     }
1407
1408   return NULL_TREE;
1409 }
1410
1411 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1412                                           struct walk_stmt_info *);
1413
1414 /* Helper for convert_local_reference.  Convert all the references in
1415    the chain of clauses at *PCLAUSES.  WI is as in convert_local_reference.  */
1416
1417 static bool
1418 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1419 {
1420   struct nesting_info *const info = (struct nesting_info *) wi->info;
1421   bool need_frame = false, need_stmts = false;
1422   tree clause, decl;
1423   int dummy;
1424   bitmap new_suppress;
1425
1426   new_suppress = BITMAP_GGC_ALLOC ();
1427   bitmap_copy (new_suppress, info->suppress_expansion);
1428
1429   for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1430     {
1431       switch (OMP_CLAUSE_CODE (clause))
1432         {
1433         case OMP_CLAUSE_REDUCTION:
1434           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1435             need_stmts = true;
1436           goto do_decl_clause;
1437
1438         case OMP_CLAUSE_LASTPRIVATE:
1439           if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1440             need_stmts = true;
1441           goto do_decl_clause;
1442
1443         case OMP_CLAUSE_PRIVATE:
1444         case OMP_CLAUSE_FIRSTPRIVATE:
1445         case OMP_CLAUSE_COPYPRIVATE:
1446         case OMP_CLAUSE_SHARED:
1447         do_decl_clause:
1448           decl = OMP_CLAUSE_DECL (clause);
1449           if (TREE_CODE (decl) == VAR_DECL
1450               && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1451             break;
1452           if (decl_function_context (decl) == info->context
1453               && !use_pointer_in_frame (decl))
1454             {
1455               tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1456               if (field)
1457                 {
1458                   bitmap_set_bit (new_suppress, DECL_UID (decl));
1459                   OMP_CLAUSE_DECL (clause)
1460                     = get_local_debug_decl (info, decl, field);
1461                   need_frame = true;
1462                 }
1463             }
1464           break;
1465
1466         case OMP_CLAUSE_SCHEDULE:
1467           if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1468             break;
1469           /* FALLTHRU */
1470         case OMP_CLAUSE_IF:
1471         case OMP_CLAUSE_NUM_THREADS:
1472           wi->val_only = true;
1473           wi->is_lhs = false;
1474           convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1475                                       wi);
1476           break;
1477
1478         case OMP_CLAUSE_NOWAIT:
1479         case OMP_CLAUSE_ORDERED:
1480         case OMP_CLAUSE_DEFAULT:
1481         case OMP_CLAUSE_COPYIN:
1482         case OMP_CLAUSE_COLLAPSE:
1483         case OMP_CLAUSE_UNTIED:
1484           break;
1485
1486         default:
1487           gcc_unreachable ();
1488         }
1489     }
1490
1491   info->suppress_expansion = new_suppress;
1492
1493   if (need_stmts)
1494     for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1495       switch (OMP_CLAUSE_CODE (clause))
1496         {
1497         case OMP_CLAUSE_REDUCTION:
1498           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1499             {
1500               tree old_context
1501                 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1502               DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1503                 = info->context;
1504               walk_body (convert_local_reference_stmt,
1505                          convert_local_reference_op, info,
1506                          OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1507               walk_body (convert_local_reference_stmt,
1508                          convert_local_reference_op, info,
1509                          OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1510               DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1511                 = old_context;
1512             }
1513           break;
1514
1515         case OMP_CLAUSE_LASTPRIVATE:
1516           walk_body (convert_local_reference_stmt,
1517                      convert_local_reference_op, info,
1518                      OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1519           break;
1520
1521         default:
1522           break;
1523         }
1524
1525   return need_frame;
1526 }
1527
1528
1529 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1530    and PARM_DECLs that were referenced by inner nested functions.
1531    The rewrite will be a structure reference to the local frame variable.  */
1532
1533 static tree
1534 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1535                               struct walk_stmt_info *wi)
1536 {
1537   struct nesting_info *info = (struct nesting_info *) wi->info;
1538   tree save_local_var_chain;
1539   bitmap save_suppress;
1540   gimple stmt = gsi_stmt (*gsi);
1541
1542   switch (gimple_code (stmt))
1543     {
1544     case GIMPLE_OMP_PARALLEL:
1545     case GIMPLE_OMP_TASK:
1546       save_suppress = info->suppress_expansion;
1547       if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1548                                      wi))
1549         {
1550           tree c;
1551           (void) get_frame_type (info);
1552           c = build_omp_clause (OMP_CLAUSE_SHARED);
1553           OMP_CLAUSE_DECL (c) = info->frame_decl;
1554           OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1555           gimple_omp_taskreg_set_clauses (stmt, c);
1556         }
1557
1558       save_local_var_chain = info->new_local_var_chain;
1559       info->new_local_var_chain = NULL;
1560
1561       walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
1562                  gimple_omp_body (stmt));
1563
1564       if (info->new_local_var_chain)
1565         declare_vars (info->new_local_var_chain,
1566                       gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
1567       info->new_local_var_chain = save_local_var_chain;
1568       info->suppress_expansion = save_suppress;
1569       break;
1570
1571     case GIMPLE_OMP_FOR:
1572       save_suppress = info->suppress_expansion;
1573       convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1574       walk_gimple_omp_for (stmt, convert_local_reference_stmt,
1575                            convert_local_reference_op, info);
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_SECTIONS:
1582       save_suppress = info->suppress_expansion;
1583       convert_local_omp_clauses (gimple_omp_sections_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_SINGLE:
1590       save_suppress = info->suppress_expansion;
1591       convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1592       walk_body (convert_local_reference_stmt, convert_local_reference_op,
1593                  info, gimple_omp_body (stmt));
1594       info->suppress_expansion = save_suppress;
1595       break;
1596
1597     case GIMPLE_OMP_SECTION:
1598     case GIMPLE_OMP_MASTER:
1599     case GIMPLE_OMP_ORDERED:
1600       walk_body (convert_local_reference_stmt, convert_local_reference_op,
1601                  info, gimple_omp_body (stmt));
1602       break;
1603
1604     default:
1605       /* For every other statement that we are not interested in
1606          handling here, let the walker traverse the operands.  */
1607       *handled_ops_p = false;
1608       return NULL_TREE;
1609     }
1610
1611   /* Indicate that we have handled all the operands ourselves.  */
1612   *handled_ops_p = true;
1613   return NULL_TREE;
1614 }
1615
1616
1617 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
1618    that reference labels from outer functions.  The rewrite will be a
1619    call to __builtin_nonlocal_goto.  */
1620
1621 static tree
1622 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1623                            struct walk_stmt_info *wi)
1624 {
1625   struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1626   tree label, new_label, target_context, x, field;
1627   void **slot;
1628   gimple call;
1629   gimple stmt = gsi_stmt (*gsi);
1630
1631   if (gimple_code (stmt) != GIMPLE_GOTO)
1632     {
1633       *handled_ops_p = false;
1634       return NULL_TREE;
1635     }
1636
1637   label = gimple_goto_dest (stmt);
1638   if (TREE_CODE (label) != LABEL_DECL)
1639     {
1640       *handled_ops_p = false;
1641       return NULL_TREE;
1642     }
1643
1644   target_context = decl_function_context (label);
1645   if (target_context == info->context)
1646     {
1647       *handled_ops_p = false;
1648       return NULL_TREE;
1649     }
1650
1651   for (i = info->outer; target_context != i->context; i = i->outer)
1652     continue;
1653
1654   /* The original user label may also be use for a normal goto, therefore
1655      we must create a new label that will actually receive the abnormal
1656      control transfer.  This new label will be marked LABEL_NONLOCAL; this
1657      mark will trigger proper behavior in the cfg, as well as cause the
1658      (hairy target-specific) non-local goto receiver code to be generated
1659      when we expand rtl.  Enter this association into var_map so that we
1660      can insert the new label into the IL during a second pass.  */
1661   slot = pointer_map_insert (i->var_map, label);
1662   if (*slot == NULL)
1663     {
1664       new_label = create_artificial_label ();
1665       DECL_NONLOCAL (new_label) = 1;
1666       *slot = new_label;
1667     }
1668   else
1669     new_label = (tree) *slot;
1670   
1671   /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field).  */
1672   field = get_nl_goto_field (i);
1673   x = get_frame_field (info, target_context, field, &wi->gsi);
1674   x = build_addr (x, target_context);
1675   x = gsi_gimplify_val (info, x, &wi->gsi);
1676   call = gimple_build_call (implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO], 2,
1677                             build_addr (new_label, target_context), x);
1678   gsi_replace (&wi->gsi, call, false);
1679
1680   /* We have handled all of STMT's operands, no need to keep going.  */
1681   *handled_ops_p = true;
1682   return NULL_TREE;
1683 }
1684
1685
1686 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
1687    are referenced via nonlocal goto from a nested function.  The rewrite
1688    will involve installing a newly generated DECL_NONLOCAL label, and
1689    (potentially) a branch around the rtl gunk that is assumed to be
1690    attached to such a label.  */
1691
1692 static tree
1693 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1694                           struct walk_stmt_info *wi)
1695 {
1696   struct nesting_info *const info = (struct nesting_info *) wi->info;
1697   tree label, new_label;
1698   gimple_stmt_iterator tmp_gsi;
1699   void **slot;
1700   gimple stmt = gsi_stmt (*gsi);
1701
1702   if (gimple_code (stmt) != GIMPLE_LABEL)
1703     {
1704       *handled_ops_p = false;
1705       return NULL_TREE;
1706     }
1707
1708   label = gimple_label_label (stmt);
1709
1710   slot = pointer_map_contains (info->var_map, label);
1711   if (!slot)
1712     {
1713       *handled_ops_p = false;
1714       return NULL_TREE;
1715     }
1716
1717   /* If there's any possibility that the previous statement falls through,
1718      then we must branch around the new non-local label.  */
1719   tmp_gsi = wi->gsi;
1720   gsi_prev (&tmp_gsi);
1721   if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
1722     {
1723       gimple stmt = gimple_build_goto (label);
1724       gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1725     }
1726
1727   new_label = (tree) *slot;
1728   stmt = gimple_build_label (new_label);
1729   gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1730
1731   *handled_ops_p = true;
1732   return NULL_TREE;
1733 }
1734
1735
1736 /* Called via walk_function+walk_stmt, rewrite all references to addresses
1737    of nested functions that require the use of trampolines.  The rewrite
1738    will involve a reference a trampoline generated for the occasion.  */
1739
1740 static tree
1741 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
1742 {
1743   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1744   struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
1745   tree t = *tp, decl, target_context, x, builtin;
1746   gimple call;
1747
1748   *walk_subtrees = 0;
1749   switch (TREE_CODE (t))
1750     {
1751     case ADDR_EXPR:
1752       /* Build
1753            T.1 = &CHAIN->tramp;
1754            T.2 = __builtin_adjust_trampoline (T.1);
1755            T.3 = (func_type)T.2;
1756       */
1757
1758       decl = TREE_OPERAND (t, 0);
1759       if (TREE_CODE (decl) != FUNCTION_DECL)
1760         break;
1761
1762       /* Only need to process nested functions.  */
1763       target_context = decl_function_context (decl);
1764       if (!target_context)
1765         break;
1766
1767       /* If the nested function doesn't use a static chain, then
1768          it doesn't need a trampoline.  */
1769       if (DECL_NO_STATIC_CHAIN (decl))
1770         break;
1771
1772       /* If we don't want a trampoline, then don't build one.  */
1773       if (TREE_NO_TRAMPOLINE (t))
1774         break;
1775
1776       /* Lookup the immediate parent of the callee, as that's where
1777          we need to insert the trampoline.  */
1778       for (i = info; i->context != target_context; i = i->outer)
1779         continue;
1780       x = lookup_tramp_for_decl (i, decl, INSERT);
1781
1782       /* Compute the address of the field holding the trampoline.  */
1783       x = get_frame_field (info, target_context, x, &wi->gsi);
1784       x = build_addr (x, target_context);
1785       x = gsi_gimplify_val (info, x, &wi->gsi);
1786
1787       /* Do machine-specific ugliness.  Normally this will involve
1788          computing extra alignment, but it can really be anything.  */
1789       builtin = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1790       call = gimple_build_call (builtin, 1, x);
1791       x = init_tmp_var_with_call (info, &wi->gsi, call);
1792
1793       /* Cast back to the proper function type.  */
1794       x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1795       x = init_tmp_var (info, x, &wi->gsi);
1796
1797       *tp = x;
1798       break;
1799
1800     default:
1801       if (!IS_TYPE_OR_DECL_P (t))
1802         *walk_subtrees = 1;
1803       break;
1804     }
1805
1806   return NULL_TREE;
1807 }
1808
1809
1810 /* Called via walk_function+walk_gimple_stmt, rewrite all references
1811    to addresses of nested functions that require the use of
1812    trampolines.  The rewrite will involve a reference a trampoline
1813    generated for the occasion.  */
1814
1815 static tree
1816 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1817                               struct walk_stmt_info *wi)
1818 {
1819   gimple stmt = gsi_stmt (*gsi);
1820
1821   switch (gimple_code (stmt))
1822     {
1823     case GIMPLE_CALL:
1824       {
1825         /* Only walk call arguments, lest we generate trampolines for
1826            direct calls.  */
1827         unsigned long i, nargs = gimple_call_num_args (stmt);
1828         for (i = 0; i < nargs; i++)
1829           walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
1830                      wi, NULL);
1831
1832         *handled_ops_p = true;
1833         return NULL_TREE;
1834       }
1835
1836     default:
1837       break;
1838     }
1839
1840   *handled_ops_p = false;
1841   return NULL_TREE;
1842 }
1843
1844
1845
1846 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
1847    that reference nested functions to make sure that the static chain
1848    is set up properly for the call.  */
1849
1850 static tree
1851 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1852                      struct walk_stmt_info *wi)
1853 {
1854   struct nesting_info *const info = (struct nesting_info *) wi->info;
1855   tree decl, target_context;
1856   char save_static_chain_added;
1857   int i;
1858   gimple stmt = gsi_stmt (*gsi);
1859
1860   switch (gimple_code (stmt))
1861     {
1862     case GIMPLE_CALL:
1863       decl = gimple_call_fndecl (stmt);
1864       if (!decl)
1865         break;
1866       target_context = decl_function_context (decl);
1867       if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1868         {
1869           gimple_call_set_chain (stmt, get_static_chain (info, target_context,
1870                                                          &wi->gsi));
1871           info->static_chain_added |= (1 << (info->context != target_context));
1872         }
1873       break;
1874
1875     case GIMPLE_OMP_PARALLEL:
1876     case GIMPLE_OMP_TASK:
1877       save_static_chain_added = info->static_chain_added;
1878       info->static_chain_added = 0;
1879       walk_body (convert_gimple_call, NULL, info, gimple_omp_body (stmt));
1880       for (i = 0; i < 2; i++)
1881         {
1882           tree c, decl;
1883           if ((info->static_chain_added & (1 << i)) == 0)
1884             continue;
1885           decl = i ? get_chain_decl (info) : info->frame_decl;
1886           /* Don't add CHAIN.* or FRAME.* twice.  */
1887           for (c = gimple_omp_taskreg_clauses (stmt);
1888                c;
1889                c = OMP_CLAUSE_CHAIN (c))
1890             if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1891                  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1892                 && OMP_CLAUSE_DECL (c) == decl)
1893               break;
1894           if (c == NULL)
1895             {
1896               c = build_omp_clause (i ? OMP_CLAUSE_FIRSTPRIVATE
1897                                       : OMP_CLAUSE_SHARED);
1898               OMP_CLAUSE_DECL (c) = decl;
1899               OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1900               gimple_omp_taskreg_set_clauses (stmt, c);
1901             }
1902         }
1903       info->static_chain_added |= save_static_chain_added;
1904       break;
1905
1906     case GIMPLE_OMP_FOR:
1907       walk_body (convert_gimple_call, NULL, info,
1908                  gimple_omp_for_pre_body (stmt));
1909       /* FALLTHRU */
1910     case GIMPLE_OMP_SECTIONS:
1911     case GIMPLE_OMP_SECTION:
1912     case GIMPLE_OMP_SINGLE:
1913     case GIMPLE_OMP_MASTER:
1914     case GIMPLE_OMP_ORDERED:
1915     case GIMPLE_OMP_CRITICAL:
1916       walk_body (convert_gimple_call, NULL, info, gimple_omp_body (stmt));
1917       break;
1918
1919     default:
1920       /* Keep looking for other operands.  */
1921       *handled_ops_p = false;
1922       return NULL_TREE;
1923     }
1924
1925   *handled_ops_p = true;
1926   return NULL_TREE;
1927 }
1928
1929
1930 /* Walk the nesting tree starting with ROOT, depth first.  Convert all
1931    trampolines and call expressions.  On the way back up, determine if
1932    a nested function actually uses its static chain; if not, remember that.  */
1933
1934 static void
1935 convert_all_function_calls (struct nesting_info *root)
1936 {
1937   do
1938     {
1939       if (root->inner)
1940         convert_all_function_calls (root->inner);
1941
1942       walk_function (convert_tramp_reference_stmt, convert_tramp_reference_op,
1943                      root);
1944       walk_function (convert_gimple_call, NULL, root);
1945
1946       /* If the function does not use a static chain, then remember that.  */
1947       if (root->outer && !root->chain_decl && !root->chain_field)
1948         DECL_NO_STATIC_CHAIN (root->context) = 1;
1949       else
1950         gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1951
1952       root = root->next;
1953     }
1954   while (root);
1955 }
1956
1957 struct nesting_copy_body_data
1958 {
1959   copy_body_data cb;
1960   struct nesting_info *root;
1961 };
1962
1963 /* A helper subroutine for debug_var_chain type remapping.  */
1964
1965 static tree
1966 nesting_copy_decl (tree decl, copy_body_data *id)
1967 {
1968   struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
1969   void **slot = pointer_map_contains (nid->root->var_map, decl);
1970
1971   if (slot)
1972     return (tree) *slot;
1973
1974   if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
1975     {
1976       tree new_decl = copy_decl_no_change (decl, id);
1977       DECL_ORIGINAL_TYPE (new_decl)
1978         = remap_type (DECL_ORIGINAL_TYPE (decl), id);
1979       return new_decl;
1980     }
1981
1982   return copy_decl_no_change (decl, id);
1983 }
1984
1985 /* Do "everything else" to clean up or complete state collected by the
1986    various walking passes -- lay out the types and decls, generate code
1987    to initialize the frame decl, store critical expressions in the
1988    struct function for rtl to find.  */
1989
1990 static void
1991 finalize_nesting_tree_1 (struct nesting_info *root)
1992 {
1993   gimple_seq stmt_list;
1994   gimple stmt;
1995   tree context = root->context;
1996   struct function *sf;
1997
1998   stmt_list = NULL;
1999
2000   /* If we created a non-local frame type or decl, we need to lay them
2001      out at this time.  */
2002   if (root->frame_type)
2003     {
2004       /* In some cases the frame type will trigger the -Wpadded warning.
2005          This is not helpful; suppress it. */
2006       int save_warn_padded = warn_padded;
2007       warn_padded = 0;
2008       layout_type (root->frame_type);
2009       warn_padded = save_warn_padded;
2010       layout_decl (root->frame_decl, 0);
2011     }
2012
2013   /* If any parameters were referenced non-locally, then we need to 
2014      insert a copy.  Likewise, if any variables were referenced by
2015      pointer, we need to initialize the address.  */
2016   if (root->any_parm_remapped)
2017     {
2018       tree p;
2019       for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
2020         {
2021           tree field, x, y;
2022
2023           field = lookup_field_for_decl (root, p, NO_INSERT);
2024           if (!field)
2025             continue;
2026
2027           if (use_pointer_in_frame (p))
2028             x = build_addr (p, context);
2029           else
2030             x = p;
2031
2032           y = build3 (COMPONENT_REF, TREE_TYPE (field),
2033                       root->frame_decl, field, NULL_TREE);
2034           stmt = gimple_build_assign (y, x);
2035           gimple_seq_add_stmt (&stmt_list, stmt);
2036           /* If the assignment is from a non-register the stmt is
2037              not valid gimple.  Make it so by using a temporary instead.  */
2038           if (!is_gimple_reg (x)
2039               && is_gimple_reg_type (TREE_TYPE (x)))
2040             {
2041               gimple_stmt_iterator gsi = gsi_last (stmt_list);
2042               x = init_tmp_var (root, x, &gsi);
2043               gimple_assign_set_rhs1 (stmt, x);
2044             }
2045         }
2046     }
2047
2048   /* If a chain_field was created, then it needs to be initialized
2049      from chain_decl.  */
2050   if (root->chain_field)
2051     {
2052       tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2053                        root->frame_decl, root->chain_field, NULL_TREE);
2054       stmt = gimple_build_assign (x, get_chain_decl (root));
2055       gimple_seq_add_stmt (&stmt_list, stmt);
2056     }
2057
2058   /* If trampolines were created, then we need to initialize them.  */
2059   if (root->any_tramp_created)
2060     {
2061       struct nesting_info *i;
2062       for (i = root->inner; i ; i = i->next)
2063         {
2064           tree arg1, arg2, arg3, x, field;
2065
2066           field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2067           if (!field)
2068             continue;
2069
2070           if (DECL_NO_STATIC_CHAIN (i->context))
2071             arg3 = null_pointer_node;
2072           else
2073             arg3 = build_addr (root->frame_decl, context);
2074
2075           arg2 = build_addr (i->context, context);
2076
2077           x = build3 (COMPONENT_REF, TREE_TYPE (field),
2078                       root->frame_decl, field, NULL_TREE);
2079           arg1 = build_addr (x, context);
2080
2081           x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
2082           stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2083           gimple_seq_add_stmt (&stmt_list, stmt);
2084         }
2085     }
2086
2087   /* If we created initialization statements, insert them.  */
2088   if (stmt_list)
2089     {
2090       gimple bind;
2091       annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2092       bind = gimple_seq_first_stmt (gimple_body (context));
2093       gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2094       gimple_bind_set_body (bind, stmt_list);
2095     }
2096
2097   /* If a chain_decl was created, then it needs to be registered with
2098      struct function so that it gets initialized from the static chain
2099      register at the beginning of the function.  */
2100   sf = DECL_STRUCT_FUNCTION (root->context);
2101   sf->static_chain_decl = root->chain_decl;
2102
2103   /* Similarly for the non-local goto save area.  */
2104   if (root->nl_goto_field)
2105     {
2106       sf->nonlocal_goto_save_area
2107         = get_frame_field (root, context, root->nl_goto_field, NULL);
2108       sf->has_nonlocal_label = 1;
2109     }
2110
2111   /* Make sure all new local variables get inserted into the
2112      proper BIND_EXPR.  */
2113   if (root->new_local_var_chain)
2114     declare_vars (root->new_local_var_chain,
2115                   gimple_seq_first_stmt (gimple_body (root->context)),
2116                   false);
2117
2118   if (root->debug_var_chain)
2119     {
2120       tree debug_var;
2121
2122       for (debug_var = root->debug_var_chain; debug_var;
2123            debug_var = TREE_CHAIN (debug_var))
2124         if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2125           break;
2126
2127       /* If there are any debug decls with variable length types,
2128          remap those types using other debug_var_chain variables.  */
2129       if (debug_var)
2130         {
2131           struct nesting_copy_body_data id;
2132
2133           memset (&id, 0, sizeof (id));
2134           id.cb.copy_decl = nesting_copy_decl;
2135           id.cb.decl_map = pointer_map_create ();
2136           id.root = root;
2137
2138           for (; debug_var; debug_var = TREE_CHAIN (debug_var))
2139             if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
2140               {
2141                 tree type = TREE_TYPE (debug_var);
2142                 tree newt, t = type;
2143                 struct nesting_info *i;
2144
2145                 for (i = root; i; i = i->outer)
2146                   if (variably_modified_type_p (type, i->context))
2147                     break;
2148
2149                 if (i == NULL)
2150                   continue;
2151
2152                 id.cb.src_fn = i->context;
2153                 id.cb.dst_fn = i->context;
2154                 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2155
2156                 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
2157                 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2158                   {
2159                     newt = TREE_TYPE (newt);
2160                     t = TREE_TYPE (t);
2161                   }
2162                 if (TYPE_NAME (newt)
2163                     && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2164                     && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2165                     && newt != t
2166                     && TYPE_NAME (newt) == TYPE_NAME (t))
2167                   TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2168               }
2169
2170           pointer_map_destroy (id.cb.decl_map);
2171         }
2172
2173       declare_vars (root->debug_var_chain,
2174                     gimple_seq_first_stmt (gimple_body (root->context)),
2175                     true);
2176     }
2177
2178   /* Dump the translated tree function.  */
2179   dump_function (TDI_nested, root->context);
2180 }
2181
2182 static void
2183 finalize_nesting_tree (struct nesting_info *root)
2184 {
2185   do
2186     {
2187       if (root->inner)
2188         finalize_nesting_tree (root->inner);
2189       finalize_nesting_tree_1 (root);
2190       root = root->next;
2191     }
2192   while (root);
2193 }
2194
2195 /* Unnest the nodes and pass them to cgraph.  */
2196
2197 static void
2198 unnest_nesting_tree_1 (struct nesting_info *root)
2199 {
2200   struct cgraph_node *node = cgraph_node (root->context);
2201
2202   /* For nested functions update the cgraph to reflect unnesting.
2203      We also delay finalizing of these functions up to this point.  */
2204   if (node->origin)
2205     {
2206        cgraph_unnest_node (cgraph_node (root->context));
2207        cgraph_finalize_function (root->context, true);
2208     }
2209 }
2210
2211 static void
2212 unnest_nesting_tree (struct nesting_info *root)
2213 {
2214   do
2215     {
2216       if (root->inner)
2217         unnest_nesting_tree (root->inner);
2218       unnest_nesting_tree_1 (root);
2219       root = root->next;
2220     }
2221   while (root);
2222 }
2223
2224 /* Free the data structures allocated during this pass.  */
2225
2226 static void
2227 free_nesting_tree (struct nesting_info *root)
2228 {
2229   struct nesting_info *next;
2230   do
2231     {
2232       if (root->inner)
2233         free_nesting_tree (root->inner);
2234       pointer_map_destroy (root->var_map);
2235       pointer_map_destroy (root->field_map);
2236       next = root->next;
2237       free (root);
2238       root = next;
2239     }
2240   while (root);
2241 }
2242
2243 /* Main entry point for this pass.  Process FNDECL and all of its nested
2244    subroutines and turn them into something less tightly bound.  */
2245
2246 void
2247 lower_nested_functions (tree fndecl)
2248 {
2249   struct cgraph_node *cgn;
2250   struct nesting_info *root;
2251
2252   /* If there are no nested functions, there's nothing to do.  */
2253   cgn = cgraph_node (fndecl);
2254   if (!cgn->nested)
2255     return;
2256
2257   bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2258   root = create_nesting_tree (cgn);
2259   walk_all_functions (convert_nonlocal_reference_stmt,
2260                       convert_nonlocal_reference_op,
2261                       root);
2262   walk_all_functions (convert_local_reference_stmt,
2263                       convert_local_reference_op,
2264                       root);
2265   walk_all_functions (convert_nl_goto_reference, NULL, root);
2266   walk_all_functions (convert_nl_goto_receiver, NULL, root);
2267   convert_all_function_calls (root);
2268   finalize_nesting_tree (root);
2269   unnest_nesting_tree (root);
2270   free_nesting_tree (root);
2271   bitmap_obstack_release (&nesting_info_bitmap_obstack);
2272 }
2273
2274 #include "gt-tree-nested.h"