OSDN Git Service

* check.c (same_type_check): Typo fix in comment.
[pf3gnuchains/gcc-fork.git] / gcc / tree-nested.c
1 /* Nested function decomposition for trees.
2    Copyright (C) 2004, 2005, 2006 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 2, 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 COPYING.  If not, write to
18    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
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 "tree-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 "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 var_map_elt GTY(())
81 {
82   tree old;
83   tree new;
84 };
85
86 struct nesting_info GTY ((chain_next ("%h.next")))
87 {
88   struct nesting_info *outer;
89   struct nesting_info *inner;
90   struct nesting_info *next;
91   
92   htab_t GTY ((param_is (struct var_map_elt))) field_map;
93   htab_t GTY ((param_is (struct var_map_elt))) var_map;
94   bitmap suppress_expansion;
95
96   tree context;
97   tree new_local_var_chain;
98   tree debug_var_chain;
99   tree frame_type;
100   tree frame_decl;
101   tree chain_field;
102   tree chain_decl;
103   tree nl_goto_field;
104
105   bool any_parm_remapped;
106   bool any_tramp_created;
107   char static_chain_added;
108 };
109
110
111 /* Hashing and equality functions for nesting_info->var_map.  */
112
113 static hashval_t
114 var_map_hash (const void *x)
115 {
116   const struct var_map_elt *a = (const struct var_map_elt *) x;
117   return htab_hash_pointer (a->old);
118 }
119
120 static int
121 var_map_eq (const void *x, const void *y)
122 {
123   const struct var_map_elt *a = (const struct var_map_elt *) x;
124   const struct var_map_elt *b = (const struct var_map_elt *) y;
125   return a->old == b->old;
126 }
127
128 /* We're working in so many different function contexts simultaneously,
129    that create_tmp_var is dangerous.  Prevent mishap.  */
130 #define create_tmp_var cant_use_create_tmp_var_here_dummy
131
132 /* Like create_tmp_var, except record the variable for registration at
133    the given nesting level.  */
134
135 static tree
136 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
137 {
138   tree tmp_var;
139
140   /* If the type is of variable size or a type which must be created by the
141      frontend, something is wrong.  Note that we explicitly allow
142      incomplete types here, since we create them ourselves here.  */
143   gcc_assert (!TREE_ADDRESSABLE (type));
144   gcc_assert (!TYPE_SIZE_UNIT (type)
145               || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
146
147   tmp_var = create_tmp_var_raw (type, prefix);
148   DECL_CONTEXT (tmp_var) = info->context;
149   TREE_CHAIN (tmp_var) = info->new_local_var_chain;
150   DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
151   if (TREE_CODE (type) == COMPLEX_TYPE)
152     DECL_COMPLEX_GIMPLE_REG_P (tmp_var) = 1;
153
154   info->new_local_var_chain = tmp_var;
155
156   return tmp_var;
157 }
158
159 /* Take the address of EXP to be used within function CONTEXT.
160    Mark it for addressability as necessary.  */
161
162 tree
163 build_addr (tree exp, tree context)
164 {
165   tree base = exp;
166   tree save_context;
167   tree retval;
168
169   while (handled_component_p (base))
170     base = TREE_OPERAND (base, 0);
171
172   if (DECL_P (base))
173     TREE_ADDRESSABLE (base) = 1;
174
175   /* Building the ADDR_EXPR will compute a set of properties for
176      that ADDR_EXPR.  Those properties are unfortunately context
177      specific.  ie, they are dependent on CURRENT_FUNCTION_DECL.
178
179      Temporarily set CURRENT_FUNCTION_DECL to the desired context,
180      build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL.  That
181      way the properties are for the ADDR_EXPR are computed properly.  */
182   save_context = current_function_decl;
183   current_function_decl = context;
184   retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
185   current_function_decl = save_context;;
186   return retval;
187 }
188
189 /* Insert FIELD into TYPE, sorted by alignment requirements.  */
190
191 void
192 insert_field_into_struct (tree type, tree field)
193 {
194   tree *p;
195
196   DECL_CONTEXT (field) = type;
197
198   for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
199     if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
200       break;
201
202   TREE_CHAIN (field) = *p;
203   *p = field;
204 }
205
206 /* Build or return the RECORD_TYPE that describes the frame state that is
207    shared between INFO->CONTEXT and its nested functions.  This record will
208    not be complete until finalize_nesting_tree; up until that point we'll
209    be adding fields as necessary.
210
211    We also build the DECL that represents this frame in the function.  */
212
213 static tree
214 get_frame_type (struct nesting_info *info)
215 {
216   tree type = info->frame_type;
217   if (!type)
218     {
219       char *name;
220
221       type = make_node (RECORD_TYPE);
222
223       name = concat ("FRAME.",
224                      IDENTIFIER_POINTER (DECL_NAME (info->context)),
225                      NULL);
226       TYPE_NAME (type) = get_identifier (name);
227       free (name);
228
229       info->frame_type = type;
230       info->frame_decl = create_tmp_var_for (info, type, "FRAME");
231
232       /* ??? Always make it addressable for now, since it is meant to
233          be pointed to by the static chain pointer.  This pessimizes
234          when it turns out that no static chains are needed because
235          the nested functions referencing non-local variables are not
236          reachable, but the true pessimization is to create the non-
237          local frame structure in the first place.  */
238       TREE_ADDRESSABLE (info->frame_decl) = 1;
239     }
240   return type;
241 }
242
243 /* Return true if DECL should be referenced by pointer in the non-local
244    frame structure.  */
245
246 static bool
247 use_pointer_in_frame (tree decl)
248 {
249   if (TREE_CODE (decl) == PARM_DECL)
250     {
251       /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
252          sized decls, and inefficient to copy large aggregates.  Don't bother
253          moving anything but scalar variables.  */
254       return AGGREGATE_TYPE_P (TREE_TYPE (decl));
255     }
256   else
257     {
258       /* Variable sized types make things "interesting" in the frame.  */
259       return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
260     }
261 }
262
263 /* Given DECL, a non-locally accessed variable, find or create a field
264    in the non-local frame structure for the given nesting context.  */
265
266 static tree
267 lookup_field_for_decl (struct nesting_info *info, tree decl,
268                        enum insert_option insert)
269 {
270   struct var_map_elt *elt, dummy;
271   void **slot;
272   tree field;
273
274   dummy.old = decl;
275   slot = htab_find_slot (info->field_map, &dummy, insert);
276   if (!slot)
277     {
278       gcc_assert (insert != INSERT);
279       return NULL;
280     }
281   elt = (struct var_map_elt *) *slot;
282
283   if (!elt && insert == INSERT)
284     {
285       field = make_node (FIELD_DECL);
286       DECL_NAME (field) = DECL_NAME (decl);
287
288       if (use_pointer_in_frame (decl))
289         {
290           TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
291           DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
292           DECL_NONADDRESSABLE_P (field) = 1;
293         }
294       else
295         {
296           TREE_TYPE (field) = TREE_TYPE (decl);
297           DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
298           DECL_ALIGN (field) = DECL_ALIGN (decl);
299           DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
300           TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
301           DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
302           TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
303         }
304
305       insert_field_into_struct (get_frame_type (info), field);
306   
307       elt = GGC_NEW (struct var_map_elt);
308       elt->old = decl;
309       elt->new = field;
310       *slot = elt;
311
312       if (TREE_CODE (decl) == PARM_DECL)
313         info->any_parm_remapped = true;
314     }
315   else
316     field = elt ? elt->new : NULL;
317
318   return field;
319 }
320
321 /* Build or return the variable that holds the static chain within
322    INFO->CONTEXT.  This variable may only be used within INFO->CONTEXT.  */
323
324 static tree
325 get_chain_decl (struct nesting_info *info)
326 {
327   tree decl = info->chain_decl;
328   if (!decl)
329     {
330       tree type;
331
332       type = get_frame_type (info->outer);
333       type = build_pointer_type (type);
334
335       /* Note that this variable is *not* entered into any BIND_EXPR;
336          the construction of this variable is handled specially in
337          expand_function_start and initialize_inlined_parameters.
338          Note also that it's represented as a parameter.  This is more
339          close to the truth, since the initial value does come from 
340          the caller.  */
341       decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
342       DECL_ARTIFICIAL (decl) = 1;
343       DECL_IGNORED_P (decl) = 1;
344       TREE_USED (decl) = 1;
345       DECL_CONTEXT (decl) = info->context;
346       DECL_ARG_TYPE (decl) = type;
347
348       /* Tell tree-inline.c that we never write to this variable, so
349          it can copy-prop the replacement value immediately.  */
350       TREE_READONLY (decl) = 1;
351
352       info->chain_decl = decl;
353     }
354   return decl;
355 }
356
357 /* Build or return the field within the non-local frame state that holds
358    the static chain for INFO->CONTEXT.  This is the way to walk back up
359    multiple nesting levels.  */
360
361 static tree
362 get_chain_field (struct nesting_info *info)
363 {
364   tree field = info->chain_field;
365   if (!field)
366     {
367       tree type = build_pointer_type (get_frame_type (info->outer));
368
369       field = make_node (FIELD_DECL);
370       DECL_NAME (field) = get_identifier ("__chain");
371       TREE_TYPE (field) = type;
372       DECL_ALIGN (field) = TYPE_ALIGN (type);
373       DECL_NONADDRESSABLE_P (field) = 1;
374
375       insert_field_into_struct (get_frame_type (info), field);
376
377       info->chain_field = field;
378     }
379   return field;
380 }
381
382 /* Copy EXP into a temporary.  Allocate the temporary in the context of
383    INFO and insert the initialization statement before TSI.  */
384
385 static tree
386 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
387 {
388   tree t, stmt;
389
390   t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
391   stmt = build2 (MODIFY_EXPR, TREE_TYPE (t), t, exp);
392   SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
393   tsi_link_before (tsi, stmt, TSI_SAME_STMT);
394
395   return t;
396 }
397
398 /* Similarly, but only do so to force EXP to satisfy is_gimple_val.  */
399
400 static tree
401 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
402 {
403   if (is_gimple_val (exp))
404     return exp;
405   else
406     return init_tmp_var (info, exp, tsi);
407 }
408
409 /* Similarly, but copy from the temporary and insert the statement
410    after the iterator.  */
411
412 static tree
413 save_tmp_var (struct nesting_info *info, tree exp,
414               tree_stmt_iterator *tsi)
415 {
416   tree t, stmt;
417
418   t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
419   stmt = build2 (MODIFY_EXPR, TREE_TYPE (t), exp, t);
420   SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
421   tsi_link_after (tsi, stmt, TSI_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   tree record, t;
434   unsigned align, size;
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   record = make_node (RECORD_TYPE);
457   TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
458   TYPE_FIELDS (record) = t;
459   layout_type (record);
460
461   return record;
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   struct var_map_elt *elt, dummy;
472   void **slot;
473   tree field;
474
475   dummy.old = decl;
476   slot = htab_find_slot (info->var_map, &dummy, insert);
477   if (!slot)
478     {
479       gcc_assert (insert != INSERT);
480       return NULL;
481     }
482   elt = (struct var_map_elt *) *slot;
483
484   if (!elt && insert == INSERT)
485     {
486       field = make_node (FIELD_DECL);
487       DECL_NAME (field) = DECL_NAME (decl);
488       TREE_TYPE (field) = get_trampoline_type ();
489       TREE_ADDRESSABLE (field) = 1;
490
491       insert_field_into_struct (get_frame_type (info), field);
492
493       elt = GGC_NEW (struct var_map_elt);
494       elt->old = decl;
495       elt->new = field;
496       *slot = elt;
497
498       info->any_tramp_created = true;
499     }
500   else
501     field = elt ? elt->new : NULL;
502
503   return field;
504
505
506 /* Build or return the field within the non-local frame state that holds
507    the non-local goto "jmp_buf".  The buffer itself is maintained by the
508    rtl middle-end as dynamic stack space is allocated.  */
509
510 static tree
511 get_nl_goto_field (struct nesting_info *info)
512 {
513   tree field = info->nl_goto_field;
514   if (!field)
515     {
516       unsigned size;
517       tree type;
518
519       /* For __builtin_nonlocal_goto, we need N words.  The first is the
520          frame pointer, the rest is for the target's stack pointer save
521          area.  The number of words is controlled by STACK_SAVEAREA_MODE;
522          not the best interface, but it'll do for now.  */
523       if (Pmode == ptr_mode)
524         type = ptr_type_node;
525       else
526         type = lang_hooks.types.type_for_mode (Pmode, 1);
527
528       size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
529       size = size / GET_MODE_SIZE (Pmode);
530       size = size + 1;
531
532       type = build_array_type
533         (type, build_index_type (build_int_cst (NULL_TREE, size)));
534
535       field = make_node (FIELD_DECL);
536       DECL_NAME (field) = get_identifier ("__nl_goto_buf");
537       TREE_TYPE (field) = type;
538       DECL_ALIGN (field) = TYPE_ALIGN (type);
539       TREE_ADDRESSABLE (field) = 1;
540
541       insert_field_into_struct (get_frame_type (info), field);
542
543       info->nl_goto_field = field;
544     }
545
546   return field;
547 }
548 \f
549 /* Iterate over all sub-statements of *TP calling walk_tree with
550    WI->CALLBACK for every sub-expression in each statement found.  */
551
552 void
553 walk_stmts (struct walk_stmt_info *wi, tree *tp)
554 {
555   tree t = *tp;
556   int walk_subtrees;
557
558   if (!t)
559     return;
560
561   if (wi->want_locations && EXPR_HAS_LOCATION (t))
562     input_location = EXPR_LOCATION (t);
563
564   switch (TREE_CODE (t))
565     {
566     case STATEMENT_LIST:
567       {
568         tree_stmt_iterator i;
569         for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
570           {
571             wi->tsi = i;
572             walk_stmts (wi, tsi_stmt_ptr (i));
573           }
574       }
575       break;
576
577     case COND_EXPR:
578       walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
579       walk_stmts (wi, &COND_EXPR_THEN (t));
580       walk_stmts (wi, &COND_EXPR_ELSE (t));
581       break;
582     case CATCH_EXPR:
583       walk_stmts (wi, &CATCH_BODY (t));
584       break;
585     case EH_FILTER_EXPR:
586       walk_stmts (wi, &EH_FILTER_FAILURE (t));
587       break;
588     case TRY_CATCH_EXPR:
589     case TRY_FINALLY_EXPR:
590       walk_stmts (wi, &TREE_OPERAND (t, 0));
591       walk_stmts (wi, &TREE_OPERAND (t, 1));
592       break;
593
594     case BIND_EXPR:
595       if (wi->want_bind_expr)
596         {
597           walk_subtrees = 1;
598           wi->callback (tp, &walk_subtrees, wi);
599           if (!walk_subtrees)
600             break;
601         }
602       walk_stmts (wi, &BIND_EXPR_BODY (t));
603       break;
604
605     case RETURN_EXPR:
606       if (wi->want_return_expr)
607         {
608           walk_subtrees = 1;
609           wi->callback (tp, &walk_subtrees, wi);
610           if (!walk_subtrees)
611             break;
612         }
613       walk_stmts (wi, &TREE_OPERAND (t, 0));
614       break;
615
616     case MODIFY_EXPR:
617       /* A formal temporary lhs may use a COMPONENT_REF rhs.  */
618       wi->val_only = !is_gimple_formal_tmp_var (TREE_OPERAND (t, 0));
619       walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
620
621       /* If the rhs is appropriate for a memory, we may use a
622          COMPONENT_REF on the lhs.  */
623       wi->val_only = !is_gimple_mem_rhs (TREE_OPERAND (t, 1));
624       wi->is_lhs = true;
625       walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
626
627       wi->val_only = true;
628       wi->is_lhs = false;
629       break;
630
631     default:
632       wi->val_only = true;
633       walk_tree (tp, wi->callback, wi, NULL);
634       break;
635     }
636 }
637
638 /* Invoke CALLBACK on all statements of *STMT_P.  */
639
640 static void
641 walk_body (walk_tree_fn callback, struct nesting_info *info, tree *stmt_p)
642 {
643   struct walk_stmt_info wi;
644
645   memset (&wi, 0, sizeof (wi));
646   wi.callback = callback;
647   wi.info = info;
648   wi.val_only = true;
649
650   walk_stmts (&wi, stmt_p);
651 }
652
653 /* Invoke CALLBACK on all statements of INFO->CONTEXT.  */
654
655 static inline void
656 walk_function (walk_tree_fn callback, struct nesting_info *info)
657 {
658   walk_body (callback, info, &DECL_SAVED_TREE (info->context));
659 }
660
661 /* Similarly for ROOT and all functions nested underneath, depth first.  */
662     
663 static void
664 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
665 {
666   do
667     {
668       if (root->inner)
669         walk_all_functions (callback, root->inner);
670       walk_function (callback, root);
671       root = root->next;
672     }
673   while (root);
674 }
675 \f
676 /* We have to check for a fairly pathological case.  The operands of function
677    nested function are to be interpreted in the context of the enclosing
678    function.  So if any are variably-sized, they will get remapped when the
679    enclosing function is inlined.  But that remapping would also have to be
680    done in the types of the PARM_DECLs of the nested function, meaning the
681    argument types of that function will disagree with the arguments in the
682    calls to that function.  So we'd either have to make a copy of the nested
683    function corresponding to each time the enclosing function was inlined or
684    add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
685    function.  The former is not practical.  The latter would still require
686    detecting this case to know when to add the conversions.  So, for now at
687    least, we don't inline such an enclosing function.
688
689    We have to do that check recursively, so here return indicating whether
690    FNDECL has such a nested function.  ORIG_FN is the function we were
691    trying to inline to use for checking whether any argument is variably
692    modified by anything in it.
693
694    It would be better to do this in tree-inline.c so that we could give
695    the appropriate warning for why a function can't be inlined, but that's
696    too late since the nesting structure has already been flattened and
697    adding a flag just to record this fact seems a waste of a flag.  */
698
699 static bool
700 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
701 {
702   struct cgraph_node *cgn = cgraph_node (fndecl);
703   tree arg;
704
705   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
706     {
707       for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
708         if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
709           return true;
710
711       if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
712         return true;
713     }
714
715   return false;
716 }
717
718 /* Construct our local datastructure describing the function nesting
719    tree rooted by CGN.  */
720
721 static struct nesting_info *
722 create_nesting_tree (struct cgraph_node *cgn)
723 {
724   struct nesting_info *info = GGC_CNEW (struct nesting_info);
725   info->field_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
726   info->var_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
727   info->suppress_expansion = BITMAP_GGC_ALLOC ();
728   info->context = cgn->decl;
729
730   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
731     {
732       struct nesting_info *sub = create_nesting_tree (cgn);
733       sub->outer = info;
734       sub->next = info->inner;
735       info->inner = sub;
736     }
737
738   /* See discussion at check_for_nested_with_variably_modified for a
739      discussion of why this has to be here.  */
740   if (check_for_nested_with_variably_modified (info->context, info->context))
741     DECL_UNINLINABLE (info->context) = true;
742
743   return info;
744 }
745
746 /* Return an expression computing the static chain for TARGET_CONTEXT
747    from INFO->CONTEXT.  Insert any necessary computations before TSI.  */
748
749 static tree
750 get_static_chain (struct nesting_info *info, tree target_context,
751                   tree_stmt_iterator *tsi)
752 {
753   struct nesting_info *i;
754   tree x;
755
756   if (info->context == target_context)
757     {
758       x = build_addr (info->frame_decl, target_context);
759     }
760   else
761     {
762       x = get_chain_decl (info);
763
764       for (i = info->outer; i->context != target_context; i = i->outer)
765         {
766           tree field = get_chain_field (i);
767
768           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
769           x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
770           x = init_tmp_var (info, x, tsi);
771         }
772     }
773
774   return x;
775 }
776
777 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
778    frame as seen from INFO->CONTEXT.  Insert any necessary computations
779    before TSI.  */
780
781 static tree
782 get_frame_field (struct nesting_info *info, tree target_context,
783                  tree field, tree_stmt_iterator *tsi)
784 {
785   struct nesting_info *i;
786   tree x;
787
788   if (info->context == target_context)
789     {
790       /* Make sure frame_decl gets created.  */
791       (void) get_frame_type (info);
792       x = info->frame_decl;
793     }
794   else
795     {
796       x = get_chain_decl (info);
797
798       for (i = info->outer; i->context != target_context; i = i->outer)
799         {
800           tree field = get_chain_field (i);
801
802           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
803           x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
804           x = init_tmp_var (info, x, tsi);
805         }
806
807       x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
808     }
809
810   x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
811   return x;
812 }
813
814 /* A subroutine of convert_nonlocal_reference.  Create a local variable
815    in the nested function with DECL_VALUE_EXPR set to reference the true
816    variable in the parent function.  This is used both for debug info 
817    and in OpenMP lowering.  */
818
819 static tree
820 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
821 {
822   struct var_map_elt *elt, dummy;
823   tree target_context;
824   struct nesting_info *i;
825   tree x, field, new_decl;
826   void **slot;
827
828   dummy.old = decl;
829   slot = htab_find_slot (info->var_map, &dummy, INSERT);
830   elt = *slot;
831
832   if (elt)
833     return elt->new;
834
835   target_context = decl_function_context (decl);
836
837   /* A copy of the code in get_frame_field, but without the temporaries.  */
838   if (info->context == target_context)
839     {
840       /* Make sure frame_decl gets created.  */
841       (void) get_frame_type (info);
842       x = info->frame_decl;
843       i = info;
844     }
845   else
846     {
847       x = get_chain_decl (info);
848       for (i = info->outer; i->context != target_context; i = i->outer)
849         {
850           field = get_chain_field (i);
851           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
852           x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
853         }
854       x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
855     }
856
857   field = lookup_field_for_decl (i, decl, INSERT);
858   x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
859   if (use_pointer_in_frame (decl))
860     x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
861
862   /* ??? We should be remapping types as well, surely.  */
863   new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
864   DECL_CONTEXT (new_decl) = info->context;
865   DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
866   DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
867   DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
868   TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
869   TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
870   TREE_READONLY (new_decl) = TREE_READONLY (decl);
871   TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
872   DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
873
874   SET_DECL_VALUE_EXPR (new_decl, x);
875   DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
876
877   elt = ggc_alloc (sizeof (*elt));
878   elt->old = decl;
879   elt->new = new_decl;
880   *slot = elt;
881
882   TREE_CHAIN (new_decl) = info->debug_var_chain;
883   info->debug_var_chain = new_decl;
884
885   return new_decl;
886 }
887
888 /* Called via walk_function+walk_tree, rewrite all references to VAR
889    and PARM_DECLs that belong to outer functions.
890
891    The rewrite will involve some number of structure accesses back up
892    the static chain.  E.g. for a variable FOO up one nesting level it'll
893    be CHAIN->FOO.  For two levels it'll be CHAIN->__chain->FOO.  Further
894    indirections apply to decls for which use_pointer_in_frame is true.  */
895
896 static bool convert_nonlocal_omp_clauses (tree *, struct walk_stmt_info *);
897
898 static tree
899 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
900 {
901   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
902   struct nesting_info *info = wi->info;
903   tree t = *tp;
904   tree save_local_var_chain;
905   bitmap save_suppress;
906
907   *walk_subtrees = 0;
908   switch (TREE_CODE (t))
909     {
910     case VAR_DECL:
911       /* Non-automatic variables are never processed.  */
912       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
913         break;
914       /* FALLTHRU */
915
916     case PARM_DECL:
917       if (decl_function_context (t) != info->context)
918         {
919           tree x;
920           wi->changed = true;
921
922           x = get_nonlocal_debug_decl (info, t);
923           if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
924             {
925               tree target_context = decl_function_context (t);
926               struct nesting_info *i;
927               for (i = info->outer; i->context != target_context; i = i->outer)
928                 continue;
929               x = lookup_field_for_decl (i, t, INSERT);
930               x = get_frame_field (info, target_context, x, &wi->tsi);
931               if (use_pointer_in_frame (t))
932                 {
933                   x = init_tmp_var (info, x, &wi->tsi);
934                   x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
935                 }
936             }
937
938           if (wi->val_only)
939             {
940               if (wi->is_lhs)
941                 x = save_tmp_var (info, x, &wi->tsi);
942               else
943                 x = init_tmp_var (info, x, &wi->tsi);
944             }
945
946           *tp = x;
947         }
948       break;
949
950     case GOTO_EXPR:
951       /* Don't walk non-local gotos for now.  */
952       if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
953         {
954           *walk_subtrees = 1;
955           wi->val_only = true;
956           wi->is_lhs = false;
957         }
958       break;
959
960     case LABEL_DECL:
961       /* We're taking the address of a label from a parent function, but
962          this is not itself a non-local goto.  Mark the label such that it
963          will not be deleted, much as we would with a label address in
964          static storage.  */
965       if (decl_function_context (t) != info->context)
966         FORCED_LABEL (t) = 1;
967       break;
968
969     case ADDR_EXPR:
970       {
971         bool save_val_only = wi->val_only;
972
973         wi->val_only = false;
974         wi->is_lhs = false;
975         wi->changed = false;
976         walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
977         wi->val_only = true;
978
979         if (wi->changed)
980           {
981             tree save_context;
982
983             /* If we changed anything, then TREE_INVARIANT is be wrong,
984                since we're no longer directly referencing a decl.  */
985             save_context = current_function_decl;
986             current_function_decl = info->context;
987             recompute_tree_invariant_for_addr_expr (t);
988             current_function_decl = save_context;
989
990             /* If the callback converted the address argument in a context
991                where we only accept variables (and min_invariant, presumably),
992                then compute the address into a temporary.  */
993             if (save_val_only)
994               *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
995           }
996       }
997       break;
998
999     case REALPART_EXPR:
1000     case IMAGPART_EXPR:
1001     case COMPONENT_REF:
1002     case ARRAY_REF:
1003     case ARRAY_RANGE_REF:
1004     case BIT_FIELD_REF:
1005       /* Go down this entire nest and just look at the final prefix and
1006          anything that describes the references.  Otherwise, we lose track
1007          of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
1008       wi->val_only = true;
1009       wi->is_lhs = false;
1010       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1011         {
1012           if (TREE_CODE (t) == COMPONENT_REF)
1013             walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1014                        NULL);
1015           else if (TREE_CODE (t) == ARRAY_REF
1016                    || TREE_CODE (t) == ARRAY_RANGE_REF)
1017             {
1018               walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1019                          NULL);
1020               walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1021                          NULL);
1022               walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
1023                          NULL);
1024             }
1025           else if (TREE_CODE (t) == BIT_FIELD_REF)
1026             {
1027               walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1028                          NULL);
1029               walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1030                          NULL);
1031             }
1032         }
1033       wi->val_only = false;
1034       walk_tree (tp, convert_nonlocal_reference, wi, NULL);
1035       break;
1036
1037     case OMP_PARALLEL:
1038       save_suppress = info->suppress_expansion;
1039       if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1040         {
1041           tree c, decl;
1042           decl = get_chain_decl (info);
1043           c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1044           OMP_CLAUSE_DECL (c) = decl;
1045           OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1046           OMP_PARALLEL_CLAUSES (t) = c;
1047         }
1048
1049       save_local_var_chain = info->new_local_var_chain;
1050       info->new_local_var_chain = NULL;
1051
1052       walk_body (convert_nonlocal_reference, info, &OMP_PARALLEL_BODY (t));
1053
1054       if (info->new_local_var_chain)
1055         declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1056       info->new_local_var_chain = save_local_var_chain;
1057       info->suppress_expansion = save_suppress;
1058       break;
1059
1060     case OMP_FOR:
1061     case OMP_SECTIONS:
1062     case OMP_SINGLE:
1063       save_suppress = info->suppress_expansion;
1064       convert_nonlocal_omp_clauses (&OMP_CLAUSES (t), wi);
1065       walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1066       info->suppress_expansion = save_suppress;
1067       break;
1068
1069     case OMP_SECTION:
1070     case OMP_MASTER:
1071     case OMP_ORDERED:
1072       walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1073       break;
1074
1075     default:
1076       if (!IS_TYPE_OR_DECL_P (t))
1077         {
1078           *walk_subtrees = 1;
1079           wi->val_only = true;
1080           wi->is_lhs = false;
1081         }
1082       break;
1083     }
1084
1085   return NULL_TREE;
1086 }
1087
1088 static bool
1089 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1090 {
1091   struct nesting_info *info = wi->info;
1092   bool need_chain = false;
1093   tree clause, decl;
1094   int dummy;
1095   bitmap new_suppress;
1096
1097   new_suppress = BITMAP_GGC_ALLOC ();
1098   bitmap_copy (new_suppress, info->suppress_expansion);
1099
1100   for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1101     {
1102       switch (OMP_CLAUSE_CODE (clause))
1103         {
1104         case OMP_CLAUSE_PRIVATE:
1105         case OMP_CLAUSE_FIRSTPRIVATE:
1106         case OMP_CLAUSE_LASTPRIVATE:
1107         case OMP_CLAUSE_REDUCTION:
1108         case OMP_CLAUSE_COPYPRIVATE:
1109         case OMP_CLAUSE_SHARED:
1110           decl = OMP_CLAUSE_DECL (clause);
1111           if (decl_function_context (decl) != info->context)
1112             {
1113               bitmap_set_bit (new_suppress, DECL_UID (decl));
1114               OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1115               need_chain = true;
1116             }
1117           break;
1118
1119         case OMP_CLAUSE_SCHEDULE:
1120           if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1121             break;
1122           /* FALLTHRU */
1123         case OMP_CLAUSE_IF:
1124         case OMP_CLAUSE_NUM_THREADS:
1125           wi->val_only = true;
1126           wi->is_lhs = false;
1127           convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1128                                       wi);
1129           break;
1130
1131         case OMP_CLAUSE_NOWAIT:
1132         case OMP_CLAUSE_ORDERED:
1133         case OMP_CLAUSE_DEFAULT:
1134         case OMP_CLAUSE_COPYIN:
1135           break;
1136
1137         default:
1138           gcc_unreachable ();
1139         }
1140     }
1141
1142   info->suppress_expansion = new_suppress;
1143
1144   return need_chain;
1145 }
1146
1147 /* A subroutine of convert_local_reference.  Create a local variable
1148    in the parent function with DECL_VALUE_EXPR set to reference the
1149    field in FRAME.  This is used both for debug info and in OpenMP
1150    lowering.  */
1151
1152 static tree
1153 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1154 {
1155   struct var_map_elt *elt, dummy;
1156   tree x, new_decl;
1157   void **slot;
1158
1159   dummy.old = decl;
1160   slot = htab_find_slot (info->var_map, &dummy, INSERT);
1161   elt = *slot;
1162
1163   if (elt)
1164     return elt->new;
1165
1166   /* Make sure frame_decl gets created.  */
1167   (void) get_frame_type (info);
1168   x = info->frame_decl;
1169   x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1170
1171   new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1172   DECL_CONTEXT (new_decl) = info->context;
1173   DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1174   DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1175   DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1176   TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1177   TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1178   TREE_READONLY (new_decl) = TREE_READONLY (decl);
1179   TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1180   DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1181
1182   SET_DECL_VALUE_EXPR (new_decl, x);
1183   DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1184
1185   elt = ggc_alloc (sizeof (*elt));
1186   elt->old = decl;
1187   elt->new = new_decl;
1188   *slot = elt;
1189
1190   TREE_CHAIN (new_decl) = info->debug_var_chain;
1191   info->debug_var_chain = new_decl;
1192
1193   /* Do not emit debug info twice.  */
1194   DECL_IGNORED_P (decl) = 1;
1195
1196   return new_decl;
1197 }
1198
1199 /* Called via walk_function+walk_tree, rewrite all references to VAR
1200    and PARM_DECLs that were referenced by inner nested functions.
1201    The rewrite will be a structure reference to the local frame variable.  */
1202
1203 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1204
1205 static tree
1206 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
1207 {
1208   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1209   struct nesting_info *info = wi->info;
1210   tree t = *tp, field, x;
1211   bool save_val_only;
1212   tree save_local_var_chain;
1213   bitmap save_suppress;
1214
1215   *walk_subtrees = 0;
1216   switch (TREE_CODE (t))
1217     {
1218     case VAR_DECL:
1219       /* Non-automatic variables are never processed.  */
1220       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1221         break;
1222       /* FALLTHRU */
1223
1224     case PARM_DECL:
1225       if (decl_function_context (t) == info->context)
1226         {
1227           /* If we copied a pointer to the frame, then the original decl
1228              is used unchanged in the parent function.  */
1229           if (use_pointer_in_frame (t))
1230             break;
1231
1232           /* No need to transform anything if no child references the
1233              variable.  */
1234           field = lookup_field_for_decl (info, t, NO_INSERT);
1235           if (!field)
1236             break;
1237           wi->changed = true;
1238
1239           x = get_local_debug_decl (info, t, field);
1240           if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1241             x = get_frame_field (info, info->context, field, &wi->tsi);
1242
1243           if (wi->val_only)
1244             {
1245               if (wi->is_lhs)
1246                 x = save_tmp_var (info, x, &wi->tsi);
1247               else
1248                 x = init_tmp_var (info, x, &wi->tsi);
1249             }
1250
1251           *tp = x;
1252         }
1253       break;
1254
1255     case ADDR_EXPR:
1256       save_val_only = wi->val_only;
1257       wi->val_only = false;
1258       wi->is_lhs = false;
1259       wi->changed = false;
1260       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
1261       wi->val_only = save_val_only;
1262
1263       /* If we converted anything ... */
1264       if (wi->changed)
1265         {
1266           tree save_context;
1267
1268           /* Then the frame decl is now addressable.  */
1269           TREE_ADDRESSABLE (info->frame_decl) = 1;
1270             
1271           save_context = current_function_decl;
1272           current_function_decl = info->context;
1273           recompute_tree_invariant_for_addr_expr (t);
1274           current_function_decl = save_context;
1275
1276           /* If we are in a context where we only accept values, then
1277              compute the address into a temporary.  */
1278           if (save_val_only)
1279             *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1280         }
1281       break;
1282
1283     case REALPART_EXPR:
1284     case IMAGPART_EXPR:
1285     case COMPONENT_REF:
1286     case ARRAY_REF:
1287     case ARRAY_RANGE_REF:
1288     case BIT_FIELD_REF:
1289       /* Go down this entire nest and just look at the final prefix and
1290          anything that describes the references.  Otherwise, we lose track
1291          of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
1292       save_val_only = wi->val_only;
1293       wi->val_only = true;
1294       wi->is_lhs = false;
1295       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1296         {
1297           if (TREE_CODE (t) == COMPONENT_REF)
1298             walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1299                        NULL);
1300           else if (TREE_CODE (t) == ARRAY_REF
1301                    || TREE_CODE (t) == ARRAY_RANGE_REF)
1302             {
1303               walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1304                          NULL);
1305               walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1306                          NULL);
1307               walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1308                          NULL);
1309             }
1310           else if (TREE_CODE (t) == BIT_FIELD_REF)
1311             {
1312               walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1313                          NULL);
1314               walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1315                          NULL);
1316             }
1317         }
1318       wi->val_only = false;
1319       walk_tree (tp, convert_local_reference, wi, NULL);
1320       wi->val_only = save_val_only;
1321       break;
1322
1323     case OMP_PARALLEL:
1324       save_suppress = info->suppress_expansion;
1325       if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1326         {
1327           tree c;
1328           (void) get_frame_type (info);
1329           c = build_omp_clause (OMP_CLAUSE_SHARED);
1330           OMP_CLAUSE_DECL (c) = info->frame_decl;
1331           OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1332           OMP_PARALLEL_CLAUSES (t) = c;
1333         }
1334
1335       save_local_var_chain = info->new_local_var_chain;
1336       info->new_local_var_chain = NULL;
1337
1338       walk_body (convert_local_reference, info, &OMP_PARALLEL_BODY (t));
1339
1340       if (info->new_local_var_chain)
1341         declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1342       info->new_local_var_chain = save_local_var_chain;
1343       info->suppress_expansion = save_suppress;
1344       break;
1345
1346     case OMP_FOR:
1347     case OMP_SECTIONS:
1348     case OMP_SINGLE:
1349       save_suppress = info->suppress_expansion;
1350       convert_local_omp_clauses (&OMP_CLAUSES (t), wi);
1351       walk_body (convert_local_reference, info, &OMP_BODY (t));
1352       info->suppress_expansion = save_suppress;
1353       break;
1354
1355     case OMP_SECTION:
1356     case OMP_MASTER:
1357     case OMP_ORDERED:
1358       walk_body (convert_local_reference, info, &OMP_BODY (t));
1359       break;
1360
1361     default:
1362       if (!IS_TYPE_OR_DECL_P (t))
1363         {
1364           *walk_subtrees = 1;
1365           wi->val_only = true;
1366           wi->is_lhs = false;
1367         }
1368       break;
1369     }
1370
1371   return NULL_TREE;
1372 }
1373
1374 static bool
1375 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1376 {
1377   struct nesting_info *info = wi->info;
1378   bool need_frame = false;
1379   tree clause, decl;
1380   int dummy;
1381   bitmap new_suppress;
1382
1383   new_suppress = BITMAP_GGC_ALLOC ();
1384   bitmap_copy (new_suppress, info->suppress_expansion);
1385
1386   for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1387     {
1388       switch (OMP_CLAUSE_CODE (clause))
1389         {
1390         case OMP_CLAUSE_PRIVATE:
1391         case OMP_CLAUSE_FIRSTPRIVATE:
1392         case OMP_CLAUSE_LASTPRIVATE:
1393         case OMP_CLAUSE_REDUCTION:
1394         case OMP_CLAUSE_COPYPRIVATE:
1395         case OMP_CLAUSE_SHARED:
1396           decl = OMP_CLAUSE_DECL (clause);
1397           if (decl_function_context (decl) == info->context
1398               && !use_pointer_in_frame (decl))
1399             {
1400               tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1401               if (field)
1402                 {
1403                   bitmap_set_bit (new_suppress, DECL_UID (decl));
1404                   OMP_CLAUSE_DECL (clause)
1405                     = get_local_debug_decl (info, decl, field);
1406                   need_frame = true;
1407                 }
1408             }
1409           break;
1410
1411         case OMP_CLAUSE_SCHEDULE:
1412           if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1413             break;
1414           /* FALLTHRU */
1415         case OMP_CLAUSE_IF:
1416         case OMP_CLAUSE_NUM_THREADS:
1417           wi->val_only = true;
1418           wi->is_lhs = false;
1419           convert_local_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy, wi);
1420           break;
1421
1422         case OMP_CLAUSE_NOWAIT:
1423         case OMP_CLAUSE_ORDERED:
1424         case OMP_CLAUSE_DEFAULT:
1425         case OMP_CLAUSE_COPYIN:
1426           break;
1427
1428         default:
1429           gcc_unreachable ();
1430         }
1431     }
1432
1433   info->suppress_expansion = new_suppress;
1434
1435   return need_frame;
1436 }
1437
1438 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that 
1439    reference labels from outer functions.  The rewrite will be a 
1440    call to __builtin_nonlocal_goto.  */
1441
1442 static tree
1443 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1444 {
1445   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1446   struct nesting_info *info = wi->info, *i;
1447   tree t = *tp, label, new_label, target_context, x, arg, field;
1448   struct var_map_elt *elt, dummy;
1449   void **slot;
1450
1451   *walk_subtrees = 0;
1452   if (TREE_CODE (t) != GOTO_EXPR)
1453     return NULL_TREE;
1454   label = GOTO_DESTINATION (t);
1455   if (TREE_CODE (label) != LABEL_DECL)
1456     return NULL_TREE;
1457   target_context = decl_function_context (label);
1458   if (target_context == info->context)
1459     return NULL_TREE;
1460
1461   for (i = info->outer; target_context != i->context; i = i->outer)
1462     continue;
1463
1464   /* The original user label may also be use for a normal goto, therefore
1465      we must create a new label that will actually receive the abnormal
1466      control transfer.  This new label will be marked LABEL_NONLOCAL; this
1467      mark will trigger proper behavior in the cfg, as well as cause the
1468      (hairy target-specific) non-local goto receiver code to be generated
1469      when we expand rtl.  Enter this association into var_map so that we
1470      can insert the new label into the IL during a second pass.  */
1471   dummy.old = label;
1472   slot = htab_find_slot (i->var_map, &dummy, INSERT);
1473   elt = (struct var_map_elt *) *slot;
1474   if (elt == NULL)
1475     {
1476       new_label = create_artificial_label ();
1477       DECL_NONLOCAL (new_label) = 1;
1478
1479       elt = GGC_NEW (struct var_map_elt); 
1480       elt->old = label;
1481       elt->new = new_label;
1482       *slot = elt;
1483     }
1484   else
1485     new_label = elt->new;
1486   
1487   /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field).  */
1488   field = get_nl_goto_field (i);
1489   x = get_frame_field (info, target_context, field, &wi->tsi);
1490   x = build_addr (x, target_context);
1491   x = tsi_gimplify_val (info, x, &wi->tsi);
1492   arg = tree_cons (NULL, x, NULL);
1493   x = build_addr (new_label, target_context);
1494   arg = tree_cons (NULL, x, arg);
1495   x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1496   x = build_function_call_expr (x, arg);
1497
1498   SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1499   *tsi_stmt_ptr (wi->tsi) = x;
1500
1501   return NULL_TREE;
1502 }
1503
1504 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that 
1505    are referenced via nonlocal goto from a nested function.  The rewrite
1506    will involve installing a newly generated DECL_NONLOCAL label, and
1507    (potentially) a branch around the rtl gunk that is assumed to be 
1508    attached to such a label.  */
1509
1510 static tree
1511 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1512 {
1513   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1514   struct nesting_info *info = wi->info;
1515   tree t = *tp, label, new_label, x;
1516   struct var_map_elt *elt, dummy;
1517   tree_stmt_iterator tmp_tsi;
1518
1519   *walk_subtrees = 0;
1520   if (TREE_CODE (t) != LABEL_EXPR)
1521     return NULL_TREE;
1522   label = LABEL_EXPR_LABEL (t);
1523
1524   dummy.old = label;
1525   elt = (struct var_map_elt *) htab_find (info->var_map, &dummy);
1526   if (!elt)
1527     return NULL_TREE;
1528   new_label = elt->new;
1529
1530   /* If there's any possibility that the previous statement falls through,
1531      then we must branch around the new non-local label.  */
1532   tmp_tsi = wi->tsi;
1533   tsi_prev (&tmp_tsi);
1534   if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1535     {
1536       x = build1 (GOTO_EXPR, void_type_node, label);
1537       tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1538     }
1539   x = build1 (LABEL_EXPR, void_type_node, new_label);
1540   tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1541
1542   return NULL_TREE;
1543 }
1544
1545 /* Called via walk_function+walk_tree, rewrite all references to addresses
1546    of nested functions that require the use of trampolines.  The rewrite
1547    will involve a reference a trampoline generated for the occasion.  */
1548
1549 static tree
1550 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1551 {
1552   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1553   struct nesting_info *info = wi->info, *i;
1554   tree t = *tp, decl, target_context, x, arg;
1555
1556   *walk_subtrees = 0;
1557   switch (TREE_CODE (t))
1558     {
1559     case ADDR_EXPR:
1560       /* Build
1561            T.1 = &CHAIN->tramp;
1562            T.2 = __builtin_adjust_trampoline (T.1);
1563            T.3 = (func_type)T.2;
1564       */
1565
1566       decl = TREE_OPERAND (t, 0);
1567       if (TREE_CODE (decl) != FUNCTION_DECL)
1568         break;
1569
1570       /* Only need to process nested functions.  */
1571       target_context = decl_function_context (decl);
1572       if (!target_context)
1573         break;
1574
1575       /* If the nested function doesn't use a static chain, then
1576          it doesn't need a trampoline.  */
1577       if (DECL_NO_STATIC_CHAIN (decl))
1578         break;
1579
1580       /* Lookup the immediate parent of the callee, as that's where
1581          we need to insert the trampoline.  */
1582       for (i = info; i->context != target_context; i = i->outer)
1583         continue;
1584       x = lookup_tramp_for_decl (i, decl, INSERT);
1585
1586       /* Compute the address of the field holding the trampoline.  */
1587       x = get_frame_field (info, target_context, x, &wi->tsi);
1588       x = build_addr (x, target_context);
1589       x = tsi_gimplify_val (info, x, &wi->tsi);
1590       arg = tree_cons (NULL, x, NULL);
1591
1592       /* Do machine-specific ugliness.  Normally this will involve
1593          computing extra alignment, but it can really be anything.  */
1594       x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1595       x = build_function_call_expr (x, arg);
1596       x = init_tmp_var (info, x, &wi->tsi);
1597
1598       /* Cast back to the proper function type.  */
1599       x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1600       x = init_tmp_var (info, x, &wi->tsi);
1601
1602       *tp = x;
1603       break;
1604
1605     case CALL_EXPR:
1606       /* Only walk call arguments, lest we generate trampolines for
1607          direct calls.  */
1608       walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1609       break;
1610
1611     default:
1612       if (!IS_TYPE_OR_DECL_P (t))
1613         *walk_subtrees = 1;
1614       break;
1615     }
1616
1617   return NULL_TREE;
1618 }
1619
1620 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that 
1621    reference nested functions to make sure that the static chain is
1622    set up properly for the call.  */
1623
1624 static tree
1625 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1626 {
1627   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1628   struct nesting_info *info = wi->info;
1629   tree t = *tp, decl, target_context;
1630   char save_static_chain_added;
1631   int i;
1632
1633   *walk_subtrees = 0;
1634   switch (TREE_CODE (t))
1635     {
1636     case CALL_EXPR:
1637       decl = get_callee_fndecl (t);
1638       if (!decl)
1639         break;
1640       target_context = decl_function_context (decl);
1641       if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1642         {
1643           TREE_OPERAND (t, 2)
1644             = get_static_chain (info, target_context, &wi->tsi);
1645           info->static_chain_added
1646             |= (1 << (info->context != target_context));
1647         }
1648       break;
1649
1650     case RETURN_EXPR:
1651     case MODIFY_EXPR:
1652     case WITH_SIZE_EXPR:
1653       /* Only return modify and with_size_expr may contain calls.  */
1654       *walk_subtrees = 1;
1655       break;
1656
1657     case OMP_PARALLEL:
1658       save_static_chain_added = info->static_chain_added;
1659       info->static_chain_added = 0;
1660       walk_body (convert_call_expr, info, &OMP_PARALLEL_BODY (t));
1661       for (i = 0; i < 2; i++)
1662         {
1663           tree c, decl;
1664           if ((info->static_chain_added & (1 << i)) == 0)
1665             continue;
1666           decl = i ? get_chain_decl (info) : info->frame_decl;
1667           /* Don't add CHAIN.* or FRAME.* twice.  */
1668           for (c = OMP_PARALLEL_CLAUSES (t); c; c = OMP_CLAUSE_CHAIN (c))
1669             if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1670                  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1671                 && OMP_CLAUSE_DECL (c) == decl)
1672               break;
1673           if (c == NULL)
1674             {
1675               c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1676               OMP_CLAUSE_DECL (c) = decl;
1677               OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1678               OMP_PARALLEL_CLAUSES (t) = c;
1679             }
1680         }
1681       info->static_chain_added |= save_static_chain_added;
1682       break;
1683
1684     case OMP_FOR:
1685     case OMP_SECTIONS:
1686     case OMP_SECTION:
1687     case OMP_SINGLE:
1688     case OMP_MASTER:
1689     case OMP_ORDERED:
1690     case OMP_CRITICAL:
1691       walk_body (convert_call_expr, info, &OMP_BODY (t));
1692       break;
1693
1694     default:
1695       break;
1696     }
1697
1698   return NULL_TREE;
1699 }
1700
1701 /* Walk the nesting tree starting with ROOT, depth first.  Convert all
1702    trampolines and call expressions.  On the way back up, determine if
1703    a nested function actually uses its static chain; if not, remember that.  */
1704
1705 static void
1706 convert_all_function_calls (struct nesting_info *root)
1707 {
1708   do
1709     {
1710       if (root->inner)
1711         convert_all_function_calls (root->inner);
1712
1713       walk_function (convert_tramp_reference, root);
1714       walk_function (convert_call_expr, root);
1715
1716       /* If the function does not use a static chain, then remember that.  */
1717       if (root->outer && !root->chain_decl && !root->chain_field)
1718         DECL_NO_STATIC_CHAIN (root->context) = 1;
1719       else
1720         gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1721
1722       root = root->next;
1723     }
1724   while (root);
1725 }
1726
1727 /* Do "everything else" to clean up or complete state collected by the
1728    various walking passes -- lay out the types and decls, generate code
1729    to initialize the frame decl, store critical expressions in the
1730    struct function for rtl to find.  */
1731
1732 static void
1733 finalize_nesting_tree_1 (struct nesting_info *root)
1734 {
1735   tree stmt_list = NULL;
1736   tree context = root->context;
1737   struct function *sf;
1738
1739   /* If we created a non-local frame type or decl, we need to lay them
1740      out at this time.  */
1741   if (root->frame_type)
1742     {
1743       /* In some cases the frame type will trigger the -Wpadded warning.
1744          This is not helpful; suppress it. */
1745       int save_warn_padded = warn_padded;
1746       warn_padded = 0;
1747       layout_type (root->frame_type);
1748       warn_padded = save_warn_padded;
1749       layout_decl (root->frame_decl, 0);
1750     }
1751
1752   /* If any parameters were referenced non-locally, then we need to 
1753      insert a copy.  Likewise, if any variables were referenced by
1754      pointer, we need to initialize the address.  */
1755   if (root->any_parm_remapped)
1756     {
1757       tree p;
1758       for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1759         {
1760           tree field, x, y;
1761
1762           field = lookup_field_for_decl (root, p, NO_INSERT);
1763           if (!field)
1764             continue;
1765
1766           if (use_pointer_in_frame (p))
1767             x = build_addr (p, context);
1768           else
1769             x = p;
1770
1771           y = build3 (COMPONENT_REF, TREE_TYPE (field),
1772                       root->frame_decl, field, NULL_TREE);
1773           x = build2 (MODIFY_EXPR, TREE_TYPE (field), y, x);
1774           append_to_statement_list (x, &stmt_list);
1775         }
1776     }
1777
1778   /* If a chain_field was created, then it needs to be initialized
1779      from chain_decl.  */
1780   if (root->chain_field)
1781     {
1782       tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
1783                        root->frame_decl, root->chain_field, NULL_TREE);
1784       x = build2 (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1785       append_to_statement_list (x, &stmt_list);
1786     }
1787
1788   /* If trampolines were created, then we need to initialize them.  */
1789   if (root->any_tramp_created)
1790     {
1791       struct nesting_info *i;
1792       for (i = root->inner; i ; i = i->next)
1793         {
1794           tree arg, x, field;
1795
1796           field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1797           if (!field)
1798             continue;
1799
1800           if (DECL_NO_STATIC_CHAIN (i->context))
1801             x = null_pointer_node;
1802           else
1803             x = build_addr (root->frame_decl, context);
1804           arg = tree_cons (NULL, x, NULL);
1805
1806           x = build_addr (i->context, context);
1807           arg = tree_cons (NULL, x, arg);
1808
1809           x = build3 (COMPONENT_REF, TREE_TYPE (field),
1810                       root->frame_decl, field, NULL_TREE);
1811           x = build_addr (x, context);
1812           arg = tree_cons (NULL, x, arg);
1813
1814           x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1815           x = build_function_call_expr (x, arg);
1816
1817           append_to_statement_list (x, &stmt_list);
1818         }
1819     }
1820
1821   /* If we created initialization statements, insert them.  */
1822   if (stmt_list)
1823     {
1824       annotate_all_with_locus (&stmt_list,
1825                                DECL_SOURCE_LOCATION (context));
1826       append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1827                                 &stmt_list);
1828       BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1829     }
1830
1831   /* If a chain_decl was created, then it needs to be registered with
1832      struct function so that it gets initialized from the static chain
1833      register at the beginning of the function.  */
1834   sf = DECL_STRUCT_FUNCTION (root->context);
1835   sf->static_chain_decl = root->chain_decl;
1836
1837   /* Similarly for the non-local goto save area.  */
1838   if (root->nl_goto_field)
1839     {
1840       sf->nonlocal_goto_save_area
1841         = get_frame_field (root, context, root->nl_goto_field, NULL);
1842       sf->has_nonlocal_label = 1;
1843     }
1844
1845   /* Make sure all new local variables get inserted into the
1846      proper BIND_EXPR.  */
1847   if (root->new_local_var_chain)
1848     declare_vars (root->new_local_var_chain, DECL_SAVED_TREE (root->context),
1849                   false);
1850   if (root->debug_var_chain)
1851     declare_vars (root->debug_var_chain, DECL_SAVED_TREE (root->context),
1852                   true);
1853
1854   /* Dump the translated tree function.  */
1855   dump_function (TDI_nested, root->context);
1856 }
1857
1858 static void
1859 finalize_nesting_tree (struct nesting_info *root)
1860 {
1861   do
1862     {
1863       if (root->inner)
1864         finalize_nesting_tree (root->inner);
1865       finalize_nesting_tree_1 (root);
1866       root = root->next;
1867     }
1868   while (root);
1869 }
1870
1871 /* Unnest the nodes and pass them to cgraph.  */
1872
1873 static void
1874 unnest_nesting_tree_1 (struct nesting_info *root)
1875 {
1876   struct cgraph_node *node = cgraph_node (root->context);
1877
1878   /* For nested functions update the cgraph to reflect unnesting.
1879      We also delay finalizing of these functions up to this point.  */
1880   if (node->origin)
1881     {
1882        cgraph_unnest_node (cgraph_node (root->context));
1883        cgraph_finalize_function (root->context, true);
1884     }
1885 }
1886
1887 static void
1888 unnest_nesting_tree (struct nesting_info *root)
1889 {
1890   do
1891     {
1892       if (root->inner)
1893         unnest_nesting_tree (root->inner);
1894       unnest_nesting_tree_1 (root);
1895       root = root->next;
1896     }
1897   while (root);
1898 }
1899
1900 /* Free the data structures allocated during this pass.  */
1901
1902 static void
1903 free_nesting_tree (struct nesting_info *root)
1904 {
1905   struct nesting_info *next;
1906   do
1907     {
1908       if (root->inner)
1909         free_nesting_tree (root->inner);
1910       htab_delete (root->var_map);
1911       next = root->next;
1912       ggc_free (root);
1913       root = next;
1914     }
1915   while (root);
1916 }
1917
1918 static GTY(()) struct nesting_info *root;
1919
1920 /* Main entry point for this pass.  Process FNDECL and all of its nested
1921    subroutines and turn them into something less tightly bound.  */
1922
1923 void
1924 lower_nested_functions (tree fndecl)
1925 {
1926   struct cgraph_node *cgn;
1927
1928   /* If there are no nested functions, there's nothing to do.  */
1929   cgn = cgraph_node (fndecl);
1930   if (!cgn->nested)
1931     return;
1932
1933   root = create_nesting_tree (cgn);
1934   walk_all_functions (convert_nonlocal_reference, root);
1935   walk_all_functions (convert_local_reference, root);
1936   walk_all_functions (convert_nl_goto_reference, root);
1937   walk_all_functions (convert_nl_goto_receiver, root);
1938   convert_all_function_calls (root);
1939   finalize_nesting_tree (root);
1940   unnest_nesting_tree (root);
1941   free_nesting_tree (root);
1942   root = NULL;
1943 }
1944
1945 #include "gt-tree-nested.h"