2016-07-13 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / tree-nested.c
blob62cb01fbba946d6c1af87243dc02c44408825b41
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2016 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 "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tm_p.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "tree-dump.h"
33 #include "tree-inline.h"
34 #include "gimplify.h"
35 #include "gimple-iterator.h"
36 #include "gimple-walk.h"
37 #include "tree-cfg.h"
38 #include "explow.h"
39 #include "langhooks.h"
40 #include "gimple-low.h"
41 #include "gomp-constants.h"
44 /* The object of this pass is to lower the representation of a set of nested
45 functions in order to expose all of the gory details of the various
46 nonlocal references. We want to do this sooner rather than later, in
47 order to give us more freedom in emitting all of the functions in question.
49 Back in olden times, when gcc was young, we developed an insanely
50 complicated scheme whereby variables which were referenced nonlocally
51 were forced to live in the stack of the declaring function, and then
52 the nested functions magically discovered where these variables were
53 placed. In order for this scheme to function properly, it required
54 that the outer function be partially expanded, then we switch to
55 compiling the inner function, and once done with those we switch back
56 to compiling the outer function. Such delicate ordering requirements
57 makes it difficult to do whole translation unit optimizations
58 involving such functions.
60 The implementation here is much more direct. Everything that can be
61 referenced by an inner function is a member of an explicitly created
62 structure herein called the "nonlocal frame struct". The incoming
63 static chain for a nested function is a pointer to this struct in
64 the parent. In this way, we settle on known offsets from a known
65 base, and so are decoupled from the logic that places objects in the
66 function's stack frame. More importantly, we don't have to wait for
67 that to happen -- since the compilation of the inner function is no
68 longer tied to a real stack frame, the nonlocal frame struct can be
69 allocated anywhere. Which means that the outer function is now
70 inlinable.
72 Theory of operation here is very simple. Iterate over all the
73 statements in all the functions (depth first) several times,
74 allocating structures and fields on demand. In general we want to
75 examine inner functions first, so that we can avoid making changes
76 to outer functions which are unnecessary.
78 The order of the passes matters a bit, in that later passes will be
79 skipped if it is discovered that the functions don't actually interact
80 at all. That is, they're nested in the lexical sense but could have
81 been written as independent functions without change. */
84 struct nesting_info
86 struct nesting_info *outer;
87 struct nesting_info *inner;
88 struct nesting_info *next;
90 hash_map<tree, tree> *field_map;
91 hash_map<tree, tree> *var_map;
92 hash_set<tree *> *mem_refs;
93 bitmap suppress_expansion;
95 tree context;
96 tree new_local_var_chain;
97 tree debug_var_chain;
98 tree frame_type;
99 tree frame_decl;
100 tree chain_field;
101 tree chain_decl;
102 tree nl_goto_field;
104 bool any_parm_remapped;
105 bool any_tramp_created;
106 char static_chain_added;
110 /* Iterate over the nesting tree, starting with ROOT, depth first. */
112 static inline struct nesting_info *
113 iter_nestinfo_start (struct nesting_info *root)
115 while (root->inner)
116 root = root->inner;
117 return root;
120 static inline struct nesting_info *
121 iter_nestinfo_next (struct nesting_info *node)
123 if (node->next)
124 return iter_nestinfo_start (node->next);
125 return node->outer;
128 #define FOR_EACH_NEST_INFO(I, ROOT) \
129 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
131 /* Obstack used for the bitmaps in the struct above. */
132 static struct bitmap_obstack nesting_info_bitmap_obstack;
135 /* We're working in so many different function contexts simultaneously,
136 that create_tmp_var is dangerous. Prevent mishap. */
137 #define create_tmp_var cant_use_create_tmp_var_here_dummy
139 /* Like create_tmp_var, except record the variable for registration at
140 the given nesting level. */
142 static tree
143 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
145 tree tmp_var;
147 /* If the type is of variable size or a type which must be created by the
148 frontend, something is wrong. Note that we explicitly allow
149 incomplete types here, since we create them ourselves here. */
150 gcc_assert (!TREE_ADDRESSABLE (type));
151 gcc_assert (!TYPE_SIZE_UNIT (type)
152 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
154 tmp_var = create_tmp_var_raw (type, prefix);
155 DECL_CONTEXT (tmp_var) = info->context;
156 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
157 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
158 if (TREE_CODE (type) == COMPLEX_TYPE
159 || TREE_CODE (type) == VECTOR_TYPE)
160 DECL_GIMPLE_REG_P (tmp_var) = 1;
162 info->new_local_var_chain = tmp_var;
164 return tmp_var;
167 /* Take the address of EXP to be used within function CONTEXT.
168 Mark it for addressability as necessary. */
170 tree
171 build_addr (tree exp)
173 mark_addressable (exp);
174 return build_fold_addr_expr (exp);
177 /* Insert FIELD into TYPE, sorted by alignment requirements. */
179 void
180 insert_field_into_struct (tree type, tree field)
182 tree *p;
184 DECL_CONTEXT (field) = type;
186 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
187 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
188 break;
190 DECL_CHAIN (field) = *p;
191 *p = field;
193 /* Set correct alignment for frame struct type. */
194 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
195 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
198 /* Build or return the RECORD_TYPE that describes the frame state that is
199 shared between INFO->CONTEXT and its nested functions. This record will
200 not be complete until finalize_nesting_tree; up until that point we'll
201 be adding fields as necessary.
203 We also build the DECL that represents this frame in the function. */
205 static tree
206 get_frame_type (struct nesting_info *info)
208 tree type = info->frame_type;
209 if (!type)
211 char *name;
213 type = make_node (RECORD_TYPE);
215 name = concat ("FRAME.",
216 IDENTIFIER_POINTER (DECL_NAME (info->context)),
217 NULL);
218 TYPE_NAME (type) = get_identifier (name);
219 free (name);
221 info->frame_type = type;
222 info->frame_decl = create_tmp_var_for (info, type, "FRAME");
223 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
225 /* ??? Always make it addressable for now, since it is meant to
226 be pointed to by the static chain pointer. This pessimizes
227 when it turns out that no static chains are needed because
228 the nested functions referencing non-local variables are not
229 reachable, but the true pessimization is to create the non-
230 local frame structure in the first place. */
231 TREE_ADDRESSABLE (info->frame_decl) = 1;
233 return type;
236 /* Return true if DECL should be referenced by pointer in the non-local
237 frame structure. */
239 static bool
240 use_pointer_in_frame (tree decl)
242 if (TREE_CODE (decl) == PARM_DECL)
244 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
245 sized decls, and inefficient to copy large aggregates. Don't bother
246 moving anything but scalar variables. */
247 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
249 else
251 /* Variable sized types make things "interesting" in the frame. */
252 return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
256 /* Given DECL, a non-locally accessed variable, find or create a field
257 in the non-local frame structure for the given nesting context. */
259 static tree
260 lookup_field_for_decl (struct nesting_info *info, tree decl,
261 enum insert_option insert)
263 if (insert == NO_INSERT)
265 tree *slot = info->field_map->get (decl);
266 return slot ? *slot : NULL_TREE;
269 tree *slot = &info->field_map->get_or_insert (decl);
270 if (!*slot)
272 tree field = make_node (FIELD_DECL);
273 DECL_NAME (field) = DECL_NAME (decl);
275 if (use_pointer_in_frame (decl))
277 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
278 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
279 DECL_NONADDRESSABLE_P (field) = 1;
281 else
283 TREE_TYPE (field) = TREE_TYPE (decl);
284 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
285 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
286 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
287 TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
288 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
289 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
292 insert_field_into_struct (get_frame_type (info), field);
293 *slot = field;
295 if (TREE_CODE (decl) == PARM_DECL)
296 info->any_parm_remapped = true;
299 return *slot;
302 /* Build or return the variable that holds the static chain within
303 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
305 static tree
306 get_chain_decl (struct nesting_info *info)
308 tree decl = info->chain_decl;
310 if (!decl)
312 tree type;
314 type = get_frame_type (info->outer);
315 type = build_pointer_type (type);
317 /* Note that this variable is *not* entered into any BIND_EXPR;
318 the construction of this variable is handled specially in
319 expand_function_start and initialize_inlined_parameters.
320 Note also that it's represented as a parameter. This is more
321 close to the truth, since the initial value does come from
322 the caller. */
323 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
324 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
325 DECL_ARTIFICIAL (decl) = 1;
326 DECL_IGNORED_P (decl) = 1;
327 TREE_USED (decl) = 1;
328 DECL_CONTEXT (decl) = info->context;
329 DECL_ARG_TYPE (decl) = type;
331 /* Tell tree-inline.c that we never write to this variable, so
332 it can copy-prop the replacement value immediately. */
333 TREE_READONLY (decl) = 1;
335 info->chain_decl = decl;
337 if (dump_file
338 && (dump_flags & TDF_DETAILS)
339 && !DECL_STATIC_CHAIN (info->context))
340 fprintf (dump_file, "Setting static-chain for %s\n",
341 lang_hooks.decl_printable_name (info->context, 2));
343 DECL_STATIC_CHAIN (info->context) = 1;
345 return decl;
348 /* Build or return the field within the non-local frame state that holds
349 the static chain for INFO->CONTEXT. This is the way to walk back up
350 multiple nesting levels. */
352 static tree
353 get_chain_field (struct nesting_info *info)
355 tree field = info->chain_field;
357 if (!field)
359 tree type = build_pointer_type (get_frame_type (info->outer));
361 field = make_node (FIELD_DECL);
362 DECL_NAME (field) = get_identifier ("__chain");
363 TREE_TYPE (field) = type;
364 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
365 DECL_NONADDRESSABLE_P (field) = 1;
367 insert_field_into_struct (get_frame_type (info), field);
369 info->chain_field = field;
371 if (dump_file
372 && (dump_flags & TDF_DETAILS)
373 && !DECL_STATIC_CHAIN (info->context))
374 fprintf (dump_file, "Setting static-chain for %s\n",
375 lang_hooks.decl_printable_name (info->context, 2));
377 DECL_STATIC_CHAIN (info->context) = 1;
379 return field;
382 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
384 static tree
385 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
386 gcall *call)
388 tree t;
390 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
391 gimple_call_set_lhs (call, t);
392 if (! gsi_end_p (*gsi))
393 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
394 gsi_insert_before (gsi, call, GSI_SAME_STMT);
396 return t;
400 /* Copy EXP into a temporary. Allocate the temporary in the context of
401 INFO and insert the initialization statement before GSI. */
403 static tree
404 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
406 tree t;
407 gimple *stmt;
409 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
410 stmt = gimple_build_assign (t, exp);
411 if (! gsi_end_p (*gsi))
412 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
413 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
415 return t;
419 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
421 static tree
422 gsi_gimplify_val (struct nesting_info *info, tree exp,
423 gimple_stmt_iterator *gsi)
425 if (is_gimple_val (exp))
426 return exp;
427 else
428 return init_tmp_var (info, exp, gsi);
431 /* Similarly, but copy from the temporary and insert the statement
432 after the iterator. */
434 static tree
435 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
437 tree t;
438 gimple *stmt;
440 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
441 stmt = gimple_build_assign (exp, t);
442 if (! gsi_end_p (*gsi))
443 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
444 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
446 return t;
449 /* Build or return the type used to represent a nested function trampoline. */
451 static GTY(()) tree trampoline_type;
453 static tree
454 get_trampoline_type (struct nesting_info *info)
456 unsigned align, size;
457 tree t;
459 if (trampoline_type)
460 return trampoline_type;
462 align = TRAMPOLINE_ALIGNMENT;
463 size = TRAMPOLINE_SIZE;
465 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
466 then allocate extra space so that we can do dynamic alignment. */
467 if (align > STACK_BOUNDARY)
469 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
470 align = STACK_BOUNDARY;
473 t = build_index_type (size_int (size - 1));
474 t = build_array_type (char_type_node, t);
475 t = build_decl (DECL_SOURCE_LOCATION (info->context),
476 FIELD_DECL, get_identifier ("__data"), t);
477 SET_DECL_ALIGN (t, align);
478 DECL_USER_ALIGN (t) = 1;
480 trampoline_type = make_node (RECORD_TYPE);
481 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
482 TYPE_FIELDS (trampoline_type) = t;
483 layout_type (trampoline_type);
484 DECL_CONTEXT (t) = trampoline_type;
486 return trampoline_type;
489 /* Given DECL, a nested function, find or create a field in the non-local
490 frame structure for a trampoline for this function. */
492 static tree
493 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
494 enum insert_option insert)
496 if (insert == NO_INSERT)
498 tree *slot = info->var_map->get (decl);
499 return slot ? *slot : NULL_TREE;
502 tree *slot = &info->var_map->get_or_insert (decl);
503 if (!*slot)
505 tree field = make_node (FIELD_DECL);
506 DECL_NAME (field) = DECL_NAME (decl);
507 TREE_TYPE (field) = get_trampoline_type (info);
508 TREE_ADDRESSABLE (field) = 1;
510 insert_field_into_struct (get_frame_type (info), field);
511 *slot = field;
513 info->any_tramp_created = true;
516 return *slot;
519 /* Build or return the field within the non-local frame state that holds
520 the non-local goto "jmp_buf". The buffer itself is maintained by the
521 rtl middle-end as dynamic stack space is allocated. */
523 static tree
524 get_nl_goto_field (struct nesting_info *info)
526 tree field = info->nl_goto_field;
527 if (!field)
529 unsigned size;
530 tree type;
532 /* For __builtin_nonlocal_goto, we need N words. The first is the
533 frame pointer, the rest is for the target's stack pointer save
534 area. The number of words is controlled by STACK_SAVEAREA_MODE;
535 not the best interface, but it'll do for now. */
536 if (Pmode == ptr_mode)
537 type = ptr_type_node;
538 else
539 type = lang_hooks.types.type_for_mode (Pmode, 1);
541 size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
542 size = size / GET_MODE_SIZE (Pmode);
543 size = size + 1;
545 type = build_array_type
546 (type, build_index_type (size_int (size)));
548 field = make_node (FIELD_DECL);
549 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
550 TREE_TYPE (field) = type;
551 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
552 TREE_ADDRESSABLE (field) = 1;
554 insert_field_into_struct (get_frame_type (info), field);
556 info->nl_goto_field = field;
559 return field;
562 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
564 static void
565 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
566 struct nesting_info *info, gimple_seq *pseq)
568 struct walk_stmt_info wi;
570 memset (&wi, 0, sizeof (wi));
571 wi.info = info;
572 wi.val_only = true;
573 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
577 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
579 static inline void
580 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
581 struct nesting_info *info)
583 gimple_seq body = gimple_body (info->context);
584 walk_body (callback_stmt, callback_op, info, &body);
585 gimple_set_body (info->context, body);
588 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
590 static void
591 walk_gimple_omp_for (gomp_for *for_stmt,
592 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
593 struct nesting_info *info)
595 struct walk_stmt_info wi;
596 gimple_seq seq;
597 tree t;
598 size_t i;
600 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
602 seq = NULL;
603 memset (&wi, 0, sizeof (wi));
604 wi.info = info;
605 wi.gsi = gsi_last (seq);
607 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
609 wi.val_only = false;
610 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
611 &wi, NULL);
612 wi.val_only = true;
613 wi.is_lhs = false;
614 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
615 &wi, NULL);
617 wi.val_only = true;
618 wi.is_lhs = false;
619 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
620 &wi, NULL);
622 t = gimple_omp_for_incr (for_stmt, i);
623 gcc_assert (BINARY_CLASS_P (t));
624 wi.val_only = false;
625 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
626 wi.val_only = true;
627 wi.is_lhs = false;
628 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
631 seq = gsi_seq (wi.gsi);
632 if (!gimple_seq_empty_p (seq))
634 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
635 annotate_all_with_location (seq, gimple_location (for_stmt));
636 gimple_seq_add_seq (&pre_body, seq);
637 gimple_omp_for_set_pre_body (for_stmt, pre_body);
641 /* Similarly for ROOT and all functions nested underneath, depth first. */
643 static void
644 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
645 struct nesting_info *root)
647 struct nesting_info *n;
648 FOR_EACH_NEST_INFO (n, root)
649 walk_function (callback_stmt, callback_op, n);
653 /* We have to check for a fairly pathological case. The operands of function
654 nested function are to be interpreted in the context of the enclosing
655 function. So if any are variably-sized, they will get remapped when the
656 enclosing function is inlined. But that remapping would also have to be
657 done in the types of the PARM_DECLs of the nested function, meaning the
658 argument types of that function will disagree with the arguments in the
659 calls to that function. So we'd either have to make a copy of the nested
660 function corresponding to each time the enclosing function was inlined or
661 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
662 function. The former is not practical. The latter would still require
663 detecting this case to know when to add the conversions. So, for now at
664 least, we don't inline such an enclosing function.
666 We have to do that check recursively, so here return indicating whether
667 FNDECL has such a nested function. ORIG_FN is the function we were
668 trying to inline to use for checking whether any argument is variably
669 modified by anything in it.
671 It would be better to do this in tree-inline.c so that we could give
672 the appropriate warning for why a function can't be inlined, but that's
673 too late since the nesting structure has already been flattened and
674 adding a flag just to record this fact seems a waste of a flag. */
676 static bool
677 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
679 struct cgraph_node *cgn = cgraph_node::get (fndecl);
680 tree arg;
682 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
684 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
685 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
686 return true;
688 if (check_for_nested_with_variably_modified (cgn->decl,
689 orig_fndecl))
690 return true;
693 return false;
696 /* Construct our local datastructure describing the function nesting
697 tree rooted by CGN. */
699 static struct nesting_info *
700 create_nesting_tree (struct cgraph_node *cgn)
702 struct nesting_info *info = XCNEW (struct nesting_info);
703 info->field_map = new hash_map<tree, tree>;
704 info->var_map = new hash_map<tree, tree>;
705 info->mem_refs = new hash_set<tree *>;
706 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
707 info->context = cgn->decl;
709 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
711 struct nesting_info *sub = create_nesting_tree (cgn);
712 sub->outer = info;
713 sub->next = info->inner;
714 info->inner = sub;
717 /* See discussion at check_for_nested_with_variably_modified for a
718 discussion of why this has to be here. */
719 if (check_for_nested_with_variably_modified (info->context, info->context))
720 DECL_UNINLINABLE (info->context) = true;
722 return info;
725 /* Return an expression computing the static chain for TARGET_CONTEXT
726 from INFO->CONTEXT. Insert any necessary computations before TSI. */
728 static tree
729 get_static_chain (struct nesting_info *info, tree target_context,
730 gimple_stmt_iterator *gsi)
732 struct nesting_info *i;
733 tree x;
735 if (info->context == target_context)
737 x = build_addr (info->frame_decl);
738 info->static_chain_added |= 1;
740 else
742 x = get_chain_decl (info);
743 info->static_chain_added |= 2;
745 for (i = info->outer; i->context != target_context; i = i->outer)
747 tree field = get_chain_field (i);
749 x = build_simple_mem_ref (x);
750 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
751 x = init_tmp_var (info, x, gsi);
755 return x;
759 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
760 frame as seen from INFO->CONTEXT. Insert any necessary computations
761 before GSI. */
763 static tree
764 get_frame_field (struct nesting_info *info, tree target_context,
765 tree field, gimple_stmt_iterator *gsi)
767 struct nesting_info *i;
768 tree x;
770 if (info->context == target_context)
772 /* Make sure frame_decl gets created. */
773 (void) get_frame_type (info);
774 x = info->frame_decl;
775 info->static_chain_added |= 1;
777 else
779 x = get_chain_decl (info);
780 info->static_chain_added |= 2;
782 for (i = info->outer; i->context != target_context; i = i->outer)
784 tree field = get_chain_field (i);
786 x = build_simple_mem_ref (x);
787 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
788 x = init_tmp_var (info, x, gsi);
791 x = build_simple_mem_ref (x);
794 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
795 return x;
798 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
800 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
801 in the nested function with DECL_VALUE_EXPR set to reference the true
802 variable in the parent function. This is used both for debug info
803 and in OMP lowering. */
805 static tree
806 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
808 tree target_context;
809 struct nesting_info *i;
810 tree x, field, new_decl;
812 tree *slot = &info->var_map->get_or_insert (decl);
814 if (*slot)
815 return *slot;
817 target_context = decl_function_context (decl);
819 /* A copy of the code in get_frame_field, but without the temporaries. */
820 if (info->context == target_context)
822 /* Make sure frame_decl gets created. */
823 (void) get_frame_type (info);
824 x = info->frame_decl;
825 i = info;
826 info->static_chain_added |= 1;
828 else
830 x = get_chain_decl (info);
831 info->static_chain_added |= 2;
832 for (i = info->outer; i->context != target_context; i = i->outer)
834 field = get_chain_field (i);
835 x = build_simple_mem_ref (x);
836 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
838 x = build_simple_mem_ref (x);
841 field = lookup_field_for_decl (i, decl, INSERT);
842 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
843 if (use_pointer_in_frame (decl))
844 x = build_simple_mem_ref (x);
846 /* ??? We should be remapping types as well, surely. */
847 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
848 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
849 DECL_CONTEXT (new_decl) = info->context;
850 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
851 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
852 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
853 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
854 TREE_READONLY (new_decl) = TREE_READONLY (decl);
855 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
856 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
857 if ((TREE_CODE (decl) == PARM_DECL
858 || TREE_CODE (decl) == RESULT_DECL
859 || TREE_CODE (decl) == VAR_DECL)
860 && DECL_BY_REFERENCE (decl))
861 DECL_BY_REFERENCE (new_decl) = 1;
863 SET_DECL_VALUE_EXPR (new_decl, x);
864 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
866 *slot = new_decl;
867 DECL_CHAIN (new_decl) = info->debug_var_chain;
868 info->debug_var_chain = new_decl;
870 if (!optimize
871 && info->context != target_context
872 && variably_modified_type_p (TREE_TYPE (decl), NULL))
873 note_nonlocal_vla_type (info, TREE_TYPE (decl));
875 return new_decl;
879 /* Callback for walk_gimple_stmt, rewrite all references to VAR
880 and PARM_DECLs that belong to outer functions.
882 The rewrite will involve some number of structure accesses back up
883 the static chain. E.g. for a variable FOO up one nesting level it'll
884 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
885 indirections apply to decls for which use_pointer_in_frame is true. */
887 static tree
888 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
890 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
891 struct nesting_info *const info = (struct nesting_info *) wi->info;
892 tree t = *tp;
894 *walk_subtrees = 0;
895 switch (TREE_CODE (t))
897 case VAR_DECL:
898 /* Non-automatic variables are never processed. */
899 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
900 break;
901 /* FALLTHRU */
903 case PARM_DECL:
904 if (decl_function_context (t) != info->context)
906 tree x;
907 wi->changed = true;
909 x = get_nonlocal_debug_decl (info, t);
910 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
912 tree target_context = decl_function_context (t);
913 struct nesting_info *i;
914 for (i = info->outer; i->context != target_context; i = i->outer)
915 continue;
916 x = lookup_field_for_decl (i, t, INSERT);
917 x = get_frame_field (info, target_context, x, &wi->gsi);
918 if (use_pointer_in_frame (t))
920 x = init_tmp_var (info, x, &wi->gsi);
921 x = build_simple_mem_ref (x);
925 if (wi->val_only)
927 if (wi->is_lhs)
928 x = save_tmp_var (info, x, &wi->gsi);
929 else
930 x = init_tmp_var (info, x, &wi->gsi);
933 *tp = x;
935 break;
937 case LABEL_DECL:
938 /* We're taking the address of a label from a parent function, but
939 this is not itself a non-local goto. Mark the label such that it
940 will not be deleted, much as we would with a label address in
941 static storage. */
942 if (decl_function_context (t) != info->context)
943 FORCED_LABEL (t) = 1;
944 break;
946 case ADDR_EXPR:
948 bool save_val_only = wi->val_only;
950 wi->val_only = false;
951 wi->is_lhs = false;
952 wi->changed = false;
953 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
954 wi->val_only = true;
956 if (wi->changed)
958 tree save_context;
960 /* If we changed anything, we might no longer be directly
961 referencing a decl. */
962 save_context = current_function_decl;
963 current_function_decl = info->context;
964 recompute_tree_invariant_for_addr_expr (t);
965 current_function_decl = save_context;
967 /* If the callback converted the address argument in a context
968 where we only accept variables (and min_invariant, presumably),
969 then compute the address into a temporary. */
970 if (save_val_only)
971 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
972 t, &wi->gsi);
975 break;
977 case REALPART_EXPR:
978 case IMAGPART_EXPR:
979 case COMPONENT_REF:
980 case ARRAY_REF:
981 case ARRAY_RANGE_REF:
982 case BIT_FIELD_REF:
983 /* Go down this entire nest and just look at the final prefix and
984 anything that describes the references. Otherwise, we lose track
985 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
986 wi->val_only = true;
987 wi->is_lhs = false;
988 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
990 if (TREE_CODE (t) == COMPONENT_REF)
991 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
992 NULL);
993 else if (TREE_CODE (t) == ARRAY_REF
994 || TREE_CODE (t) == ARRAY_RANGE_REF)
996 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
997 wi, NULL);
998 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
999 wi, NULL);
1000 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1001 wi, NULL);
1004 wi->val_only = false;
1005 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1006 break;
1008 case VIEW_CONVERT_EXPR:
1009 /* Just request to look at the subtrees, leaving val_only and lhs
1010 untouched. This might actually be for !val_only + lhs, in which
1011 case we don't want to force a replacement by a temporary. */
1012 *walk_subtrees = 1;
1013 break;
1015 default:
1016 if (!IS_TYPE_OR_DECL_P (t))
1018 *walk_subtrees = 1;
1019 wi->val_only = true;
1020 wi->is_lhs = false;
1022 break;
1025 return NULL_TREE;
1028 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1029 struct walk_stmt_info *);
1031 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1032 and PARM_DECLs that belong to outer functions. */
1034 static bool
1035 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1037 struct nesting_info *const info = (struct nesting_info *) wi->info;
1038 bool need_chain = false, need_stmts = false;
1039 tree clause, decl;
1040 int dummy;
1041 bitmap new_suppress;
1043 new_suppress = BITMAP_GGC_ALLOC ();
1044 bitmap_copy (new_suppress, info->suppress_expansion);
1046 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1048 switch (OMP_CLAUSE_CODE (clause))
1050 case OMP_CLAUSE_REDUCTION:
1051 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1052 need_stmts = true;
1053 goto do_decl_clause;
1055 case OMP_CLAUSE_LASTPRIVATE:
1056 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1057 need_stmts = true;
1058 goto do_decl_clause;
1060 case OMP_CLAUSE_LINEAR:
1061 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1062 need_stmts = true;
1063 wi->val_only = true;
1064 wi->is_lhs = false;
1065 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1066 &dummy, wi);
1067 goto do_decl_clause;
1069 case OMP_CLAUSE_PRIVATE:
1070 case OMP_CLAUSE_FIRSTPRIVATE:
1071 case OMP_CLAUSE_COPYPRIVATE:
1072 case OMP_CLAUSE_SHARED:
1073 case OMP_CLAUSE_TO_DECLARE:
1074 case OMP_CLAUSE_LINK:
1075 case OMP_CLAUSE_USE_DEVICE_PTR:
1076 case OMP_CLAUSE_IS_DEVICE_PTR:
1077 do_decl_clause:
1078 decl = OMP_CLAUSE_DECL (clause);
1079 if (TREE_CODE (decl) == VAR_DECL
1080 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1081 break;
1082 if (decl_function_context (decl) != info->context)
1084 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1085 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1086 bitmap_set_bit (new_suppress, DECL_UID (decl));
1087 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1088 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1089 need_chain = true;
1091 break;
1093 case OMP_CLAUSE_SCHEDULE:
1094 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1095 break;
1096 /* FALLTHRU */
1097 case OMP_CLAUSE_FINAL:
1098 case OMP_CLAUSE_IF:
1099 case OMP_CLAUSE_NUM_THREADS:
1100 case OMP_CLAUSE_DEPEND:
1101 case OMP_CLAUSE_DEVICE:
1102 case OMP_CLAUSE_NUM_TEAMS:
1103 case OMP_CLAUSE_THREAD_LIMIT:
1104 case OMP_CLAUSE_SAFELEN:
1105 case OMP_CLAUSE_SIMDLEN:
1106 case OMP_CLAUSE_PRIORITY:
1107 case OMP_CLAUSE_GRAINSIZE:
1108 case OMP_CLAUSE_NUM_TASKS:
1109 case OMP_CLAUSE_HINT:
1110 case OMP_CLAUSE__CILK_FOR_COUNT_:
1111 case OMP_CLAUSE_NUM_GANGS:
1112 case OMP_CLAUSE_NUM_WORKERS:
1113 case OMP_CLAUSE_VECTOR_LENGTH:
1114 case OMP_CLAUSE_GANG:
1115 case OMP_CLAUSE_WORKER:
1116 case OMP_CLAUSE_VECTOR:
1117 case OMP_CLAUSE_ASYNC:
1118 case OMP_CLAUSE_WAIT:
1119 /* Several OpenACC clauses have optional arguments. Check if they
1120 are present. */
1121 if (OMP_CLAUSE_OPERAND (clause, 0))
1123 wi->val_only = true;
1124 wi->is_lhs = false;
1125 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1126 &dummy, wi);
1129 /* The gang clause accepts two arguments. */
1130 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1131 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1133 wi->val_only = true;
1134 wi->is_lhs = false;
1135 convert_nonlocal_reference_op
1136 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1138 break;
1140 case OMP_CLAUSE_DIST_SCHEDULE:
1141 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1143 wi->val_only = true;
1144 wi->is_lhs = false;
1145 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1146 &dummy, wi);
1148 break;
1150 case OMP_CLAUSE_MAP:
1151 case OMP_CLAUSE_TO:
1152 case OMP_CLAUSE_FROM:
1153 if (OMP_CLAUSE_SIZE (clause))
1155 wi->val_only = true;
1156 wi->is_lhs = false;
1157 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1158 &dummy, wi);
1160 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1161 goto do_decl_clause;
1162 wi->val_only = true;
1163 wi->is_lhs = false;
1164 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1165 wi, NULL);
1166 break;
1168 case OMP_CLAUSE_ALIGNED:
1169 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1171 wi->val_only = true;
1172 wi->is_lhs = false;
1173 convert_nonlocal_reference_op
1174 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1176 /* Like do_decl_clause, but don't add any suppression. */
1177 decl = OMP_CLAUSE_DECL (clause);
1178 if (TREE_CODE (decl) == VAR_DECL
1179 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1180 break;
1181 if (decl_function_context (decl) != info->context)
1183 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1184 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1185 need_chain = true;
1187 break;
1189 case OMP_CLAUSE_NOWAIT:
1190 case OMP_CLAUSE_ORDERED:
1191 case OMP_CLAUSE_DEFAULT:
1192 case OMP_CLAUSE_COPYIN:
1193 case OMP_CLAUSE_COLLAPSE:
1194 case OMP_CLAUSE_UNTIED:
1195 case OMP_CLAUSE_MERGEABLE:
1196 case OMP_CLAUSE_PROC_BIND:
1197 case OMP_CLAUSE_NOGROUP:
1198 case OMP_CLAUSE_THREADS:
1199 case OMP_CLAUSE_SIMD:
1200 case OMP_CLAUSE_DEFAULTMAP:
1201 case OMP_CLAUSE_SEQ:
1202 case OMP_CLAUSE_INDEPENDENT:
1203 case OMP_CLAUSE_AUTO:
1204 break;
1206 /* OpenACC tile clauses are discarded during gimplification. */
1207 case OMP_CLAUSE_TILE:
1208 /* The following clause belongs to the OpenACC cache directive, which
1209 is discarded during gimplification. */
1210 case OMP_CLAUSE__CACHE_:
1211 /* The following clauses are only allowed in the OpenMP declare simd
1212 directive, so not seen here. */
1213 case OMP_CLAUSE_UNIFORM:
1214 case OMP_CLAUSE_INBRANCH:
1215 case OMP_CLAUSE_NOTINBRANCH:
1216 /* The following clauses are only allowed on OpenMP cancel and
1217 cancellation point directives, which at this point have already
1218 been lowered into a function call. */
1219 case OMP_CLAUSE_FOR:
1220 case OMP_CLAUSE_PARALLEL:
1221 case OMP_CLAUSE_SECTIONS:
1222 case OMP_CLAUSE_TASKGROUP:
1223 /* The following clauses are only added during OMP lowering; nested
1224 function decomposition happens before that. */
1225 case OMP_CLAUSE__LOOPTEMP_:
1226 case OMP_CLAUSE__SIMDUID_:
1227 case OMP_CLAUSE__GRIDDIM_:
1228 /* Anything else. */
1229 default:
1230 gcc_unreachable ();
1234 info->suppress_expansion = new_suppress;
1236 if (need_stmts)
1237 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1238 switch (OMP_CLAUSE_CODE (clause))
1240 case OMP_CLAUSE_REDUCTION:
1241 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1243 tree old_context
1244 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1245 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1246 = info->context;
1247 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1248 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1249 = info->context;
1250 walk_body (convert_nonlocal_reference_stmt,
1251 convert_nonlocal_reference_op, info,
1252 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1253 walk_body (convert_nonlocal_reference_stmt,
1254 convert_nonlocal_reference_op, info,
1255 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1256 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1257 = old_context;
1258 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1259 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1260 = old_context;
1262 break;
1264 case OMP_CLAUSE_LASTPRIVATE:
1265 walk_body (convert_nonlocal_reference_stmt,
1266 convert_nonlocal_reference_op, info,
1267 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1268 break;
1270 case OMP_CLAUSE_LINEAR:
1271 walk_body (convert_nonlocal_reference_stmt,
1272 convert_nonlocal_reference_op, info,
1273 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1274 break;
1276 default:
1277 break;
1280 return need_chain;
1283 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1285 static void
1286 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1288 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1289 type = TREE_TYPE (type);
1291 if (TYPE_NAME (type)
1292 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1293 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1294 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1296 while (POINTER_TYPE_P (type)
1297 || TREE_CODE (type) == VECTOR_TYPE
1298 || TREE_CODE (type) == FUNCTION_TYPE
1299 || TREE_CODE (type) == METHOD_TYPE)
1300 type = TREE_TYPE (type);
1302 if (TREE_CODE (type) == ARRAY_TYPE)
1304 tree domain, t;
1306 note_nonlocal_vla_type (info, TREE_TYPE (type));
1307 domain = TYPE_DOMAIN (type);
1308 if (domain)
1310 t = TYPE_MIN_VALUE (domain);
1311 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1312 && decl_function_context (t) != info->context)
1313 get_nonlocal_debug_decl (info, t);
1314 t = TYPE_MAX_VALUE (domain);
1315 if (t && (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
1316 && decl_function_context (t) != info->context)
1317 get_nonlocal_debug_decl (info, t);
1322 /* Create nonlocal debug decls for nonlocal VLA array bounds for VLAs
1323 in BLOCK. */
1325 static void
1326 note_nonlocal_block_vlas (struct nesting_info *info, tree block)
1328 tree var;
1330 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
1331 if (TREE_CODE (var) == VAR_DECL
1332 && variably_modified_type_p (TREE_TYPE (var), NULL)
1333 && DECL_HAS_VALUE_EXPR_P (var)
1334 && decl_function_context (var) != info->context)
1335 note_nonlocal_vla_type (info, TREE_TYPE (var));
1338 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1339 PARM_DECLs that belong to outer functions. This handles statements
1340 that are not handled via the standard recursion done in
1341 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1342 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1343 operands of STMT have been handled by this function. */
1345 static tree
1346 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1347 struct walk_stmt_info *wi)
1349 struct nesting_info *info = (struct nesting_info *) wi->info;
1350 tree save_local_var_chain;
1351 bitmap save_suppress;
1352 gimple *stmt = gsi_stmt (*gsi);
1354 switch (gimple_code (stmt))
1356 case GIMPLE_GOTO:
1357 /* Don't walk non-local gotos for now. */
1358 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1360 wi->val_only = true;
1361 wi->is_lhs = false;
1362 *handled_ops_p = false;
1363 return NULL_TREE;
1365 break;
1367 case GIMPLE_OMP_PARALLEL:
1368 case GIMPLE_OMP_TASK:
1369 save_suppress = info->suppress_expansion;
1370 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1371 wi))
1373 tree c, decl;
1374 decl = get_chain_decl (info);
1375 c = build_omp_clause (gimple_location (stmt),
1376 OMP_CLAUSE_FIRSTPRIVATE);
1377 OMP_CLAUSE_DECL (c) = decl;
1378 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1379 gimple_omp_taskreg_set_clauses (stmt, c);
1382 save_local_var_chain = info->new_local_var_chain;
1383 info->new_local_var_chain = NULL;
1385 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1386 info, gimple_omp_body_ptr (stmt));
1388 if (info->new_local_var_chain)
1389 declare_vars (info->new_local_var_chain,
1390 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1391 false);
1392 info->new_local_var_chain = save_local_var_chain;
1393 info->suppress_expansion = save_suppress;
1394 break;
1396 case GIMPLE_OMP_FOR:
1397 save_suppress = info->suppress_expansion;
1398 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1399 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1400 convert_nonlocal_reference_stmt,
1401 convert_nonlocal_reference_op, info);
1402 walk_body (convert_nonlocal_reference_stmt,
1403 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1404 info->suppress_expansion = save_suppress;
1405 break;
1407 case GIMPLE_OMP_SECTIONS:
1408 save_suppress = info->suppress_expansion;
1409 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1410 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1411 info, gimple_omp_body_ptr (stmt));
1412 info->suppress_expansion = save_suppress;
1413 break;
1415 case GIMPLE_OMP_SINGLE:
1416 save_suppress = info->suppress_expansion;
1417 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1418 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1419 info, gimple_omp_body_ptr (stmt));
1420 info->suppress_expansion = save_suppress;
1421 break;
1423 case GIMPLE_OMP_TARGET:
1424 if (!is_gimple_omp_offloaded (stmt))
1426 save_suppress = info->suppress_expansion;
1427 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1428 wi);
1429 info->suppress_expansion = save_suppress;
1430 walk_body (convert_nonlocal_reference_stmt,
1431 convert_nonlocal_reference_op, info,
1432 gimple_omp_body_ptr (stmt));
1433 break;
1435 save_suppress = info->suppress_expansion;
1436 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1437 wi))
1439 tree c, decl;
1440 decl = get_chain_decl (info);
1441 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1442 OMP_CLAUSE_DECL (c) = decl;
1443 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1444 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1445 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1446 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1449 save_local_var_chain = info->new_local_var_chain;
1450 info->new_local_var_chain = NULL;
1452 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1453 info, gimple_omp_body_ptr (stmt));
1455 if (info->new_local_var_chain)
1456 declare_vars (info->new_local_var_chain,
1457 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1458 false);
1459 info->new_local_var_chain = save_local_var_chain;
1460 info->suppress_expansion = save_suppress;
1461 break;
1463 case GIMPLE_OMP_TEAMS:
1464 save_suppress = info->suppress_expansion;
1465 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
1466 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1467 info, gimple_omp_body_ptr (stmt));
1468 info->suppress_expansion = save_suppress;
1469 break;
1471 case GIMPLE_OMP_SECTION:
1472 case GIMPLE_OMP_MASTER:
1473 case GIMPLE_OMP_TASKGROUP:
1474 case GIMPLE_OMP_ORDERED:
1475 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1476 info, gimple_omp_body_ptr (stmt));
1477 break;
1479 case GIMPLE_BIND:
1481 gbind *bind_stmt = as_a <gbind *> (stmt);
1482 if (!optimize && gimple_bind_block (bind_stmt))
1483 note_nonlocal_block_vlas (info, gimple_bind_block (bind_stmt));
1485 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1486 if (TREE_CODE (var) == NAMELIST_DECL)
1488 /* Adjust decls mentioned in NAMELIST_DECL. */
1489 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1490 tree decl;
1491 unsigned int i;
1493 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1495 if (TREE_CODE (decl) == VAR_DECL
1496 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1497 continue;
1498 if (decl_function_context (decl) != info->context)
1499 CONSTRUCTOR_ELT (decls, i)->value
1500 = get_nonlocal_debug_decl (info, decl);
1504 *handled_ops_p = false;
1505 return NULL_TREE;
1507 case GIMPLE_COND:
1508 wi->val_only = true;
1509 wi->is_lhs = false;
1510 *handled_ops_p = false;
1511 return NULL_TREE;
1513 default:
1514 /* For every other statement that we are not interested in
1515 handling here, let the walker traverse the operands. */
1516 *handled_ops_p = false;
1517 return NULL_TREE;
1520 /* We have handled all of STMT operands, no need to traverse the operands. */
1521 *handled_ops_p = true;
1522 return NULL_TREE;
1526 /* A subroutine of convert_local_reference. Create a local variable
1527 in the parent function with DECL_VALUE_EXPR set to reference the
1528 field in FRAME. This is used both for debug info and in OMP
1529 lowering. */
1531 static tree
1532 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1534 tree x, new_decl;
1536 tree *slot = &info->var_map->get_or_insert (decl);
1537 if (*slot)
1538 return *slot;
1540 /* Make sure frame_decl gets created. */
1541 (void) get_frame_type (info);
1542 x = info->frame_decl;
1543 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1545 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1546 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1547 DECL_CONTEXT (new_decl) = info->context;
1548 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1549 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1550 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1551 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1552 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1553 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1554 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1555 if ((TREE_CODE (decl) == PARM_DECL
1556 || TREE_CODE (decl) == RESULT_DECL
1557 || TREE_CODE (decl) == VAR_DECL)
1558 && DECL_BY_REFERENCE (decl))
1559 DECL_BY_REFERENCE (new_decl) = 1;
1561 SET_DECL_VALUE_EXPR (new_decl, x);
1562 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1563 *slot = new_decl;
1565 DECL_CHAIN (new_decl) = info->debug_var_chain;
1566 info->debug_var_chain = new_decl;
1568 /* Do not emit debug info twice. */
1569 DECL_IGNORED_P (decl) = 1;
1571 return new_decl;
1575 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1576 and PARM_DECLs that were referenced by inner nested functions.
1577 The rewrite will be a structure reference to the local frame variable. */
1579 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1581 static tree
1582 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1584 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1585 struct nesting_info *const info = (struct nesting_info *) wi->info;
1586 tree t = *tp, field, x;
1587 bool save_val_only;
1589 *walk_subtrees = 0;
1590 switch (TREE_CODE (t))
1592 case VAR_DECL:
1593 /* Non-automatic variables are never processed. */
1594 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1595 break;
1596 /* FALLTHRU */
1598 case PARM_DECL:
1599 if (decl_function_context (t) == info->context)
1601 /* If we copied a pointer to the frame, then the original decl
1602 is used unchanged in the parent function. */
1603 if (use_pointer_in_frame (t))
1604 break;
1606 /* No need to transform anything if no child references the
1607 variable. */
1608 field = lookup_field_for_decl (info, t, NO_INSERT);
1609 if (!field)
1610 break;
1611 wi->changed = true;
1613 x = get_local_debug_decl (info, t, field);
1614 if (!bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1615 x = get_frame_field (info, info->context, field, &wi->gsi);
1617 if (wi->val_only)
1619 if (wi->is_lhs)
1620 x = save_tmp_var (info, x, &wi->gsi);
1621 else
1622 x = init_tmp_var (info, x, &wi->gsi);
1625 *tp = x;
1627 break;
1629 case ADDR_EXPR:
1630 save_val_only = wi->val_only;
1631 wi->val_only = false;
1632 wi->is_lhs = false;
1633 wi->changed = false;
1634 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1635 wi->val_only = save_val_only;
1637 /* If we converted anything ... */
1638 if (wi->changed)
1640 tree save_context;
1642 /* Then the frame decl is now addressable. */
1643 TREE_ADDRESSABLE (info->frame_decl) = 1;
1645 save_context = current_function_decl;
1646 current_function_decl = info->context;
1647 recompute_tree_invariant_for_addr_expr (t);
1648 current_function_decl = save_context;
1650 /* If we are in a context where we only accept values, then
1651 compute the address into a temporary. */
1652 if (save_val_only)
1653 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1654 t, &wi->gsi);
1656 break;
1658 case REALPART_EXPR:
1659 case IMAGPART_EXPR:
1660 case COMPONENT_REF:
1661 case ARRAY_REF:
1662 case ARRAY_RANGE_REF:
1663 case BIT_FIELD_REF:
1664 /* Go down this entire nest and just look at the final prefix and
1665 anything that describes the references. Otherwise, we lose track
1666 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1667 save_val_only = wi->val_only;
1668 wi->val_only = true;
1669 wi->is_lhs = false;
1670 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1672 if (TREE_CODE (t) == COMPONENT_REF)
1673 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1674 NULL);
1675 else if (TREE_CODE (t) == ARRAY_REF
1676 || TREE_CODE (t) == ARRAY_RANGE_REF)
1678 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1679 NULL);
1680 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1681 NULL);
1682 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1683 NULL);
1686 wi->val_only = false;
1687 walk_tree (tp, convert_local_reference_op, wi, NULL);
1688 wi->val_only = save_val_only;
1689 break;
1691 case MEM_REF:
1692 save_val_only = wi->val_only;
1693 wi->val_only = true;
1694 wi->is_lhs = false;
1695 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1696 wi, NULL);
1697 /* We need to re-fold the MEM_REF as component references as
1698 part of a ADDR_EXPR address are not allowed. But we cannot
1699 fold here, as the chain record type is not yet finalized. */
1700 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1701 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1702 info->mem_refs->add (tp);
1703 wi->val_only = save_val_only;
1704 break;
1706 case VIEW_CONVERT_EXPR:
1707 /* Just request to look at the subtrees, leaving val_only and lhs
1708 untouched. This might actually be for !val_only + lhs, in which
1709 case we don't want to force a replacement by a temporary. */
1710 *walk_subtrees = 1;
1711 break;
1713 default:
1714 if (!IS_TYPE_OR_DECL_P (t))
1716 *walk_subtrees = 1;
1717 wi->val_only = true;
1718 wi->is_lhs = false;
1720 break;
1723 return NULL_TREE;
1726 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1727 struct walk_stmt_info *);
1729 /* Helper for convert_local_reference. Convert all the references in
1730 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1732 static bool
1733 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1735 struct nesting_info *const info = (struct nesting_info *) wi->info;
1736 bool need_frame = false, need_stmts = false;
1737 tree clause, decl;
1738 int dummy;
1739 bitmap new_suppress;
1741 new_suppress = BITMAP_GGC_ALLOC ();
1742 bitmap_copy (new_suppress, info->suppress_expansion);
1744 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1746 switch (OMP_CLAUSE_CODE (clause))
1748 case OMP_CLAUSE_REDUCTION:
1749 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1750 need_stmts = true;
1751 goto do_decl_clause;
1753 case OMP_CLAUSE_LASTPRIVATE:
1754 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1755 need_stmts = true;
1756 goto do_decl_clause;
1758 case OMP_CLAUSE_LINEAR:
1759 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1760 need_stmts = true;
1761 wi->val_only = true;
1762 wi->is_lhs = false;
1763 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1764 wi);
1765 goto do_decl_clause;
1767 case OMP_CLAUSE_PRIVATE:
1768 case OMP_CLAUSE_FIRSTPRIVATE:
1769 case OMP_CLAUSE_COPYPRIVATE:
1770 case OMP_CLAUSE_SHARED:
1771 case OMP_CLAUSE_TO_DECLARE:
1772 case OMP_CLAUSE_LINK:
1773 case OMP_CLAUSE_USE_DEVICE_PTR:
1774 case OMP_CLAUSE_IS_DEVICE_PTR:
1775 do_decl_clause:
1776 decl = OMP_CLAUSE_DECL (clause);
1777 if (TREE_CODE (decl) == VAR_DECL
1778 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1779 break;
1780 if (decl_function_context (decl) == info->context
1781 && !use_pointer_in_frame (decl))
1783 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1784 if (field)
1786 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1787 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1788 bitmap_set_bit (new_suppress, DECL_UID (decl));
1789 OMP_CLAUSE_DECL (clause)
1790 = get_local_debug_decl (info, decl, field);
1791 need_frame = true;
1794 break;
1796 case OMP_CLAUSE_SCHEDULE:
1797 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1798 break;
1799 /* FALLTHRU */
1800 case OMP_CLAUSE_FINAL:
1801 case OMP_CLAUSE_IF:
1802 case OMP_CLAUSE_NUM_THREADS:
1803 case OMP_CLAUSE_DEPEND:
1804 case OMP_CLAUSE_DEVICE:
1805 case OMP_CLAUSE_NUM_TEAMS:
1806 case OMP_CLAUSE_THREAD_LIMIT:
1807 case OMP_CLAUSE_SAFELEN:
1808 case OMP_CLAUSE_SIMDLEN:
1809 case OMP_CLAUSE_PRIORITY:
1810 case OMP_CLAUSE_GRAINSIZE:
1811 case OMP_CLAUSE_NUM_TASKS:
1812 case OMP_CLAUSE_HINT:
1813 case OMP_CLAUSE__CILK_FOR_COUNT_:
1814 case OMP_CLAUSE_NUM_GANGS:
1815 case OMP_CLAUSE_NUM_WORKERS:
1816 case OMP_CLAUSE_VECTOR_LENGTH:
1817 case OMP_CLAUSE_GANG:
1818 case OMP_CLAUSE_WORKER:
1819 case OMP_CLAUSE_VECTOR:
1820 case OMP_CLAUSE_ASYNC:
1821 case OMP_CLAUSE_WAIT:
1822 /* Several OpenACC clauses have optional arguments. Check if they
1823 are present. */
1824 if (OMP_CLAUSE_OPERAND (clause, 0))
1826 wi->val_only = true;
1827 wi->is_lhs = false;
1828 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1829 &dummy, wi);
1832 /* The gang clause accepts two arguments. */
1833 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1834 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1836 wi->val_only = true;
1837 wi->is_lhs = false;
1838 convert_nonlocal_reference_op
1839 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1841 break;
1843 case OMP_CLAUSE_DIST_SCHEDULE:
1844 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1846 wi->val_only = true;
1847 wi->is_lhs = false;
1848 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1849 &dummy, wi);
1851 break;
1853 case OMP_CLAUSE_MAP:
1854 case OMP_CLAUSE_TO:
1855 case OMP_CLAUSE_FROM:
1856 if (OMP_CLAUSE_SIZE (clause))
1858 wi->val_only = true;
1859 wi->is_lhs = false;
1860 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
1861 &dummy, wi);
1863 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1864 goto do_decl_clause;
1865 wi->val_only = true;
1866 wi->is_lhs = false;
1867 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
1868 wi, NULL);
1869 break;
1871 case OMP_CLAUSE_ALIGNED:
1872 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1874 wi->val_only = true;
1875 wi->is_lhs = false;
1876 convert_local_reference_op
1877 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1879 /* Like do_decl_clause, but don't add any suppression. */
1880 decl = OMP_CLAUSE_DECL (clause);
1881 if (TREE_CODE (decl) == VAR_DECL
1882 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1883 break;
1884 if (decl_function_context (decl) == info->context
1885 && !use_pointer_in_frame (decl))
1887 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
1888 if (field)
1890 OMP_CLAUSE_DECL (clause)
1891 = get_local_debug_decl (info, decl, field);
1892 need_frame = true;
1895 break;
1897 case OMP_CLAUSE_NOWAIT:
1898 case OMP_CLAUSE_ORDERED:
1899 case OMP_CLAUSE_DEFAULT:
1900 case OMP_CLAUSE_COPYIN:
1901 case OMP_CLAUSE_COLLAPSE:
1902 case OMP_CLAUSE_UNTIED:
1903 case OMP_CLAUSE_MERGEABLE:
1904 case OMP_CLAUSE_PROC_BIND:
1905 case OMP_CLAUSE_NOGROUP:
1906 case OMP_CLAUSE_THREADS:
1907 case OMP_CLAUSE_SIMD:
1908 case OMP_CLAUSE_DEFAULTMAP:
1909 case OMP_CLAUSE_SEQ:
1910 case OMP_CLAUSE_INDEPENDENT:
1911 case OMP_CLAUSE_AUTO:
1912 break;
1914 /* OpenACC tile clauses are discarded during gimplification. */
1915 case OMP_CLAUSE_TILE:
1916 /* The following clause belongs to the OpenACC cache directive, which
1917 is discarded during gimplification. */
1918 case OMP_CLAUSE__CACHE_:
1919 /* The following clauses are only allowed in the OpenMP declare simd
1920 directive, so not seen here. */
1921 case OMP_CLAUSE_UNIFORM:
1922 case OMP_CLAUSE_INBRANCH:
1923 case OMP_CLAUSE_NOTINBRANCH:
1924 /* The following clauses are only allowed on OpenMP cancel and
1925 cancellation point directives, which at this point have already
1926 been lowered into a function call. */
1927 case OMP_CLAUSE_FOR:
1928 case OMP_CLAUSE_PARALLEL:
1929 case OMP_CLAUSE_SECTIONS:
1930 case OMP_CLAUSE_TASKGROUP:
1931 /* The following clauses are only added during OMP lowering; nested
1932 function decomposition happens before that. */
1933 case OMP_CLAUSE__LOOPTEMP_:
1934 case OMP_CLAUSE__SIMDUID_:
1935 case OMP_CLAUSE__GRIDDIM_:
1936 /* Anything else. */
1937 default:
1938 gcc_unreachable ();
1942 info->suppress_expansion = new_suppress;
1944 if (need_stmts)
1945 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1946 switch (OMP_CLAUSE_CODE (clause))
1948 case OMP_CLAUSE_REDUCTION:
1949 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1951 tree old_context
1952 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1953 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1954 = info->context;
1955 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1956 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1957 = info->context;
1958 walk_body (convert_local_reference_stmt,
1959 convert_local_reference_op, info,
1960 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
1961 walk_body (convert_local_reference_stmt,
1962 convert_local_reference_op, info,
1963 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
1964 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1965 = old_context;
1966 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1967 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1968 = old_context;
1970 break;
1972 case OMP_CLAUSE_LASTPRIVATE:
1973 walk_body (convert_local_reference_stmt,
1974 convert_local_reference_op, info,
1975 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
1976 break;
1978 case OMP_CLAUSE_LINEAR:
1979 walk_body (convert_local_reference_stmt,
1980 convert_local_reference_op, info,
1981 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
1982 break;
1984 default:
1985 break;
1988 return need_frame;
1992 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1993 and PARM_DECLs that were referenced by inner nested functions.
1994 The rewrite will be a structure reference to the local frame variable. */
1996 static tree
1997 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1998 struct walk_stmt_info *wi)
2000 struct nesting_info *info = (struct nesting_info *) wi->info;
2001 tree save_local_var_chain;
2002 bitmap save_suppress;
2003 gimple *stmt = gsi_stmt (*gsi);
2005 switch (gimple_code (stmt))
2007 case GIMPLE_OMP_PARALLEL:
2008 case GIMPLE_OMP_TASK:
2009 save_suppress = info->suppress_expansion;
2010 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2011 wi))
2013 tree c;
2014 (void) get_frame_type (info);
2015 c = build_omp_clause (gimple_location (stmt),
2016 OMP_CLAUSE_SHARED);
2017 OMP_CLAUSE_DECL (c) = info->frame_decl;
2018 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2019 gimple_omp_taskreg_set_clauses (stmt, c);
2022 save_local_var_chain = info->new_local_var_chain;
2023 info->new_local_var_chain = NULL;
2025 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2026 gimple_omp_body_ptr (stmt));
2028 if (info->new_local_var_chain)
2029 declare_vars (info->new_local_var_chain,
2030 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2031 info->new_local_var_chain = save_local_var_chain;
2032 info->suppress_expansion = save_suppress;
2033 break;
2035 case GIMPLE_OMP_FOR:
2036 save_suppress = info->suppress_expansion;
2037 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2038 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2039 convert_local_reference_stmt,
2040 convert_local_reference_op, info);
2041 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2042 info, gimple_omp_body_ptr (stmt));
2043 info->suppress_expansion = save_suppress;
2044 break;
2046 case GIMPLE_OMP_SECTIONS:
2047 save_suppress = info->suppress_expansion;
2048 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2049 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2050 info, gimple_omp_body_ptr (stmt));
2051 info->suppress_expansion = save_suppress;
2052 break;
2054 case GIMPLE_OMP_SINGLE:
2055 save_suppress = info->suppress_expansion;
2056 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2057 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2058 info, gimple_omp_body_ptr (stmt));
2059 info->suppress_expansion = save_suppress;
2060 break;
2062 case GIMPLE_OMP_TARGET:
2063 if (!is_gimple_omp_offloaded (stmt))
2065 save_suppress = info->suppress_expansion;
2066 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2067 info->suppress_expansion = save_suppress;
2068 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2069 info, gimple_omp_body_ptr (stmt));
2070 break;
2072 save_suppress = info->suppress_expansion;
2073 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2075 tree c;
2076 (void) get_frame_type (info);
2077 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2078 OMP_CLAUSE_DECL (c) = info->frame_decl;
2079 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2080 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2081 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2082 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2085 save_local_var_chain = info->new_local_var_chain;
2086 info->new_local_var_chain = NULL;
2088 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2089 gimple_omp_body_ptr (stmt));
2091 if (info->new_local_var_chain)
2092 declare_vars (info->new_local_var_chain,
2093 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2094 info->new_local_var_chain = save_local_var_chain;
2095 info->suppress_expansion = save_suppress;
2096 break;
2098 case GIMPLE_OMP_TEAMS:
2099 save_suppress = info->suppress_expansion;
2100 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2101 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2102 info, gimple_omp_body_ptr (stmt));
2103 info->suppress_expansion = save_suppress;
2104 break;
2106 case GIMPLE_OMP_SECTION:
2107 case GIMPLE_OMP_MASTER:
2108 case GIMPLE_OMP_TASKGROUP:
2109 case GIMPLE_OMP_ORDERED:
2110 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2111 info, gimple_omp_body_ptr (stmt));
2112 break;
2114 case GIMPLE_COND:
2115 wi->val_only = true;
2116 wi->is_lhs = false;
2117 *handled_ops_p = false;
2118 return NULL_TREE;
2120 case GIMPLE_ASSIGN:
2121 if (gimple_clobber_p (stmt))
2123 tree lhs = gimple_assign_lhs (stmt);
2124 if (!use_pointer_in_frame (lhs)
2125 && lookup_field_for_decl (info, lhs, NO_INSERT))
2127 gsi_replace (gsi, gimple_build_nop (), true);
2128 break;
2131 *handled_ops_p = false;
2132 return NULL_TREE;
2134 case GIMPLE_BIND:
2135 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2136 var;
2137 var = DECL_CHAIN (var))
2138 if (TREE_CODE (var) == NAMELIST_DECL)
2140 /* Adjust decls mentioned in NAMELIST_DECL. */
2141 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2142 tree decl;
2143 unsigned int i;
2145 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2147 if (TREE_CODE (decl) == VAR_DECL
2148 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2149 continue;
2150 if (decl_function_context (decl) == info->context
2151 && !use_pointer_in_frame (decl))
2153 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2154 if (field)
2156 CONSTRUCTOR_ELT (decls, i)->value
2157 = get_local_debug_decl (info, decl, field);
2163 *handled_ops_p = false;
2164 return NULL_TREE;
2166 default:
2167 /* For every other statement that we are not interested in
2168 handling here, let the walker traverse the operands. */
2169 *handled_ops_p = false;
2170 return NULL_TREE;
2173 /* Indicate that we have handled all the operands ourselves. */
2174 *handled_ops_p = true;
2175 return NULL_TREE;
2179 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2180 that reference labels from outer functions. The rewrite will be a
2181 call to __builtin_nonlocal_goto. */
2183 static tree
2184 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2185 struct walk_stmt_info *wi)
2187 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2188 tree label, new_label, target_context, x, field;
2189 gcall *call;
2190 gimple *stmt = gsi_stmt (*gsi);
2192 if (gimple_code (stmt) != GIMPLE_GOTO)
2194 *handled_ops_p = false;
2195 return NULL_TREE;
2198 label = gimple_goto_dest (stmt);
2199 if (TREE_CODE (label) != LABEL_DECL)
2201 *handled_ops_p = false;
2202 return NULL_TREE;
2205 target_context = decl_function_context (label);
2206 if (target_context == info->context)
2208 *handled_ops_p = false;
2209 return NULL_TREE;
2212 for (i = info->outer; target_context != i->context; i = i->outer)
2213 continue;
2215 /* The original user label may also be use for a normal goto, therefore
2216 we must create a new label that will actually receive the abnormal
2217 control transfer. This new label will be marked LABEL_NONLOCAL; this
2218 mark will trigger proper behavior in the cfg, as well as cause the
2219 (hairy target-specific) non-local goto receiver code to be generated
2220 when we expand rtl. Enter this association into var_map so that we
2221 can insert the new label into the IL during a second pass. */
2222 tree *slot = &i->var_map->get_or_insert (label);
2223 if (*slot == NULL)
2225 new_label = create_artificial_label (UNKNOWN_LOCATION);
2226 DECL_NONLOCAL (new_label) = 1;
2227 *slot = new_label;
2229 else
2230 new_label = *slot;
2232 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2233 field = get_nl_goto_field (i);
2234 x = get_frame_field (info, target_context, field, gsi);
2235 x = build_addr (x);
2236 x = gsi_gimplify_val (info, x, gsi);
2237 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2238 2, build_addr (new_label), x);
2239 gsi_replace (gsi, call, false);
2241 /* We have handled all of STMT's operands, no need to keep going. */
2242 *handled_ops_p = true;
2243 return NULL_TREE;
2247 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2248 are referenced via nonlocal goto from a nested function. The rewrite
2249 will involve installing a newly generated DECL_NONLOCAL label, and
2250 (potentially) a branch around the rtl gunk that is assumed to be
2251 attached to such a label. */
2253 static tree
2254 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2255 struct walk_stmt_info *wi)
2257 struct nesting_info *const info = (struct nesting_info *) wi->info;
2258 tree label, new_label;
2259 gimple_stmt_iterator tmp_gsi;
2260 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2262 if (!stmt)
2264 *handled_ops_p = false;
2265 return NULL_TREE;
2268 label = gimple_label_label (stmt);
2270 tree *slot = info->var_map->get (label);
2271 if (!slot)
2273 *handled_ops_p = false;
2274 return NULL_TREE;
2277 /* If there's any possibility that the previous statement falls through,
2278 then we must branch around the new non-local label. */
2279 tmp_gsi = wi->gsi;
2280 gsi_prev (&tmp_gsi);
2281 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2283 gimple *stmt = gimple_build_goto (label);
2284 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2287 new_label = (tree) *slot;
2288 stmt = gimple_build_label (new_label);
2289 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2291 *handled_ops_p = true;
2292 return NULL_TREE;
2296 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2297 of nested functions that require the use of trampolines. The rewrite
2298 will involve a reference a trampoline generated for the occasion. */
2300 static tree
2301 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2303 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2304 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2305 tree t = *tp, decl, target_context, x, builtin;
2306 gcall *call;
2308 *walk_subtrees = 0;
2309 switch (TREE_CODE (t))
2311 case ADDR_EXPR:
2312 /* Build
2313 T.1 = &CHAIN->tramp;
2314 T.2 = __builtin_adjust_trampoline (T.1);
2315 T.3 = (func_type)T.2;
2318 decl = TREE_OPERAND (t, 0);
2319 if (TREE_CODE (decl) != FUNCTION_DECL)
2320 break;
2322 /* Only need to process nested functions. */
2323 target_context = decl_function_context (decl);
2324 if (!target_context)
2325 break;
2327 /* If the nested function doesn't use a static chain, then
2328 it doesn't need a trampoline. */
2329 if (!DECL_STATIC_CHAIN (decl))
2330 break;
2332 /* If we don't want a trampoline, then don't build one. */
2333 if (TREE_NO_TRAMPOLINE (t))
2334 break;
2336 /* Lookup the immediate parent of the callee, as that's where
2337 we need to insert the trampoline. */
2338 for (i = info; i->context != target_context; i = i->outer)
2339 continue;
2340 x = lookup_tramp_for_decl (i, decl, INSERT);
2342 /* Compute the address of the field holding the trampoline. */
2343 x = get_frame_field (info, target_context, x, &wi->gsi);
2344 x = build_addr (x);
2345 x = gsi_gimplify_val (info, x, &wi->gsi);
2347 /* Do machine-specific ugliness. Normally this will involve
2348 computing extra alignment, but it can really be anything. */
2349 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2350 call = gimple_build_call (builtin, 1, x);
2351 x = init_tmp_var_with_call (info, &wi->gsi, call);
2353 /* Cast back to the proper function type. */
2354 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2355 x = init_tmp_var (info, x, &wi->gsi);
2357 *tp = x;
2358 break;
2360 default:
2361 if (!IS_TYPE_OR_DECL_P (t))
2362 *walk_subtrees = 1;
2363 break;
2366 return NULL_TREE;
2370 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2371 to addresses of nested functions that require the use of
2372 trampolines. The rewrite will involve a reference a trampoline
2373 generated for the occasion. */
2375 static tree
2376 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2377 struct walk_stmt_info *wi)
2379 struct nesting_info *info = (struct nesting_info *) wi->info;
2380 gimple *stmt = gsi_stmt (*gsi);
2382 switch (gimple_code (stmt))
2384 case GIMPLE_CALL:
2386 /* Only walk call arguments, lest we generate trampolines for
2387 direct calls. */
2388 unsigned long i, nargs = gimple_call_num_args (stmt);
2389 for (i = 0; i < nargs; i++)
2390 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2391 wi, NULL);
2392 break;
2395 case GIMPLE_OMP_TARGET:
2396 if (!is_gimple_omp_offloaded (stmt))
2398 *handled_ops_p = false;
2399 return NULL_TREE;
2401 /* FALLTHRU */
2402 case GIMPLE_OMP_PARALLEL:
2403 case GIMPLE_OMP_TASK:
2405 tree save_local_var_chain = info->new_local_var_chain;
2406 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2407 info->new_local_var_chain = NULL;
2408 char save_static_chain_added = info->static_chain_added;
2409 info->static_chain_added = 0;
2410 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2411 info, gimple_omp_body_ptr (stmt));
2412 if (info->new_local_var_chain)
2413 declare_vars (info->new_local_var_chain,
2414 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2415 false);
2416 for (int i = 0; i < 2; i++)
2418 tree c, decl;
2419 if ((info->static_chain_added & (1 << i)) == 0)
2420 continue;
2421 decl = i ? get_chain_decl (info) : info->frame_decl;
2422 /* Don't add CHAIN.* or FRAME.* twice. */
2423 for (c = gimple_omp_taskreg_clauses (stmt);
2425 c = OMP_CLAUSE_CHAIN (c))
2426 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2427 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2428 && OMP_CLAUSE_DECL (c) == decl)
2429 break;
2430 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2432 c = build_omp_clause (gimple_location (stmt),
2433 i ? OMP_CLAUSE_FIRSTPRIVATE
2434 : OMP_CLAUSE_SHARED);
2435 OMP_CLAUSE_DECL (c) = decl;
2436 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2437 gimple_omp_taskreg_set_clauses (stmt, c);
2439 else if (c == NULL)
2441 c = build_omp_clause (gimple_location (stmt),
2442 OMP_CLAUSE_MAP);
2443 OMP_CLAUSE_DECL (c) = decl;
2444 OMP_CLAUSE_SET_MAP_KIND (c,
2445 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2446 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2447 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2448 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2452 info->new_local_var_chain = save_local_var_chain;
2453 info->static_chain_added |= save_static_chain_added;
2455 break;
2457 default:
2458 *handled_ops_p = false;
2459 return NULL_TREE;
2462 *handled_ops_p = true;
2463 return NULL_TREE;
2468 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2469 that reference nested functions to make sure that the static chain
2470 is set up properly for the call. */
2472 static tree
2473 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2474 struct walk_stmt_info *wi)
2476 struct nesting_info *const info = (struct nesting_info *) wi->info;
2477 tree decl, target_context;
2478 char save_static_chain_added;
2479 int i;
2480 gimple *stmt = gsi_stmt (*gsi);
2482 switch (gimple_code (stmt))
2484 case GIMPLE_CALL:
2485 if (gimple_call_chain (stmt))
2486 break;
2487 decl = gimple_call_fndecl (stmt);
2488 if (!decl)
2489 break;
2490 target_context = decl_function_context (decl);
2491 if (target_context && DECL_STATIC_CHAIN (decl))
2493 gimple_call_set_chain (as_a <gcall *> (stmt),
2494 get_static_chain (info, target_context,
2495 &wi->gsi));
2496 info->static_chain_added |= (1 << (info->context != target_context));
2498 break;
2500 case GIMPLE_OMP_PARALLEL:
2501 case GIMPLE_OMP_TASK:
2502 save_static_chain_added = info->static_chain_added;
2503 info->static_chain_added = 0;
2504 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2505 for (i = 0; i < 2; i++)
2507 tree c, decl;
2508 if ((info->static_chain_added & (1 << i)) == 0)
2509 continue;
2510 decl = i ? get_chain_decl (info) : info->frame_decl;
2511 /* Don't add CHAIN.* or FRAME.* twice. */
2512 for (c = gimple_omp_taskreg_clauses (stmt);
2514 c = OMP_CLAUSE_CHAIN (c))
2515 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2516 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2517 && OMP_CLAUSE_DECL (c) == decl)
2518 break;
2519 if (c == NULL)
2521 c = build_omp_clause (gimple_location (stmt),
2522 i ? OMP_CLAUSE_FIRSTPRIVATE
2523 : OMP_CLAUSE_SHARED);
2524 OMP_CLAUSE_DECL (c) = decl;
2525 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2526 gimple_omp_taskreg_set_clauses (stmt, c);
2529 info->static_chain_added |= save_static_chain_added;
2530 break;
2532 case GIMPLE_OMP_TARGET:
2533 if (!is_gimple_omp_offloaded (stmt))
2535 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2536 break;
2538 save_static_chain_added = info->static_chain_added;
2539 info->static_chain_added = 0;
2540 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2541 for (i = 0; i < 2; i++)
2543 tree c, decl;
2544 if ((info->static_chain_added & (1 << i)) == 0)
2545 continue;
2546 decl = i ? get_chain_decl (info) : info->frame_decl;
2547 /* Don't add CHAIN.* or FRAME.* twice. */
2548 for (c = gimple_omp_target_clauses (stmt);
2550 c = OMP_CLAUSE_CHAIN (c))
2551 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2552 && OMP_CLAUSE_DECL (c) == decl)
2553 break;
2554 if (c == NULL)
2556 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2557 OMP_CLAUSE_DECL (c) = decl;
2558 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2559 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2560 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2561 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2565 info->static_chain_added |= save_static_chain_added;
2566 break;
2568 case GIMPLE_OMP_FOR:
2569 walk_body (convert_gimple_call, NULL, info,
2570 gimple_omp_for_pre_body_ptr (stmt));
2571 /* FALLTHRU */
2572 case GIMPLE_OMP_SECTIONS:
2573 case GIMPLE_OMP_SECTION:
2574 case GIMPLE_OMP_SINGLE:
2575 case GIMPLE_OMP_TEAMS:
2576 case GIMPLE_OMP_MASTER:
2577 case GIMPLE_OMP_TASKGROUP:
2578 case GIMPLE_OMP_ORDERED:
2579 case GIMPLE_OMP_CRITICAL:
2580 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2581 break;
2583 default:
2584 /* Keep looking for other operands. */
2585 *handled_ops_p = false;
2586 return NULL_TREE;
2589 *handled_ops_p = true;
2590 return NULL_TREE;
2593 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2594 call expressions. At the same time, determine if a nested function
2595 actually uses its static chain; if not, remember that. */
2597 static void
2598 convert_all_function_calls (struct nesting_info *root)
2600 unsigned int chain_count = 0, old_chain_count, iter_count;
2601 struct nesting_info *n;
2603 /* First, optimistically clear static_chain for all decls that haven't
2604 used the static chain already for variable access. But always create
2605 it if not optimizing. This makes it possible to reconstruct the static
2606 nesting tree at run time and thus to resolve up-level references from
2607 within the debugger. */
2608 FOR_EACH_NEST_INFO (n, root)
2610 tree decl = n->context;
2611 if (!optimize)
2613 if (n->inner)
2614 (void) get_frame_type (n);
2615 if (n->outer)
2616 (void) get_chain_decl (n);
2618 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2620 DECL_STATIC_CHAIN (decl) = 0;
2621 if (dump_file && (dump_flags & TDF_DETAILS))
2622 fprintf (dump_file, "Guessing no static-chain for %s\n",
2623 lang_hooks.decl_printable_name (decl, 2));
2625 else
2626 DECL_STATIC_CHAIN (decl) = 1;
2627 chain_count += DECL_STATIC_CHAIN (decl);
2630 /* Walk the functions and perform transformations. Note that these
2631 transformations can induce new uses of the static chain, which in turn
2632 require re-examining all users of the decl. */
2633 /* ??? It would make sense to try to use the call graph to speed this up,
2634 but the call graph hasn't really been built yet. Even if it did, we
2635 would still need to iterate in this loop since address-of references
2636 wouldn't show up in the callgraph anyway. */
2637 iter_count = 0;
2640 old_chain_count = chain_count;
2641 chain_count = 0;
2642 iter_count++;
2644 if (dump_file && (dump_flags & TDF_DETAILS))
2645 fputc ('\n', dump_file);
2647 FOR_EACH_NEST_INFO (n, root)
2649 tree decl = n->context;
2650 walk_function (convert_tramp_reference_stmt,
2651 convert_tramp_reference_op, n);
2652 walk_function (convert_gimple_call, NULL, n);
2653 chain_count += DECL_STATIC_CHAIN (decl);
2656 while (chain_count != old_chain_count);
2658 if (dump_file && (dump_flags & TDF_DETAILS))
2659 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2660 iter_count);
2663 struct nesting_copy_body_data
2665 copy_body_data cb;
2666 struct nesting_info *root;
2669 /* A helper subroutine for debug_var_chain type remapping. */
2671 static tree
2672 nesting_copy_decl (tree decl, copy_body_data *id)
2674 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
2675 tree *slot = nid->root->var_map->get (decl);
2677 if (slot)
2678 return (tree) *slot;
2680 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
2682 tree new_decl = copy_decl_no_change (decl, id);
2683 DECL_ORIGINAL_TYPE (new_decl)
2684 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
2685 return new_decl;
2688 if (TREE_CODE (decl) == VAR_DECL
2689 || TREE_CODE (decl) == PARM_DECL
2690 || TREE_CODE (decl) == RESULT_DECL)
2691 return decl;
2693 return copy_decl_no_change (decl, id);
2696 /* A helper function for remap_vla_decls. See if *TP contains
2697 some remapped variables. */
2699 static tree
2700 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
2702 struct nesting_info *root = (struct nesting_info *) data;
2703 tree t = *tp;
2705 if (DECL_P (t))
2707 *walk_subtrees = 0;
2708 tree *slot = root->var_map->get (t);
2710 if (slot)
2711 return *slot;
2713 return NULL;
2716 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
2717 involved. */
2719 static void
2720 remap_vla_decls (tree block, struct nesting_info *root)
2722 tree var, subblock, val, type;
2723 struct nesting_copy_body_data id;
2725 for (subblock = BLOCK_SUBBLOCKS (block);
2726 subblock;
2727 subblock = BLOCK_CHAIN (subblock))
2728 remap_vla_decls (subblock, root);
2730 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
2731 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2733 val = DECL_VALUE_EXPR (var);
2734 type = TREE_TYPE (var);
2736 if (!(TREE_CODE (val) == INDIRECT_REF
2737 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2738 && variably_modified_type_p (type, NULL)))
2739 continue;
2741 if (root->var_map->get (TREE_OPERAND (val, 0))
2742 || walk_tree (&type, contains_remapped_vars, root, NULL))
2743 break;
2746 if (var == NULL_TREE)
2747 return;
2749 memset (&id, 0, sizeof (id));
2750 id.cb.copy_decl = nesting_copy_decl;
2751 id.cb.decl_map = new hash_map<tree, tree>;
2752 id.root = root;
2754 for (; var; var = DECL_CHAIN (var))
2755 if (TREE_CODE (var) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (var))
2757 struct nesting_info *i;
2758 tree newt, context;
2760 val = DECL_VALUE_EXPR (var);
2761 type = TREE_TYPE (var);
2763 if (!(TREE_CODE (val) == INDIRECT_REF
2764 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
2765 && variably_modified_type_p (type, NULL)))
2766 continue;
2768 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
2769 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
2770 continue;
2772 context = decl_function_context (var);
2773 for (i = root; i; i = i->outer)
2774 if (i->context == context)
2775 break;
2777 if (i == NULL)
2778 continue;
2780 /* Fully expand value expressions. This avoids having debug variables
2781 only referenced from them and that can be swept during GC. */
2782 if (slot)
2784 tree t = (tree) *slot;
2785 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
2786 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
2789 id.cb.src_fn = i->context;
2790 id.cb.dst_fn = i->context;
2791 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
2793 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
2794 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
2796 newt = TREE_TYPE (newt);
2797 type = TREE_TYPE (type);
2799 if (TYPE_NAME (newt)
2800 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
2801 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
2802 && newt != type
2803 && TYPE_NAME (newt) == TYPE_NAME (type))
2804 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
2806 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
2807 if (val != DECL_VALUE_EXPR (var))
2808 SET_DECL_VALUE_EXPR (var, val);
2811 delete id.cb.decl_map;
2814 /* Fold the MEM_REF *E. */
2815 bool
2816 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
2818 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
2819 *ref_p = fold (*ref_p);
2820 return true;
2823 /* Do "everything else" to clean up or complete state collected by the various
2824 walking passes -- create a field to hold the frame base address, lay out the
2825 types and decls, generate code to initialize the frame decl, store critical
2826 expressions in the struct function for rtl to find. */
2828 static void
2829 finalize_nesting_tree_1 (struct nesting_info *root)
2831 gimple_seq stmt_list;
2832 gimple *stmt;
2833 tree context = root->context;
2834 struct function *sf;
2836 stmt_list = NULL;
2838 /* If we created a non-local frame type or decl, we need to lay them
2839 out at this time. */
2840 if (root->frame_type)
2842 /* Debugging information needs to compute the frame base address of the
2843 parent frame out of the static chain from the nested frame.
2845 The static chain is the address of the FRAME record, so one could
2846 imagine it would be possible to compute the frame base address just
2847 adding a constant offset to this address. Unfortunately, this is not
2848 possible: if the FRAME object has alignment constraints that are
2849 stronger than the stack, then the offset between the frame base and
2850 the FRAME object will be dynamic.
2852 What we do instead is to append a field to the FRAME object that holds
2853 the frame base address: then debug info just has to fetch this
2854 field. */
2856 /* Debugging information will refer to the CFA as the frame base
2857 address: we will do the same here. */
2858 const tree frame_addr_fndecl
2859 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
2861 /* Create a field in the FRAME record to hold the frame base address for
2862 this stack frame. Since it will be used only by the debugger, put it
2863 at the end of the record in order not to shift all other offsets. */
2864 tree fb_decl = make_node (FIELD_DECL);
2866 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
2867 TREE_TYPE (fb_decl) = ptr_type_node;
2868 TREE_ADDRESSABLE (fb_decl) = 1;
2869 DECL_CONTEXT (fb_decl) = root->frame_type;
2870 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
2871 fb_decl);
2873 /* In some cases the frame type will trigger the -Wpadded warning.
2874 This is not helpful; suppress it. */
2875 int save_warn_padded = warn_padded;
2876 warn_padded = 0;
2877 layout_type (root->frame_type);
2878 warn_padded = save_warn_padded;
2879 layout_decl (root->frame_decl, 0);
2881 /* Initialize the frame base address field. If the builtin we need is
2882 not available, set it to NULL so that debugging information does not
2883 reference junk. */
2884 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
2885 root->frame_decl, fb_decl, NULL_TREE);
2886 tree fb_tmp;
2888 if (frame_addr_fndecl != NULL_TREE)
2890 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
2891 integer_zero_node);
2892 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2894 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
2896 else
2897 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
2898 gimple_seq_add_stmt (&stmt_list,
2899 gimple_build_assign (fb_ref, fb_tmp));
2901 /* Remove root->frame_decl from root->new_local_var_chain, so
2902 that we can declare it also in the lexical blocks, which
2903 helps ensure virtual regs that end up appearing in its RTL
2904 expression get substituted in instantiate_virtual_regs(). */
2905 tree *adjust;
2906 for (adjust = &root->new_local_var_chain;
2907 *adjust != root->frame_decl;
2908 adjust = &DECL_CHAIN (*adjust))
2909 gcc_assert (DECL_CHAIN (*adjust));
2910 *adjust = DECL_CHAIN (*adjust);
2912 DECL_CHAIN (root->frame_decl) = NULL_TREE;
2913 declare_vars (root->frame_decl,
2914 gimple_seq_first_stmt (gimple_body (context)), true);
2917 /* If any parameters were referenced non-locally, then we need to
2918 insert a copy. Likewise, if any variables were referenced by
2919 pointer, we need to initialize the address. */
2920 if (root->any_parm_remapped)
2922 tree p;
2923 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
2925 tree field, x, y;
2927 field = lookup_field_for_decl (root, p, NO_INSERT);
2928 if (!field)
2929 continue;
2931 if (use_pointer_in_frame (p))
2932 x = build_addr (p);
2933 else
2934 x = p;
2936 /* If the assignment is from a non-register the stmt is
2937 not valid gimple. Make it so by using a temporary instead. */
2938 if (!is_gimple_reg (x)
2939 && is_gimple_reg_type (TREE_TYPE (x)))
2941 gimple_stmt_iterator gsi = gsi_last (stmt_list);
2942 x = init_tmp_var (root, x, &gsi);
2945 y = build3 (COMPONENT_REF, TREE_TYPE (field),
2946 root->frame_decl, field, NULL_TREE);
2947 stmt = gimple_build_assign (y, x);
2948 gimple_seq_add_stmt (&stmt_list, stmt);
2952 /* If a chain_field was created, then it needs to be initialized
2953 from chain_decl. */
2954 if (root->chain_field)
2956 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
2957 root->frame_decl, root->chain_field, NULL_TREE);
2958 stmt = gimple_build_assign (x, get_chain_decl (root));
2959 gimple_seq_add_stmt (&stmt_list, stmt);
2962 /* If trampolines were created, then we need to initialize them. */
2963 if (root->any_tramp_created)
2965 struct nesting_info *i;
2966 for (i = root->inner; i ; i = i->next)
2968 tree arg1, arg2, arg3, x, field;
2970 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
2971 if (!field)
2972 continue;
2974 gcc_assert (DECL_STATIC_CHAIN (i->context));
2975 arg3 = build_addr (root->frame_decl);
2977 arg2 = build_addr (i->context);
2979 x = build3 (COMPONENT_REF, TREE_TYPE (field),
2980 root->frame_decl, field, NULL_TREE);
2981 arg1 = build_addr (x);
2983 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
2984 stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
2985 gimple_seq_add_stmt (&stmt_list, stmt);
2989 /* If we created initialization statements, insert them. */
2990 if (stmt_list)
2992 gbind *bind;
2993 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
2994 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
2995 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
2996 gimple_bind_set_body (bind, stmt_list);
2999 /* If a chain_decl was created, then it needs to be registered with
3000 struct function so that it gets initialized from the static chain
3001 register at the beginning of the function. */
3002 sf = DECL_STRUCT_FUNCTION (root->context);
3003 sf->static_chain_decl = root->chain_decl;
3005 /* Similarly for the non-local goto save area. */
3006 if (root->nl_goto_field)
3008 sf->nonlocal_goto_save_area
3009 = get_frame_field (root, context, root->nl_goto_field, NULL);
3010 sf->has_nonlocal_label = 1;
3013 /* Make sure all new local variables get inserted into the
3014 proper BIND_EXPR. */
3015 if (root->new_local_var_chain)
3016 declare_vars (root->new_local_var_chain,
3017 gimple_seq_first_stmt (gimple_body (root->context)),
3018 false);
3020 if (root->debug_var_chain)
3022 tree debug_var;
3023 gbind *scope;
3025 remap_vla_decls (DECL_INITIAL (root->context), root);
3027 for (debug_var = root->debug_var_chain; debug_var;
3028 debug_var = DECL_CHAIN (debug_var))
3029 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3030 break;
3032 /* If there are any debug decls with variable length types,
3033 remap those types using other debug_var_chain variables. */
3034 if (debug_var)
3036 struct nesting_copy_body_data id;
3038 memset (&id, 0, sizeof (id));
3039 id.cb.copy_decl = nesting_copy_decl;
3040 id.cb.decl_map = new hash_map<tree, tree>;
3041 id.root = root;
3043 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3044 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3046 tree type = TREE_TYPE (debug_var);
3047 tree newt, t = type;
3048 struct nesting_info *i;
3050 for (i = root; i; i = i->outer)
3051 if (variably_modified_type_p (type, i->context))
3052 break;
3054 if (i == NULL)
3055 continue;
3057 id.cb.src_fn = i->context;
3058 id.cb.dst_fn = i->context;
3059 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3061 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3062 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3064 newt = TREE_TYPE (newt);
3065 t = TREE_TYPE (t);
3067 if (TYPE_NAME (newt)
3068 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3069 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3070 && newt != t
3071 && TYPE_NAME (newt) == TYPE_NAME (t))
3072 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3075 delete id.cb.decl_map;
3078 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3079 if (gimple_bind_block (scope))
3080 declare_vars (root->debug_var_chain, scope, true);
3081 else
3082 BLOCK_VARS (DECL_INITIAL (root->context))
3083 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3084 root->debug_var_chain);
3087 /* Fold the rewritten MEM_REF trees. */
3088 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3090 /* Dump the translated tree function. */
3091 if (dump_file)
3093 fputs ("\n\n", dump_file);
3094 dump_function_to_file (root->context, dump_file, dump_flags);
3098 static void
3099 finalize_nesting_tree (struct nesting_info *root)
3101 struct nesting_info *n;
3102 FOR_EACH_NEST_INFO (n, root)
3103 finalize_nesting_tree_1 (n);
3106 /* Unnest the nodes and pass them to cgraph. */
3108 static void
3109 unnest_nesting_tree_1 (struct nesting_info *root)
3111 struct cgraph_node *node = cgraph_node::get (root->context);
3113 /* For nested functions update the cgraph to reflect unnesting.
3114 We also delay finalizing of these functions up to this point. */
3115 if (node->origin)
3117 node->unnest ();
3118 cgraph_node::finalize_function (root->context, true);
3122 static void
3123 unnest_nesting_tree (struct nesting_info *root)
3125 struct nesting_info *n;
3126 FOR_EACH_NEST_INFO (n, root)
3127 unnest_nesting_tree_1 (n);
3130 /* Free the data structures allocated during this pass. */
3132 static void
3133 free_nesting_tree (struct nesting_info *root)
3135 struct nesting_info *node, *next;
3137 node = iter_nestinfo_start (root);
3140 next = iter_nestinfo_next (node);
3141 delete node->var_map;
3142 delete node->field_map;
3143 delete node->mem_refs;
3144 free (node);
3145 node = next;
3147 while (node);
3150 /* Gimplify a function and all its nested functions. */
3151 static void
3152 gimplify_all_functions (struct cgraph_node *root)
3154 struct cgraph_node *iter;
3155 if (!gimple_body (root->decl))
3156 gimplify_function_tree (root->decl);
3157 for (iter = root->nested; iter; iter = iter->next_nested)
3158 gimplify_all_functions (iter);
3161 /* Main entry point for this pass. Process FNDECL and all of its nested
3162 subroutines and turn them into something less tightly bound. */
3164 void
3165 lower_nested_functions (tree fndecl)
3167 struct cgraph_node *cgn;
3168 struct nesting_info *root;
3170 /* If there are no nested functions, there's nothing to do. */
3171 cgn = cgraph_node::get (fndecl);
3172 if (!cgn->nested)
3173 return;
3175 gimplify_all_functions (cgn);
3177 dump_file = dump_begin (TDI_nested, &dump_flags);
3178 if (dump_file)
3179 fprintf (dump_file, "\n;; Function %s\n\n",
3180 lang_hooks.decl_printable_name (fndecl, 2));
3182 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3183 root = create_nesting_tree (cgn);
3185 walk_all_functions (convert_nonlocal_reference_stmt,
3186 convert_nonlocal_reference_op,
3187 root);
3188 walk_all_functions (convert_local_reference_stmt,
3189 convert_local_reference_op,
3190 root);
3191 walk_all_functions (convert_nl_goto_reference, NULL, root);
3192 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3194 convert_all_function_calls (root);
3195 finalize_nesting_tree (root);
3196 unnest_nesting_tree (root);
3198 free_nesting_tree (root);
3199 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3201 if (dump_file)
3203 dump_end (TDI_nested, dump_file);
3204 dump_file = NULL;
3208 #include "gt-tree-nested.h"