1 /* Nested function decomposition for trees.
2    Copyright (C) 2004, 2005 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))) var_map;
93   tree context;
94   tree new_local_var_chain;
95   tree frame_type;
96   tree frame_decl;
97   tree chain_field;
98   tree chain_decl;
99   tree nl_goto_field;
100 
101   bool any_parm_remapped;
102   bool any_tramp_created;
103 };
104 
105 
106 /* Hashing and equality functions for nesting_info->var_map.  */
107 
108 static hashval_t
var_map_hash(const void * x)109 var_map_hash (const void *x)
110 {
111   const struct var_map_elt *a = x;
112   return htab_hash_pointer (a->old);
113 }
114 
115 static int
var_map_eq(const void * x,const void * y)116 var_map_eq (const void *x, const void *y)
117 {
118   const struct var_map_elt *a = x;
119   const struct var_map_elt *b = y;
120   return a->old == b->old;
121 }
122 
123 /* We're working in so many different function contexts simultaneously,
124    that create_tmp_var is dangerous.  Prevent mishap.  */
125 #define create_tmp_var cant_use_create_tmp_var_here_dummy
126 
127 /* Like create_tmp_var, except record the variable for registration at
128    the given nesting level.  */
129 
130 static tree
create_tmp_var_for(struct nesting_info * info,tree type,const char * prefix)131 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
132 {
133   tree tmp_var;
134 
135   /* If the type is of variable size or a type which must be created by the
136      frontend, something is wrong.  Note that we explicitly allow
137      incomplete types here, since we create them ourselves here.  */
138   gcc_assert (!TREE_ADDRESSABLE (type));
139   gcc_assert (!TYPE_SIZE_UNIT (type)
140 	      || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
141 
142   tmp_var = create_tmp_var_raw (type, prefix);
143   DECL_CONTEXT (tmp_var) = info->context;
144   TREE_CHAIN (tmp_var) = info->new_local_var_chain;
145   DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
146   if (TREE_CODE (type) == COMPLEX_TYPE)
147     DECL_COMPLEX_GIMPLE_REG_P (tmp_var) = 1;
148 
149   info->new_local_var_chain = tmp_var;
150 
151   return tmp_var;
152 }
153 
154 /* Take the address of EXP to be used within function CONTEXT.
155    Mark it for addressability as necessary.  */
156 
157 tree
build_addr(tree exp,tree context)158 build_addr (tree exp, tree context)
159 {
160   tree base = exp;
161   tree save_context;
162   tree retval;
163 
164   while (handled_component_p (base))
165     base = TREE_OPERAND (base, 0);
166 
167   if (DECL_P (base))
168     TREE_ADDRESSABLE (base) = 1;
169 
170   /* Building the ADDR_EXPR will compute a set of properties for
171      that ADDR_EXPR.  Those properties are unfortunately context
172      specific.  ie, they are dependent on CURRENT_FUNCTION_DECL.
173 
174      Temporarily set CURRENT_FUNCTION_DECL to the desired context,
175      build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL.  That
176      way the properties are for the ADDR_EXPR are computed properly.  */
177   save_context = current_function_decl;
178   current_function_decl = context;
179   retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
180   current_function_decl = save_context;;
181   return retval;
182 }
183 
184 /* Insert FIELD into TYPE, sorted by alignment requirements.  */
185 
186 static void
insert_field_into_struct(tree type,tree field)187 insert_field_into_struct (tree type, tree field)
188 {
189   tree *p;
190 
191   DECL_CONTEXT (field) = type;
192 
193   for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
194     if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
195       break;
196 
197   TREE_CHAIN (field) = *p;
198   *p = field;
199 }
200 
201 /* Build or return the RECORD_TYPE that describes the frame state that is
202    shared between INFO->CONTEXT and its nested functions.  This record will
203    not be complete until finalize_nesting_tree; up until that point we'll
204    be adding fields as necessary.
205 
206    We also build the DECL that represents this frame in the function.  */
207 
208 static tree
get_frame_type(struct nesting_info * info)209 get_frame_type (struct nesting_info *info)
210 {
211   tree type = info->frame_type;
212   if (!type)
213     {
214       char *name;
215 
216       type = make_node (RECORD_TYPE);
217 
218       name = concat ("FRAME.",
219 		     IDENTIFIER_POINTER (DECL_NAME (info->context)),
220 		     NULL);
221       TYPE_NAME (type) = get_identifier (name);
222       free (name);
223 
224       info->frame_type = type;
225       info->frame_decl = create_tmp_var_for (info, type, "FRAME");
226 
227       /* ??? Always make it addressable for now, since it is meant to
228 	 be pointed to by the static chain pointer.  This pessimizes
229 	 when it turns out that no static chains are needed because
230 	 the nested functions referencing non-local variables are not
231 	 reachable, but the true pessimization is to create the non-
232 	 local frame structure in the first place.  */
233       TREE_ADDRESSABLE (info->frame_decl) = 1;
234     }
235   return type;
236 }
237 
238 /* Return true if DECL should be referenced by pointer in the non-local
239    frame structure.  */
240 
241 static bool
use_pointer_in_frame(tree decl)242 use_pointer_in_frame (tree decl)
243 {
244   if (TREE_CODE (decl) == PARM_DECL)
245     {
246       /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
247          sized decls, and inefficient to copy large aggregates.  Don't bother
248          moving anything but scalar variables.  */
249       return AGGREGATE_TYPE_P (TREE_TYPE (decl));
250     }
251   else
252     {
253       /* Variable sized types make things "interesting" in the frame.  */
254       return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
255     }
256 }
257 
258 /* Given DECL, a non-locally accessed variable, find or create a field
259    in the non-local frame structure for the given nesting context.  */
260 
261 static tree
lookup_field_for_decl(struct nesting_info * info,tree decl,enum insert_option insert)262 lookup_field_for_decl (struct nesting_info *info, tree decl,
263 		       enum insert_option insert)
264 {
265   struct var_map_elt *elt, dummy;
266   void **slot;
267   tree field;
268 
269   dummy.old = decl;
270   slot = htab_find_slot (info->var_map, &dummy, insert);
271   if (!slot)
272     {
273       gcc_assert (insert != INSERT);
274       return NULL;
275     }
276   elt = *slot;
277 
278   if (!elt && insert == INSERT)
279     {
280       field = make_node (FIELD_DECL);
281       DECL_NAME (field) = DECL_NAME (decl);
282 
283       if (use_pointer_in_frame (decl))
284 	{
285 	  TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
286 	  DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
287 	  DECL_NONADDRESSABLE_P (field) = 1;
288 	}
289       else
290 	{
291           TREE_TYPE (field) = TREE_TYPE (decl);
292           DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
293           DECL_ALIGN (field) = DECL_ALIGN (decl);
294           DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
295           TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
296           DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
297           TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
298 	}
299 
300       insert_field_into_struct (get_frame_type (info), field);
301 
302       elt = ggc_alloc (sizeof (*elt));
303       elt->old = decl;
304       elt->new = field;
305       *slot = elt;
306 
307       if (TREE_CODE (decl) == PARM_DECL)
308 	info->any_parm_remapped = true;
309     }
310   else
311     field = elt ? elt->new : NULL;
312 
313   return field;
314 }
315 
316 /* Build or return the variable that holds the static chain within
317    INFO->CONTEXT.  This variable may only be used within INFO->CONTEXT.  */
318 
319 static tree
get_chain_decl(struct nesting_info * info)320 get_chain_decl (struct nesting_info *info)
321 {
322   tree decl = info->chain_decl;
323   if (!decl)
324     {
325       tree type;
326 
327       type = get_frame_type (info->outer);
328       type = build_pointer_type (type);
329 
330       /* Note that this variable is *not* entered into any BIND_EXPR;
331 	 the construction of this variable is handled specially in
332 	 expand_function_start and initialize_inlined_parameters.
333 	 Note also that it's represented as a parameter.  This is more
334 	 close to the truth, since the initial value does come from
335 	 the caller.  */
336       decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
337       DECL_ARTIFICIAL (decl) = 1;
338       DECL_IGNORED_P (decl) = 1;
339       TREE_USED (decl) = 1;
340       DECL_CONTEXT (decl) = info->context;
341       DECL_ARG_TYPE (decl) = type;
342 
343       /* Tell tree-inline.c that we never write to this variable, so
344 	 it can copy-prop the replacement value immediately.  */
345       TREE_READONLY (decl) = 1;
346 
347       info->chain_decl = decl;
348     }
349   return decl;
350 }
351 
352 /* Build or return the field within the non-local frame state that holds
353    the static chain for INFO->CONTEXT.  This is the way to walk back up
354    multiple nesting levels.  */
355 
356 static tree
get_chain_field(struct nesting_info * info)357 get_chain_field (struct nesting_info *info)
358 {
359   tree field = info->chain_field;
360   if (!field)
361     {
362       tree type = build_pointer_type (get_frame_type (info->outer));
363 
364       field = make_node (FIELD_DECL);
365       DECL_NAME (field) = get_identifier ("__chain");
366       TREE_TYPE (field) = type;
367       DECL_ALIGN (field) = TYPE_ALIGN (type);
368       DECL_NONADDRESSABLE_P (field) = 1;
369 
370       insert_field_into_struct (get_frame_type (info), field);
371 
372       info->chain_field = field;
373     }
374   return field;
375 }
376 
377 /* Copy EXP into a temporary.  Allocate the temporary in the context of
378    INFO and insert the initialization statement before TSI.  */
379 
380 static tree
init_tmp_var(struct nesting_info * info,tree exp,tree_stmt_iterator * tsi)381 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
382 {
383   tree t, stmt;
384 
385   t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
386   stmt = build (MODIFY_EXPR, TREE_TYPE (t), t, exp);
387   SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
388   tsi_link_before (tsi, stmt, TSI_SAME_STMT);
389 
390   return t;
391 }
392 
393 /* Similarly, but only do so to force EXP to satisfy is_gimple_val.  */
394 
395 static tree
tsi_gimplify_val(struct nesting_info * info,tree exp,tree_stmt_iterator * tsi)396 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
397 {
398   if (is_gimple_val (exp))
399     return exp;
400   else
401     return init_tmp_var (info, exp, tsi);
402 }
403 
404 /* Similarly, but copy from the temporary and insert the statement
405    after the iterator.  */
406 
407 static tree
save_tmp_var(struct nesting_info * info,tree exp,tree_stmt_iterator * tsi)408 save_tmp_var (struct nesting_info *info, tree exp,
409 	      tree_stmt_iterator *tsi)
410 {
411   tree t, stmt;
412 
413   t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
414   stmt = build (MODIFY_EXPR, TREE_TYPE (t), exp, t);
415   SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
416   tsi_link_after (tsi, stmt, TSI_SAME_STMT);
417 
418   return t;
419 }
420 
421 /* Build or return the type used to represent a nested function trampoline.  */
422 
423 static GTY(()) tree trampoline_type;
424 
425 static tree
get_trampoline_type(void)426 get_trampoline_type (void)
427 {
428   tree record, t;
429   unsigned align, size;
430 
431   if (trampoline_type)
432     return trampoline_type;
433 
434   align = TRAMPOLINE_ALIGNMENT;
435   size = TRAMPOLINE_SIZE;
436 
437   /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
438      then allocate extra space so that we can do dynamic alignment.  */
439   if (align > STACK_BOUNDARY)
440     {
441       size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
442       align = STACK_BOUNDARY;
443     }
444 
445   t = build_index_type (build_int_cst (NULL_TREE, size - 1));
446   t = build_array_type (char_type_node, t);
447   t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
448   DECL_ALIGN (t) = align;
449   DECL_USER_ALIGN (t) = 1;
450 
451   record = make_node (RECORD_TYPE);
452   TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
453   TYPE_FIELDS (record) = t;
454   layout_type (record);
455 
456   return record;
457 }
458 
459 /* Given DECL, a nested function, find or create a field in the non-local
460    frame structure for a trampoline for this function.  */
461 
462 static tree
lookup_tramp_for_decl(struct nesting_info * info,tree decl,enum insert_option insert)463 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
464 		       enum insert_option insert)
465 {
466   struct var_map_elt *elt, dummy;
467   void **slot;
468   tree field;
469 
470   dummy.old = decl;
471   slot = htab_find_slot (info->var_map, &dummy, insert);
472   if (!slot)
473     {
474       gcc_assert (insert != INSERT);
475       return NULL;
476     }
477   elt = *slot;
478 
479   if (!elt && insert == INSERT)
480     {
481       field = make_node (FIELD_DECL);
482       DECL_NAME (field) = DECL_NAME (decl);
483       TREE_TYPE (field) = get_trampoline_type ();
484       TREE_ADDRESSABLE (field) = 1;
485 
486       insert_field_into_struct (get_frame_type (info), field);
487 
488       elt = ggc_alloc (sizeof (*elt));
489       elt->old = decl;
490       elt->new = field;
491       *slot = elt;
492 
493       info->any_tramp_created = true;
494     }
495   else
496     field = elt ? elt->new : NULL;
497 
498   return field;
499 }
500 
501 /* Build or return the field within the non-local frame state that holds
502    the non-local goto "jmp_buf".  The buffer itself is maintained by the
503    rtl middle-end as dynamic stack space is allocated.  */
504 
505 static tree
get_nl_goto_field(struct nesting_info * info)506 get_nl_goto_field (struct nesting_info *info)
507 {
508   tree field = info->nl_goto_field;
509   if (!field)
510     {
511       unsigned size;
512       tree type;
513 
514       /* For __builtin_nonlocal_goto, we need N words.  The first is the
515 	 frame pointer, the rest is for the target's stack pointer save
516 	 area.  The number of words is controlled by STACK_SAVEAREA_MODE;
517 	 not the best interface, but it'll do for now.  */
518       if (Pmode == ptr_mode)
519 	type = ptr_type_node;
520       else
521 	type = lang_hooks.types.type_for_mode (Pmode, 1);
522 
523       size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
524       size = size / GET_MODE_SIZE (Pmode);
525       size = size + 1;
526 
527       type = build_array_type
528 	(type, build_index_type (build_int_cst (NULL_TREE, size)));
529 
530       field = make_node (FIELD_DECL);
531       DECL_NAME (field) = get_identifier ("__nl_goto_buf");
532       TREE_TYPE (field) = type;
533       DECL_ALIGN (field) = TYPE_ALIGN (type);
534       TREE_ADDRESSABLE (field) = 1;
535 
536       insert_field_into_struct (get_frame_type (info), field);
537 
538       info->nl_goto_field = field;
539     }
540 
541   return field;
542 }
543 
544 /* Convenience routines to walk all statements of a gimple function.
545 
546    For each statement, we invoke CALLBACK via walk_tree.  The passed
547    data is a walk_stmt_info structure.  Of note here is a TSI that
548    points to the current statement being walked.  The VAL_ONLY flag
549    that indicates whether the *TP being examined may be replaced
550    with something that matches is_gimple_val (if true) or something
551    slightly more complicated (if false).  "Something" technically
552    means the common subset of is_gimple_lvalue and is_gimple_rhs,
553    but we never try to form anything more complicated than that, so
554    we don't bother checking.  */
555 
556 struct walk_stmt_info
557 {
558   walk_tree_fn callback;
559   tree_stmt_iterator tsi;
560   struct nesting_info *info;
561   bool val_only;
562   bool is_lhs;
563   bool changed;
564 };
565 
566 /* A subroutine of walk_function.  Iterate over all sub-statements of *TP.  */
567 
568 static void
walk_stmts(struct walk_stmt_info * wi,tree * tp)569 walk_stmts (struct walk_stmt_info *wi, tree *tp)
570 {
571   tree t = *tp;
572   if (!t)
573     return;
574 
575   switch (TREE_CODE (t))
576     {
577     case STATEMENT_LIST:
578       {
579 	tree_stmt_iterator i;
580 	for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
581 	  {
582 	    wi->tsi = i;
583 	    walk_stmts (wi, tsi_stmt_ptr (i));
584 	  }
585       }
586       break;
587 
588     case COND_EXPR:
589       walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
590       walk_stmts (wi, &COND_EXPR_THEN (t));
591       walk_stmts (wi, &COND_EXPR_ELSE (t));
592       break;
593     case CATCH_EXPR:
594       walk_stmts (wi, &CATCH_BODY (t));
595       break;
596     case EH_FILTER_EXPR:
597       walk_stmts (wi, &EH_FILTER_FAILURE (t));
598       break;
599     case TRY_CATCH_EXPR:
600     case TRY_FINALLY_EXPR:
601       walk_stmts (wi, &TREE_OPERAND (t, 0));
602       walk_stmts (wi, &TREE_OPERAND (t, 1));
603       break;
604     case BIND_EXPR:
605       walk_stmts (wi, &BIND_EXPR_BODY (t));
606       break;
607 
608     case RETURN_EXPR:
609       walk_stmts (wi, &TREE_OPERAND (t, 0));
610       break;
611 
612     case MODIFY_EXPR:
613       /* A formal temporary lhs may use a COMPONENT_REF rhs.  */
614       wi->val_only = !is_gimple_formal_tmp_var (TREE_OPERAND (t, 0));
615       walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
616 
617       /* If the rhs is appropriate for a memory, we may use a
618 	 COMPONENT_REF on the lhs.  */
619       wi->val_only = !is_gimple_mem_rhs (TREE_OPERAND (t, 1));
620       wi->is_lhs = true;
621       walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
622 
623       wi->val_only = true;
624       wi->is_lhs = false;
625       break;
626 
627     default:
628       wi->val_only = true;
629       walk_tree (tp, wi->callback, wi, NULL);
630       break;
631     }
632 }
633 
634 /* Invoke CALLBACK on all statements of INFO->CONTEXT.  */
635 
636 static void
walk_function(walk_tree_fn callback,struct nesting_info * info)637 walk_function (walk_tree_fn callback, struct nesting_info *info)
638 {
639   struct walk_stmt_info wi;
640 
641   memset (&wi, 0, sizeof (wi));
642   wi.callback = callback;
643   wi.info = info;
644   wi.val_only = true;
645 
646   walk_stmts (&wi, &DECL_SAVED_TREE (info->context));
647 }
648 
649 /* Similarly for ROOT and all functions nested underneath, depth first.  */
650 
651 static void
walk_all_functions(walk_tree_fn callback,struct nesting_info * root)652 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
653 {
654   do
655     {
656       if (root->inner)
657 	walk_all_functions (callback, root->inner);
658       walk_function (callback, root);
659       root = root->next;
660     }
661   while (root);
662 }
663 
664 /* We have to check for a fairly pathological case.  The operands of function
665    nested function are to be interpreted in the context of the enclosing
666    function.  So if any are variably-sized, they will get remapped when the
667    enclosing function is inlined.  But that remapping would also have to be
668    done in the types of the PARM_DECLs of the nested function, meaning the
669    argument types of that function will disagree with the arguments in the
670    calls to that function.  So we'd either have to make a copy of the nested
671    function corresponding to each time the enclosing function was inlined or
672    add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
673    function.  The former is not practical.  The latter would still require
674    detecting this case to know when to add the conversions.  So, for now at
675    least, we don't inline such an enclosing function.
676 
677    We have to do that check recursively, so here return indicating whether
678    FNDECL has such a nested function.  ORIG_FN is the function we were
679    trying to inline to use for checking whether any argument is variably
680    modified by anything in it.
681 
682    It would be better to do this in tree-inline.c so that we could give
683    the appropriate warning for why a function can't be inlined, but that's
684    too late since the nesting structure has already been flattened and
685    adding a flag just to record this fact seems a waste of a flag.  */
686 
687 static bool
check_for_nested_with_variably_modified(tree fndecl,tree orig_fndecl)688 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
689 {
690   struct cgraph_node *cgn = cgraph_node (fndecl);
691   tree arg;
692 
693   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
694     {
695       for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
696 	if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
697 	  return true;
698 
699       if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
700 	return true;
701     }
702 
703   return false;
704 }
705 
706 /* Construct our local datastructure describing the function nesting
707    tree rooted by CGN.  */
708 
709 static struct nesting_info *
create_nesting_tree(struct cgraph_node * cgn)710 create_nesting_tree (struct cgraph_node *cgn)
711 {
712   struct nesting_info *info = ggc_calloc (1, sizeof (*info));
713   info->var_map = htab_create_ggc (7, var_map_hash, var_map_eq, ggc_free);
714   info->context = cgn->decl;
715 
716   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
717     {
718       struct nesting_info *sub = create_nesting_tree (cgn);
719       sub->outer = info;
720       sub->next = info->inner;
721       info->inner = sub;
722     }
723 
724   /* See discussion at check_for_nested_with_variably_modified for a
725      discussion of why this has to be here.  */
726   if (check_for_nested_with_variably_modified (info->context, info->context))
727     DECL_UNINLINABLE (info->context) = true;
728 
729   return info;
730 }
731 
732 /* Return an expression computing the static chain for TARGET_CONTEXT
733    from INFO->CONTEXT.  Insert any necessary computations before TSI.  */
734 
735 static tree
get_static_chain(struct nesting_info * info,tree target_context,tree_stmt_iterator * tsi)736 get_static_chain (struct nesting_info *info, tree target_context,
737 		  tree_stmt_iterator *tsi)
738 {
739   struct nesting_info *i;
740   tree x;
741 
742   if (info->context == target_context)
743     {
744       x = build_addr (info->frame_decl, target_context);
745     }
746   else
747     {
748       x = get_chain_decl (info);
749 
750       for (i = info->outer; i->context != target_context; i = i->outer)
751 	{
752 	  tree field = get_chain_field (i);
753 
754 	  x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
755 	  x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
756 	  x = init_tmp_var (info, x, tsi);
757 	}
758     }
759 
760   return x;
761 }
762 
763 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
764    frame as seen from INFO->CONTEXT.  Insert any necessary computations
765    before TSI.  */
766 
767 static tree
get_frame_field(struct nesting_info * info,tree target_context,tree field,tree_stmt_iterator * tsi)768 get_frame_field (struct nesting_info *info, tree target_context,
769 		 tree field, tree_stmt_iterator *tsi)
770 {
771   struct nesting_info *i;
772   tree x;
773 
774   if (info->context == target_context)
775     {
776       /* Make sure frame_decl gets created.  */
777       (void) get_frame_type (info);
778       x = info->frame_decl;
779     }
780   else
781     {
782       x = get_chain_decl (info);
783 
784       for (i = info->outer; i->context != target_context; i = i->outer)
785 	{
786 	  tree field = get_chain_field (i);
787 
788 	  x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
789 	  x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
790 	  x = init_tmp_var (info, x, tsi);
791 	}
792 
793       x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
794     }
795 
796   x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
797   return x;
798 }
799 
800 /* Called via walk_function+walk_tree, rewrite all references to VAR
801    and PARM_DECLs that belong to outer functions.
802 
803    The rewrite will involve some number of structure accesses back up
804    the static chain.  E.g. for a variable FOO up one nesting level it'll
805    be CHAIN->FOO.  For two levels it'll be CHAIN->__chain->FOO.  Further
806    indirections apply to decls for which use_pointer_in_frame is true.  */
807 
808 static tree
convert_nonlocal_reference(tree * tp,int * walk_subtrees,void * data)809 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
810 {
811   struct walk_stmt_info *wi = data;
812   struct nesting_info *info = wi->info;
813   tree t = *tp;
814 
815   *walk_subtrees = 0;
816   switch (TREE_CODE (t))
817     {
818     case VAR_DECL:
819       /* Non-automatic variables are never processed.  */
820       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
821 	break;
822       /* FALLTHRU */
823 
824     case PARM_DECL:
825       if (decl_function_context (t) != info->context)
826 	{
827 	  tree target_context = decl_function_context (t);
828 	  struct nesting_info *i;
829 	  tree x;
830 	  wi->changed = true;
831 
832 	  for (i = info->outer; i->context != target_context; i = i->outer)
833 	    continue;
834 	  x = lookup_field_for_decl (i, t, INSERT);
835 	  x = get_frame_field (info, target_context, x, &wi->tsi);
836 	  if (use_pointer_in_frame (t))
837 	    {
838 	      x = init_tmp_var (info, x, &wi->tsi);
839 	      x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
840 	    }
841 
842 	  if (wi->val_only)
843 	    {
844 	      if (wi->is_lhs)
845 		x = save_tmp_var (info, x, &wi->tsi);
846 	      else
847 		x = init_tmp_var (info, x, &wi->tsi);
848 	    }
849 
850 	  *tp = x;
851 	}
852       break;
853 
854     case GOTO_EXPR:
855       /* Don't walk non-local gotos for now.  */
856       if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
857 	{
858 	  *walk_subtrees = 1;
859 	  wi->val_only = true;
860 	  wi->is_lhs = false;
861 	}
862       break;
863 
864     case LABEL_DECL:
865       /* We're taking the address of a label from a parent function, but
866 	 this is not itself a non-local goto.  Mark the label such that it
867 	 will not be deleted, much as we would with a label address in
868 	 static storage.  */
869       if (decl_function_context (t) != info->context)
870         FORCED_LABEL (t) = 1;
871       break;
872 
873     case ADDR_EXPR:
874       {
875 	bool save_val_only = wi->val_only;
876 
877 	wi->val_only = false;
878 	wi->is_lhs = false;
879 	wi->changed = false;
880 	walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
881 	wi->val_only = true;
882 
883 	if (wi->changed)
884 	  {
885 	    tree save_context;
886 
887 	    /* If we changed anything, then TREE_INVARIANT is be wrong,
888 	       since we're no longer directly referencing a decl.  */
889 	    save_context = current_function_decl;
890 	    current_function_decl = info->context;
891 	    recompute_tree_invarant_for_addr_expr (t);
892 	    current_function_decl = save_context;
893 
894 	    /* If the callback converted the address argument in a context
895 	       where we only accept variables (and min_invariant, presumably),
896 	       then compute the address into a temporary.  */
897 	    if (save_val_only)
898 	      *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
899 	  }
900       }
901       break;
902 
903     case REALPART_EXPR:
904     case IMAGPART_EXPR:
905     case COMPONENT_REF:
906     case ARRAY_REF:
907     case ARRAY_RANGE_REF:
908     case BIT_FIELD_REF:
909       /* Go down this entire nest and just look at the final prefix and
910 	 anything that describes the references.  Otherwise, we lose track
911 	 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
912       wi->val_only = true;
913       wi->is_lhs = false;
914       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
915 	{
916 	  if (TREE_CODE (t) == COMPONENT_REF)
917 	    walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
918 		       NULL);
919 	  else if (TREE_CODE (t) == ARRAY_REF
920 		   || TREE_CODE (t) == ARRAY_RANGE_REF)
921 	    {
922 	      walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
923 			 NULL);
924 	      walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
925 			 NULL);
926 	      walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
927 			 NULL);
928 	    }
929 	  else if (TREE_CODE (t) == BIT_FIELD_REF)
930 	    {
931 	      walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
932 			 NULL);
933 	      walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
934 			 NULL);
935 	    }
936 	}
937       wi->val_only = false;
938       walk_tree (tp, convert_nonlocal_reference, wi, NULL);
939       break;
940 
941     default:
942       if (!IS_TYPE_OR_DECL_P (t))
943 	{
944 	  *walk_subtrees = 1;
945           wi->val_only = true;
946 	  wi->is_lhs = false;
947 	}
948       break;
949     }
950 
951   return NULL_TREE;
952 }
953 
954 /* Called via walk_function+walk_tree, rewrite all references to VAR
955    and PARM_DECLs that were referenced by inner nested functions.
956    The rewrite will be a structure reference to the local frame variable.  */
957 
958 static tree
convert_local_reference(tree * tp,int * walk_subtrees,void * data)959 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
960 {
961   struct walk_stmt_info *wi = data;
962   struct nesting_info *info = wi->info;
963   tree t = *tp, field, x;
964   bool save_val_only;
965 
966   *walk_subtrees = 0;
967   switch (TREE_CODE (t))
968     {
969     case VAR_DECL:
970       /* Non-automatic variables are never processed.  */
971       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
972 	break;
973       /* FALLTHRU */
974 
975     case PARM_DECL:
976       if (decl_function_context (t) == info->context)
977 	{
978 	  /* If we copied a pointer to the frame, then the original decl
979 	     is used unchanged in the parent function.  */
980 	  if (use_pointer_in_frame (t))
981 	    break;
982 
983 	  /* No need to transform anything if no child references the
984 	     variable.  */
985 	  field = lookup_field_for_decl (info, t, NO_INSERT);
986 	  if (!field)
987 	    break;
988 	  wi->changed = true;
989 
990 	  x = get_frame_field (info, info->context, field, &wi->tsi);
991 
992 	  if (wi->val_only)
993 	    {
994 	      if (wi->is_lhs)
995 		x = save_tmp_var (info, x, &wi->tsi);
996 	      else
997 		x = init_tmp_var (info, x, &wi->tsi);
998 	    }
999 
1000 	  *tp = x;
1001 	}
1002       break;
1003 
1004     case ADDR_EXPR:
1005       save_val_only = wi->val_only;
1006       wi->val_only = false;
1007       wi->is_lhs = false;
1008       wi->changed = false;
1009       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
1010       wi->val_only = save_val_only;
1011 
1012       /* If we converted anything ... */
1013       if (wi->changed)
1014 	{
1015 	  tree save_context;
1016 
1017 	  /* Then the frame decl is now addressable.  */
1018 	  TREE_ADDRESSABLE (info->frame_decl) = 1;
1019 
1020 	  save_context = current_function_decl;
1021 	  current_function_decl = info->context;
1022 	  recompute_tree_invarant_for_addr_expr (t);
1023 	  current_function_decl = save_context;
1024 
1025 	  /* If we are in a context where we only accept values, then
1026 	     compute the address into a temporary.  */
1027 	  if (save_val_only)
1028 	    *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1029 	}
1030       break;
1031 
1032     case REALPART_EXPR:
1033     case IMAGPART_EXPR:
1034     case COMPONENT_REF:
1035     case ARRAY_REF:
1036     case ARRAY_RANGE_REF:
1037     case BIT_FIELD_REF:
1038       /* Go down this entire nest and just look at the final prefix and
1039 	 anything that describes the references.  Otherwise, we lose track
1040 	 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
1041       save_val_only = wi->val_only;
1042       wi->val_only = true;
1043       wi->is_lhs = false;
1044       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1045 	{
1046 	  if (TREE_CODE (t) == COMPONENT_REF)
1047 	    walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1048 		       NULL);
1049 	  else if (TREE_CODE (t) == ARRAY_REF
1050 		   || TREE_CODE (t) == ARRAY_RANGE_REF)
1051 	    {
1052 	      walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1053 			 NULL);
1054 	      walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1055 			 NULL);
1056 	      walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1057 			 NULL);
1058 	    }
1059 	  else if (TREE_CODE (t) == BIT_FIELD_REF)
1060 	    {
1061 	      walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1062 			 NULL);
1063 	      walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1064 			 NULL);
1065 	    }
1066 	}
1067       wi->val_only = false;
1068       walk_tree (tp, convert_local_reference, wi, NULL);
1069       wi->val_only = save_val_only;
1070       break;
1071 
1072     default:
1073       if (!IS_TYPE_OR_DECL_P (t))
1074 	{
1075 	  *walk_subtrees = 1;
1076 	  wi->val_only = true;
1077 	  wi->is_lhs = false;
1078 	}
1079       break;
1080     }
1081 
1082   return NULL_TREE;
1083 }
1084 
1085 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1086    reference labels from outer functions.  The rewrite will be a
1087    call to __builtin_nonlocal_goto.  */
1088 
1089 static tree
convert_nl_goto_reference(tree * tp,int * walk_subtrees,void * data)1090 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1091 {
1092   struct walk_stmt_info *wi = data;
1093   struct nesting_info *info = wi->info, *i;
1094   tree t = *tp, label, new_label, target_context, x, arg, field;
1095   struct var_map_elt *elt, dummy;
1096   void **slot;
1097 
1098   *walk_subtrees = 0;
1099   if (TREE_CODE (t) != GOTO_EXPR)
1100     return NULL_TREE;
1101   label = GOTO_DESTINATION (t);
1102   if (TREE_CODE (label) != LABEL_DECL)
1103     return NULL_TREE;
1104   target_context = decl_function_context (label);
1105   if (target_context == info->context)
1106     return NULL_TREE;
1107 
1108   for (i = info->outer; target_context != i->context; i = i->outer)
1109     continue;
1110 
1111   /* The original user label may also be use for a normal goto, therefore
1112      we must create a new label that will actually receive the abnormal
1113      control transfer.  This new label will be marked LABEL_NONLOCAL; this
1114      mark will trigger proper behavior in the cfg, as well as cause the
1115      (hairy target-specific) non-local goto receiver code to be generated
1116      when we expand rtl.  Enter this association into var_map so that we
1117      can insert the new label into the IL during a second pass.  */
1118   dummy.old = label;
1119   slot = htab_find_slot (i->var_map, &dummy, INSERT);
1120   elt = *slot;
1121   if (elt == NULL)
1122     {
1123       new_label = create_artificial_label ();
1124       DECL_NONLOCAL (new_label) = 1;
1125 
1126       elt = ggc_alloc (sizeof (*elt));
1127       elt->old = label;
1128       elt->new = new_label;
1129       *slot = elt;
1130     }
1131   else
1132     new_label = elt->new;
1133 
1134   /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field).  */
1135   field = get_nl_goto_field (i);
1136   x = get_frame_field (info, target_context, field, &wi->tsi);
1137   x = build_addr (x, target_context);
1138   x = tsi_gimplify_val (info, x, &wi->tsi);
1139   arg = tree_cons (NULL, x, NULL);
1140   x = build_addr (new_label, target_context);
1141   arg = tree_cons (NULL, x, arg);
1142   x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1143   x = build_function_call_expr (x, arg);
1144 
1145   SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1146   *tsi_stmt_ptr (wi->tsi) = x;
1147 
1148   return NULL_TREE;
1149 }
1150 
1151 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1152    are referenced via nonlocal goto from a nested function.  The rewrite
1153    will involve installing a newly generated DECL_NONLOCAL label, and
1154    (potentially) a branch around the rtl gunk that is assumed to be
1155    attached to such a label.  */
1156 
1157 static tree
convert_nl_goto_receiver(tree * tp,int * walk_subtrees,void * data)1158 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1159 {
1160   struct walk_stmt_info *wi = data;
1161   struct nesting_info *info = wi->info;
1162   tree t = *tp, label, new_label, x;
1163   struct var_map_elt *elt, dummy;
1164   tree_stmt_iterator tmp_tsi;
1165 
1166   *walk_subtrees = 0;
1167   if (TREE_CODE (t) != LABEL_EXPR)
1168     return NULL_TREE;
1169   label = LABEL_EXPR_LABEL (t);
1170 
1171   dummy.old = label;
1172   elt = htab_find (info->var_map, &dummy);
1173   if (!elt)
1174     return NULL_TREE;
1175   new_label = elt->new;
1176 
1177   /* If there's any possibility that the previous statement falls through,
1178      then we must branch around the new non-local label.  */
1179   tmp_tsi = wi->tsi;
1180   tsi_prev (&tmp_tsi);
1181   if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1182     {
1183       x = build1 (GOTO_EXPR, void_type_node, label);
1184       tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1185     }
1186   x = build1 (LABEL_EXPR, void_type_node, new_label);
1187   tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1188 
1189   return NULL_TREE;
1190 }
1191 
1192 /* Called via walk_function+walk_tree, rewrite all references to addresses
1193    of nested functions that require the use of trampolines.  The rewrite
1194    will involve a reference a trampoline generated for the occasion.  */
1195 
1196 static tree
convert_tramp_reference(tree * tp,int * walk_subtrees,void * data)1197 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1198 {
1199   struct walk_stmt_info *wi = data;
1200   struct nesting_info *info = wi->info, *i;
1201   tree t = *tp, decl, target_context, x, arg;
1202 
1203   *walk_subtrees = 0;
1204   switch (TREE_CODE (t))
1205     {
1206     case ADDR_EXPR:
1207       /* Build
1208 	   T.1 = &CHAIN->tramp;
1209 	   T.2 = __builtin_adjust_trampoline (T.1);
1210 	   T.3 = (func_type)T.2;
1211       */
1212 
1213       decl = TREE_OPERAND (t, 0);
1214       if (TREE_CODE (decl) != FUNCTION_DECL)
1215 	break;
1216 
1217       /* Only need to process nested functions.  */
1218       target_context = decl_function_context (decl);
1219       if (!target_context)
1220 	break;
1221 
1222       /* If the nested function doesn't use a static chain, then
1223 	 it doesn't need a trampoline.  */
1224       if (DECL_NO_STATIC_CHAIN (decl))
1225 	break;
1226 
1227       /* Lookup the immediate parent of the callee, as that's where
1228 	 we need to insert the trampoline.  */
1229       for (i = info; i->context != target_context; i = i->outer)
1230 	continue;
1231       x = lookup_tramp_for_decl (i, decl, INSERT);
1232 
1233       /* Compute the address of the field holding the trampoline.  */
1234       x = get_frame_field (info, target_context, x, &wi->tsi);
1235       x = build_addr (x, target_context);
1236       x = tsi_gimplify_val (info, x, &wi->tsi);
1237       arg = tree_cons (NULL, x, NULL);
1238 
1239       /* Do machine-specific ugliness.  Normally this will involve
1240 	 computing extra alignment, but it can really be anything.  */
1241       x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1242       x = build_function_call_expr (x, arg);
1243       x = init_tmp_var (info, x, &wi->tsi);
1244 
1245       /* Cast back to the proper function type.  */
1246       x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1247       x = init_tmp_var (info, x, &wi->tsi);
1248 
1249       *tp = x;
1250       break;
1251 
1252     case CALL_EXPR:
1253       /* Only walk call arguments, lest we generate trampolines for
1254 	 direct calls.  */
1255       walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1256       break;
1257 
1258     default:
1259       if (!IS_TYPE_OR_DECL_P (t))
1260 	*walk_subtrees = 1;
1261       break;
1262     }
1263 
1264   return NULL_TREE;
1265 }
1266 
1267 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1268    reference nested functions to make sure that the static chain is
1269    set up properly for the call.  */
1270 
1271 static tree
convert_call_expr(tree * tp,int * walk_subtrees,void * data)1272 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1273 {
1274   struct walk_stmt_info *wi = data;
1275   struct nesting_info *info = wi->info;
1276   tree t = *tp, decl, target_context;
1277 
1278   *walk_subtrees = 0;
1279   switch (TREE_CODE (t))
1280     {
1281     case CALL_EXPR:
1282       decl = get_callee_fndecl (t);
1283       if (!decl)
1284 	break;
1285       target_context = decl_function_context (decl);
1286       if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1287 	TREE_OPERAND (t, 2)
1288 	  = get_static_chain (info, target_context, &wi->tsi);
1289       break;
1290 
1291     case RETURN_EXPR:
1292     case MODIFY_EXPR:
1293     case WITH_SIZE_EXPR:
1294       /* Only return modify and with_size_expr may contain calls.  */
1295       *walk_subtrees = 1;
1296       break;
1297 
1298     default:
1299       break;
1300     }
1301 
1302   return NULL_TREE;
1303 }
1304 
1305 /* Walk the nesting tree starting with ROOT, depth first.  Convert all
1306    trampolines and call expressions.  On the way back up, determine if
1307    a nested function actually uses its static chain; if not, remember that.  */
1308 
1309 static void
convert_all_function_calls(struct nesting_info * root)1310 convert_all_function_calls (struct nesting_info *root)
1311 {
1312   do
1313     {
1314       if (root->inner)
1315 	convert_all_function_calls (root->inner);
1316 
1317       walk_function (convert_tramp_reference, root);
1318       walk_function (convert_call_expr, root);
1319 
1320       /* If the function does not use a static chain, then remember that.  */
1321       if (root->outer && !root->chain_decl && !root->chain_field)
1322 	DECL_NO_STATIC_CHAIN (root->context) = 1;
1323       else
1324 	gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1325 
1326       root = root->next;
1327     }
1328   while (root);
1329 }
1330 
1331 /* Do "everything else" to clean up or complete state collected by the
1332    various walking passes -- lay out the types and decls, generate code
1333    to initialize the frame decl, store critical expressions in the
1334    struct function for rtl to find.  */
1335 
1336 static void
finalize_nesting_tree_1(struct nesting_info * root)1337 finalize_nesting_tree_1 (struct nesting_info *root)
1338 {
1339   tree stmt_list = NULL;
1340   tree context = root->context;
1341   struct function *sf;
1342   struct cgraph_node *node;
1343 
1344   /* If we created a non-local frame type or decl, we need to lay them
1345      out at this time.  */
1346   if (root->frame_type)
1347     {
1348       /* In some cases the frame type will trigger the -Wpadded warning.
1349 	 This is not helpful; suppress it. */
1350       int save_warn_padded = warn_padded;
1351       warn_padded = 0;
1352       layout_type (root->frame_type);
1353       warn_padded = save_warn_padded;
1354       layout_decl (root->frame_decl, 0);
1355     }
1356 
1357   /* If any parameters were referenced non-locally, then we need to
1358      insert a copy.  Likewise, if any variables were referenced by
1359      pointer, we need to initialize the address.  */
1360   if (root->any_parm_remapped)
1361     {
1362       tree p;
1363       for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1364 	{
1365 	  tree field, x, y;
1366 
1367 	  field = lookup_field_for_decl (root, p, NO_INSERT);
1368 	  if (!field)
1369 	    continue;
1370 
1371 	  if (use_pointer_in_frame (p))
1372 	    x = build_addr (p, context);
1373 	  else
1374 	    x = p;
1375 
1376 	  y = build (COMPONENT_REF, TREE_TYPE (field),
1377 		     root->frame_decl, field, NULL_TREE);
1378 	  x = build (MODIFY_EXPR, TREE_TYPE (field), y, x);
1379 	  append_to_statement_list (x, &stmt_list);
1380 	}
1381     }
1382 
1383   /* If a chain_field was created, then it needs to be initialized
1384      from chain_decl.  */
1385   if (root->chain_field)
1386     {
1387       tree x = build (COMPONENT_REF, TREE_TYPE (root->chain_field),
1388 		      root->frame_decl, root->chain_field, NULL_TREE);
1389       x = build (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1390       append_to_statement_list (x, &stmt_list);
1391     }
1392 
1393   /* If trampolines were created, then we need to initialize them.  */
1394   if (root->any_tramp_created)
1395     {
1396       struct nesting_info *i;
1397       for (i = root->inner; i ; i = i->next)
1398 	{
1399 	  tree arg, x, field;
1400 
1401 	  field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1402 	  if (!field)
1403 	    continue;
1404 
1405 	  if (DECL_NO_STATIC_CHAIN (i->context))
1406 	    x = null_pointer_node;
1407 	  else
1408 	    x = build_addr (root->frame_decl, context);
1409 	  arg = tree_cons (NULL, x, NULL);
1410 
1411 	  x = build_addr (i->context, context);
1412 	  arg = tree_cons (NULL, x, arg);
1413 
1414 	  x = build (COMPONENT_REF, TREE_TYPE (field),
1415 		     root->frame_decl, field, NULL_TREE);
1416 	  x = build_addr (x, context);
1417 	  arg = tree_cons (NULL, x, arg);
1418 
1419 	  x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1420 	  x = build_function_call_expr (x, arg);
1421 
1422 	  append_to_statement_list (x, &stmt_list);
1423 	}
1424     }
1425 
1426   /* If we created initialization statements, insert them.  */
1427   if (stmt_list)
1428     {
1429       annotate_all_with_locus (&stmt_list,
1430 			       DECL_SOURCE_LOCATION (context));
1431       append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1432 				&stmt_list);
1433       BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1434     }
1435 
1436   /* If a chain_decl was created, then it needs to be registered with
1437      struct function so that it gets initialized from the static chain
1438      register at the beginning of the function.  */
1439   sf = DECL_STRUCT_FUNCTION (root->context);
1440   sf->static_chain_decl = root->chain_decl;
1441 
1442   /* Similarly for the non-local goto save area.  */
1443   if (root->nl_goto_field)
1444     {
1445       sf->nonlocal_goto_save_area
1446 	= get_frame_field (root, context, root->nl_goto_field, NULL);
1447       sf->has_nonlocal_label = 1;
1448     }
1449 
1450   /* Make sure all new local variables get inserted into the
1451      proper BIND_EXPR.  */
1452   if (root->new_local_var_chain)
1453     declare_tmp_vars (root->new_local_var_chain,
1454 		      DECL_SAVED_TREE (root->context));
1455 
1456   /* Dump the translated tree function.  */
1457   dump_function (TDI_nested, root->context);
1458   node = cgraph_node (root->context);
1459 
1460   /* For nested functions update the cgraph to reflect unnesting.
1461      We also delay finalizing of these functions up to this point.  */
1462   if (node->origin)
1463     {
1464        cgraph_unnest_node (cgraph_node (root->context));
1465        cgraph_finalize_function (root->context, true);
1466     }
1467 }
1468 
1469 static void
finalize_nesting_tree(struct nesting_info * root)1470 finalize_nesting_tree (struct nesting_info *root)
1471 {
1472   do
1473     {
1474       if (root->inner)
1475 	finalize_nesting_tree (root->inner);
1476       finalize_nesting_tree_1 (root);
1477       root = root->next;
1478     }
1479   while (root);
1480 }
1481 
1482 /* Free the data structures allocated during this pass.  */
1483 
1484 static void
free_nesting_tree(struct nesting_info * root)1485 free_nesting_tree (struct nesting_info *root)
1486 {
1487   struct nesting_info *next;
1488   do
1489     {
1490       if (root->inner)
1491 	free_nesting_tree (root->inner);
1492       htab_delete (root->var_map);
1493       next = root->next;
1494       ggc_free (root);
1495       root = next;
1496     }
1497   while (root);
1498 }
1499 
1500 static GTY(()) struct nesting_info *root;
1501 
1502 /* Main entry point for this pass.  Process FNDECL and all of its nested
1503    subroutines and turn them into something less tightly bound.  */
1504 
1505 void
lower_nested_functions(tree fndecl)1506 lower_nested_functions (tree fndecl)
1507 {
1508   struct cgraph_node *cgn;
1509 
1510   /* If there are no nested functions, there's nothing to do.  */
1511   cgn = cgraph_node (fndecl);
1512   if (!cgn->nested)
1513     return;
1514 
1515   root = create_nesting_tree (cgn);
1516   walk_all_functions (convert_nonlocal_reference, root);
1517   walk_all_functions (convert_local_reference, root);
1518   walk_all_functions (convert_nl_goto_reference, root);
1519   walk_all_functions (convert_nl_goto_receiver, root);
1520   convert_all_function_calls (root);
1521   finalize_nesting_tree (root);
1522   free_nesting_tree (root);
1523   root = NULL;
1524 }
1525 
1526 #include "gt-tree-nested.h"
1527