* c-parser.c (c_parser_lex_all): Don't enforce location constraint
[official-gcc.git] / gcc / tree-nested.c
blobca0d4a1435811b79db0009b51e148c55ba56c701
1 /* Nested function decomposition for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "function.h"
28 #include "tree-dump.h"
29 #include "tree-inline.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-flow.h"
33 #include "cgraph.h"
34 #include "expr.h"
35 #include "langhooks.h"
36 #include "pointer-set.h"
37 #include "ggc.h"
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.
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.
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.
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.
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. */
80 struct nesting_info
82 struct nesting_info *outer;
83 struct nesting_info *inner;
84 struct nesting_info *next;
86 struct pointer_map_t *field_map;
87 struct pointer_map_t *var_map;
88 bitmap suppress_expansion;
90 tree context;
91 tree new_local_var_chain;
92 tree debug_var_chain;
93 tree frame_type;
94 tree frame_decl;
95 tree chain_field;
96 tree chain_decl;
97 tree nl_goto_field;
99 bool any_parm_remapped;
100 bool any_tramp_created;
101 char static_chain_added;
105 /* Obstack used for the bitmaps in the struct above. */
106 static struct bitmap_obstack nesting_info_bitmap_obstack;
109 /* We're working in so many different function contexts simultaneously,
110 that create_tmp_var is dangerous. Prevent mishap. */
111 #define create_tmp_var cant_use_create_tmp_var_here_dummy
113 /* Like create_tmp_var, except record the variable for registration at
114 the given nesting level. */
116 static tree
117 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
119 tree tmp_var;
121 /* If the type is of variable size or a type which must be created by the
122 frontend, something is wrong. Note that we explicitly allow
123 incomplete types here, since we create them ourselves here. */
124 gcc_assert (!TREE_ADDRESSABLE (type));
125 gcc_assert (!TYPE_SIZE_UNIT (type)
126 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
128 tmp_var = create_tmp_var_raw (type, prefix);
129 DECL_CONTEXT (tmp_var) = info->context;
130 TREE_CHAIN (tmp_var) = info->new_local_var_chain;
131 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
132 if (TREE_CODE (type) == COMPLEX_TYPE
133 || TREE_CODE (type) == VECTOR_TYPE)
134 DECL_GIMPLE_REG_P (tmp_var) = 1;
136 info->new_local_var_chain = tmp_var;
138 return tmp_var;
141 /* Take the address of EXP to be used within function CONTEXT.
142 Mark it for addressability as necessary. */
144 tree
145 build_addr (tree exp, tree context)
147 tree base = exp;
148 tree save_context;
149 tree retval;
151 while (handled_component_p (base))
152 base = TREE_OPERAND (base, 0);
154 if (DECL_P (base))
155 TREE_ADDRESSABLE (base) = 1;
157 /* Building the ADDR_EXPR will compute a set of properties for
158 that ADDR_EXPR. Those properties are unfortunately context
159 specific. ie, they are dependent on CURRENT_FUNCTION_DECL.
161 Temporarily set CURRENT_FUNCTION_DECL to the desired context,
162 build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL. That
163 way the properties are for the ADDR_EXPR are computed properly. */
164 save_context = current_function_decl;
165 current_function_decl = context;
166 retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
167 current_function_decl = save_context;
168 return retval;
171 /* Insert FIELD into TYPE, sorted by alignment requirements. */
173 void
174 insert_field_into_struct (tree type, tree field)
176 tree *p;
178 DECL_CONTEXT (field) = type;
180 for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
181 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
182 break;
184 TREE_CHAIN (field) = *p;
185 *p = field;
187 /* Set correct alignment for frame struct type. */
188 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
189 TYPE_ALIGN (type) = DECL_ALIGN (field);
192 /* Build or return the RECORD_TYPE that describes the frame state that is
193 shared between INFO->CONTEXT and its nested functions. This record will
194 not be complete until finalize_nesting_tree; up until that point we'll
195 be adding fields as necessary.
197 We also build the DECL that represents this frame in the function. */
199 static tree
200 get_frame_type (struct nesting_info *info)
202 tree type = info->frame_type;
203 if (!type)
205 char *name;
207 type = make_node (RECORD_TYPE);
209 name = concat ("FRAME.",
210 IDENTIFIER_POINTER (DECL_NAME (info->context)),
211 NULL);
212 TYPE_NAME (type) = get_identifier (name);
213 free (name);
215 info->frame_type = type;
216 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
218 /* ??? Always make it addressable for now, since it is meant to
219 be pointed to by the static chain pointer. This pessimizes
220 when it turns out that no static chains are needed because
221 the nested functions referencing non-local variables are not
222 reachable, but the true pessimization is to create the non-
223 local frame structure in the first place. */
224 TREE_ADDRESSABLE (info->frame_decl) = 1;
226 return type;
229 /* Return true if DECL should be referenced by pointer in the non-local
230 frame structure. */
232 static bool
233 use_pointer_in_frame (tree decl)
235 if (TREE_CODE (decl) == PARM_DECL)
237 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
238 sized decls, and inefficient to copy large aggregates. Don't bother
239 moving anything but scalar variables. */
240 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
242 else
244 /* Variable sized types make things "interesting" in the frame. */
245 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
249 /* Given DECL, a non-locally accessed variable, find or create a field
250 in the non-local frame structure for the given nesting context. */
252 static tree
253 lookup_field_for_decl (struct nesting_info *info, tree decl,
254 enum insert_option insert)
256 void **slot;
258 if (insert == NO_INSERT)
260 slot = pointer_map_contains (info->field_map, decl);
261 return slot ? *slot : NULL;
264 slot = pointer_map_insert (info->field_map, decl);
265 if (!*slot)
267 tree field = make_node (FIELD_DECL);
268 DECL_NAME (field) = DECL_NAME (decl);
270 if (use_pointer_in_frame (decl))
272 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
273 DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
274 DECL_NONADDRESSABLE_P (field) = 1;
276 else
278 TREE_TYPE (field) = TREE_TYPE (decl);
279 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
280 DECL_ALIGN (field) = DECL_ALIGN (decl);
281 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
282 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
283 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
284 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
287 insert_field_into_struct (get_frame_type (info), field);
288 *slot = field;
290 if (TREE_CODE (decl) == PARM_DECL)
291 info->any_parm_remapped = true;
294 return *slot;
297 /* Build or return the variable that holds the static chain within
298 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
300 static tree
301 get_chain_decl (struct nesting_info *info)
303 tree decl = info->chain_decl;
304 if (!decl)
306 tree type;
308 type = get_frame_type (info->outer);
309 type = build_pointer_type (type);
311 /* Note that this variable is *not* entered into any BIND_EXPR;
312 the construction of this variable is handled specially in
313 expand_function_start and initialize_inlined_parameters.
314 Note also that it's represented as a parameter. This is more
315 close to the truth, since the initial value does come from
316 the caller. */
317 decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
318 DECL_ARTIFICIAL (decl) = 1;
319 DECL_IGNORED_P (decl) = 1;
320 TREE_USED (decl) = 1;
321 DECL_CONTEXT (decl) = info->context;
322 DECL_ARG_TYPE (decl) = type;
324 /* Tell tree-inline.c that we never write to this variable, so
325 it can copy-prop the replacement value immediately. */
326 TREE_READONLY (decl) = 1;
328 info->chain_decl = decl;
330 return decl;
333 /* Build or return the field within the non-local frame state that holds
334 the static chain for INFO->CONTEXT. This is the way to walk back up
335 multiple nesting levels. */
337 static tree
338 get_chain_field (struct nesting_info *info)
340 tree field = info->chain_field;
341 if (!field)
343 tree type = build_pointer_type (get_frame_type (info->outer));
345 field = make_node (FIELD_DECL);
346 DECL_NAME (field) = get_identifier ("__chain");
347 TREE_TYPE (field) = type;
348 DECL_ALIGN (field) = TYPE_ALIGN (type);
349 DECL_NONADDRESSABLE_P (field) = 1;
351 insert_field_into_struct (get_frame_type (info), field);
353 info->chain_field = field;
355 return field;
358 /* Copy EXP into a temporary. Allocate the temporary in the context of
359 INFO and insert the initialization statement before TSI. */
361 static tree
362 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
364 tree t, stmt;
366 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
367 stmt = build_gimple_modify_stmt (t, exp);
368 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
369 tsi_link_before (tsi, stmt, TSI_SAME_STMT);
371 return t;
374 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
376 static tree
377 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
379 if (is_gimple_val (exp))
380 return exp;
381 else
382 return init_tmp_var (info, exp, tsi);
385 /* Similarly, but copy from the temporary and insert the statement
386 after the iterator. */
388 static tree
389 save_tmp_var (struct nesting_info *info, tree exp,
390 tree_stmt_iterator *tsi)
392 tree t, stmt;
394 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
395 stmt = build_gimple_modify_stmt (exp, t);
396 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
397 tsi_link_after (tsi, stmt, TSI_SAME_STMT);
399 return t;
402 /* Build or return the type used to represent a nested function trampoline. */
404 static GTY(()) tree trampoline_type;
406 static tree
407 get_trampoline_type (void)
409 unsigned align, size;
410 tree t;
412 if (trampoline_type)
413 return trampoline_type;
415 align = TRAMPOLINE_ALIGNMENT;
416 size = TRAMPOLINE_SIZE;
418 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
419 then allocate extra space so that we can do dynamic alignment. */
420 if (align > STACK_BOUNDARY)
422 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
423 align = STACK_BOUNDARY;
426 t = build_index_type (build_int_cst (NULL_TREE, size - 1));
427 t = build_array_type (char_type_node, t);
428 t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
429 DECL_ALIGN (t) = align;
430 DECL_USER_ALIGN (t) = 1;
432 trampoline_type = make_node (RECORD_TYPE);
433 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
434 TYPE_FIELDS (trampoline_type) = t;
435 layout_type (trampoline_type);
437 return trampoline_type;
440 /* Given DECL, a nested function, find or create a field in the non-local
441 frame structure for a trampoline for this function. */
443 static tree
444 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
445 enum insert_option insert)
447 void **slot;
449 if (insert == NO_INSERT)
451 slot = pointer_map_contains (info->var_map, decl);
452 return slot ? *slot : NULL;
455 slot = pointer_map_insert (info->var_map, decl);
456 if (!*slot)
458 tree field = make_node (FIELD_DECL);
459 DECL_NAME (field) = DECL_NAME (decl);
460 TREE_TYPE (field) = get_trampoline_type ();
461 TREE_ADDRESSABLE (field) = 1;
463 insert_field_into_struct (get_frame_type (info), field);
464 *slot = field;
466 info->any_tramp_created = true;
469 return *slot;
472 /* Build or return the field within the non-local frame state that holds
473 the non-local goto "jmp_buf". The buffer itself is maintained by the
474 rtl middle-end as dynamic stack space is allocated. */
476 static tree
477 get_nl_goto_field (struct nesting_info *info)
479 tree field = info->nl_goto_field;
480 if (!field)
482 unsigned size;
483 tree type;
485 /* For __builtin_nonlocal_goto, we need N words. The first is the
486 frame pointer, the rest is for the target's stack pointer save
487 area. The number of words is controlled by STACK_SAVEAREA_MODE;
488 not the best interface, but it'll do for now. */
489 if (Pmode == ptr_mode)
490 type = ptr_type_node;
491 else
492 type = lang_hooks.types.type_for_mode (Pmode, 1);
494 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
495 size = size / GET_MODE_SIZE (Pmode);
496 size = size + 1;
498 type = build_array_type
499 (type, build_index_type (build_int_cst (NULL_TREE, size)));
501 field = make_node (FIELD_DECL);
502 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
503 TREE_TYPE (field) = type;
504 DECL_ALIGN (field) = TYPE_ALIGN (type);
505 TREE_ADDRESSABLE (field) = 1;
507 insert_field_into_struct (get_frame_type (info), field);
509 info->nl_goto_field = field;
512 return field;
515 /* Helper function for walk_stmts. Walk output operands of an ASM_EXPR. */
517 static void
518 walk_asm_expr (struct walk_stmt_info *wi, tree stmt)
520 int noutputs = list_length (ASM_OUTPUTS (stmt));
521 const char **oconstraints
522 = (const char **) alloca ((noutputs) * sizeof (const char *));
523 int i;
524 tree link;
525 const char *constraint;
526 bool allows_mem, allows_reg, is_inout;
528 wi->is_lhs = true;
529 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
531 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
532 oconstraints[i] = constraint;
533 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
534 &allows_reg, &is_inout);
536 wi->val_only = (allows_reg || !allows_mem);
537 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
540 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
542 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
543 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
544 oconstraints, &allows_mem, &allows_reg);
546 wi->val_only = (allows_reg || !allows_mem);
547 /* Although input "m" is not really a LHS, we need a lvalue. */
548 wi->is_lhs = !wi->val_only;
549 walk_tree (&TREE_VALUE (link), wi->callback, wi, NULL);
552 wi->is_lhs = false;
553 wi->val_only = true;
556 /* Iterate over all sub-statements of *TP calling walk_tree with
557 WI->CALLBACK for every sub-expression in each statement found. */
559 void
560 walk_stmts (struct walk_stmt_info *wi, tree *tp)
562 tree t = *tp;
563 int walk_subtrees;
565 if (!t)
566 return;
568 if (wi->want_locations && EXPR_HAS_LOCATION (t))
569 input_location = EXPR_LOCATION (t);
571 switch (TREE_CODE (t))
573 case STATEMENT_LIST:
575 tree_stmt_iterator i;
576 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
578 wi->tsi = i;
579 walk_stmts (wi, tsi_stmt_ptr (i));
582 break;
584 case COND_EXPR:
585 walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
586 walk_stmts (wi, &COND_EXPR_THEN (t));
587 walk_stmts (wi, &COND_EXPR_ELSE (t));
588 break;
589 case CATCH_EXPR:
590 walk_stmts (wi, &CATCH_BODY (t));
591 break;
592 case EH_FILTER_EXPR:
593 walk_stmts (wi, &EH_FILTER_FAILURE (t));
594 break;
595 case TRY_CATCH_EXPR:
596 case TRY_FINALLY_EXPR:
597 walk_stmts (wi, &TREE_OPERAND (t, 0));
598 walk_stmts (wi, &TREE_OPERAND (t, 1));
599 break;
601 case BIND_EXPR:
602 if (wi->want_bind_expr)
604 walk_subtrees = 1;
605 wi->callback (tp, &walk_subtrees, wi);
606 if (!walk_subtrees)
607 break;
609 walk_stmts (wi, &BIND_EXPR_BODY (t));
610 break;
612 case RETURN_EXPR:
613 if (wi->want_return_expr)
615 walk_subtrees = 1;
616 wi->callback (tp, &walk_subtrees, wi);
617 if (!walk_subtrees)
618 break;
620 walk_stmts (wi, &TREE_OPERAND (t, 0));
621 break;
623 case GIMPLE_MODIFY_STMT:
624 /* A formal temporary lhs may use a COMPONENT_REF rhs. */
625 wi->val_only = !is_gimple_formal_tmp_var (GIMPLE_STMT_OPERAND (t, 0));
626 walk_tree (&GIMPLE_STMT_OPERAND (t, 1), wi->callback, wi, NULL);
628 /* If the rhs is appropriate for a memory, we may use a
629 COMPONENT_REF on the lhs. */
630 wi->val_only = !is_gimple_mem_rhs (GIMPLE_STMT_OPERAND (t, 1));
631 wi->is_lhs = true;
632 walk_tree (&GIMPLE_STMT_OPERAND (t, 0), wi->callback, wi, NULL);
634 wi->val_only = true;
635 wi->is_lhs = false;
636 break;
638 case ASM_EXPR:
639 walk_asm_expr (wi, *tp);
640 break;
642 default:
643 wi->val_only = true;
644 walk_tree (tp, wi->callback, wi, NULL);
645 break;
649 /* Invoke CALLBACK on all statements of *STMT_P. */
651 static void
652 walk_body (walk_tree_fn callback, struct nesting_info *info, tree *stmt_p)
654 struct walk_stmt_info wi;
656 memset (&wi, 0, sizeof (wi));
657 wi.callback = callback;
658 wi.info = info;
659 wi.val_only = true;
661 walk_stmts (&wi, stmt_p);
664 /* Invoke CALLBACK on all statements of INFO->CONTEXT. */
666 static inline void
667 walk_function (walk_tree_fn callback, struct nesting_info *info)
669 walk_body (callback, info, &DECL_SAVED_TREE (info->context));
672 /* Invoke CALLBACK on OMP_FOR init, cond, incr and pre-body. */
674 static void
675 walk_omp_for (walk_tree_fn callback, struct nesting_info *info, tree for_stmt)
677 struct walk_stmt_info wi;
678 tree t, list = NULL, empty;
680 walk_body (callback, info, &OMP_FOR_PRE_BODY (for_stmt));
682 empty = build_empty_stmt ();
683 append_to_statement_list_force (empty, &list);
684 memset (&wi, 0, sizeof (wi));
685 wi.callback = callback;
686 wi.info = info;
687 wi.tsi = tsi_last (list);
689 t = OMP_FOR_INIT (for_stmt);
690 gcc_assert (TREE_CODE (t) == GIMPLE_MODIFY_STMT);
691 SET_EXPR_LOCUS (empty, EXPR_LOCUS (t));
692 wi.val_only = false;
693 walk_tree (&GIMPLE_STMT_OPERAND (t, 0), callback, &wi, NULL);
694 wi.val_only = true;
695 wi.is_lhs = false;
696 walk_tree (&GIMPLE_STMT_OPERAND (t, 1), callback, &wi, NULL);
698 t = OMP_FOR_COND (for_stmt);
699 gcc_assert (COMPARISON_CLASS_P (t));
700 SET_EXPR_LOCUS (empty, EXPR_LOCUS (t));
701 wi.val_only = false;
702 walk_tree (&TREE_OPERAND (t, 0), callback, &wi, NULL);
703 wi.val_only = true;
704 wi.is_lhs = false;
705 walk_tree (&TREE_OPERAND (t, 1), callback, &wi, NULL);
707 t = OMP_FOR_INCR (for_stmt);
708 gcc_assert (TREE_CODE (t) == GIMPLE_MODIFY_STMT);
709 SET_EXPR_LOCUS (empty, EXPR_LOCUS (t));
710 wi.val_only = false;
711 walk_tree (&GIMPLE_STMT_OPERAND (t, 0), callback, &wi, NULL);
712 t = GIMPLE_STMT_OPERAND (t, 1);
713 gcc_assert (BINARY_CLASS_P (t));
714 wi.val_only = false;
715 walk_tree (&TREE_OPERAND (t, 0), callback, &wi, NULL);
716 wi.val_only = true;
717 wi.is_lhs = false;
718 walk_tree (&TREE_OPERAND (t, 1), callback, &wi, NULL);
720 /* Remove empty statement added above from the end of statement list. */
721 tsi_delink (&wi.tsi);
722 append_to_statement_list (list, &OMP_FOR_PRE_BODY (for_stmt));
725 /* Similarly for ROOT and all functions nested underneath, depth first. */
727 static void
728 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
732 if (root->inner)
733 walk_all_functions (callback, root->inner);
734 walk_function (callback, root);
735 root = root->next;
737 while (root);
740 /* We have to check for a fairly pathological case. The operands of function
741 nested function are to be interpreted in the context of the enclosing
742 function. So if any are variably-sized, they will get remapped when the
743 enclosing function is inlined. But that remapping would also have to be
744 done in the types of the PARM_DECLs of the nested function, meaning the
745 argument types of that function will disagree with the arguments in the
746 calls to that function. So we'd either have to make a copy of the nested
747 function corresponding to each time the enclosing function was inlined or
748 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
749 function. The former is not practical. The latter would still require
750 detecting this case to know when to add the conversions. So, for now at
751 least, we don't inline such an enclosing function.
753 We have to do that check recursively, so here return indicating whether
754 FNDECL has such a nested function. ORIG_FN is the function we were
755 trying to inline to use for checking whether any argument is variably
756 modified by anything in it.
758 It would be better to do this in tree-inline.c so that we could give
759 the appropriate warning for why a function can't be inlined, but that's
760 too late since the nesting structure has already been flattened and
761 adding a flag just to record this fact seems a waste of a flag. */
763 static bool
764 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
766 struct cgraph_node *cgn = cgraph_node (fndecl);
767 tree arg;
769 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
771 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
772 if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
773 return true;
775 if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
776 return true;
779 return false;
782 /* Construct our local datastructure describing the function nesting
783 tree rooted by CGN. */
785 static struct nesting_info *
786 create_nesting_tree (struct cgraph_node *cgn)
788 struct nesting_info *info = XCNEW (struct nesting_info);
789 info->field_map = pointer_map_create ();
790 info->var_map = pointer_map_create ();
791 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
792 info->context = cgn->decl;
794 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
796 struct nesting_info *sub = create_nesting_tree (cgn);
797 sub->outer = info;
798 sub->next = info->inner;
799 info->inner = sub;
802 /* See discussion at check_for_nested_with_variably_modified for a
803 discussion of why this has to be here. */
804 if (check_for_nested_with_variably_modified (info->context, info->context))
805 DECL_UNINLINABLE (info->context) = true;
807 return info;
810 /* Return an expression computing the static chain for TARGET_CONTEXT
811 from INFO->CONTEXT. Insert any necessary computations before TSI. */
813 static tree
814 get_static_chain (struct nesting_info *info, tree target_context,
815 tree_stmt_iterator *tsi)
817 struct nesting_info *i;
818 tree x;
820 if (info->context == target_context)
822 x = build_addr (info->frame_decl, target_context);
824 else
826 x = get_chain_decl (info);
828 for (i = info->outer; i->context != target_context; i = i->outer)
830 tree field = get_chain_field (i);
832 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
833 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
834 x = init_tmp_var (info, x, tsi);
838 return x;
841 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
842 frame as seen from INFO->CONTEXT. Insert any necessary computations
843 before TSI. */
845 static tree
846 get_frame_field (struct nesting_info *info, tree target_context,
847 tree field, tree_stmt_iterator *tsi)
849 struct nesting_info *i;
850 tree x;
852 if (info->context == target_context)
854 /* Make sure frame_decl gets created. */
855 (void) get_frame_type (info);
856 x = info->frame_decl;
858 else
860 x = get_chain_decl (info);
862 for (i = info->outer; i->context != target_context; i = i->outer)
864 tree field = get_chain_field (i);
866 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
867 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
868 x = init_tmp_var (info, x, tsi);
871 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
874 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
875 return x;
878 /* A subroutine of convert_nonlocal_reference. Create a local variable
879 in the nested function with DECL_VALUE_EXPR set to reference the true
880 variable in the parent function. This is used both for debug info
881 and in OpenMP lowering. */
883 static tree
884 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
886 tree target_context;
887 struct nesting_info *i;
888 tree x, field, new_decl;
889 void **slot;
891 slot = pointer_map_insert (info->var_map, decl);
893 if (*slot)
894 return *slot;
896 target_context = decl_function_context (decl);
898 /* A copy of the code in get_frame_field, but without the temporaries. */
899 if (info->context == target_context)
901 /* Make sure frame_decl gets created. */
902 (void) get_frame_type (info);
903 x = info->frame_decl;
904 i = info;
906 else
908 x = get_chain_decl (info);
909 for (i = info->outer; i->context != target_context; i = i->outer)
911 field = get_chain_field (i);
912 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
913 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
915 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
918 field = lookup_field_for_decl (i, decl, INSERT);
919 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
920 if (use_pointer_in_frame (decl))
921 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
923 /* ??? We should be remapping types as well, surely. */
924 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
925 DECL_CONTEXT (new_decl) = info->context;
926 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
927 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
928 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
929 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
930 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
931 TREE_READONLY (new_decl) = TREE_READONLY (decl);
932 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
933 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
935 SET_DECL_VALUE_EXPR (new_decl, x);
936 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
938 *slot = new_decl;
939 TREE_CHAIN (new_decl) = info->debug_var_chain;
940 info->debug_var_chain = new_decl;
942 return new_decl;
945 /* Called via walk_function+walk_tree, rewrite all references to VAR
946 and PARM_DECLs that belong to outer functions.
948 The rewrite will involve some number of structure accesses back up
949 the static chain. E.g. for a variable FOO up one nesting level it'll
950 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
951 indirections apply to decls for which use_pointer_in_frame is true. */
953 static bool convert_nonlocal_omp_clauses (tree *, struct walk_stmt_info *);
955 static tree
956 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
958 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
959 struct nesting_info *info = wi->info;
960 tree t = *tp;
961 tree save_local_var_chain;
962 bitmap save_suppress;
964 *walk_subtrees = 0;
965 switch (TREE_CODE (t))
967 case VAR_DECL:
968 /* Non-automatic variables are never processed. */
969 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
970 break;
971 /* FALLTHRU */
973 case PARM_DECL:
974 if (decl_function_context (t) != info->context)
976 tree x;
977 wi->changed = true;
979 x = get_nonlocal_debug_decl (info, t);
980 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
982 tree target_context = decl_function_context (t);
983 struct nesting_info *i;
984 for (i = info->outer; i->context != target_context; i = i->outer)
985 continue;
986 x = lookup_field_for_decl (i, t, INSERT);
987 x = get_frame_field (info, target_context, x, &wi->tsi);
988 if (use_pointer_in_frame (t))
990 x = init_tmp_var (info, x, &wi->tsi);
991 x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
995 if (wi->val_only)
997 if (wi->is_lhs)
998 x = save_tmp_var (info, x, &wi->tsi);
999 else
1000 x = init_tmp_var (info, x, &wi->tsi);
1003 *tp = x;
1005 break;
1007 case GOTO_EXPR:
1008 /* Don't walk non-local gotos for now. */
1009 if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
1011 *walk_subtrees = 1;
1012 wi->val_only = true;
1013 wi->is_lhs = false;
1015 break;
1017 case LABEL_DECL:
1018 /* We're taking the address of a label from a parent function, but
1019 this is not itself a non-local goto. Mark the label such that it
1020 will not be deleted, much as we would with a label address in
1021 static storage. */
1022 if (decl_function_context (t) != info->context)
1023 FORCED_LABEL (t) = 1;
1024 break;
1026 case ADDR_EXPR:
1028 bool save_val_only = wi->val_only;
1030 wi->val_only = false;
1031 wi->is_lhs = false;
1032 wi->changed = false;
1033 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
1034 wi->val_only = true;
1036 if (wi->changed)
1038 tree save_context;
1040 /* If we changed anything, then TREE_INVARIANT is be wrong,
1041 since we're no longer directly referencing a decl. */
1042 save_context = current_function_decl;
1043 current_function_decl = info->context;
1044 recompute_tree_invariant_for_addr_expr (t);
1045 current_function_decl = save_context;
1047 /* If the callback converted the address argument in a context
1048 where we only accept variables (and min_invariant, presumably),
1049 then compute the address into a temporary. */
1050 if (save_val_only)
1051 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1054 break;
1056 case REALPART_EXPR:
1057 case IMAGPART_EXPR:
1058 case COMPONENT_REF:
1059 case ARRAY_REF:
1060 case ARRAY_RANGE_REF:
1061 case BIT_FIELD_REF:
1062 /* Go down this entire nest and just look at the final prefix and
1063 anything that describes the references. Otherwise, we lose track
1064 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1065 wi->val_only = true;
1066 wi->is_lhs = false;
1067 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1069 if (TREE_CODE (t) == COMPONENT_REF)
1070 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1071 NULL);
1072 else if (TREE_CODE (t) == ARRAY_REF
1073 || TREE_CODE (t) == ARRAY_RANGE_REF)
1075 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1076 NULL);
1077 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1078 NULL);
1079 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
1080 NULL);
1082 else if (TREE_CODE (t) == BIT_FIELD_REF)
1084 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
1085 NULL);
1086 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
1087 NULL);
1090 wi->val_only = false;
1091 walk_tree (tp, convert_nonlocal_reference, wi, NULL);
1092 break;
1094 case VIEW_CONVERT_EXPR:
1095 /* Just request to look at the subtrees, leaving val_only and lhs
1096 untouched. This might actually be for !val_only + lhs, in which
1097 case we don't want to force a replacement by a temporary. */
1098 *walk_subtrees = 1;
1099 break;
1101 case OMP_PARALLEL:
1102 save_suppress = info->suppress_expansion;
1103 if (convert_nonlocal_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1105 tree c, decl;
1106 decl = get_chain_decl (info);
1107 c = build_omp_clause (OMP_CLAUSE_FIRSTPRIVATE);
1108 OMP_CLAUSE_DECL (c) = decl;
1109 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1110 OMP_PARALLEL_CLAUSES (t) = c;
1113 save_local_var_chain = info->new_local_var_chain;
1114 info->new_local_var_chain = NULL;
1116 walk_body (convert_nonlocal_reference, info, &OMP_PARALLEL_BODY (t));
1118 if (info->new_local_var_chain)
1119 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1120 info->new_local_var_chain = save_local_var_chain;
1121 info->suppress_expansion = save_suppress;
1122 break;
1124 case OMP_FOR:
1125 save_suppress = info->suppress_expansion;
1126 convert_nonlocal_omp_clauses (&OMP_FOR_CLAUSES (t), wi);
1127 walk_omp_for (convert_nonlocal_reference, info, t);
1128 walk_body (convert_nonlocal_reference, info, &OMP_FOR_BODY (t));
1129 info->suppress_expansion = save_suppress;
1130 break;
1132 case OMP_SECTIONS:
1133 case OMP_SINGLE:
1134 save_suppress = info->suppress_expansion;
1135 convert_nonlocal_omp_clauses (&OMP_CLAUSES (t), wi);
1136 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1137 info->suppress_expansion = save_suppress;
1138 break;
1140 case OMP_SECTION:
1141 case OMP_MASTER:
1142 case OMP_ORDERED:
1143 walk_body (convert_nonlocal_reference, info, &OMP_BODY (t));
1144 break;
1146 default:
1147 if (!IS_TYPE_OR_DECL_P (t))
1149 *walk_subtrees = 1;
1150 wi->val_only = true;
1151 wi->is_lhs = false;
1153 break;
1156 return NULL_TREE;
1159 static bool
1160 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1162 struct nesting_info *info = wi->info;
1163 bool need_chain = false;
1164 tree clause, decl;
1165 int dummy;
1166 bitmap new_suppress;
1168 new_suppress = BITMAP_GGC_ALLOC ();
1169 bitmap_copy (new_suppress, info->suppress_expansion);
1171 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1173 switch (OMP_CLAUSE_CODE (clause))
1175 case OMP_CLAUSE_PRIVATE:
1176 case OMP_CLAUSE_FIRSTPRIVATE:
1177 case OMP_CLAUSE_LASTPRIVATE:
1178 case OMP_CLAUSE_REDUCTION:
1179 case OMP_CLAUSE_COPYPRIVATE:
1180 case OMP_CLAUSE_SHARED:
1181 decl = OMP_CLAUSE_DECL (clause);
1182 if (decl_function_context (decl) != info->context)
1184 bitmap_set_bit (new_suppress, DECL_UID (decl));
1185 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1186 need_chain = true;
1188 break;
1190 case OMP_CLAUSE_SCHEDULE:
1191 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1192 break;
1193 /* FALLTHRU */
1194 case OMP_CLAUSE_IF:
1195 case OMP_CLAUSE_NUM_THREADS:
1196 wi->val_only = true;
1197 wi->is_lhs = false;
1198 convert_nonlocal_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy,
1199 wi);
1200 break;
1202 case OMP_CLAUSE_NOWAIT:
1203 case OMP_CLAUSE_ORDERED:
1204 case OMP_CLAUSE_DEFAULT:
1205 case OMP_CLAUSE_COPYIN:
1206 break;
1208 default:
1209 gcc_unreachable ();
1213 info->suppress_expansion = new_suppress;
1215 return need_chain;
1218 /* A subroutine of convert_local_reference. Create a local variable
1219 in the parent function with DECL_VALUE_EXPR set to reference the
1220 field in FRAME. This is used both for debug info and in OpenMP
1221 lowering. */
1223 static tree
1224 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1226 tree x, new_decl;
1227 void **slot;
1229 slot = pointer_map_insert (info->var_map, decl);
1230 if (*slot)
1231 return *slot;
1233 /* Make sure frame_decl gets created. */
1234 (void) get_frame_type (info);
1235 x = info->frame_decl;
1236 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1238 new_decl = build_decl (VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1239 DECL_CONTEXT (new_decl) = info->context;
1240 DECL_SOURCE_LOCATION (new_decl) = DECL_SOURCE_LOCATION (decl);
1241 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1242 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1243 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1244 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1245 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1246 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1247 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1249 SET_DECL_VALUE_EXPR (new_decl, x);
1250 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1251 *slot = new_decl;
1253 TREE_CHAIN (new_decl) = info->debug_var_chain;
1254 info->debug_var_chain = new_decl;
1256 /* Do not emit debug info twice. */
1257 DECL_IGNORED_P (decl) = 1;
1259 return new_decl;
1262 /* Called via walk_function+walk_tree, rewrite all references to VAR
1263 and PARM_DECLs that were referenced by inner nested functions.
1264 The rewrite will be a structure reference to the local frame variable. */
1266 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1268 static tree
1269 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
1271 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1272 struct nesting_info *info = wi->info;
1273 tree t = *tp, field, x;
1274 bool save_val_only;
1275 tree save_local_var_chain;
1276 bitmap save_suppress;
1278 *walk_subtrees = 0;
1279 switch (TREE_CODE (t))
1281 case VAR_DECL:
1282 /* Non-automatic variables are never processed. */
1283 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1284 break;
1285 /* FALLTHRU */
1287 case PARM_DECL:
1288 if (decl_function_context (t) == info->context)
1290 /* If we copied a pointer to the frame, then the original decl
1291 is used unchanged in the parent function. */
1292 if (use_pointer_in_frame (t))
1293 break;
1295 /* No need to transform anything if no child references the
1296 variable. */
1297 field = lookup_field_for_decl (info, t, NO_INSERT);
1298 if (!field)
1299 break;
1300 wi->changed = true;
1302 x = get_local_debug_decl (info, t, field);
1303 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1304 x = get_frame_field (info, info->context, field, &wi->tsi);
1306 if (wi->val_only)
1308 if (wi->is_lhs)
1309 x = save_tmp_var (info, x, &wi->tsi);
1310 else
1311 x = init_tmp_var (info, x, &wi->tsi);
1314 *tp = x;
1316 break;
1318 case ADDR_EXPR:
1319 save_val_only = wi->val_only;
1320 wi->val_only = false;
1321 wi->is_lhs = false;
1322 wi->changed = false;
1323 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
1324 wi->val_only = save_val_only;
1326 /* If we converted anything ... */
1327 if (wi->changed)
1329 tree save_context;
1331 /* Then the frame decl is now addressable. */
1332 TREE_ADDRESSABLE (info->frame_decl) = 1;
1334 save_context = current_function_decl;
1335 current_function_decl = info->context;
1336 recompute_tree_invariant_for_addr_expr (t);
1337 current_function_decl = save_context;
1339 /* If we are in a context where we only accept values, then
1340 compute the address into a temporary. */
1341 if (save_val_only)
1342 *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1344 break;
1346 case REALPART_EXPR:
1347 case IMAGPART_EXPR:
1348 case COMPONENT_REF:
1349 case ARRAY_REF:
1350 case ARRAY_RANGE_REF:
1351 case BIT_FIELD_REF:
1352 /* Go down this entire nest and just look at the final prefix and
1353 anything that describes the references. Otherwise, we lose track
1354 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1355 save_val_only = wi->val_only;
1356 wi->val_only = true;
1357 wi->is_lhs = false;
1358 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1360 if (TREE_CODE (t) == COMPONENT_REF)
1361 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1362 NULL);
1363 else if (TREE_CODE (t) == ARRAY_REF
1364 || TREE_CODE (t) == ARRAY_RANGE_REF)
1366 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1367 NULL);
1368 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1369 NULL);
1370 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1371 NULL);
1373 else if (TREE_CODE (t) == BIT_FIELD_REF)
1375 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1376 NULL);
1377 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1378 NULL);
1381 wi->val_only = false;
1382 walk_tree (tp, convert_local_reference, wi, NULL);
1383 wi->val_only = save_val_only;
1384 break;
1386 case VIEW_CONVERT_EXPR:
1387 /* Just request to look at the subtrees, leaving val_only and lhs
1388 untouched. This might actually be for !val_only + lhs, in which
1389 case we don't want to force a replacement by a temporary. */
1390 *walk_subtrees = 1;
1391 break;
1393 case OMP_PARALLEL:
1394 save_suppress = info->suppress_expansion;
1395 if (convert_local_omp_clauses (&OMP_PARALLEL_CLAUSES (t), wi))
1397 tree c;
1398 (void) get_frame_type (info);
1399 c = build_omp_clause (OMP_CLAUSE_SHARED);
1400 OMP_CLAUSE_DECL (c) = info->frame_decl;
1401 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1402 OMP_PARALLEL_CLAUSES (t) = c;
1405 save_local_var_chain = info->new_local_var_chain;
1406 info->new_local_var_chain = NULL;
1408 walk_body (convert_local_reference, info, &OMP_PARALLEL_BODY (t));
1410 if (info->new_local_var_chain)
1411 declare_vars (info->new_local_var_chain, OMP_PARALLEL_BODY (t), false);
1412 info->new_local_var_chain = save_local_var_chain;
1413 info->suppress_expansion = save_suppress;
1414 break;
1416 case OMP_FOR:
1417 save_suppress = info->suppress_expansion;
1418 convert_local_omp_clauses (&OMP_FOR_CLAUSES (t), wi);
1419 walk_omp_for (convert_local_reference, info, t);
1420 walk_body (convert_local_reference, info, &OMP_FOR_BODY (t));
1421 info->suppress_expansion = save_suppress;
1422 break;
1424 case OMP_SECTIONS:
1425 case OMP_SINGLE:
1426 save_suppress = info->suppress_expansion;
1427 convert_local_omp_clauses (&OMP_CLAUSES (t), wi);
1428 walk_body (convert_local_reference, info, &OMP_BODY (t));
1429 info->suppress_expansion = save_suppress;
1430 break;
1432 case OMP_SECTION:
1433 case OMP_MASTER:
1434 case OMP_ORDERED:
1435 walk_body (convert_local_reference, info, &OMP_BODY (t));
1436 break;
1438 default:
1439 if (!IS_TYPE_OR_DECL_P (t))
1441 *walk_subtrees = 1;
1442 wi->val_only = true;
1443 wi->is_lhs = false;
1445 break;
1448 return NULL_TREE;
1451 static bool
1452 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1454 struct nesting_info *info = wi->info;
1455 bool need_frame = false;
1456 tree clause, decl;
1457 int dummy;
1458 bitmap new_suppress;
1460 new_suppress = BITMAP_GGC_ALLOC ();
1461 bitmap_copy (new_suppress, info->suppress_expansion);
1463 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1465 switch (OMP_CLAUSE_CODE (clause))
1467 case OMP_CLAUSE_PRIVATE:
1468 case OMP_CLAUSE_FIRSTPRIVATE:
1469 case OMP_CLAUSE_LASTPRIVATE:
1470 case OMP_CLAUSE_REDUCTION:
1471 case OMP_CLAUSE_COPYPRIVATE:
1472 case OMP_CLAUSE_SHARED:
1473 decl = OMP_CLAUSE_DECL (clause);
1474 if (decl_function_context (decl) == info->context
1475 && !use_pointer_in_frame (decl))
1477 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1478 if (field)
1480 bitmap_set_bit (new_suppress, DECL_UID (decl));
1481 OMP_CLAUSE_DECL (clause)
1482 = get_local_debug_decl (info, decl, field);
1483 need_frame = true;
1486 break;
1488 case OMP_CLAUSE_SCHEDULE:
1489 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1490 break;
1491 /* FALLTHRU */
1492 case OMP_CLAUSE_IF:
1493 case OMP_CLAUSE_NUM_THREADS:
1494 wi->val_only = true;
1495 wi->is_lhs = false;
1496 convert_local_reference (&OMP_CLAUSE_OPERAND (clause, 0), &dummy, wi);
1497 break;
1499 case OMP_CLAUSE_NOWAIT:
1500 case OMP_CLAUSE_ORDERED:
1501 case OMP_CLAUSE_DEFAULT:
1502 case OMP_CLAUSE_COPYIN:
1503 break;
1505 default:
1506 gcc_unreachable ();
1510 info->suppress_expansion = new_suppress;
1512 return need_frame;
1515 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that
1516 reference labels from outer functions. The rewrite will be a
1517 call to __builtin_nonlocal_goto. */
1519 static tree
1520 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1522 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1523 struct nesting_info *info = wi->info, *i;
1524 tree t = *tp, label, new_label, target_context, x, field;
1525 void **slot;
1527 *walk_subtrees = 0;
1528 if (TREE_CODE (t) != GOTO_EXPR)
1529 return NULL_TREE;
1530 label = GOTO_DESTINATION (t);
1531 if (TREE_CODE (label) != LABEL_DECL)
1532 return NULL_TREE;
1533 target_context = decl_function_context (label);
1534 if (target_context == info->context)
1535 return NULL_TREE;
1537 for (i = info->outer; target_context != i->context; i = i->outer)
1538 continue;
1540 /* The original user label may also be use for a normal goto, therefore
1541 we must create a new label that will actually receive the abnormal
1542 control transfer. This new label will be marked LABEL_NONLOCAL; this
1543 mark will trigger proper behavior in the cfg, as well as cause the
1544 (hairy target-specific) non-local goto receiver code to be generated
1545 when we expand rtl. Enter this association into var_map so that we
1546 can insert the new label into the IL during a second pass. */
1547 slot = pointer_map_insert (i->var_map, label);
1548 if (*slot == NULL)
1550 new_label = create_artificial_label ();
1551 DECL_NONLOCAL (new_label) = 1;
1552 *slot = new_label;
1554 else
1555 new_label = *slot;
1557 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
1558 field = get_nl_goto_field (i);
1559 x = get_frame_field (info, target_context, field, &wi->tsi);
1560 x = build_addr (x, target_context);
1561 x = tsi_gimplify_val (info, x, &wi->tsi);
1562 x = build_call_expr (implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO], 2,
1563 build_addr (new_label, target_context), x);
1565 SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1566 *tsi_stmt_ptr (wi->tsi) = x;
1568 return NULL_TREE;
1571 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that
1572 are referenced via nonlocal goto from a nested function. The rewrite
1573 will involve installing a newly generated DECL_NONLOCAL label, and
1574 (potentially) a branch around the rtl gunk that is assumed to be
1575 attached to such a label. */
1577 static tree
1578 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1580 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1581 struct nesting_info *info = wi->info;
1582 tree t = *tp, label, new_label, x;
1583 tree_stmt_iterator tmp_tsi;
1584 void **slot;
1586 *walk_subtrees = 0;
1587 if (TREE_CODE (t) != LABEL_EXPR)
1588 return NULL_TREE;
1589 label = LABEL_EXPR_LABEL (t);
1591 slot = pointer_map_contains (info->var_map, label);
1592 if (!slot)
1593 return NULL_TREE;
1595 /* If there's any possibility that the previous statement falls through,
1596 then we must branch around the new non-local label. */
1597 tmp_tsi = wi->tsi;
1598 tsi_prev (&tmp_tsi);
1599 if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1601 x = build1 (GOTO_EXPR, void_type_node, label);
1602 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1605 new_label = (tree) *slot;
1606 x = build1 (LABEL_EXPR, void_type_node, new_label);
1607 tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1609 return NULL_TREE;
1612 /* Called via walk_function+walk_tree, rewrite all references to addresses
1613 of nested functions that require the use of trampolines. The rewrite
1614 will involve a reference a trampoline generated for the occasion. */
1616 static tree
1617 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1619 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1620 struct nesting_info *info = wi->info, *i;
1621 tree t = *tp, decl, target_context, x;
1623 *walk_subtrees = 0;
1624 switch (TREE_CODE (t))
1626 case ADDR_EXPR:
1627 /* Build
1628 T.1 = &CHAIN->tramp;
1629 T.2 = __builtin_adjust_trampoline (T.1);
1630 T.3 = (func_type)T.2;
1633 decl = TREE_OPERAND (t, 0);
1634 if (TREE_CODE (decl) != FUNCTION_DECL)
1635 break;
1637 /* Only need to process nested functions. */
1638 target_context = decl_function_context (decl);
1639 if (!target_context)
1640 break;
1642 /* If the nested function doesn't use a static chain, then
1643 it doesn't need a trampoline. */
1644 if (DECL_NO_STATIC_CHAIN (decl))
1645 break;
1647 /* Lookup the immediate parent of the callee, as that's where
1648 we need to insert the trampoline. */
1649 for (i = info; i->context != target_context; i = i->outer)
1650 continue;
1651 x = lookup_tramp_for_decl (i, decl, INSERT);
1653 /* Compute the address of the field holding the trampoline. */
1654 x = get_frame_field (info, target_context, x, &wi->tsi);
1655 x = build_addr (x, target_context);
1656 x = tsi_gimplify_val (info, x, &wi->tsi);
1658 /* Do machine-specific ugliness. Normally this will involve
1659 computing extra alignment, but it can really be anything. */
1660 x = build_call_expr (implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE],
1661 1, x);
1662 x = init_tmp_var (info, x, &wi->tsi);
1664 /* Cast back to the proper function type. */
1665 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1666 x = init_tmp_var (info, x, &wi->tsi);
1668 *tp = x;
1669 break;
1671 case CALL_EXPR:
1672 /* Only walk call arguments, lest we generate trampolines for
1673 direct calls. */
1675 int nargs = call_expr_nargs (t);
1676 int i;
1677 for (i = 0; i < nargs; i++)
1678 walk_tree (&CALL_EXPR_ARG (t, i), convert_tramp_reference, wi, NULL);
1680 break;
1682 default:
1683 if (!IS_TYPE_OR_DECL_P (t))
1684 *walk_subtrees = 1;
1685 break;
1688 return NULL_TREE;
1691 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that
1692 reference nested functions to make sure that the static chain is
1693 set up properly for the call. */
1695 static tree
1696 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1698 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1699 struct nesting_info *info = wi->info;
1700 tree t = *tp, decl, target_context;
1701 char save_static_chain_added;
1702 int i;
1704 *walk_subtrees = 0;
1705 switch (TREE_CODE (t))
1707 case CALL_EXPR:
1708 decl = get_callee_fndecl (t);
1709 if (!decl)
1710 break;
1711 target_context = decl_function_context (decl);
1712 if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1714 CALL_EXPR_STATIC_CHAIN (t)
1715 = get_static_chain (info, target_context, &wi->tsi);
1716 info->static_chain_added
1717 |= (1 << (info->context != target_context));
1719 break;
1721 case RETURN_EXPR:
1722 case GIMPLE_MODIFY_STMT:
1723 case WITH_SIZE_EXPR:
1724 /* Only return modify and with_size_expr may contain calls. */
1725 *walk_subtrees = 1;
1726 break;
1728 case OMP_PARALLEL:
1729 save_static_chain_added = info->static_chain_added;
1730 info->static_chain_added = 0;
1731 walk_body (convert_call_expr, info, &OMP_PARALLEL_BODY (t));
1732 for (i = 0; i < 2; i++)
1734 tree c, decl;
1735 if ((info->static_chain_added & (1 << i)) == 0)
1736 continue;
1737 decl = i ? get_chain_decl (info) : info->frame_decl;
1738 /* Don't add CHAIN.* or FRAME.* twice. */
1739 for (c = OMP_PARALLEL_CLAUSES (t); c; c = OMP_CLAUSE_CHAIN (c))
1740 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1741 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
1742 && OMP_CLAUSE_DECL (c) == decl)
1743 break;
1744 if (c == NULL)
1746 c = build_omp_clause (i ? OMP_CLAUSE_FIRSTPRIVATE
1747 : OMP_CLAUSE_SHARED);
1748 OMP_CLAUSE_DECL (c) = decl;
1749 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (t);
1750 OMP_PARALLEL_CLAUSES (t) = c;
1753 info->static_chain_added |= save_static_chain_added;
1754 break;
1756 case OMP_FOR:
1757 walk_body (convert_call_expr, info, &OMP_FOR_PRE_BODY (t));
1758 /* FALLTHRU */
1759 case OMP_SECTIONS:
1760 case OMP_SECTION:
1761 case OMP_SINGLE:
1762 case OMP_MASTER:
1763 case OMP_ORDERED:
1764 case OMP_CRITICAL:
1765 walk_body (convert_call_expr, info, &OMP_BODY (t));
1766 break;
1768 default:
1769 break;
1772 return NULL_TREE;
1775 /* Walk the nesting tree starting with ROOT, depth first. Convert all
1776 trampolines and call expressions. On the way back up, determine if
1777 a nested function actually uses its static chain; if not, remember that. */
1779 static void
1780 convert_all_function_calls (struct nesting_info *root)
1784 if (root->inner)
1785 convert_all_function_calls (root->inner);
1787 walk_function (convert_tramp_reference, root);
1788 walk_function (convert_call_expr, root);
1790 /* If the function does not use a static chain, then remember that. */
1791 if (root->outer && !root->chain_decl && !root->chain_field)
1792 DECL_NO_STATIC_CHAIN (root->context) = 1;
1793 else
1794 gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1796 root = root->next;
1798 while (root);
1801 /* Do "everything else" to clean up or complete state collected by the
1802 various walking passes -- lay out the types and decls, generate code
1803 to initialize the frame decl, store critical expressions in the
1804 struct function for rtl to find. */
1806 static void
1807 finalize_nesting_tree_1 (struct nesting_info *root)
1809 tree stmt_list = NULL;
1810 tree context = root->context;
1811 struct function *sf;
1813 /* If we created a non-local frame type or decl, we need to lay them
1814 out at this time. */
1815 if (root->frame_type)
1817 /* In some cases the frame type will trigger the -Wpadded warning.
1818 This is not helpful; suppress it. */
1819 int save_warn_padded = warn_padded;
1820 warn_padded = 0;
1821 layout_type (root->frame_type);
1822 warn_padded = save_warn_padded;
1823 layout_decl (root->frame_decl, 0);
1826 /* If any parameters were referenced non-locally, then we need to
1827 insert a copy. Likewise, if any variables were referenced by
1828 pointer, we need to initialize the address. */
1829 if (root->any_parm_remapped)
1831 tree p;
1832 for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1834 tree field, x, y;
1836 field = lookup_field_for_decl (root, p, NO_INSERT);
1837 if (!field)
1838 continue;
1840 if (use_pointer_in_frame (p))
1841 x = build_addr (p, context);
1842 else
1843 x = p;
1845 y = build3 (COMPONENT_REF, TREE_TYPE (field),
1846 root->frame_decl, field, NULL_TREE);
1847 x = build_gimple_modify_stmt (y, x);
1848 append_to_statement_list (x, &stmt_list);
1852 /* If a chain_field was created, then it needs to be initialized
1853 from chain_decl. */
1854 if (root->chain_field)
1856 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
1857 root->frame_decl, root->chain_field, NULL_TREE);
1858 x = build_gimple_modify_stmt (x, get_chain_decl (root));
1859 append_to_statement_list (x, &stmt_list);
1862 /* If trampolines were created, then we need to initialize them. */
1863 if (root->any_tramp_created)
1865 struct nesting_info *i;
1866 for (i = root->inner; i ; i = i->next)
1868 tree arg1, arg2, arg3, x, field;
1870 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1871 if (!field)
1872 continue;
1874 if (DECL_NO_STATIC_CHAIN (i->context))
1875 arg3 = null_pointer_node;
1876 else
1877 arg3 = build_addr (root->frame_decl, context);
1879 arg2 = build_addr (i->context, context);
1881 x = build3 (COMPONENT_REF, TREE_TYPE (field),
1882 root->frame_decl, field, NULL_TREE);
1883 arg1 = build_addr (x, context);
1885 x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1886 x = build_call_expr (x, 3, arg1, arg2, arg3);
1887 append_to_statement_list (x, &stmt_list);
1891 /* If we created initialization statements, insert them. */
1892 if (stmt_list)
1894 annotate_all_with_locus (&stmt_list,
1895 DECL_SOURCE_LOCATION (context));
1896 append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1897 &stmt_list);
1898 BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1901 /* If a chain_decl was created, then it needs to be registered with
1902 struct function so that it gets initialized from the static chain
1903 register at the beginning of the function. */
1904 sf = DECL_STRUCT_FUNCTION (root->context);
1905 sf->static_chain_decl = root->chain_decl;
1907 /* Similarly for the non-local goto save area. */
1908 if (root->nl_goto_field)
1910 sf->nonlocal_goto_save_area
1911 = get_frame_field (root, context, root->nl_goto_field, NULL);
1912 sf->has_nonlocal_label = 1;
1915 /* Make sure all new local variables get inserted into the
1916 proper BIND_EXPR. */
1917 if (root->new_local_var_chain)
1918 declare_vars (root->new_local_var_chain, DECL_SAVED_TREE (root->context),
1919 false);
1920 if (root->debug_var_chain)
1921 declare_vars (root->debug_var_chain, DECL_SAVED_TREE (root->context),
1922 true);
1924 /* Dump the translated tree function. */
1925 dump_function (TDI_nested, root->context);
1928 static void
1929 finalize_nesting_tree (struct nesting_info *root)
1933 if (root->inner)
1934 finalize_nesting_tree (root->inner);
1935 finalize_nesting_tree_1 (root);
1936 root = root->next;
1938 while (root);
1941 /* Unnest the nodes and pass them to cgraph. */
1943 static void
1944 unnest_nesting_tree_1 (struct nesting_info *root)
1946 struct cgraph_node *node = cgraph_node (root->context);
1948 /* For nested functions update the cgraph to reflect unnesting.
1949 We also delay finalizing of these functions up to this point. */
1950 if (node->origin)
1952 cgraph_unnest_node (cgraph_node (root->context));
1953 cgraph_finalize_function (root->context, true);
1957 static void
1958 unnest_nesting_tree (struct nesting_info *root)
1962 if (root->inner)
1963 unnest_nesting_tree (root->inner);
1964 unnest_nesting_tree_1 (root);
1965 root = root->next;
1967 while (root);
1970 /* Free the data structures allocated during this pass. */
1972 static void
1973 free_nesting_tree (struct nesting_info *root)
1975 struct nesting_info *next;
1978 if (root->inner)
1979 free_nesting_tree (root->inner);
1980 pointer_map_destroy (root->var_map);
1981 pointer_map_destroy (root->field_map);
1982 next = root->next;
1983 free (root);
1984 root = next;
1986 while (root);
1989 /* Gimplify a function and all its nested functions. */
1990 static void
1991 gimplify_all_functions (struct cgraph_node *root)
1993 struct cgraph_node *iter;
1994 gimplify_function_tree (root->decl);
1995 for (iter = root->nested; iter; iter = iter->next_nested)
1996 gimplify_all_functions (iter);
1999 /* Main entry point for this pass. Process FNDECL and all of its nested
2000 subroutines and turn them into something less tightly bound. */
2002 void
2003 lower_nested_functions (tree fndecl)
2005 struct cgraph_node *cgn;
2006 struct nesting_info *root;
2008 /* If there are no nested functions, there's nothing to do. */
2009 cgn = cgraph_node (fndecl);
2010 if (!cgn->nested)
2011 return;
2013 gimplify_all_functions (cgn);
2015 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
2016 root = create_nesting_tree (cgn);
2017 walk_all_functions (convert_nonlocal_reference, root);
2018 walk_all_functions (convert_local_reference, root);
2019 walk_all_functions (convert_nl_goto_reference, root);
2020 walk_all_functions (convert_nl_goto_receiver, root);
2021 convert_all_function_calls (root);
2022 finalize_nesting_tree (root);
2023 unnest_nesting_tree (root);
2024 free_nesting_tree (root);
2025 bitmap_obstack_release (&nesting_info_bitmap_obstack);
2028 #include "gt-tree-nested.h"