Improve costs for DImode shifts of interger constants.
[official-gcc.git] / gcc / tree-nested.c
blobf74a727dea15f5da963bfe4326a042e78b9d50c4
1 /* Nested function decomposition for GIMPLE.
2 Copyright (C) 2004-2020 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 "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "dumpfile.h"
35 #include "tree-inline.h"
36 #include "gimplify.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "tree-cfg.h"
40 #include "explow.h"
41 #include "langhooks.h"
42 #include "gimple-low.h"
43 #include "gomp-constants.h"
44 #include "diagnostic.h"
47 /* The object of this pass is to lower the representation of a set of nested
48 functions in order to expose all of the gory details of the various
49 nonlocal references. We want to do this sooner rather than later, in
50 order to give us more freedom in emitting all of the functions in question.
52 Back in olden times, when gcc was young, we developed an insanely
53 complicated scheme whereby variables which were referenced nonlocally
54 were forced to live in the stack of the declaring function, and then
55 the nested functions magically discovered where these variables were
56 placed. In order for this scheme to function properly, it required
57 that the outer function be partially expanded, then we switch to
58 compiling the inner function, and once done with those we switch back
59 to compiling the outer function. Such delicate ordering requirements
60 makes it difficult to do whole translation unit optimizations
61 involving such functions.
63 The implementation here is much more direct. Everything that can be
64 referenced by an inner function is a member of an explicitly created
65 structure herein called the "nonlocal frame struct". The incoming
66 static chain for a nested function is a pointer to this struct in
67 the parent. In this way, we settle on known offsets from a known
68 base, and so are decoupled from the logic that places objects in the
69 function's stack frame. More importantly, we don't have to wait for
70 that to happen -- since the compilation of the inner function is no
71 longer tied to a real stack frame, the nonlocal frame struct can be
72 allocated anywhere. Which means that the outer function is now
73 inlinable.
75 Theory of operation here is very simple. Iterate over all the
76 statements in all the functions (depth first) several times,
77 allocating structures and fields on demand. In general we want to
78 examine inner functions first, so that we can avoid making changes
79 to outer functions which are unnecessary.
81 The order of the passes matters a bit, in that later passes will be
82 skipped if it is discovered that the functions don't actually interact
83 at all. That is, they're nested in the lexical sense but could have
84 been written as independent functions without change. */
87 struct nesting_info
89 struct nesting_info *outer;
90 struct nesting_info *inner;
91 struct nesting_info *next;
93 hash_map<tree, tree> *field_map;
94 hash_map<tree, tree> *var_map;
95 hash_set<tree *> *mem_refs;
96 bitmap suppress_expansion;
98 tree context;
99 tree new_local_var_chain;
100 tree debug_var_chain;
101 tree frame_type;
102 tree frame_decl;
103 tree chain_field;
104 tree chain_decl;
105 tree nl_goto_field;
107 bool thunk_p;
108 bool any_parm_remapped;
109 bool any_tramp_created;
110 bool any_descr_created;
111 char static_chain_added;
115 /* Iterate over the nesting tree, starting with ROOT, depth first. */
117 static inline struct nesting_info *
118 iter_nestinfo_start (struct nesting_info *root)
120 while (root->inner)
121 root = root->inner;
122 return root;
125 static inline struct nesting_info *
126 iter_nestinfo_next (struct nesting_info *node)
128 if (node->next)
129 return iter_nestinfo_start (node->next);
130 return node->outer;
133 #define FOR_EACH_NEST_INFO(I, ROOT) \
134 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
136 /* Obstack used for the bitmaps in the struct above. */
137 static struct bitmap_obstack nesting_info_bitmap_obstack;
140 /* We're working in so many different function contexts simultaneously,
141 that create_tmp_var is dangerous. Prevent mishap. */
142 #define create_tmp_var cant_use_create_tmp_var_here_dummy
144 /* Like create_tmp_var, except record the variable for registration at
145 the given nesting level. */
147 static tree
148 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
150 tree tmp_var;
152 /* If the type is of variable size or a type which must be created by the
153 frontend, something is wrong. Note that we explicitly allow
154 incomplete types here, since we create them ourselves here. */
155 gcc_assert (!TREE_ADDRESSABLE (type));
156 gcc_assert (!TYPE_SIZE_UNIT (type)
157 || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
159 tmp_var = create_tmp_var_raw (type, prefix);
160 DECL_CONTEXT (tmp_var) = info->context;
161 DECL_CHAIN (tmp_var) = info->new_local_var_chain;
162 DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
164 info->new_local_var_chain = tmp_var;
166 return tmp_var;
169 /* Like build_simple_mem_ref, but set TREE_THIS_NOTRAP on the result. */
171 static tree
172 build_simple_mem_ref_notrap (tree ptr)
174 tree t = build_simple_mem_ref (ptr);
175 TREE_THIS_NOTRAP (t) = 1;
176 return t;
179 /* Take the address of EXP to be used within function CONTEXT.
180 Mark it for addressability as necessary. */
182 tree
183 build_addr (tree exp)
185 mark_addressable (exp);
186 return build_fold_addr_expr (exp);
189 /* Insert FIELD into TYPE, sorted by alignment requirements. */
191 void
192 insert_field_into_struct (tree type, tree field)
194 tree *p;
196 DECL_CONTEXT (field) = type;
198 for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
199 if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
200 break;
202 DECL_CHAIN (field) = *p;
203 *p = field;
205 /* Set correct alignment for frame struct type. */
206 if (TYPE_ALIGN (type) < DECL_ALIGN (field))
207 SET_TYPE_ALIGN (type, DECL_ALIGN (field));
210 /* Build or return the RECORD_TYPE that describes the frame state that is
211 shared between INFO->CONTEXT and its nested functions. This record will
212 not be complete until finalize_nesting_tree; up until that point we'll
213 be adding fields as necessary.
215 We also build the DECL that represents this frame in the function. */
217 static tree
218 get_frame_type (struct nesting_info *info)
220 tree type = info->frame_type;
221 if (!type)
223 char *name;
225 type = make_node (RECORD_TYPE);
227 name = concat ("FRAME.",
228 IDENTIFIER_POINTER (DECL_NAME (info->context)),
229 NULL);
230 TYPE_NAME (type) = get_identifier (name);
231 free (name);
233 info->frame_type = type;
235 /* Do not put info->frame_decl on info->new_local_var_chain,
236 so that we can declare it in the lexical blocks, which
237 makes sure virtual regs that end up appearing in its RTL
238 expression get substituted in instantiate_virtual_regs. */
239 info->frame_decl = create_tmp_var_raw (type, "FRAME");
240 DECL_CONTEXT (info->frame_decl) = info->context;
241 DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
242 DECL_SEEN_IN_BIND_EXPR_P (info->frame_decl) = 1;
244 /* ??? Always make it addressable for now, since it is meant to
245 be pointed to by the static chain pointer. This pessimizes
246 when it turns out that no static chains are needed because
247 the nested functions referencing non-local variables are not
248 reachable, but the true pessimization is to create the non-
249 local frame structure in the first place. */
250 TREE_ADDRESSABLE (info->frame_decl) = 1;
253 return type;
256 /* Return true if DECL should be referenced by pointer in the non-local frame
257 structure. */
259 static bool
260 use_pointer_in_frame (tree decl)
262 if (TREE_CODE (decl) == PARM_DECL)
264 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
265 sized DECLs, and inefficient to copy large aggregates. Don't bother
266 moving anything but scalar parameters. */
267 return AGGREGATE_TYPE_P (TREE_TYPE (decl));
269 else
271 /* Variable-sized DECLs can only come from OMP clauses at this point
272 since the gimplifier has already turned the regular variables into
273 pointers. Do the same as the gimplifier. */
274 return !DECL_SIZE (decl) || TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST;
278 /* Given DECL, a non-locally accessed variable, find or create a field
279 in the non-local frame structure for the given nesting context. */
281 static tree
282 lookup_field_for_decl (struct nesting_info *info, tree decl,
283 enum insert_option insert)
285 gcc_checking_assert (decl_function_context (decl) == info->context);
287 if (insert == NO_INSERT)
289 tree *slot = info->field_map->get (decl);
290 return slot ? *slot : NULL_TREE;
293 tree *slot = &info->field_map->get_or_insert (decl);
294 if (!*slot)
296 tree type = get_frame_type (info);
297 tree field = make_node (FIELD_DECL);
298 DECL_NAME (field) = DECL_NAME (decl);
300 if (use_pointer_in_frame (decl))
302 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
303 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
304 DECL_NONADDRESSABLE_P (field) = 1;
306 else
308 TREE_TYPE (field) = TREE_TYPE (decl);
309 DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
310 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
311 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
312 DECL_IGNORED_P (field) = DECL_IGNORED_P (decl);
313 DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
314 TREE_NO_WARNING (field) = TREE_NO_WARNING (decl);
315 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
317 /* Declare the transformation and adjust the original DECL. For a
318 variable or for a parameter when not optimizing, we make it point
319 to the field in the frame directly. For a parameter, we don't do
320 it when optimizing because the variable tracking pass will already
321 do the job, */
322 if (VAR_P (decl) || !optimize)
324 tree x
325 = build3 (COMPONENT_REF, TREE_TYPE (field), info->frame_decl,
326 field, NULL_TREE);
328 /* If the next declaration is a PARM_DECL pointing to the DECL,
329 we need to adjust its VALUE_EXPR directly, since chains of
330 VALUE_EXPRs run afoul of garbage collection. This occurs
331 in Ada for Out parameters that aren't copied in. */
332 tree next = DECL_CHAIN (decl);
333 if (next
334 && TREE_CODE (next) == PARM_DECL
335 && DECL_HAS_VALUE_EXPR_P (next)
336 && DECL_VALUE_EXPR (next) == decl)
337 SET_DECL_VALUE_EXPR (next, x);
339 SET_DECL_VALUE_EXPR (decl, x);
340 DECL_HAS_VALUE_EXPR_P (decl) = 1;
344 insert_field_into_struct (type, field);
345 *slot = field;
347 if (TREE_CODE (decl) == PARM_DECL)
348 info->any_parm_remapped = true;
351 return *slot;
354 /* Build or return the variable that holds the static chain within
355 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
357 static tree
358 get_chain_decl (struct nesting_info *info)
360 tree decl = info->chain_decl;
362 if (!decl)
364 tree type;
366 type = get_frame_type (info->outer);
367 type = build_pointer_type (type);
369 /* Note that this variable is *not* entered into any BIND_EXPR;
370 the construction of this variable is handled specially in
371 expand_function_start and initialize_inlined_parameters.
372 Note also that it's represented as a parameter. This is more
373 close to the truth, since the initial value does come from
374 the caller. */
375 decl = build_decl (DECL_SOURCE_LOCATION (info->context),
376 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
377 DECL_ARTIFICIAL (decl) = 1;
378 DECL_IGNORED_P (decl) = 1;
379 TREE_USED (decl) = 1;
380 DECL_CONTEXT (decl) = info->context;
381 DECL_ARG_TYPE (decl) = type;
383 /* Tell tree-inline.c that we never write to this variable, so
384 it can copy-prop the replacement value immediately. */
385 TREE_READONLY (decl) = 1;
387 info->chain_decl = decl;
389 if (dump_file
390 && (dump_flags & TDF_DETAILS)
391 && !DECL_STATIC_CHAIN (info->context))
392 fprintf (dump_file, "Setting static-chain for %s\n",
393 lang_hooks.decl_printable_name (info->context, 2));
395 DECL_STATIC_CHAIN (info->context) = 1;
397 return decl;
400 /* Build or return the field within the non-local frame state that holds
401 the static chain for INFO->CONTEXT. This is the way to walk back up
402 multiple nesting levels. */
404 static tree
405 get_chain_field (struct nesting_info *info)
407 tree field = info->chain_field;
409 if (!field)
411 tree type = build_pointer_type (get_frame_type (info->outer));
413 field = make_node (FIELD_DECL);
414 DECL_NAME (field) = get_identifier ("__chain");
415 TREE_TYPE (field) = type;
416 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
417 DECL_NONADDRESSABLE_P (field) = 1;
419 insert_field_into_struct (get_frame_type (info), field);
421 info->chain_field = field;
423 if (dump_file
424 && (dump_flags & TDF_DETAILS)
425 && !DECL_STATIC_CHAIN (info->context))
426 fprintf (dump_file, "Setting static-chain for %s\n",
427 lang_hooks.decl_printable_name (info->context, 2));
429 DECL_STATIC_CHAIN (info->context) = 1;
431 return field;
434 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
436 static tree
437 init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
438 gcall *call)
440 tree t;
442 t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
443 gimple_call_set_lhs (call, t);
444 if (! gsi_end_p (*gsi))
445 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
446 gsi_insert_before (gsi, call, GSI_SAME_STMT);
448 return t;
452 /* Copy EXP into a temporary. Allocate the temporary in the context of
453 INFO and insert the initialization statement before GSI. */
455 static tree
456 init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
458 tree t;
459 gimple *stmt;
461 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
462 stmt = gimple_build_assign (t, exp);
463 if (! gsi_end_p (*gsi))
464 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
465 gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
467 return t;
471 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
473 static tree
474 gsi_gimplify_val (struct nesting_info *info, tree exp,
475 gimple_stmt_iterator *gsi)
477 if (is_gimple_val (exp))
478 return exp;
479 else
480 return init_tmp_var (info, exp, gsi);
483 /* Similarly, but copy from the temporary and insert the statement
484 after the iterator. */
486 static tree
487 save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
489 tree t;
490 gimple *stmt;
492 t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
493 stmt = gimple_build_assign (exp, t);
494 if (! gsi_end_p (*gsi))
495 gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
496 gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
498 return t;
501 /* Build or return the type used to represent a nested function trampoline. */
503 static GTY(()) tree trampoline_type;
505 static tree
506 get_trampoline_type (struct nesting_info *info)
508 unsigned align, size;
509 tree t;
511 if (trampoline_type)
512 return trampoline_type;
514 align = TRAMPOLINE_ALIGNMENT;
515 size = TRAMPOLINE_SIZE;
517 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
518 then allocate extra space so that we can do dynamic alignment. */
519 if (align > STACK_BOUNDARY)
521 size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
522 align = STACK_BOUNDARY;
525 t = build_index_type (size_int (size - 1));
526 t = build_array_type (char_type_node, t);
527 t = build_decl (DECL_SOURCE_LOCATION (info->context),
528 FIELD_DECL, get_identifier ("__data"), t);
529 SET_DECL_ALIGN (t, align);
530 DECL_USER_ALIGN (t) = 1;
532 trampoline_type = make_node (RECORD_TYPE);
533 TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
534 TYPE_FIELDS (trampoline_type) = t;
535 layout_type (trampoline_type);
536 DECL_CONTEXT (t) = trampoline_type;
538 return trampoline_type;
541 /* Build or return the type used to represent a nested function descriptor. */
543 static GTY(()) tree descriptor_type;
545 static tree
546 get_descriptor_type (struct nesting_info *info)
548 /* The base alignment is that of a function. */
549 const unsigned align = FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY);
550 tree t;
552 if (descriptor_type)
553 return descriptor_type;
555 t = build_index_type (integer_one_node);
556 t = build_array_type (ptr_type_node, t);
557 t = build_decl (DECL_SOURCE_LOCATION (info->context),
558 FIELD_DECL, get_identifier ("__data"), t);
559 SET_DECL_ALIGN (t, MAX (TYPE_ALIGN (ptr_type_node), align));
560 DECL_USER_ALIGN (t) = 1;
562 descriptor_type = make_node (RECORD_TYPE);
563 TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
564 TYPE_FIELDS (descriptor_type) = t;
565 layout_type (descriptor_type);
566 DECL_CONTEXT (t) = descriptor_type;
568 return descriptor_type;
571 /* Given DECL, a nested function, find or create an element in the
572 var map for this function. */
574 static tree
575 lookup_element_for_decl (struct nesting_info *info, tree decl,
576 enum insert_option insert)
578 if (insert == NO_INSERT)
580 tree *slot = info->var_map->get (decl);
581 return slot ? *slot : NULL_TREE;
584 tree *slot = &info->var_map->get_or_insert (decl);
585 if (!*slot)
586 *slot = build_tree_list (NULL_TREE, NULL_TREE);
588 return (tree) *slot;
591 /* Given DECL, a nested function, create a field in the non-local
592 frame structure for this function. */
594 static tree
595 create_field_for_decl (struct nesting_info *info, tree decl, tree type)
597 tree field = make_node (FIELD_DECL);
598 DECL_NAME (field) = DECL_NAME (decl);
599 TREE_TYPE (field) = type;
600 TREE_ADDRESSABLE (field) = 1;
601 insert_field_into_struct (get_frame_type (info), field);
602 return field;
605 /* Given DECL, a nested function, find or create a field in the non-local
606 frame structure for a trampoline for this function. */
608 static tree
609 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
610 enum insert_option insert)
612 tree elt, field;
614 elt = lookup_element_for_decl (info, decl, insert);
615 if (!elt)
616 return NULL_TREE;
618 field = TREE_PURPOSE (elt);
620 if (!field && insert == INSERT)
622 field = create_field_for_decl (info, decl, get_trampoline_type (info));
623 TREE_PURPOSE (elt) = field;
624 info->any_tramp_created = true;
627 return field;
630 /* Given DECL, a nested function, find or create a field in the non-local
631 frame structure for a descriptor for this function. */
633 static tree
634 lookup_descr_for_decl (struct nesting_info *info, tree decl,
635 enum insert_option insert)
637 tree elt, field;
639 elt = lookup_element_for_decl (info, decl, insert);
640 if (!elt)
641 return NULL_TREE;
643 field = TREE_VALUE (elt);
645 if (!field && insert == INSERT)
647 field = create_field_for_decl (info, decl, get_descriptor_type (info));
648 TREE_VALUE (elt) = field;
649 info->any_descr_created = true;
652 return field;
655 /* Build or return the field within the non-local frame state that holds
656 the non-local goto "jmp_buf". The buffer itself is maintained by the
657 rtl middle-end as dynamic stack space is allocated. */
659 static tree
660 get_nl_goto_field (struct nesting_info *info)
662 tree field = info->nl_goto_field;
663 if (!field)
665 unsigned size;
666 tree type;
668 /* For __builtin_nonlocal_goto, we need N words. The first is the
669 frame pointer, the rest is for the target's stack pointer save
670 area. The number of words is controlled by STACK_SAVEAREA_MODE;
671 not the best interface, but it'll do for now. */
672 if (Pmode == ptr_mode)
673 type = ptr_type_node;
674 else
675 type = lang_hooks.types.type_for_mode (Pmode, 1);
677 scalar_int_mode mode
678 = as_a <scalar_int_mode> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
679 size = GET_MODE_SIZE (mode);
680 size = size / GET_MODE_SIZE (Pmode);
681 size = size + 1;
683 type = build_array_type
684 (type, build_index_type (size_int (size)));
686 field = make_node (FIELD_DECL);
687 DECL_NAME (field) = get_identifier ("__nl_goto_buf");
688 TREE_TYPE (field) = type;
689 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
690 TREE_ADDRESSABLE (field) = 1;
692 insert_field_into_struct (get_frame_type (info), field);
694 info->nl_goto_field = field;
697 return field;
700 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
702 static void
703 walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
704 struct nesting_info *info, gimple_seq *pseq)
706 struct walk_stmt_info wi;
708 memset (&wi, 0, sizeof (wi));
709 wi.info = info;
710 wi.val_only = true;
711 walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
715 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
717 static inline void
718 walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
719 struct nesting_info *info)
721 gimple_seq body = gimple_body (info->context);
722 walk_body (callback_stmt, callback_op, info, &body);
723 gimple_set_body (info->context, body);
726 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
728 static void
729 walk_gimple_omp_for (gomp_for *for_stmt,
730 walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
731 struct nesting_info *info)
733 struct walk_stmt_info wi;
734 gimple_seq seq;
735 tree t;
736 size_t i;
738 walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
740 seq = NULL;
741 memset (&wi, 0, sizeof (wi));
742 wi.info = info;
743 wi.gsi = gsi_last (seq);
745 for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
747 wi.val_only = false;
748 walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
749 &wi, NULL);
750 wi.val_only = true;
751 wi.is_lhs = false;
752 walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
753 &wi, NULL);
755 wi.val_only = true;
756 wi.is_lhs = false;
757 walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
758 &wi, NULL);
760 t = gimple_omp_for_incr (for_stmt, i);
761 gcc_assert (BINARY_CLASS_P (t));
762 wi.val_only = false;
763 walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
764 wi.val_only = true;
765 wi.is_lhs = false;
766 walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
769 seq = gsi_seq (wi.gsi);
770 if (!gimple_seq_empty_p (seq))
772 gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
773 annotate_all_with_location (seq, gimple_location (for_stmt));
774 gimple_seq_add_seq (&pre_body, seq);
775 gimple_omp_for_set_pre_body (for_stmt, pre_body);
779 /* Similarly for ROOT and all functions nested underneath, depth first. */
781 static void
782 walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
783 struct nesting_info *root)
785 struct nesting_info *n;
786 FOR_EACH_NEST_INFO (n, root)
787 walk_function (callback_stmt, callback_op, n);
791 /* We have to check for a fairly pathological case. The operands of function
792 nested function are to be interpreted in the context of the enclosing
793 function. So if any are variably-sized, they will get remapped when the
794 enclosing function is inlined. But that remapping would also have to be
795 done in the types of the PARM_DECLs of the nested function, meaning the
796 argument types of that function will disagree with the arguments in the
797 calls to that function. So we'd either have to make a copy of the nested
798 function corresponding to each time the enclosing function was inlined or
799 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
800 function. The former is not practical. The latter would still require
801 detecting this case to know when to add the conversions. So, for now at
802 least, we don't inline such an enclosing function.
804 We have to do that check recursively, so here return indicating whether
805 FNDECL has such a nested function. ORIG_FN is the function we were
806 trying to inline to use for checking whether any argument is variably
807 modified by anything in it.
809 It would be better to do this in tree-inline.c so that we could give
810 the appropriate warning for why a function can't be inlined, but that's
811 too late since the nesting structure has already been flattened and
812 adding a flag just to record this fact seems a waste of a flag. */
814 static bool
815 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
817 struct cgraph_node *cgn = cgraph_node::get (fndecl);
818 tree arg;
820 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
822 for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
823 if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
824 return true;
826 if (check_for_nested_with_variably_modified (cgn->decl,
827 orig_fndecl))
828 return true;
831 return false;
834 /* Construct our local datastructure describing the function nesting
835 tree rooted by CGN. */
837 static struct nesting_info *
838 create_nesting_tree (struct cgraph_node *cgn)
840 struct nesting_info *info = XCNEW (struct nesting_info);
841 info->field_map = new hash_map<tree, tree>;
842 info->var_map = new hash_map<tree, tree>;
843 info->mem_refs = new hash_set<tree *>;
844 info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
845 info->context = cgn->decl;
846 info->thunk_p = cgn->thunk.thunk_p;
848 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
850 struct nesting_info *sub = create_nesting_tree (cgn);
851 sub->outer = info;
852 sub->next = info->inner;
853 info->inner = sub;
856 /* See discussion at check_for_nested_with_variably_modified for a
857 discussion of why this has to be here. */
858 if (check_for_nested_with_variably_modified (info->context, info->context))
859 DECL_UNINLINABLE (info->context) = true;
861 return info;
864 /* Return an expression computing the static chain for TARGET_CONTEXT
865 from INFO->CONTEXT. Insert any necessary computations before TSI. */
867 static tree
868 get_static_chain (struct nesting_info *info, tree target_context,
869 gimple_stmt_iterator *gsi)
871 struct nesting_info *i;
872 tree x;
874 if (info->context == target_context)
876 x = build_addr (info->frame_decl);
877 info->static_chain_added |= 1;
879 else
881 x = get_chain_decl (info);
882 info->static_chain_added |= 2;
884 for (i = info->outer; i->context != target_context; i = i->outer)
886 tree field = get_chain_field (i);
888 x = build_simple_mem_ref_notrap (x);
889 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
890 x = init_tmp_var (info, x, gsi);
894 return x;
898 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
899 frame as seen from INFO->CONTEXT. Insert any necessary computations
900 before GSI. */
902 static tree
903 get_frame_field (struct nesting_info *info, tree target_context,
904 tree field, gimple_stmt_iterator *gsi)
906 struct nesting_info *i;
907 tree x;
909 if (info->context == target_context)
911 /* Make sure frame_decl gets created. */
912 (void) get_frame_type (info);
913 x = info->frame_decl;
914 info->static_chain_added |= 1;
916 else
918 x = get_chain_decl (info);
919 info->static_chain_added |= 2;
921 for (i = info->outer; i->context != target_context; i = i->outer)
923 tree field = get_chain_field (i);
925 x = build_simple_mem_ref_notrap (x);
926 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
927 x = init_tmp_var (info, x, gsi);
930 x = build_simple_mem_ref_notrap (x);
933 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
934 return x;
937 static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
939 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
940 in the nested function with DECL_VALUE_EXPR set to reference the true
941 variable in the parent function. This is used both for debug info
942 and in OMP lowering. */
944 static tree
945 get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
947 tree target_context;
948 struct nesting_info *i;
949 tree x, field, new_decl;
951 tree *slot = &info->var_map->get_or_insert (decl);
953 if (*slot)
954 return *slot;
956 target_context = decl_function_context (decl);
958 /* A copy of the code in get_frame_field, but without the temporaries. */
959 if (info->context == target_context)
961 /* Make sure frame_decl gets created. */
962 (void) get_frame_type (info);
963 x = info->frame_decl;
964 i = info;
965 info->static_chain_added |= 1;
967 else
969 x = get_chain_decl (info);
970 info->static_chain_added |= 2;
971 for (i = info->outer; i->context != target_context; i = i->outer)
973 field = get_chain_field (i);
974 x = build_simple_mem_ref_notrap (x);
975 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
977 x = build_simple_mem_ref_notrap (x);
980 field = lookup_field_for_decl (i, decl, INSERT);
981 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
982 if (use_pointer_in_frame (decl))
983 x = build_simple_mem_ref_notrap (x);
985 /* ??? We should be remapping types as well, surely. */
986 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
987 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
988 DECL_CONTEXT (new_decl) = info->context;
989 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
990 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
991 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
992 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
993 TREE_READONLY (new_decl) = TREE_READONLY (decl);
994 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
995 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
996 if ((TREE_CODE (decl) == PARM_DECL
997 || TREE_CODE (decl) == RESULT_DECL
998 || VAR_P (decl))
999 && DECL_BY_REFERENCE (decl))
1000 DECL_BY_REFERENCE (new_decl) = 1;
1002 SET_DECL_VALUE_EXPR (new_decl, x);
1003 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1005 *slot = new_decl;
1006 DECL_CHAIN (new_decl) = info->debug_var_chain;
1007 info->debug_var_chain = new_decl;
1009 if (!optimize
1010 && info->context != target_context
1011 && variably_modified_type_p (TREE_TYPE (decl), NULL))
1012 note_nonlocal_vla_type (info, TREE_TYPE (decl));
1014 return new_decl;
1018 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1019 and PARM_DECLs that belong to outer functions.
1021 The rewrite will involve some number of structure accesses back up
1022 the static chain. E.g. for a variable FOO up one nesting level it'll
1023 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1024 indirections apply to decls for which use_pointer_in_frame is true. */
1026 static tree
1027 convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
1029 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1030 struct nesting_info *const info = (struct nesting_info *) wi->info;
1031 tree t = *tp;
1033 *walk_subtrees = 0;
1034 switch (TREE_CODE (t))
1036 case VAR_DECL:
1037 /* Non-automatic variables are never processed. */
1038 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1039 break;
1040 /* FALLTHRU */
1042 case PARM_DECL:
1044 tree x, target_context = decl_function_context (t);
1046 if (info->context == target_context)
1047 break;
1049 wi->changed = true;
1051 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1052 x = get_nonlocal_debug_decl (info, t);
1053 else
1055 struct nesting_info *i = info;
1056 while (i && i->context != target_context)
1057 i = i->outer;
1058 /* If none of the outer contexts is the target context, this means
1059 that the VAR or PARM_DECL is referenced in a wrong context. */
1060 if (!i)
1061 internal_error ("%s from %s referenced in %s",
1062 IDENTIFIER_POINTER (DECL_NAME (t)),
1063 IDENTIFIER_POINTER (DECL_NAME (target_context)),
1064 IDENTIFIER_POINTER (DECL_NAME (info->context)));
1066 x = lookup_field_for_decl (i, t, INSERT);
1067 x = get_frame_field (info, target_context, x, &wi->gsi);
1068 if (use_pointer_in_frame (t))
1070 x = init_tmp_var (info, x, &wi->gsi);
1071 x = build_simple_mem_ref_notrap (x);
1075 if (wi->val_only)
1077 if (wi->is_lhs)
1078 x = save_tmp_var (info, x, &wi->gsi);
1079 else
1080 x = init_tmp_var (info, x, &wi->gsi);
1083 *tp = x;
1085 break;
1087 case LABEL_DECL:
1088 /* We're taking the address of a label from a parent function, but
1089 this is not itself a non-local goto. Mark the label such that it
1090 will not be deleted, much as we would with a label address in
1091 static storage. */
1092 if (decl_function_context (t) != info->context)
1093 FORCED_LABEL (t) = 1;
1094 break;
1096 case ADDR_EXPR:
1098 bool save_val_only = wi->val_only;
1100 wi->val_only = false;
1101 wi->is_lhs = false;
1102 wi->changed = false;
1103 walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1104 wi->val_only = true;
1106 if (wi->changed)
1108 tree save_context;
1110 /* If we changed anything, we might no longer be directly
1111 referencing a decl. */
1112 save_context = current_function_decl;
1113 current_function_decl = info->context;
1114 recompute_tree_invariant_for_addr_expr (t);
1115 current_function_decl = save_context;
1117 /* If the callback converted the address argument in a context
1118 where we only accept variables (and min_invariant, presumably),
1119 then compute the address into a temporary. */
1120 if (save_val_only)
1121 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1122 t, &wi->gsi);
1125 break;
1127 case REALPART_EXPR:
1128 case IMAGPART_EXPR:
1129 case COMPONENT_REF:
1130 case ARRAY_REF:
1131 case ARRAY_RANGE_REF:
1132 case BIT_FIELD_REF:
1133 /* Go down this entire nest and just look at the final prefix and
1134 anything that describes the references. Otherwise, we lose track
1135 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1136 wi->val_only = true;
1137 wi->is_lhs = false;
1138 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1140 if (TREE_CODE (t) == COMPONENT_REF)
1141 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1142 NULL);
1143 else if (TREE_CODE (t) == ARRAY_REF
1144 || TREE_CODE (t) == ARRAY_RANGE_REF)
1146 walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1147 wi, NULL);
1148 walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1149 wi, NULL);
1150 walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1151 wi, NULL);
1154 wi->val_only = false;
1155 walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1156 break;
1158 case VIEW_CONVERT_EXPR:
1159 /* Just request to look at the subtrees, leaving val_only and lhs
1160 untouched. This might actually be for !val_only + lhs, in which
1161 case we don't want to force a replacement by a temporary. */
1162 *walk_subtrees = 1;
1163 break;
1165 default:
1166 if (!IS_TYPE_OR_DECL_P (t))
1168 *walk_subtrees = 1;
1169 wi->val_only = true;
1170 wi->is_lhs = false;
1172 break;
1175 return NULL_TREE;
1178 static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1179 struct walk_stmt_info *);
1181 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1182 and PARM_DECLs that belong to outer functions. */
1184 static bool
1185 convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1187 struct nesting_info *const info = (struct nesting_info *) wi->info;
1188 bool need_chain = false, need_stmts = false;
1189 tree clause, decl, *pdecl;
1190 int dummy;
1191 bitmap new_suppress;
1193 new_suppress = BITMAP_GGC_ALLOC ();
1194 bitmap_copy (new_suppress, info->suppress_expansion);
1196 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1198 pdecl = NULL;
1199 switch (OMP_CLAUSE_CODE (clause))
1201 case OMP_CLAUSE_REDUCTION:
1202 case OMP_CLAUSE_IN_REDUCTION:
1203 case OMP_CLAUSE_TASK_REDUCTION:
1204 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1205 need_stmts = true;
1206 if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
1208 pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
1209 if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
1210 pdecl = &TREE_OPERAND (*pdecl, 0);
1211 if (TREE_CODE (*pdecl) == INDIRECT_REF
1212 || TREE_CODE (*pdecl) == ADDR_EXPR)
1213 pdecl = &TREE_OPERAND (*pdecl, 0);
1215 goto do_decl_clause;
1217 case OMP_CLAUSE_LASTPRIVATE:
1218 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1219 need_stmts = true;
1220 goto do_decl_clause;
1222 case OMP_CLAUSE_LINEAR:
1223 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1224 need_stmts = true;
1225 wi->val_only = true;
1226 wi->is_lhs = false;
1227 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1228 &dummy, wi);
1229 goto do_decl_clause;
1231 case OMP_CLAUSE_PRIVATE:
1232 case OMP_CLAUSE_FIRSTPRIVATE:
1233 case OMP_CLAUSE_COPYPRIVATE:
1234 case OMP_CLAUSE_SHARED:
1235 case OMP_CLAUSE_TO_DECLARE:
1236 case OMP_CLAUSE_LINK:
1237 case OMP_CLAUSE_USE_DEVICE_PTR:
1238 case OMP_CLAUSE_USE_DEVICE_ADDR:
1239 case OMP_CLAUSE_IS_DEVICE_PTR:
1240 do_decl_clause:
1241 if (pdecl == NULL)
1242 pdecl = &OMP_CLAUSE_DECL (clause);
1243 decl = *pdecl;
1244 if (VAR_P (decl)
1245 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1246 break;
1247 if (decl_function_context (decl) != info->context)
1249 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1250 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1251 bitmap_set_bit (new_suppress, DECL_UID (decl));
1252 *pdecl = get_nonlocal_debug_decl (info, decl);
1253 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1254 need_chain = true;
1256 break;
1258 case OMP_CLAUSE_SCHEDULE:
1259 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1260 break;
1261 /* FALLTHRU */
1262 case OMP_CLAUSE_FINAL:
1263 case OMP_CLAUSE_IF:
1264 case OMP_CLAUSE_NUM_THREADS:
1265 case OMP_CLAUSE_DEPEND:
1266 case OMP_CLAUSE_DEVICE:
1267 case OMP_CLAUSE_NUM_TEAMS:
1268 case OMP_CLAUSE_THREAD_LIMIT:
1269 case OMP_CLAUSE_SAFELEN:
1270 case OMP_CLAUSE_SIMDLEN:
1271 case OMP_CLAUSE_PRIORITY:
1272 case OMP_CLAUSE_GRAINSIZE:
1273 case OMP_CLAUSE_NUM_TASKS:
1274 case OMP_CLAUSE_HINT:
1275 case OMP_CLAUSE_NUM_GANGS:
1276 case OMP_CLAUSE_NUM_WORKERS:
1277 case OMP_CLAUSE_VECTOR_LENGTH:
1278 case OMP_CLAUSE_GANG:
1279 case OMP_CLAUSE_WORKER:
1280 case OMP_CLAUSE_VECTOR:
1281 case OMP_CLAUSE_ASYNC:
1282 case OMP_CLAUSE_WAIT:
1283 /* Several OpenACC clauses have optional arguments. Check if they
1284 are present. */
1285 if (OMP_CLAUSE_OPERAND (clause, 0))
1287 wi->val_only = true;
1288 wi->is_lhs = false;
1289 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1290 &dummy, wi);
1293 /* The gang clause accepts two arguments. */
1294 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1295 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1297 wi->val_only = true;
1298 wi->is_lhs = false;
1299 convert_nonlocal_reference_op
1300 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1302 break;
1304 case OMP_CLAUSE_DIST_SCHEDULE:
1305 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1307 wi->val_only = true;
1308 wi->is_lhs = false;
1309 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1310 &dummy, wi);
1312 break;
1314 case OMP_CLAUSE_MAP:
1315 case OMP_CLAUSE_TO:
1316 case OMP_CLAUSE_FROM:
1317 if (OMP_CLAUSE_SIZE (clause))
1319 wi->val_only = true;
1320 wi->is_lhs = false;
1321 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1322 &dummy, wi);
1324 if (DECL_P (OMP_CLAUSE_DECL (clause)))
1325 goto do_decl_clause;
1326 wi->val_only = true;
1327 wi->is_lhs = false;
1328 walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1329 wi, NULL);
1330 break;
1332 case OMP_CLAUSE_ALIGNED:
1333 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1335 wi->val_only = true;
1336 wi->is_lhs = false;
1337 convert_nonlocal_reference_op
1338 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1340 /* FALLTHRU */
1341 case OMP_CLAUSE_NONTEMPORAL:
1342 /* Like do_decl_clause, but don't add any suppression. */
1343 decl = OMP_CLAUSE_DECL (clause);
1344 if (VAR_P (decl)
1345 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1346 break;
1347 if (decl_function_context (decl) != info->context)
1349 OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1350 need_chain = true;
1352 break;
1354 case OMP_CLAUSE_NOWAIT:
1355 case OMP_CLAUSE_ORDERED:
1356 case OMP_CLAUSE_DEFAULT:
1357 case OMP_CLAUSE_COPYIN:
1358 case OMP_CLAUSE_COLLAPSE:
1359 case OMP_CLAUSE_TILE:
1360 case OMP_CLAUSE_UNTIED:
1361 case OMP_CLAUSE_MERGEABLE:
1362 case OMP_CLAUSE_PROC_BIND:
1363 case OMP_CLAUSE_NOGROUP:
1364 case OMP_CLAUSE_THREADS:
1365 case OMP_CLAUSE_SIMD:
1366 case OMP_CLAUSE_DEFAULTMAP:
1367 case OMP_CLAUSE_ORDER:
1368 case OMP_CLAUSE_SEQ:
1369 case OMP_CLAUSE_INDEPENDENT:
1370 case OMP_CLAUSE_AUTO:
1371 case OMP_CLAUSE_IF_PRESENT:
1372 case OMP_CLAUSE_FINALIZE:
1373 case OMP_CLAUSE__CONDTEMP_:
1374 case OMP_CLAUSE__SCANTEMP_:
1375 break;
1377 /* The following clause belongs to the OpenACC cache directive, which
1378 is discarded during gimplification. */
1379 case OMP_CLAUSE__CACHE_:
1380 /* The following clauses are only allowed in the OpenMP declare simd
1381 directive, so not seen here. */
1382 case OMP_CLAUSE_UNIFORM:
1383 case OMP_CLAUSE_INBRANCH:
1384 case OMP_CLAUSE_NOTINBRANCH:
1385 /* The following clauses are only allowed on OpenMP cancel and
1386 cancellation point directives, which at this point have already
1387 been lowered into a function call. */
1388 case OMP_CLAUSE_FOR:
1389 case OMP_CLAUSE_PARALLEL:
1390 case OMP_CLAUSE_SECTIONS:
1391 case OMP_CLAUSE_TASKGROUP:
1392 /* The following clauses are only added during OMP lowering; nested
1393 function decomposition happens before that. */
1394 case OMP_CLAUSE__LOOPTEMP_:
1395 case OMP_CLAUSE__REDUCTEMP_:
1396 case OMP_CLAUSE__SIMDUID_:
1397 case OMP_CLAUSE__SIMT_:
1398 /* Anything else. */
1399 default:
1400 gcc_unreachable ();
1404 info->suppress_expansion = new_suppress;
1406 if (need_stmts)
1407 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1408 switch (OMP_CLAUSE_CODE (clause))
1410 case OMP_CLAUSE_REDUCTION:
1411 case OMP_CLAUSE_IN_REDUCTION:
1412 case OMP_CLAUSE_TASK_REDUCTION:
1413 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1415 tree old_context
1416 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1417 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1418 = info->context;
1419 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1420 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1421 = info->context;
1422 tree save_local_var_chain = info->new_local_var_chain;
1423 info->new_local_var_chain = NULL;
1424 gimple_seq *seq = &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause);
1425 walk_body (convert_nonlocal_reference_stmt,
1426 convert_nonlocal_reference_op, info, seq);
1427 if (info->new_local_var_chain)
1428 declare_vars (info->new_local_var_chain,
1429 gimple_seq_first_stmt (*seq), false);
1430 info->new_local_var_chain = NULL;
1431 seq = &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause);
1432 walk_body (convert_nonlocal_reference_stmt,
1433 convert_nonlocal_reference_op, info, seq);
1434 if (info->new_local_var_chain)
1435 declare_vars (info->new_local_var_chain,
1436 gimple_seq_first_stmt (*seq), false);
1437 info->new_local_var_chain = save_local_var_chain;
1438 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1439 = old_context;
1440 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1441 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1442 = old_context;
1444 break;
1446 case OMP_CLAUSE_LASTPRIVATE:
1448 tree save_local_var_chain = info->new_local_var_chain;
1449 info->new_local_var_chain = NULL;
1450 gimple_seq *seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause);
1451 walk_body (convert_nonlocal_reference_stmt,
1452 convert_nonlocal_reference_op, info, seq);
1453 if (info->new_local_var_chain)
1454 declare_vars (info->new_local_var_chain,
1455 gimple_seq_first_stmt (*seq), false);
1456 info->new_local_var_chain = save_local_var_chain;
1458 break;
1460 case OMP_CLAUSE_LINEAR:
1462 tree save_local_var_chain = info->new_local_var_chain;
1463 info->new_local_var_chain = NULL;
1464 gimple_seq *seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause);
1465 walk_body (convert_nonlocal_reference_stmt,
1466 convert_nonlocal_reference_op, info, seq);
1467 if (info->new_local_var_chain)
1468 declare_vars (info->new_local_var_chain,
1469 gimple_seq_first_stmt (*seq), false);
1470 info->new_local_var_chain = save_local_var_chain;
1472 break;
1474 default:
1475 break;
1478 return need_chain;
1481 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1483 static void
1484 note_nonlocal_vla_type (struct nesting_info *info, tree type)
1486 while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1487 type = TREE_TYPE (type);
1489 if (TYPE_NAME (type)
1490 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1491 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1492 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1494 while (POINTER_TYPE_P (type)
1495 || TREE_CODE (type) == VECTOR_TYPE
1496 || TREE_CODE (type) == FUNCTION_TYPE
1497 || TREE_CODE (type) == METHOD_TYPE)
1498 type = TREE_TYPE (type);
1500 if (TREE_CODE (type) == ARRAY_TYPE)
1502 tree domain, t;
1504 note_nonlocal_vla_type (info, TREE_TYPE (type));
1505 domain = TYPE_DOMAIN (type);
1506 if (domain)
1508 t = TYPE_MIN_VALUE (domain);
1509 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1510 && decl_function_context (t) != info->context)
1511 get_nonlocal_debug_decl (info, t);
1512 t = TYPE_MAX_VALUE (domain);
1513 if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1514 && decl_function_context (t) != info->context)
1515 get_nonlocal_debug_decl (info, t);
1520 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1521 PARM_DECLs that belong to outer functions. This handles statements
1522 that are not handled via the standard recursion done in
1523 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1524 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1525 operands of STMT have been handled by this function. */
1527 static tree
1528 convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1529 struct walk_stmt_info *wi)
1531 struct nesting_info *info = (struct nesting_info *) wi->info;
1532 tree save_local_var_chain;
1533 bitmap save_suppress;
1534 gimple *stmt = gsi_stmt (*gsi);
1536 switch (gimple_code (stmt))
1538 case GIMPLE_GOTO:
1539 /* Don't walk non-local gotos for now. */
1540 if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1542 wi->val_only = true;
1543 wi->is_lhs = false;
1544 *handled_ops_p = false;
1545 return NULL_TREE;
1547 break;
1549 case GIMPLE_OMP_TEAMS:
1550 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
1552 save_suppress = info->suppress_expansion;
1553 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt),
1554 wi);
1555 walk_body (convert_nonlocal_reference_stmt,
1556 convert_nonlocal_reference_op, info,
1557 gimple_omp_body_ptr (stmt));
1558 info->suppress_expansion = save_suppress;
1559 break;
1561 /* FALLTHRU */
1563 case GIMPLE_OMP_PARALLEL:
1564 case GIMPLE_OMP_TASK:
1565 save_suppress = info->suppress_expansion;
1566 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1567 wi))
1569 tree c, decl;
1570 decl = get_chain_decl (info);
1571 c = build_omp_clause (gimple_location (stmt),
1572 OMP_CLAUSE_FIRSTPRIVATE);
1573 OMP_CLAUSE_DECL (c) = decl;
1574 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1575 gimple_omp_taskreg_set_clauses (stmt, c);
1578 save_local_var_chain = info->new_local_var_chain;
1579 info->new_local_var_chain = NULL;
1581 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1582 info, gimple_omp_body_ptr (stmt));
1584 if (info->new_local_var_chain)
1585 declare_vars (info->new_local_var_chain,
1586 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1587 false);
1588 info->new_local_var_chain = save_local_var_chain;
1589 info->suppress_expansion = save_suppress;
1590 break;
1592 case GIMPLE_OMP_FOR:
1593 save_suppress = info->suppress_expansion;
1594 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1595 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1596 convert_nonlocal_reference_stmt,
1597 convert_nonlocal_reference_op, info);
1598 walk_body (convert_nonlocal_reference_stmt,
1599 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1600 info->suppress_expansion = save_suppress;
1601 break;
1603 case GIMPLE_OMP_SECTIONS:
1604 save_suppress = info->suppress_expansion;
1605 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1606 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1607 info, gimple_omp_body_ptr (stmt));
1608 info->suppress_expansion = save_suppress;
1609 break;
1611 case GIMPLE_OMP_SINGLE:
1612 save_suppress = info->suppress_expansion;
1613 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1614 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1615 info, gimple_omp_body_ptr (stmt));
1616 info->suppress_expansion = save_suppress;
1617 break;
1619 case GIMPLE_OMP_TASKGROUP:
1620 save_suppress = info->suppress_expansion;
1621 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
1622 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1623 info, gimple_omp_body_ptr (stmt));
1624 info->suppress_expansion = save_suppress;
1625 break;
1627 case GIMPLE_OMP_TARGET:
1628 if (!is_gimple_omp_offloaded (stmt))
1630 save_suppress = info->suppress_expansion;
1631 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1632 wi);
1633 info->suppress_expansion = save_suppress;
1634 walk_body (convert_nonlocal_reference_stmt,
1635 convert_nonlocal_reference_op, info,
1636 gimple_omp_body_ptr (stmt));
1637 break;
1639 save_suppress = info->suppress_expansion;
1640 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1641 wi))
1643 tree c, decl;
1644 decl = get_chain_decl (info);
1645 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1646 OMP_CLAUSE_DECL (c) = decl;
1647 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1648 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1649 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1650 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1653 save_local_var_chain = info->new_local_var_chain;
1654 info->new_local_var_chain = NULL;
1656 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1657 info, gimple_omp_body_ptr (stmt));
1659 if (info->new_local_var_chain)
1660 declare_vars (info->new_local_var_chain,
1661 gimple_seq_first_stmt (gimple_omp_body (stmt)),
1662 false);
1663 info->new_local_var_chain = save_local_var_chain;
1664 info->suppress_expansion = save_suppress;
1665 break;
1667 case GIMPLE_OMP_SECTION:
1668 case GIMPLE_OMP_MASTER:
1669 case GIMPLE_OMP_ORDERED:
1670 case GIMPLE_OMP_SCAN:
1671 walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1672 info, gimple_omp_body_ptr (stmt));
1673 break;
1675 case GIMPLE_BIND:
1677 gbind *bind_stmt = as_a <gbind *> (stmt);
1679 for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1680 if (TREE_CODE (var) == NAMELIST_DECL)
1682 /* Adjust decls mentioned in NAMELIST_DECL. */
1683 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1684 tree decl;
1685 unsigned int i;
1687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1689 if (VAR_P (decl)
1690 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1691 continue;
1692 if (decl_function_context (decl) != info->context)
1693 CONSTRUCTOR_ELT (decls, i)->value
1694 = get_nonlocal_debug_decl (info, decl);
1698 *handled_ops_p = false;
1699 return NULL_TREE;
1701 case GIMPLE_COND:
1702 wi->val_only = true;
1703 wi->is_lhs = false;
1704 *handled_ops_p = false;
1705 return NULL_TREE;
1707 case GIMPLE_ASSIGN:
1708 if (gimple_clobber_p (stmt))
1710 tree lhs = gimple_assign_lhs (stmt);
1711 if (DECL_P (lhs)
1712 && !(TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
1713 && decl_function_context (lhs) != info->context)
1715 gsi_replace (gsi, gimple_build_nop (), true);
1716 break;
1719 *handled_ops_p = false;
1720 return NULL_TREE;
1722 default:
1723 /* For every other statement that we are not interested in
1724 handling here, let the walker traverse the operands. */
1725 *handled_ops_p = false;
1726 return NULL_TREE;
1729 /* We have handled all of STMT operands, no need to traverse the operands. */
1730 *handled_ops_p = true;
1731 return NULL_TREE;
1735 /* A subroutine of convert_local_reference. Create a local variable
1736 in the parent function with DECL_VALUE_EXPR set to reference the
1737 field in FRAME. This is used both for debug info and in OMP
1738 lowering. */
1740 static tree
1741 get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1743 tree x, new_decl;
1745 tree *slot = &info->var_map->get_or_insert (decl);
1746 if (*slot)
1747 return *slot;
1749 /* Make sure frame_decl gets created. */
1750 (void) get_frame_type (info);
1751 x = info->frame_decl;
1752 x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1754 new_decl = build_decl (DECL_SOURCE_LOCATION (decl),
1755 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1756 DECL_CONTEXT (new_decl) = info->context;
1757 DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1758 DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1759 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1760 TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1761 TREE_READONLY (new_decl) = TREE_READONLY (decl);
1762 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1763 DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1764 if ((TREE_CODE (decl) == PARM_DECL
1765 || TREE_CODE (decl) == RESULT_DECL
1766 || VAR_P (decl))
1767 && DECL_BY_REFERENCE (decl))
1768 DECL_BY_REFERENCE (new_decl) = 1;
1770 SET_DECL_VALUE_EXPR (new_decl, x);
1771 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1772 *slot = new_decl;
1774 DECL_CHAIN (new_decl) = info->debug_var_chain;
1775 info->debug_var_chain = new_decl;
1777 /* Do not emit debug info twice. */
1778 DECL_IGNORED_P (decl) = 1;
1780 return new_decl;
1784 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1785 and PARM_DECLs that were referenced by inner nested functions.
1786 The rewrite will be a structure reference to the local frame variable. */
1788 static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1790 static tree
1791 convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1793 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1794 struct nesting_info *const info = (struct nesting_info *) wi->info;
1795 tree t = *tp, field, x;
1796 bool save_val_only;
1798 *walk_subtrees = 0;
1799 switch (TREE_CODE (t))
1801 case VAR_DECL:
1802 /* Non-automatic variables are never processed. */
1803 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1804 break;
1805 /* FALLTHRU */
1807 case PARM_DECL:
1808 if (t != info->frame_decl && decl_function_context (t) == info->context)
1810 /* If we copied a pointer to the frame, then the original decl
1811 is used unchanged in the parent function. */
1812 if (use_pointer_in_frame (t))
1813 break;
1815 /* No need to transform anything if no child references the
1816 variable. */
1817 field = lookup_field_for_decl (info, t, NO_INSERT);
1818 if (!field)
1819 break;
1820 wi->changed = true;
1822 if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1823 x = get_local_debug_decl (info, t, field);
1824 else
1825 x = get_frame_field (info, info->context, field, &wi->gsi);
1827 if (wi->val_only)
1829 if (wi->is_lhs)
1830 x = save_tmp_var (info, x, &wi->gsi);
1831 else
1832 x = init_tmp_var (info, x, &wi->gsi);
1835 *tp = x;
1837 break;
1839 case ADDR_EXPR:
1840 save_val_only = wi->val_only;
1841 wi->val_only = false;
1842 wi->is_lhs = false;
1843 wi->changed = false;
1844 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
1845 wi->val_only = save_val_only;
1847 /* If we converted anything ... */
1848 if (wi->changed)
1850 tree save_context;
1852 /* Then the frame decl is now addressable. */
1853 TREE_ADDRESSABLE (info->frame_decl) = 1;
1855 save_context = current_function_decl;
1856 current_function_decl = info->context;
1857 recompute_tree_invariant_for_addr_expr (t);
1858 current_function_decl = save_context;
1860 /* If we are in a context where we only accept values, then
1861 compute the address into a temporary. */
1862 if (save_val_only)
1863 *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1864 t, &wi->gsi);
1866 break;
1868 case REALPART_EXPR:
1869 case IMAGPART_EXPR:
1870 case COMPONENT_REF:
1871 case ARRAY_REF:
1872 case ARRAY_RANGE_REF:
1873 case BIT_FIELD_REF:
1874 /* Go down this entire nest and just look at the final prefix and
1875 anything that describes the references. Otherwise, we lose track
1876 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1877 save_val_only = wi->val_only;
1878 wi->val_only = true;
1879 wi->is_lhs = false;
1880 for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1882 if (TREE_CODE (t) == COMPONENT_REF)
1883 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1884 NULL);
1885 else if (TREE_CODE (t) == ARRAY_REF
1886 || TREE_CODE (t) == ARRAY_RANGE_REF)
1888 walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
1889 NULL);
1890 walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
1891 NULL);
1892 walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
1893 NULL);
1896 wi->val_only = false;
1897 walk_tree (tp, convert_local_reference_op, wi, NULL);
1898 wi->val_only = save_val_only;
1899 break;
1901 case MEM_REF:
1902 save_val_only = wi->val_only;
1903 wi->val_only = true;
1904 wi->is_lhs = false;
1905 walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
1906 wi, NULL);
1907 /* We need to re-fold the MEM_REF as component references as
1908 part of a ADDR_EXPR address are not allowed. But we cannot
1909 fold here, as the chain record type is not yet finalized. */
1910 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
1911 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
1912 info->mem_refs->add (tp);
1913 wi->val_only = save_val_only;
1914 break;
1916 case VIEW_CONVERT_EXPR:
1917 /* Just request to look at the subtrees, leaving val_only and lhs
1918 untouched. This might actually be for !val_only + lhs, in which
1919 case we don't want to force a replacement by a temporary. */
1920 *walk_subtrees = 1;
1921 break;
1923 default:
1924 if (!IS_TYPE_OR_DECL_P (t))
1926 *walk_subtrees = 1;
1927 wi->val_only = true;
1928 wi->is_lhs = false;
1930 break;
1933 return NULL_TREE;
1936 static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
1937 struct walk_stmt_info *);
1939 /* Helper for convert_local_reference. Convert all the references in
1940 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1942 static bool
1943 convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1945 struct nesting_info *const info = (struct nesting_info *) wi->info;
1946 bool need_frame = false, need_stmts = false;
1947 tree clause, decl, *pdecl;
1948 int dummy;
1949 bitmap new_suppress;
1951 new_suppress = BITMAP_GGC_ALLOC ();
1952 bitmap_copy (new_suppress, info->suppress_expansion);
1954 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1956 pdecl = NULL;
1957 switch (OMP_CLAUSE_CODE (clause))
1959 case OMP_CLAUSE_REDUCTION:
1960 case OMP_CLAUSE_IN_REDUCTION:
1961 case OMP_CLAUSE_TASK_REDUCTION:
1962 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1963 need_stmts = true;
1964 if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
1966 pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
1967 if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
1968 pdecl = &TREE_OPERAND (*pdecl, 0);
1969 if (TREE_CODE (*pdecl) == INDIRECT_REF
1970 || TREE_CODE (*pdecl) == ADDR_EXPR)
1971 pdecl = &TREE_OPERAND (*pdecl, 0);
1973 goto do_decl_clause;
1975 case OMP_CLAUSE_LASTPRIVATE:
1976 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1977 need_stmts = true;
1978 goto do_decl_clause;
1980 case OMP_CLAUSE_LINEAR:
1981 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1982 need_stmts = true;
1983 wi->val_only = true;
1984 wi->is_lhs = false;
1985 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
1986 wi);
1987 goto do_decl_clause;
1989 case OMP_CLAUSE_PRIVATE:
1990 case OMP_CLAUSE_FIRSTPRIVATE:
1991 case OMP_CLAUSE_COPYPRIVATE:
1992 case OMP_CLAUSE_SHARED:
1993 case OMP_CLAUSE_TO_DECLARE:
1994 case OMP_CLAUSE_LINK:
1995 case OMP_CLAUSE_USE_DEVICE_PTR:
1996 case OMP_CLAUSE_USE_DEVICE_ADDR:
1997 case OMP_CLAUSE_IS_DEVICE_PTR:
1998 do_decl_clause:
1999 if (pdecl == NULL)
2000 pdecl = &OMP_CLAUSE_DECL (clause);
2001 decl = *pdecl;
2002 if (VAR_P (decl)
2003 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2004 break;
2005 if (decl_function_context (decl) == info->context
2006 && !use_pointer_in_frame (decl))
2008 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2009 if (field)
2011 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
2012 OMP_CLAUSE_SHARED_READONLY (clause) = 0;
2013 bitmap_set_bit (new_suppress, DECL_UID (decl));
2014 *pdecl = get_local_debug_decl (info, decl, field);
2015 need_frame = true;
2018 break;
2020 case OMP_CLAUSE_SCHEDULE:
2021 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
2022 break;
2023 /* FALLTHRU */
2024 case OMP_CLAUSE_FINAL:
2025 case OMP_CLAUSE_IF:
2026 case OMP_CLAUSE_NUM_THREADS:
2027 case OMP_CLAUSE_DEPEND:
2028 case OMP_CLAUSE_DEVICE:
2029 case OMP_CLAUSE_NUM_TEAMS:
2030 case OMP_CLAUSE_THREAD_LIMIT:
2031 case OMP_CLAUSE_SAFELEN:
2032 case OMP_CLAUSE_SIMDLEN:
2033 case OMP_CLAUSE_PRIORITY:
2034 case OMP_CLAUSE_GRAINSIZE:
2035 case OMP_CLAUSE_NUM_TASKS:
2036 case OMP_CLAUSE_HINT:
2037 case OMP_CLAUSE_NUM_GANGS:
2038 case OMP_CLAUSE_NUM_WORKERS:
2039 case OMP_CLAUSE_VECTOR_LENGTH:
2040 case OMP_CLAUSE_GANG:
2041 case OMP_CLAUSE_WORKER:
2042 case OMP_CLAUSE_VECTOR:
2043 case OMP_CLAUSE_ASYNC:
2044 case OMP_CLAUSE_WAIT:
2045 /* Several OpenACC clauses have optional arguments. Check if they
2046 are present. */
2047 if (OMP_CLAUSE_OPERAND (clause, 0))
2049 wi->val_only = true;
2050 wi->is_lhs = false;
2051 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2052 &dummy, wi);
2055 /* The gang clause accepts two arguments. */
2056 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
2057 && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
2059 wi->val_only = true;
2060 wi->is_lhs = false;
2061 convert_nonlocal_reference_op
2062 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
2064 break;
2066 case OMP_CLAUSE_DIST_SCHEDULE:
2067 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
2069 wi->val_only = true;
2070 wi->is_lhs = false;
2071 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2072 &dummy, wi);
2074 break;
2076 case OMP_CLAUSE_MAP:
2077 case OMP_CLAUSE_TO:
2078 case OMP_CLAUSE_FROM:
2079 if (OMP_CLAUSE_SIZE (clause))
2081 wi->val_only = true;
2082 wi->is_lhs = false;
2083 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
2084 &dummy, wi);
2086 if (DECL_P (OMP_CLAUSE_DECL (clause)))
2087 goto do_decl_clause;
2088 wi->val_only = true;
2089 wi->is_lhs = false;
2090 walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
2091 wi, NULL);
2092 break;
2094 case OMP_CLAUSE_ALIGNED:
2095 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2097 wi->val_only = true;
2098 wi->is_lhs = false;
2099 convert_local_reference_op
2100 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2102 /* FALLTHRU */
2103 case OMP_CLAUSE_NONTEMPORAL:
2104 /* Like do_decl_clause, but don't add any suppression. */
2105 decl = OMP_CLAUSE_DECL (clause);
2106 if (VAR_P (decl)
2107 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2108 break;
2109 if (decl_function_context (decl) == info->context
2110 && !use_pointer_in_frame (decl))
2112 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2113 if (field)
2115 OMP_CLAUSE_DECL (clause)
2116 = get_local_debug_decl (info, decl, field);
2117 need_frame = true;
2120 break;
2122 case OMP_CLAUSE_NOWAIT:
2123 case OMP_CLAUSE_ORDERED:
2124 case OMP_CLAUSE_DEFAULT:
2125 case OMP_CLAUSE_COPYIN:
2126 case OMP_CLAUSE_COLLAPSE:
2127 case OMP_CLAUSE_TILE:
2128 case OMP_CLAUSE_UNTIED:
2129 case OMP_CLAUSE_MERGEABLE:
2130 case OMP_CLAUSE_PROC_BIND:
2131 case OMP_CLAUSE_NOGROUP:
2132 case OMP_CLAUSE_THREADS:
2133 case OMP_CLAUSE_SIMD:
2134 case OMP_CLAUSE_DEFAULTMAP:
2135 case OMP_CLAUSE_ORDER:
2136 case OMP_CLAUSE_SEQ:
2137 case OMP_CLAUSE_INDEPENDENT:
2138 case OMP_CLAUSE_AUTO:
2139 case OMP_CLAUSE_IF_PRESENT:
2140 case OMP_CLAUSE_FINALIZE:
2141 case OMP_CLAUSE__CONDTEMP_:
2142 case OMP_CLAUSE__SCANTEMP_:
2143 break;
2145 /* The following clause belongs to the OpenACC cache directive, which
2146 is discarded during gimplification. */
2147 case OMP_CLAUSE__CACHE_:
2148 /* The following clauses are only allowed in the OpenMP declare simd
2149 directive, so not seen here. */
2150 case OMP_CLAUSE_UNIFORM:
2151 case OMP_CLAUSE_INBRANCH:
2152 case OMP_CLAUSE_NOTINBRANCH:
2153 /* The following clauses are only allowed on OpenMP cancel and
2154 cancellation point directives, which at this point have already
2155 been lowered into a function call. */
2156 case OMP_CLAUSE_FOR:
2157 case OMP_CLAUSE_PARALLEL:
2158 case OMP_CLAUSE_SECTIONS:
2159 case OMP_CLAUSE_TASKGROUP:
2160 /* The following clauses are only added during OMP lowering; nested
2161 function decomposition happens before that. */
2162 case OMP_CLAUSE__LOOPTEMP_:
2163 case OMP_CLAUSE__REDUCTEMP_:
2164 case OMP_CLAUSE__SIMDUID_:
2165 case OMP_CLAUSE__SIMT_:
2166 /* Anything else. */
2167 default:
2168 gcc_unreachable ();
2172 info->suppress_expansion = new_suppress;
2174 if (need_stmts)
2175 for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2176 switch (OMP_CLAUSE_CODE (clause))
2178 case OMP_CLAUSE_REDUCTION:
2179 case OMP_CLAUSE_IN_REDUCTION:
2180 case OMP_CLAUSE_TASK_REDUCTION:
2181 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2183 tree old_context
2184 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2185 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2186 = info->context;
2187 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2188 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2189 = info->context;
2190 walk_body (convert_local_reference_stmt,
2191 convert_local_reference_op, info,
2192 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2193 walk_body (convert_local_reference_stmt,
2194 convert_local_reference_op, info,
2195 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2196 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2197 = old_context;
2198 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2199 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2200 = old_context;
2202 break;
2204 case OMP_CLAUSE_LASTPRIVATE:
2205 walk_body (convert_local_reference_stmt,
2206 convert_local_reference_op, info,
2207 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2208 break;
2210 case OMP_CLAUSE_LINEAR:
2211 walk_body (convert_local_reference_stmt,
2212 convert_local_reference_op, info,
2213 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2214 break;
2216 default:
2217 break;
2220 return need_frame;
2224 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2225 and PARM_DECLs that were referenced by inner nested functions.
2226 The rewrite will be a structure reference to the local frame variable. */
2228 static tree
2229 convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2230 struct walk_stmt_info *wi)
2232 struct nesting_info *info = (struct nesting_info *) wi->info;
2233 tree save_local_var_chain;
2234 bitmap save_suppress;
2235 char save_static_chain_added;
2236 bool frame_decl_added;
2237 gimple *stmt = gsi_stmt (*gsi);
2239 switch (gimple_code (stmt))
2241 case GIMPLE_OMP_TEAMS:
2242 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2244 save_suppress = info->suppress_expansion;
2245 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2246 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2247 info, gimple_omp_body_ptr (stmt));
2248 info->suppress_expansion = save_suppress;
2249 break;
2251 /* FALLTHRU */
2253 case GIMPLE_OMP_PARALLEL:
2254 case GIMPLE_OMP_TASK:
2255 save_suppress = info->suppress_expansion;
2256 frame_decl_added = false;
2257 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2258 wi))
2260 tree c = build_omp_clause (gimple_location (stmt),
2261 OMP_CLAUSE_SHARED);
2262 (void) get_frame_type (info);
2263 OMP_CLAUSE_DECL (c) = info->frame_decl;
2264 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2265 gimple_omp_taskreg_set_clauses (stmt, c);
2266 info->static_chain_added |= 4;
2267 frame_decl_added = true;
2270 save_local_var_chain = info->new_local_var_chain;
2271 save_static_chain_added = info->static_chain_added;
2272 info->new_local_var_chain = NULL;
2273 info->static_chain_added = 0;
2275 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2276 gimple_omp_body_ptr (stmt));
2278 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2280 tree c = build_omp_clause (gimple_location (stmt),
2281 OMP_CLAUSE_SHARED);
2282 (void) get_frame_type (info);
2283 OMP_CLAUSE_DECL (c) = info->frame_decl;
2284 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2285 info->static_chain_added |= 4;
2286 gimple_omp_taskreg_set_clauses (stmt, c);
2288 if (info->new_local_var_chain)
2289 declare_vars (info->new_local_var_chain,
2290 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2291 info->new_local_var_chain = save_local_var_chain;
2292 info->suppress_expansion = save_suppress;
2293 info->static_chain_added |= save_static_chain_added;
2294 break;
2296 case GIMPLE_OMP_FOR:
2297 save_suppress = info->suppress_expansion;
2298 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2299 walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2300 convert_local_reference_stmt,
2301 convert_local_reference_op, info);
2302 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2303 info, gimple_omp_body_ptr (stmt));
2304 info->suppress_expansion = save_suppress;
2305 break;
2307 case GIMPLE_OMP_SECTIONS:
2308 save_suppress = info->suppress_expansion;
2309 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2310 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2311 info, gimple_omp_body_ptr (stmt));
2312 info->suppress_expansion = save_suppress;
2313 break;
2315 case GIMPLE_OMP_SINGLE:
2316 save_suppress = info->suppress_expansion;
2317 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2318 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2319 info, gimple_omp_body_ptr (stmt));
2320 info->suppress_expansion = save_suppress;
2321 break;
2323 case GIMPLE_OMP_TASKGROUP:
2324 save_suppress = info->suppress_expansion;
2325 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
2326 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2327 info, gimple_omp_body_ptr (stmt));
2328 info->suppress_expansion = save_suppress;
2329 break;
2331 case GIMPLE_OMP_TARGET:
2332 if (!is_gimple_omp_offloaded (stmt))
2334 save_suppress = info->suppress_expansion;
2335 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2336 info->suppress_expansion = save_suppress;
2337 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2338 info, gimple_omp_body_ptr (stmt));
2339 break;
2341 save_suppress = info->suppress_expansion;
2342 frame_decl_added = false;
2343 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2345 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2346 (void) get_frame_type (info);
2347 OMP_CLAUSE_DECL (c) = info->frame_decl;
2348 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2349 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2350 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2351 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2352 info->static_chain_added |= 4;
2353 frame_decl_added = true;
2356 save_local_var_chain = info->new_local_var_chain;
2357 save_static_chain_added = info->static_chain_added;
2358 info->new_local_var_chain = NULL;
2359 info->static_chain_added = 0;
2361 walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2362 gimple_omp_body_ptr (stmt));
2364 if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2366 tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2367 (void) get_frame_type (info);
2368 OMP_CLAUSE_DECL (c) = info->frame_decl;
2369 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2370 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2371 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2372 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2373 info->static_chain_added |= 4;
2376 if (info->new_local_var_chain)
2377 declare_vars (info->new_local_var_chain,
2378 gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2379 info->new_local_var_chain = save_local_var_chain;
2380 info->suppress_expansion = save_suppress;
2381 info->static_chain_added |= save_static_chain_added;
2382 break;
2384 case GIMPLE_OMP_SECTION:
2385 case GIMPLE_OMP_MASTER:
2386 case GIMPLE_OMP_ORDERED:
2387 case GIMPLE_OMP_SCAN:
2388 walk_body (convert_local_reference_stmt, convert_local_reference_op,
2389 info, gimple_omp_body_ptr (stmt));
2390 break;
2392 case GIMPLE_COND:
2393 wi->val_only = true;
2394 wi->is_lhs = false;
2395 *handled_ops_p = false;
2396 return NULL_TREE;
2398 case GIMPLE_ASSIGN:
2399 if (gimple_clobber_p (stmt))
2401 tree lhs = gimple_assign_lhs (stmt);
2402 if (DECL_P (lhs)
2403 && !use_pointer_in_frame (lhs)
2404 && lookup_field_for_decl (info, lhs, NO_INSERT))
2406 gsi_replace (gsi, gimple_build_nop (), true);
2407 break;
2410 *handled_ops_p = false;
2411 return NULL_TREE;
2413 case GIMPLE_BIND:
2414 for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2415 var;
2416 var = DECL_CHAIN (var))
2417 if (TREE_CODE (var) == NAMELIST_DECL)
2419 /* Adjust decls mentioned in NAMELIST_DECL. */
2420 tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2421 tree decl;
2422 unsigned int i;
2424 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2426 if (VAR_P (decl)
2427 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2428 continue;
2429 if (decl_function_context (decl) == info->context
2430 && !use_pointer_in_frame (decl))
2432 tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2433 if (field)
2435 CONSTRUCTOR_ELT (decls, i)->value
2436 = get_local_debug_decl (info, decl, field);
2442 *handled_ops_p = false;
2443 return NULL_TREE;
2445 default:
2446 /* For every other statement that we are not interested in
2447 handling here, let the walker traverse the operands. */
2448 *handled_ops_p = false;
2449 return NULL_TREE;
2452 /* Indicate that we have handled all the operands ourselves. */
2453 *handled_ops_p = true;
2454 return NULL_TREE;
2458 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2459 that reference labels from outer functions. The rewrite will be a
2460 call to __builtin_nonlocal_goto. */
2462 static tree
2463 convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2464 struct walk_stmt_info *wi)
2466 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2467 tree label, new_label, target_context, x, field;
2468 gcall *call;
2469 gimple *stmt = gsi_stmt (*gsi);
2471 if (gimple_code (stmt) != GIMPLE_GOTO)
2473 *handled_ops_p = false;
2474 return NULL_TREE;
2477 label = gimple_goto_dest (stmt);
2478 if (TREE_CODE (label) != LABEL_DECL)
2480 *handled_ops_p = false;
2481 return NULL_TREE;
2484 target_context = decl_function_context (label);
2485 if (target_context == info->context)
2487 *handled_ops_p = false;
2488 return NULL_TREE;
2491 for (i = info->outer; target_context != i->context; i = i->outer)
2492 continue;
2494 /* The original user label may also be use for a normal goto, therefore
2495 we must create a new label that will actually receive the abnormal
2496 control transfer. This new label will be marked LABEL_NONLOCAL; this
2497 mark will trigger proper behavior in the cfg, as well as cause the
2498 (hairy target-specific) non-local goto receiver code to be generated
2499 when we expand rtl. Enter this association into var_map so that we
2500 can insert the new label into the IL during a second pass. */
2501 tree *slot = &i->var_map->get_or_insert (label);
2502 if (*slot == NULL)
2504 new_label = create_artificial_label (UNKNOWN_LOCATION);
2505 DECL_NONLOCAL (new_label) = 1;
2506 *slot = new_label;
2508 else
2509 new_label = *slot;
2511 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2512 field = get_nl_goto_field (i);
2513 x = get_frame_field (info, target_context, field, gsi);
2514 x = build_addr (x);
2515 x = gsi_gimplify_val (info, x, gsi);
2516 call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2517 2, build_addr (new_label), x);
2518 gsi_replace (gsi, call, false);
2520 /* We have handled all of STMT's operands, no need to keep going. */
2521 *handled_ops_p = true;
2522 return NULL_TREE;
2526 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2527 are referenced via nonlocal goto from a nested function. The rewrite
2528 will involve installing a newly generated DECL_NONLOCAL label, and
2529 (potentially) a branch around the rtl gunk that is assumed to be
2530 attached to such a label. */
2532 static tree
2533 convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2534 struct walk_stmt_info *wi)
2536 struct nesting_info *const info = (struct nesting_info *) wi->info;
2537 tree label, new_label;
2538 gimple_stmt_iterator tmp_gsi;
2539 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2541 if (!stmt)
2543 *handled_ops_p = false;
2544 return NULL_TREE;
2547 label = gimple_label_label (stmt);
2549 tree *slot = info->var_map->get (label);
2550 if (!slot)
2552 *handled_ops_p = false;
2553 return NULL_TREE;
2556 /* If there's any possibility that the previous statement falls through,
2557 then we must branch around the new non-local label. */
2558 tmp_gsi = wi->gsi;
2559 gsi_prev (&tmp_gsi);
2560 if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2562 gimple *stmt = gimple_build_goto (label);
2563 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2566 new_label = (tree) *slot;
2567 stmt = gimple_build_label (new_label);
2568 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2570 *handled_ops_p = true;
2571 return NULL_TREE;
2575 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2576 of nested functions that require the use of trampolines. The rewrite
2577 will involve a reference a trampoline generated for the occasion. */
2579 static tree
2580 convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2582 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2583 struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2584 tree t = *tp, decl, target_context, x, builtin;
2585 bool descr;
2586 gcall *call;
2588 *walk_subtrees = 0;
2589 switch (TREE_CODE (t))
2591 case ADDR_EXPR:
2592 /* Build
2593 T.1 = &CHAIN->tramp;
2594 T.2 = __builtin_adjust_trampoline (T.1);
2595 T.3 = (func_type)T.2;
2598 decl = TREE_OPERAND (t, 0);
2599 if (TREE_CODE (decl) != FUNCTION_DECL)
2600 break;
2602 /* Only need to process nested functions. */
2603 target_context = decl_function_context (decl);
2604 if (!target_context)
2605 break;
2607 /* If the nested function doesn't use a static chain, then
2608 it doesn't need a trampoline. */
2609 if (!DECL_STATIC_CHAIN (decl))
2610 break;
2612 /* If we don't want a trampoline, then don't build one. */
2613 if (TREE_NO_TRAMPOLINE (t))
2614 break;
2616 /* Lookup the immediate parent of the callee, as that's where
2617 we need to insert the trampoline. */
2618 for (i = info; i->context != target_context; i = i->outer)
2619 continue;
2621 /* Decide whether to generate a descriptor or a trampoline. */
2622 descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2624 if (descr)
2625 x = lookup_descr_for_decl (i, decl, INSERT);
2626 else
2627 x = lookup_tramp_for_decl (i, decl, INSERT);
2629 /* Compute the address of the field holding the trampoline. */
2630 x = get_frame_field (info, target_context, x, &wi->gsi);
2631 x = build_addr (x);
2632 x = gsi_gimplify_val (info, x, &wi->gsi);
2634 /* Do machine-specific ugliness. Normally this will involve
2635 computing extra alignment, but it can really be anything. */
2636 if (descr)
2637 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2638 else
2639 builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2640 call = gimple_build_call (builtin, 1, x);
2641 x = init_tmp_var_with_call (info, &wi->gsi, call);
2643 /* Cast back to the proper function type. */
2644 x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2645 x = init_tmp_var (info, x, &wi->gsi);
2647 *tp = x;
2648 break;
2650 default:
2651 if (!IS_TYPE_OR_DECL_P (t))
2652 *walk_subtrees = 1;
2653 break;
2656 return NULL_TREE;
2660 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2661 to addresses of nested functions that require the use of
2662 trampolines. The rewrite will involve a reference a trampoline
2663 generated for the occasion. */
2665 static tree
2666 convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2667 struct walk_stmt_info *wi)
2669 struct nesting_info *info = (struct nesting_info *) wi->info;
2670 gimple *stmt = gsi_stmt (*gsi);
2672 switch (gimple_code (stmt))
2674 case GIMPLE_CALL:
2676 /* Only walk call arguments, lest we generate trampolines for
2677 direct calls. */
2678 unsigned long i, nargs = gimple_call_num_args (stmt);
2679 for (i = 0; i < nargs; i++)
2680 walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2681 wi, NULL);
2682 break;
2685 case GIMPLE_OMP_TEAMS:
2686 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2688 *handled_ops_p = false;
2689 return NULL_TREE;
2691 goto do_parallel;
2693 case GIMPLE_OMP_TARGET:
2694 if (!is_gimple_omp_offloaded (stmt))
2696 *handled_ops_p = false;
2697 return NULL_TREE;
2699 /* FALLTHRU */
2700 case GIMPLE_OMP_PARALLEL:
2701 case GIMPLE_OMP_TASK:
2702 do_parallel:
2704 tree save_local_var_chain = info->new_local_var_chain;
2705 walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2706 info->new_local_var_chain = NULL;
2707 char save_static_chain_added = info->static_chain_added;
2708 info->static_chain_added = 0;
2709 walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2710 info, gimple_omp_body_ptr (stmt));
2711 if (info->new_local_var_chain)
2712 declare_vars (info->new_local_var_chain,
2713 gimple_seq_first_stmt (gimple_omp_body (stmt)),
2714 false);
2715 for (int i = 0; i < 2; i++)
2717 tree c, decl;
2718 if ((info->static_chain_added & (1 << i)) == 0)
2719 continue;
2720 decl = i ? get_chain_decl (info) : info->frame_decl;
2721 /* Don't add CHAIN.* or FRAME.* twice. */
2722 for (c = gimple_omp_taskreg_clauses (stmt);
2724 c = OMP_CLAUSE_CHAIN (c))
2725 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2727 && OMP_CLAUSE_DECL (c) == decl)
2728 break;
2729 if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2731 c = build_omp_clause (gimple_location (stmt),
2732 i ? OMP_CLAUSE_FIRSTPRIVATE
2733 : OMP_CLAUSE_SHARED);
2734 OMP_CLAUSE_DECL (c) = decl;
2735 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2736 gimple_omp_taskreg_set_clauses (stmt, c);
2738 else if (c == NULL)
2740 c = build_omp_clause (gimple_location (stmt),
2741 OMP_CLAUSE_MAP);
2742 OMP_CLAUSE_DECL (c) = decl;
2743 OMP_CLAUSE_SET_MAP_KIND (c,
2744 i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2745 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2746 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2747 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2751 info->new_local_var_chain = save_local_var_chain;
2752 info->static_chain_added |= save_static_chain_added;
2754 break;
2756 default:
2757 *handled_ops_p = false;
2758 return NULL_TREE;
2761 *handled_ops_p = true;
2762 return NULL_TREE;
2767 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2768 that reference nested functions to make sure that the static chain
2769 is set up properly for the call. */
2771 static tree
2772 convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2773 struct walk_stmt_info *wi)
2775 struct nesting_info *const info = (struct nesting_info *) wi->info;
2776 tree decl, target_context;
2777 char save_static_chain_added;
2778 int i;
2779 gimple *stmt = gsi_stmt (*gsi);
2781 switch (gimple_code (stmt))
2783 case GIMPLE_CALL:
2784 if (gimple_call_chain (stmt))
2785 break;
2786 decl = gimple_call_fndecl (stmt);
2787 if (!decl)
2788 break;
2789 target_context = decl_function_context (decl);
2790 if (target_context && DECL_STATIC_CHAIN (decl))
2792 struct nesting_info *i = info;
2793 while (i && i->context != target_context)
2794 i = i->outer;
2795 /* If none of the outer contexts is the target context, this means
2796 that the function is called in a wrong context. */
2797 if (!i)
2798 internal_error ("%s from %s called in %s",
2799 IDENTIFIER_POINTER (DECL_NAME (decl)),
2800 IDENTIFIER_POINTER (DECL_NAME (target_context)),
2801 IDENTIFIER_POINTER (DECL_NAME (info->context)));
2803 gimple_call_set_chain (as_a <gcall *> (stmt),
2804 get_static_chain (info, target_context,
2805 &wi->gsi));
2806 info->static_chain_added |= (1 << (info->context != target_context));
2808 break;
2810 case GIMPLE_OMP_TEAMS:
2811 if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2813 walk_body (convert_gimple_call, NULL, info,
2814 gimple_omp_body_ptr (stmt));
2815 break;
2817 /* FALLTHRU */
2819 case GIMPLE_OMP_PARALLEL:
2820 case GIMPLE_OMP_TASK:
2821 save_static_chain_added = info->static_chain_added;
2822 info->static_chain_added = 0;
2823 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2824 for (i = 0; i < 2; i++)
2826 tree c, decl;
2827 if ((info->static_chain_added & (1 << i)) == 0)
2828 continue;
2829 decl = i ? get_chain_decl (info) : info->frame_decl;
2830 /* Don't add CHAIN.* or FRAME.* twice. */
2831 for (c = gimple_omp_taskreg_clauses (stmt);
2833 c = OMP_CLAUSE_CHAIN (c))
2834 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2835 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2836 && OMP_CLAUSE_DECL (c) == decl)
2837 break;
2838 if (c == NULL)
2840 c = build_omp_clause (gimple_location (stmt),
2841 i ? OMP_CLAUSE_FIRSTPRIVATE
2842 : OMP_CLAUSE_SHARED);
2843 OMP_CLAUSE_DECL (c) = decl;
2844 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2845 gimple_omp_taskreg_set_clauses (stmt, c);
2848 info->static_chain_added |= save_static_chain_added;
2849 break;
2851 case GIMPLE_OMP_TARGET:
2852 if (!is_gimple_omp_offloaded (stmt))
2854 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2855 break;
2857 save_static_chain_added = info->static_chain_added;
2858 info->static_chain_added = 0;
2859 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2860 for (i = 0; i < 2; i++)
2862 tree c, decl;
2863 if ((info->static_chain_added & (1 << i)) == 0)
2864 continue;
2865 decl = i ? get_chain_decl (info) : info->frame_decl;
2866 /* Don't add CHAIN.* or FRAME.* twice. */
2867 for (c = gimple_omp_target_clauses (stmt);
2869 c = OMP_CLAUSE_CHAIN (c))
2870 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
2871 && OMP_CLAUSE_DECL (c) == decl)
2872 break;
2873 if (c == NULL)
2875 c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2876 OMP_CLAUSE_DECL (c) = decl;
2877 OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2878 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2879 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2880 gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2884 info->static_chain_added |= save_static_chain_added;
2885 break;
2887 case GIMPLE_OMP_FOR:
2888 walk_body (convert_gimple_call, NULL, info,
2889 gimple_omp_for_pre_body_ptr (stmt));
2890 /* FALLTHRU */
2891 case GIMPLE_OMP_SECTIONS:
2892 case GIMPLE_OMP_SECTION:
2893 case GIMPLE_OMP_SINGLE:
2894 case GIMPLE_OMP_MASTER:
2895 case GIMPLE_OMP_TASKGROUP:
2896 case GIMPLE_OMP_ORDERED:
2897 case GIMPLE_OMP_SCAN:
2898 case GIMPLE_OMP_CRITICAL:
2899 walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
2900 break;
2902 default:
2903 /* Keep looking for other operands. */
2904 *handled_ops_p = false;
2905 return NULL_TREE;
2908 *handled_ops_p = true;
2909 return NULL_TREE;
2912 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2913 call expressions. At the same time, determine if a nested function
2914 actually uses its static chain; if not, remember that. */
2916 static void
2917 convert_all_function_calls (struct nesting_info *root)
2919 unsigned int chain_count = 0, old_chain_count, iter_count;
2920 struct nesting_info *n;
2922 /* First, optimistically clear static_chain for all decls that haven't
2923 used the static chain already for variable access. But always create
2924 it if not optimizing. This makes it possible to reconstruct the static
2925 nesting tree at run time and thus to resolve up-level references from
2926 within the debugger. */
2927 FOR_EACH_NEST_INFO (n, root)
2929 if (n->thunk_p)
2930 continue;
2931 tree decl = n->context;
2932 if (!optimize)
2934 if (n->inner)
2935 (void) get_frame_type (n);
2936 if (n->outer)
2937 (void) get_chain_decl (n);
2939 else if (!n->outer || (!n->chain_decl && !n->chain_field))
2941 DECL_STATIC_CHAIN (decl) = 0;
2942 if (dump_file && (dump_flags & TDF_DETAILS))
2943 fprintf (dump_file, "Guessing no static-chain for %s\n",
2944 lang_hooks.decl_printable_name (decl, 2));
2946 else
2947 DECL_STATIC_CHAIN (decl) = 1;
2948 chain_count += DECL_STATIC_CHAIN (decl);
2951 FOR_EACH_NEST_INFO (n, root)
2952 if (n->thunk_p)
2954 tree decl = n->context;
2955 tree alias = cgraph_node::get (decl)->thunk.alias;
2956 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2959 /* Walk the functions and perform transformations. Note that these
2960 transformations can induce new uses of the static chain, which in turn
2961 require re-examining all users of the decl. */
2962 /* ??? It would make sense to try to use the call graph to speed this up,
2963 but the call graph hasn't really been built yet. Even if it did, we
2964 would still need to iterate in this loop since address-of references
2965 wouldn't show up in the callgraph anyway. */
2966 iter_count = 0;
2969 old_chain_count = chain_count;
2970 chain_count = 0;
2971 iter_count++;
2973 if (dump_file && (dump_flags & TDF_DETAILS))
2974 fputc ('\n', dump_file);
2976 FOR_EACH_NEST_INFO (n, root)
2978 if (n->thunk_p)
2979 continue;
2980 tree decl = n->context;
2981 walk_function (convert_tramp_reference_stmt,
2982 convert_tramp_reference_op, n);
2983 walk_function (convert_gimple_call, NULL, n);
2984 chain_count += DECL_STATIC_CHAIN (decl);
2987 FOR_EACH_NEST_INFO (n, root)
2988 if (n->thunk_p)
2990 tree decl = n->context;
2991 tree alias = cgraph_node::get (decl)->thunk.alias;
2992 DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
2995 while (chain_count != old_chain_count);
2997 if (dump_file && (dump_flags & TDF_DETAILS))
2998 fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
2999 iter_count);
3002 struct nesting_copy_body_data
3004 copy_body_data cb;
3005 struct nesting_info *root;
3008 /* A helper subroutine for debug_var_chain type remapping. */
3010 static tree
3011 nesting_copy_decl (tree decl, copy_body_data *id)
3013 struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
3014 tree *slot = nid->root->var_map->get (decl);
3016 if (slot)
3017 return (tree) *slot;
3019 if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
3021 tree new_decl = copy_decl_no_change (decl, id);
3022 DECL_ORIGINAL_TYPE (new_decl)
3023 = remap_type (DECL_ORIGINAL_TYPE (decl), id);
3024 return new_decl;
3027 if (VAR_P (decl)
3028 || TREE_CODE (decl) == PARM_DECL
3029 || TREE_CODE (decl) == RESULT_DECL)
3030 return decl;
3032 return copy_decl_no_change (decl, id);
3035 /* A helper function for remap_vla_decls. See if *TP contains
3036 some remapped variables. */
3038 static tree
3039 contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
3041 struct nesting_info *root = (struct nesting_info *) data;
3042 tree t = *tp;
3044 if (DECL_P (t))
3046 *walk_subtrees = 0;
3047 tree *slot = root->var_map->get (t);
3049 if (slot)
3050 return *slot;
3052 return NULL;
3055 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
3056 involved. */
3058 static void
3059 remap_vla_decls (tree block, struct nesting_info *root)
3061 tree var, subblock, val, type;
3062 struct nesting_copy_body_data id;
3064 for (subblock = BLOCK_SUBBLOCKS (block);
3065 subblock;
3066 subblock = BLOCK_CHAIN (subblock))
3067 remap_vla_decls (subblock, root);
3069 for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3070 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3072 val = DECL_VALUE_EXPR (var);
3073 type = TREE_TYPE (var);
3075 if (!(TREE_CODE (val) == INDIRECT_REF
3076 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3077 && variably_modified_type_p (type, NULL)))
3078 continue;
3080 if (root->var_map->get (TREE_OPERAND (val, 0))
3081 || walk_tree (&type, contains_remapped_vars, root, NULL))
3082 break;
3085 if (var == NULL_TREE)
3086 return;
3088 memset (&id, 0, sizeof (id));
3089 id.cb.copy_decl = nesting_copy_decl;
3090 id.cb.decl_map = new hash_map<tree, tree>;
3091 id.root = root;
3093 for (; var; var = DECL_CHAIN (var))
3094 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3096 struct nesting_info *i;
3097 tree newt, context;
3099 val = DECL_VALUE_EXPR (var);
3100 type = TREE_TYPE (var);
3102 if (!(TREE_CODE (val) == INDIRECT_REF
3103 && TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
3104 && variably_modified_type_p (type, NULL)))
3105 continue;
3107 tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
3108 if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
3109 continue;
3111 context = decl_function_context (var);
3112 for (i = root; i; i = i->outer)
3113 if (i->context == context)
3114 break;
3116 if (i == NULL)
3117 continue;
3119 /* Fully expand value expressions. This avoids having debug variables
3120 only referenced from them and that can be swept during GC. */
3121 if (slot)
3123 tree t = (tree) *slot;
3124 gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
3125 val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
3128 id.cb.src_fn = i->context;
3129 id.cb.dst_fn = i->context;
3130 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3132 TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3133 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3135 newt = TREE_TYPE (newt);
3136 type = TREE_TYPE (type);
3138 if (TYPE_NAME (newt)
3139 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3140 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3141 && newt != type
3142 && TYPE_NAME (newt) == TYPE_NAME (type))
3143 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3145 walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3146 if (val != DECL_VALUE_EXPR (var))
3147 SET_DECL_VALUE_EXPR (var, val);
3150 delete id.cb.decl_map;
3153 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3154 involved. */
3156 static void
3157 fixup_vla_decls (tree block)
3159 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3160 if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3162 tree val = DECL_VALUE_EXPR (var);
3164 if (!(TREE_CODE (val) == INDIRECT_REF
3165 && VAR_P (TREE_OPERAND (val, 0))
3166 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3167 continue;
3169 /* Fully expand value expressions. This avoids having debug variables
3170 only referenced from them and that can be swept during GC. */
3171 val = build1 (INDIRECT_REF, TREE_TYPE (val),
3172 DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3173 SET_DECL_VALUE_EXPR (var, val);
3176 for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3177 fixup_vla_decls (sub);
3180 /* Fold the MEM_REF *E. */
3181 bool
3182 fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3184 tree *ref_p = CONST_CAST2 (tree *, const tree *, (const tree *)e);
3185 *ref_p = fold (*ref_p);
3186 return true;
3189 /* Given DECL, a nested function, build an initialization call for FIELD,
3190 the trampoline or descriptor for DECL, using FUNC as the function. */
3192 static gcall *
3193 build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3194 tree func)
3196 tree arg1, arg2, arg3, x;
3198 gcc_assert (DECL_STATIC_CHAIN (decl));
3199 arg3 = build_addr (info->frame_decl);
3201 arg2 = build_addr (decl);
3203 x = build3 (COMPONENT_REF, TREE_TYPE (field),
3204 info->frame_decl, field, NULL_TREE);
3205 arg1 = build_addr (x);
3207 return gimple_build_call (func, 3, arg1, arg2, arg3);
3210 /* Do "everything else" to clean up or complete state collected by the various
3211 walking passes -- create a field to hold the frame base address, lay out the
3212 types and decls, generate code to initialize the frame decl, store critical
3213 expressions in the struct function for rtl to find. */
3215 static void
3216 finalize_nesting_tree_1 (struct nesting_info *root)
3218 gimple_seq stmt_list = NULL;
3219 gimple *stmt;
3220 tree context = root->context;
3221 struct function *sf;
3223 if (root->thunk_p)
3224 return;
3226 /* If we created a non-local frame type or decl, we need to lay them
3227 out at this time. */
3228 if (root->frame_type)
3230 /* Debugging information needs to compute the frame base address of the
3231 parent frame out of the static chain from the nested frame.
3233 The static chain is the address of the FRAME record, so one could
3234 imagine it would be possible to compute the frame base address just
3235 adding a constant offset to this address. Unfortunately, this is not
3236 possible: if the FRAME object has alignment constraints that are
3237 stronger than the stack, then the offset between the frame base and
3238 the FRAME object will be dynamic.
3240 What we do instead is to append a field to the FRAME object that holds
3241 the frame base address: then debug info just has to fetch this
3242 field. */
3244 /* Debugging information will refer to the CFA as the frame base
3245 address: we will do the same here. */
3246 const tree frame_addr_fndecl
3247 = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3249 /* Create a field in the FRAME record to hold the frame base address for
3250 this stack frame. Since it will be used only by the debugger, put it
3251 at the end of the record in order not to shift all other offsets. */
3252 tree fb_decl = make_node (FIELD_DECL);
3254 DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3255 TREE_TYPE (fb_decl) = ptr_type_node;
3256 TREE_ADDRESSABLE (fb_decl) = 1;
3257 DECL_CONTEXT (fb_decl) = root->frame_type;
3258 TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3259 fb_decl);
3261 /* In some cases the frame type will trigger the -Wpadded warning.
3262 This is not helpful; suppress it. */
3263 int save_warn_padded = warn_padded;
3264 warn_padded = 0;
3265 layout_type (root->frame_type);
3266 warn_padded = save_warn_padded;
3267 layout_decl (root->frame_decl, 0);
3269 /* Initialize the frame base address field. If the builtin we need is
3270 not available, set it to NULL so that debugging information does not
3271 reference junk. */
3272 tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3273 root->frame_decl, fb_decl, NULL_TREE);
3274 tree fb_tmp;
3276 if (frame_addr_fndecl != NULL_TREE)
3278 gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3279 integer_zero_node);
3280 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3282 fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3284 else
3285 fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3286 gimple_seq_add_stmt (&stmt_list,
3287 gimple_build_assign (fb_ref, fb_tmp));
3289 declare_vars (root->frame_decl,
3290 gimple_seq_first_stmt (gimple_body (context)), true);
3293 /* If any parameters were referenced non-locally, then we need to insert
3294 a copy or a pointer. */
3295 if (root->any_parm_remapped)
3297 tree p;
3298 for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3300 tree field, x, y;
3302 field = lookup_field_for_decl (root, p, NO_INSERT);
3303 if (!field)
3304 continue;
3306 if (use_pointer_in_frame (p))
3307 x = build_addr (p);
3308 else
3309 x = p;
3311 /* If the assignment is from a non-register the stmt is
3312 not valid gimple. Make it so by using a temporary instead. */
3313 if (!is_gimple_reg (x)
3314 && is_gimple_reg_type (TREE_TYPE (x)))
3316 gimple_stmt_iterator gsi = gsi_last (stmt_list);
3317 x = init_tmp_var (root, x, &gsi);
3320 y = build3 (COMPONENT_REF, TREE_TYPE (field),
3321 root->frame_decl, field, NULL_TREE);
3322 stmt = gimple_build_assign (y, x);
3323 gimple_seq_add_stmt (&stmt_list, stmt);
3327 /* If a chain_field was created, then it needs to be initialized
3328 from chain_decl. */
3329 if (root->chain_field)
3331 tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3332 root->frame_decl, root->chain_field, NULL_TREE);
3333 stmt = gimple_build_assign (x, get_chain_decl (root));
3334 gimple_seq_add_stmt (&stmt_list, stmt);
3337 /* If trampolines were created, then we need to initialize them. */
3338 if (root->any_tramp_created)
3340 struct nesting_info *i;
3341 for (i = root->inner; i ; i = i->next)
3343 tree field, x;
3345 field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3346 if (!field)
3347 continue;
3349 x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3350 stmt = build_init_call_stmt (root, i->context, field, x);
3351 gimple_seq_add_stmt (&stmt_list, stmt);
3355 /* If descriptors were created, then we need to initialize them. */
3356 if (root->any_descr_created)
3358 struct nesting_info *i;
3359 for (i = root->inner; i ; i = i->next)
3361 tree field, x;
3363 field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3364 if (!field)
3365 continue;
3367 x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3368 stmt = build_init_call_stmt (root, i->context, field, x);
3369 gimple_seq_add_stmt (&stmt_list, stmt);
3373 /* If we created initialization statements, insert them. */
3374 if (stmt_list)
3376 gbind *bind;
3377 annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3378 bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3379 gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3380 gimple_bind_set_body (bind, stmt_list);
3383 /* If a chain_decl was created, then it needs to be registered with
3384 struct function so that it gets initialized from the static chain
3385 register at the beginning of the function. */
3386 sf = DECL_STRUCT_FUNCTION (root->context);
3387 sf->static_chain_decl = root->chain_decl;
3389 /* Similarly for the non-local goto save area. */
3390 if (root->nl_goto_field)
3392 sf->nonlocal_goto_save_area
3393 = get_frame_field (root, context, root->nl_goto_field, NULL);
3394 sf->has_nonlocal_label = 1;
3397 /* Make sure all new local variables get inserted into the
3398 proper BIND_EXPR. */
3399 if (root->new_local_var_chain)
3400 declare_vars (root->new_local_var_chain,
3401 gimple_seq_first_stmt (gimple_body (root->context)),
3402 false);
3404 if (root->debug_var_chain)
3406 tree debug_var;
3407 gbind *scope;
3409 remap_vla_decls (DECL_INITIAL (root->context), root);
3411 for (debug_var = root->debug_var_chain; debug_var;
3412 debug_var = DECL_CHAIN (debug_var))
3413 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3414 break;
3416 /* If there are any debug decls with variable length types,
3417 remap those types using other debug_var_chain variables. */
3418 if (debug_var)
3420 struct nesting_copy_body_data id;
3422 memset (&id, 0, sizeof (id));
3423 id.cb.copy_decl = nesting_copy_decl;
3424 id.cb.decl_map = new hash_map<tree, tree>;
3425 id.root = root;
3427 for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3428 if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3430 tree type = TREE_TYPE (debug_var);
3431 tree newt, t = type;
3432 struct nesting_info *i;
3434 for (i = root; i; i = i->outer)
3435 if (variably_modified_type_p (type, i->context))
3436 break;
3438 if (i == NULL)
3439 continue;
3441 id.cb.src_fn = i->context;
3442 id.cb.dst_fn = i->context;
3443 id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3445 TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3446 while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3448 newt = TREE_TYPE (newt);
3449 t = TREE_TYPE (t);
3451 if (TYPE_NAME (newt)
3452 && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3453 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3454 && newt != t
3455 && TYPE_NAME (newt) == TYPE_NAME (t))
3456 TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3459 delete id.cb.decl_map;
3462 scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3463 if (gimple_bind_block (scope))
3464 declare_vars (root->debug_var_chain, scope, true);
3465 else
3466 BLOCK_VARS (DECL_INITIAL (root->context))
3467 = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3468 root->debug_var_chain);
3470 else
3471 fixup_vla_decls (DECL_INITIAL (root->context));
3473 /* Fold the rewritten MEM_REF trees. */
3474 root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3476 /* Dump the translated tree function. */
3477 if (dump_file)
3479 fputs ("\n\n", dump_file);
3480 dump_function_to_file (root->context, dump_file, dump_flags);
3484 static void
3485 finalize_nesting_tree (struct nesting_info *root)
3487 struct nesting_info *n;
3488 FOR_EACH_NEST_INFO (n, root)
3489 finalize_nesting_tree_1 (n);
3492 /* Unnest the nodes and pass them to cgraph. */
3494 static void
3495 unnest_nesting_tree_1 (struct nesting_info *root)
3497 struct cgraph_node *node = cgraph_node::get (root->context);
3499 /* For nested functions update the cgraph to reflect unnesting.
3500 We also delay finalizing of these functions up to this point. */
3501 if (node->origin)
3503 node->unnest ();
3504 if (!root->thunk_p)
3505 cgraph_node::finalize_function (root->context, true);
3509 static void
3510 unnest_nesting_tree (struct nesting_info *root)
3512 struct nesting_info *n;
3513 FOR_EACH_NEST_INFO (n, root)
3514 unnest_nesting_tree_1 (n);
3517 /* Free the data structures allocated during this pass. */
3519 static void
3520 free_nesting_tree (struct nesting_info *root)
3522 struct nesting_info *node, *next;
3524 node = iter_nestinfo_start (root);
3527 next = iter_nestinfo_next (node);
3528 delete node->var_map;
3529 delete node->field_map;
3530 delete node->mem_refs;
3531 free (node);
3532 node = next;
3534 while (node);
3537 /* Gimplify a function and all its nested functions. */
3538 static void
3539 gimplify_all_functions (struct cgraph_node *root)
3541 struct cgraph_node *iter;
3542 if (!gimple_body (root->decl))
3543 gimplify_function_tree (root->decl);
3544 for (iter = root->nested; iter; iter = iter->next_nested)
3545 if (!iter->thunk.thunk_p)
3546 gimplify_all_functions (iter);
3549 /* Main entry point for this pass. Process FNDECL and all of its nested
3550 subroutines and turn them into something less tightly bound. */
3552 void
3553 lower_nested_functions (tree fndecl)
3555 struct cgraph_node *cgn;
3556 struct nesting_info *root;
3558 /* If there are no nested functions, there's nothing to do. */
3559 cgn = cgraph_node::get (fndecl);
3560 if (!cgn->nested)
3561 return;
3563 gimplify_all_functions (cgn);
3565 set_dump_file (dump_begin (TDI_nested, &dump_flags));
3566 if (dump_file)
3567 fprintf (dump_file, "\n;; Function %s\n\n",
3568 lang_hooks.decl_printable_name (fndecl, 2));
3570 bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3571 root = create_nesting_tree (cgn);
3573 walk_all_functions (convert_nonlocal_reference_stmt,
3574 convert_nonlocal_reference_op,
3575 root);
3576 walk_all_functions (convert_local_reference_stmt,
3577 convert_local_reference_op,
3578 root);
3579 walk_all_functions (convert_nl_goto_reference, NULL, root);
3580 walk_all_functions (convert_nl_goto_receiver, NULL, root);
3582 convert_all_function_calls (root);
3583 finalize_nesting_tree (root);
3584 unnest_nesting_tree (root);
3586 free_nesting_tree (root);
3587 bitmap_obstack_release (&nesting_info_bitmap_obstack);
3589 if (dump_file)
3591 dump_end (TDI_nested, dump_file);
3592 set_dump_file (NULL);
3596 #include "gt-tree-nested.h"