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)
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/>. */
22 #include "coretypes.h"
30 #include "stringpool.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
35 #include "tree-inline.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
41 #include "langhooks.h"
42 #include "gimple-low.h"
43 #include "gomp-constants.h"
44 #include "diagnostic.h"
45 #include "alloc-pool.h"
46 #include "tree-nested.h"
47 #include "symbol-summary.h"
48 #include "symtab-thunks.h"
50 /* Summary of nested functions. */
51 static function_summary
<nested_function_info
*>
52 *nested_function_sum
= NULL
;
54 /* Return nested_function_info, if available. */
55 nested_function_info
*
56 nested_function_info::get (cgraph_node
*node
)
58 if (!nested_function_sum
)
60 return nested_function_sum
->get (node
);
63 /* Return nested_function_info possibly creating new one. */
64 nested_function_info
*
65 nested_function_info::get_create (cgraph_node
*node
)
67 if (!nested_function_sum
)
69 nested_function_sum
= new function_summary
<nested_function_info
*>
71 nested_function_sum
->disable_insertion_hook ();
73 return nested_function_sum
->get_create (node
);
76 /* cgraph_node is no longer nested function; update cgraph accordingly. */
78 unnest_function (cgraph_node
*node
)
80 nested_function_info
*info
= nested_function_info::get (node
);
81 cgraph_node
**node2
= &nested_function_info::get
82 (nested_function_origin (node
))->nested
;
84 gcc_checking_assert (info
->origin
);
85 while (*node2
!= node
)
86 node2
= &nested_function_info::get (*node2
)->next_nested
;
87 *node2
= info
->next_nested
;
88 info
->next_nested
= NULL
;
90 nested_function_sum
->remove (node
);
93 /* Destructor: unlink function from nested function lists. */
94 nested_function_info::~nested_function_info ()
97 for (cgraph_node
*n
= nested
; n
; n
= next
)
99 nested_function_info
*info
= nested_function_info::get (n
);
100 next
= info
->next_nested
;
102 info
->next_nested
= NULL
;
108 = &nested_function_info::get (origin
)->nested
;
110 nested_function_info
*info
;
111 while ((info
= nested_function_info::get (*node2
)) != this && info
)
112 node2
= &info
->next_nested
;
113 *node2
= next_nested
;
117 /* Free nested function info summaries. */
119 nested_function_info::release ()
121 if (nested_function_sum
)
122 delete (nested_function_sum
);
123 nested_function_sum
= NULL
;
126 /* If NODE is nested function, record it. */
128 maybe_record_nested_function (cgraph_node
*node
)
130 /* All nested functions gets lowered during the construction of symtab. */
131 if (symtab
->state
> CONSTRUCTION
)
133 if (DECL_CONTEXT (node
->decl
)
134 && TREE_CODE (DECL_CONTEXT (node
->decl
)) == FUNCTION_DECL
)
136 cgraph_node
*origin
= cgraph_node::get_create (DECL_CONTEXT (node
->decl
));
137 nested_function_info
*info
= nested_function_info::get_create (node
);
138 nested_function_info
*origin_info
139 = nested_function_info::get_create (origin
);
141 info
->origin
= origin
;
142 info
->next_nested
= origin_info
->nested
;
143 origin_info
->nested
= node
;
147 /* The object of this pass is to lower the representation of a set of nested
148 functions in order to expose all of the gory details of the various
149 nonlocal references. We want to do this sooner rather than later, in
150 order to give us more freedom in emitting all of the functions in question.
152 Back in olden times, when gcc was young, we developed an insanely
153 complicated scheme whereby variables which were referenced nonlocally
154 were forced to live in the stack of the declaring function, and then
155 the nested functions magically discovered where these variables were
156 placed. In order for this scheme to function properly, it required
157 that the outer function be partially expanded, then we switch to
158 compiling the inner function, and once done with those we switch back
159 to compiling the outer function. Such delicate ordering requirements
160 makes it difficult to do whole translation unit optimizations
161 involving such functions.
163 The implementation here is much more direct. Everything that can be
164 referenced by an inner function is a member of an explicitly created
165 structure herein called the "nonlocal frame struct". The incoming
166 static chain for a nested function is a pointer to this struct in
167 the parent. In this way, we settle on known offsets from a known
168 base, and so are decoupled from the logic that places objects in the
169 function's stack frame. More importantly, we don't have to wait for
170 that to happen -- since the compilation of the inner function is no
171 longer tied to a real stack frame, the nonlocal frame struct can be
172 allocated anywhere. Which means that the outer function is now
175 Theory of operation here is very simple. Iterate over all the
176 statements in all the functions (depth first) several times,
177 allocating structures and fields on demand. In general we want to
178 examine inner functions first, so that we can avoid making changes
179 to outer functions which are unnecessary.
181 The order of the passes matters a bit, in that later passes will be
182 skipped if it is discovered that the functions don't actually interact
183 at all. That is, they're nested in the lexical sense but could have
184 been written as independent functions without change. */
189 struct nesting_info
*outer
;
190 struct nesting_info
*inner
;
191 struct nesting_info
*next
;
193 hash_map
<tree
, tree
> *field_map
;
194 hash_map
<tree
, tree
> *var_map
;
195 hash_set
<tree
*> *mem_refs
;
196 bitmap suppress_expansion
;
199 tree new_local_var_chain
;
200 tree debug_var_chain
;
208 bool any_parm_remapped
;
209 bool any_tramp_created
;
210 bool any_descr_created
;
211 char static_chain_added
;
215 /* Iterate over the nesting tree, starting with ROOT, depth first. */
217 static inline struct nesting_info
*
218 iter_nestinfo_start (struct nesting_info
*root
)
225 static inline struct nesting_info
*
226 iter_nestinfo_next (struct nesting_info
*node
)
229 return iter_nestinfo_start (node
->next
);
233 #define FOR_EACH_NEST_INFO(I, ROOT) \
234 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
236 /* Obstack used for the bitmaps in the struct above. */
237 static struct bitmap_obstack nesting_info_bitmap_obstack
;
240 /* We're working in so many different function contexts simultaneously,
241 that create_tmp_var is dangerous. Prevent mishap. */
242 #define create_tmp_var cant_use_create_tmp_var_here_dummy
244 /* Like create_tmp_var, except record the variable for registration at
245 the given nesting level. */
248 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
252 /* If the type is of variable size or a type which must be created by the
253 frontend, something is wrong. Note that we explicitly allow
254 incomplete types here, since we create them ourselves here. */
255 gcc_assert (!TREE_ADDRESSABLE (type
));
256 gcc_assert (!TYPE_SIZE_UNIT (type
)
257 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
259 tmp_var
= create_tmp_var_raw (type
, prefix
);
260 DECL_CONTEXT (tmp_var
) = info
->context
;
261 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
262 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
264 info
->new_local_var_chain
= tmp_var
;
269 /* Like build_simple_mem_ref, but set TREE_THIS_NOTRAP on the result. */
272 build_simple_mem_ref_notrap (tree ptr
)
274 tree t
= build_simple_mem_ref (ptr
);
275 TREE_THIS_NOTRAP (t
) = 1;
279 /* Take the address of EXP to be used within function CONTEXT.
280 Mark it for addressability as necessary. */
283 build_addr (tree exp
)
285 mark_addressable (exp
);
286 return build_fold_addr_expr (exp
);
289 /* Insert FIELD into TYPE, sorted by alignment requirements. */
292 insert_field_into_struct (tree type
, tree field
)
296 DECL_CONTEXT (field
) = type
;
298 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
299 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
302 DECL_CHAIN (field
) = *p
;
305 /* Set correct alignment for frame struct type. */
306 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
307 SET_TYPE_ALIGN (type
, DECL_ALIGN (field
));
310 /* Build or return the RECORD_TYPE that describes the frame state that is
311 shared between INFO->CONTEXT and its nested functions. This record will
312 not be complete until finalize_nesting_tree; up until that point we'll
313 be adding fields as necessary.
315 We also build the DECL that represents this frame in the function. */
318 get_frame_type (struct nesting_info
*info
)
320 tree type
= info
->frame_type
;
325 type
= make_node (RECORD_TYPE
);
327 name
= concat ("FRAME.",
328 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
330 TYPE_NAME (type
) = get_identifier (name
);
333 info
->frame_type
= type
;
335 /* Do not put info->frame_decl on info->new_local_var_chain,
336 so that we can declare it in the lexical blocks, which
337 makes sure virtual regs that end up appearing in its RTL
338 expression get substituted in instantiate_virtual_regs. */
339 info
->frame_decl
= create_tmp_var_raw (type
, "FRAME");
340 DECL_CONTEXT (info
->frame_decl
) = info
->context
;
341 DECL_NONLOCAL_FRAME (info
->frame_decl
) = 1;
342 DECL_SEEN_IN_BIND_EXPR_P (info
->frame_decl
) = 1;
344 /* ??? Always make it addressable for now, since it is meant to
345 be pointed to by the static chain pointer. This pessimizes
346 when it turns out that no static chains are needed because
347 the nested functions referencing non-local variables are not
348 reachable, but the true pessimization is to create the non-
349 local frame structure in the first place. */
350 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
356 /* Return true if DECL should be referenced by pointer in the non-local frame
360 use_pointer_in_frame (tree decl
)
362 if (TREE_CODE (decl
) == PARM_DECL
)
364 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
365 sized DECLs, and inefficient to copy large aggregates. Don't bother
366 moving anything but scalar parameters. */
367 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
371 /* Variable-sized DECLs can only come from OMP clauses at this point
372 since the gimplifier has already turned the regular variables into
373 pointers. Do the same as the gimplifier. */
374 return !DECL_SIZE (decl
) || TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
;
378 /* Given DECL, a non-locally accessed variable, find or create a field
379 in the non-local frame structure for the given nesting context. */
382 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
383 enum insert_option insert
)
385 gcc_checking_assert (decl_function_context (decl
) == info
->context
);
387 if (insert
== NO_INSERT
)
389 tree
*slot
= info
->field_map
->get (decl
);
390 return slot
? *slot
: NULL_TREE
;
393 tree
*slot
= &info
->field_map
->get_or_insert (decl
);
396 tree type
= get_frame_type (info
);
397 tree field
= make_node (FIELD_DECL
);
398 DECL_NAME (field
) = DECL_NAME (decl
);
400 if (use_pointer_in_frame (decl
))
402 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
403 SET_DECL_ALIGN (field
, TYPE_ALIGN (TREE_TYPE (field
)));
404 DECL_NONADDRESSABLE_P (field
) = 1;
408 TREE_TYPE (field
) = TREE_TYPE (decl
);
409 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
410 SET_DECL_ALIGN (field
, DECL_ALIGN (decl
));
411 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
412 DECL_IGNORED_P (field
) = DECL_IGNORED_P (decl
);
413 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
414 TREE_NO_WARNING (field
) = TREE_NO_WARNING (decl
);
415 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
417 /* Declare the transformation and adjust the original DECL. For a
418 variable or for a parameter when not optimizing, we make it point
419 to the field in the frame directly. For a parameter, we don't do
420 it when optimizing because the variable tracking pass will already
422 if (VAR_P (decl
) || !optimize
)
425 = build3 (COMPONENT_REF
, TREE_TYPE (field
), info
->frame_decl
,
428 /* If the next declaration is a PARM_DECL pointing to the DECL,
429 we need to adjust its VALUE_EXPR directly, since chains of
430 VALUE_EXPRs run afoul of garbage collection. This occurs
431 in Ada for Out parameters that aren't copied in. */
432 tree next
= DECL_CHAIN (decl
);
434 && TREE_CODE (next
) == PARM_DECL
435 && DECL_HAS_VALUE_EXPR_P (next
)
436 && DECL_VALUE_EXPR (next
) == decl
)
437 SET_DECL_VALUE_EXPR (next
, x
);
439 SET_DECL_VALUE_EXPR (decl
, x
);
440 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
444 insert_field_into_struct (type
, field
);
447 if (TREE_CODE (decl
) == PARM_DECL
)
448 info
->any_parm_remapped
= true;
454 /* Build or return the variable that holds the static chain within
455 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
458 get_chain_decl (struct nesting_info
*info
)
460 tree decl
= info
->chain_decl
;
466 type
= get_frame_type (info
->outer
);
467 type
= build_pointer_type (type
);
469 /* Note that this variable is *not* entered into any BIND_EXPR;
470 the construction of this variable is handled specially in
471 expand_function_start and initialize_inlined_parameters.
472 Note also that it's represented as a parameter. This is more
473 close to the truth, since the initial value does come from
475 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
476 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
477 DECL_ARTIFICIAL (decl
) = 1;
478 DECL_IGNORED_P (decl
) = 1;
479 TREE_USED (decl
) = 1;
480 DECL_CONTEXT (decl
) = info
->context
;
481 DECL_ARG_TYPE (decl
) = type
;
483 /* Tell tree-inline.c that we never write to this variable, so
484 it can copy-prop the replacement value immediately. */
485 TREE_READONLY (decl
) = 1;
487 info
->chain_decl
= decl
;
490 && (dump_flags
& TDF_DETAILS
)
491 && !DECL_STATIC_CHAIN (info
->context
))
492 fprintf (dump_file
, "Setting static-chain for %s\n",
493 lang_hooks
.decl_printable_name (info
->context
, 2));
495 DECL_STATIC_CHAIN (info
->context
) = 1;
500 /* Build or return the field within the non-local frame state that holds
501 the static chain for INFO->CONTEXT. This is the way to walk back up
502 multiple nesting levels. */
505 get_chain_field (struct nesting_info
*info
)
507 tree field
= info
->chain_field
;
511 tree type
= build_pointer_type (get_frame_type (info
->outer
));
513 field
= make_node (FIELD_DECL
);
514 DECL_NAME (field
) = get_identifier ("__chain");
515 TREE_TYPE (field
) = type
;
516 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
517 DECL_NONADDRESSABLE_P (field
) = 1;
519 insert_field_into_struct (get_frame_type (info
), field
);
521 info
->chain_field
= field
;
524 && (dump_flags
& TDF_DETAILS
)
525 && !DECL_STATIC_CHAIN (info
->context
))
526 fprintf (dump_file
, "Setting static-chain for %s\n",
527 lang_hooks
.decl_printable_name (info
->context
, 2));
529 DECL_STATIC_CHAIN (info
->context
) = 1;
534 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
537 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
542 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
543 gimple_call_set_lhs (call
, t
);
544 if (! gsi_end_p (*gsi
))
545 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
546 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
552 /* Copy EXP into a temporary. Allocate the temporary in the context of
553 INFO and insert the initialization statement before GSI. */
556 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
561 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
562 stmt
= gimple_build_assign (t
, exp
);
563 if (! gsi_end_p (*gsi
))
564 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
565 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
571 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
574 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
575 gimple_stmt_iterator
*gsi
)
577 if (is_gimple_val (exp
))
580 return init_tmp_var (info
, exp
, gsi
);
583 /* Similarly, but copy from the temporary and insert the statement
584 after the iterator. */
587 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
592 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
593 stmt
= gimple_build_assign (exp
, t
);
594 if (! gsi_end_p (*gsi
))
595 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
596 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
601 /* Build or return the type used to represent a nested function trampoline. */
603 static GTY(()) tree trampoline_type
;
606 get_trampoline_type (struct nesting_info
*info
)
608 unsigned align
, size
;
612 return trampoline_type
;
614 align
= TRAMPOLINE_ALIGNMENT
;
615 size
= TRAMPOLINE_SIZE
;
617 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
618 then allocate extra space so that we can do dynamic alignment. */
619 if (align
> STACK_BOUNDARY
)
621 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
622 align
= STACK_BOUNDARY
;
625 t
= build_index_type (size_int (size
- 1));
626 t
= build_array_type (char_type_node
, t
);
627 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
628 FIELD_DECL
, get_identifier ("__data"), t
);
629 SET_DECL_ALIGN (t
, align
);
630 DECL_USER_ALIGN (t
) = 1;
632 trampoline_type
= make_node (RECORD_TYPE
);
633 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
634 TYPE_FIELDS (trampoline_type
) = t
;
635 layout_type (trampoline_type
);
636 DECL_CONTEXT (t
) = trampoline_type
;
638 return trampoline_type
;
641 /* Build or return the type used to represent a nested function descriptor. */
643 static GTY(()) tree descriptor_type
;
646 get_descriptor_type (struct nesting_info
*info
)
648 /* The base alignment is that of a function. */
649 const unsigned align
= FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY
);
653 return descriptor_type
;
655 t
= build_index_type (integer_one_node
);
656 t
= build_array_type (ptr_type_node
, t
);
657 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
658 FIELD_DECL
, get_identifier ("__data"), t
);
659 SET_DECL_ALIGN (t
, MAX (TYPE_ALIGN (ptr_type_node
), align
));
660 DECL_USER_ALIGN (t
) = 1;
662 descriptor_type
= make_node (RECORD_TYPE
);
663 TYPE_NAME (descriptor_type
) = get_identifier ("__builtin_descriptor");
664 TYPE_FIELDS (descriptor_type
) = t
;
665 layout_type (descriptor_type
);
666 DECL_CONTEXT (t
) = descriptor_type
;
668 return descriptor_type
;
671 /* Given DECL, a nested function, find or create an element in the
672 var map for this function. */
675 lookup_element_for_decl (struct nesting_info
*info
, tree decl
,
676 enum insert_option insert
)
678 if (insert
== NO_INSERT
)
680 tree
*slot
= info
->var_map
->get (decl
);
681 return slot
? *slot
: NULL_TREE
;
684 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
686 *slot
= build_tree_list (NULL_TREE
, NULL_TREE
);
691 /* Given DECL, a nested function, create a field in the non-local
692 frame structure for this function. */
695 create_field_for_decl (struct nesting_info
*info
, tree decl
, tree type
)
697 tree field
= make_node (FIELD_DECL
);
698 DECL_NAME (field
) = DECL_NAME (decl
);
699 TREE_TYPE (field
) = type
;
700 TREE_ADDRESSABLE (field
) = 1;
701 insert_field_into_struct (get_frame_type (info
), field
);
705 /* Given DECL, a nested function, find or create a field in the non-local
706 frame structure for a trampoline for this function. */
709 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
710 enum insert_option insert
)
714 elt
= lookup_element_for_decl (info
, decl
, insert
);
718 field
= TREE_PURPOSE (elt
);
720 if (!field
&& insert
== INSERT
)
722 field
= create_field_for_decl (info
, decl
, get_trampoline_type (info
));
723 TREE_PURPOSE (elt
) = field
;
724 info
->any_tramp_created
= true;
730 /* Given DECL, a nested function, find or create a field in the non-local
731 frame structure for a descriptor for this function. */
734 lookup_descr_for_decl (struct nesting_info
*info
, tree decl
,
735 enum insert_option insert
)
739 elt
= lookup_element_for_decl (info
, decl
, insert
);
743 field
= TREE_VALUE (elt
);
745 if (!field
&& insert
== INSERT
)
747 field
= create_field_for_decl (info
, decl
, get_descriptor_type (info
));
748 TREE_VALUE (elt
) = field
;
749 info
->any_descr_created
= true;
755 /* Build or return the field within the non-local frame state that holds
756 the non-local goto "jmp_buf". The buffer itself is maintained by the
757 rtl middle-end as dynamic stack space is allocated. */
760 get_nl_goto_field (struct nesting_info
*info
)
762 tree field
= info
->nl_goto_field
;
768 /* For __builtin_nonlocal_goto, we need N words. The first is the
769 frame pointer, the rest is for the target's stack pointer save
770 area. The number of words is controlled by STACK_SAVEAREA_MODE;
771 not the best interface, but it'll do for now. */
772 if (Pmode
== ptr_mode
)
773 type
= ptr_type_node
;
775 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
778 = as_a
<scalar_int_mode
> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
779 size
= GET_MODE_SIZE (mode
);
780 size
= size
/ GET_MODE_SIZE (Pmode
);
783 type
= build_array_type
784 (type
, build_index_type (size_int (size
)));
786 field
= make_node (FIELD_DECL
);
787 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
788 TREE_TYPE (field
) = type
;
789 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
790 TREE_ADDRESSABLE (field
) = 1;
792 insert_field_into_struct (get_frame_type (info
), field
);
794 info
->nl_goto_field
= field
;
800 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
803 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
804 struct nesting_info
*info
, gimple_seq
*pseq
)
806 struct walk_stmt_info wi
;
808 memset (&wi
, 0, sizeof (wi
));
811 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
815 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
818 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
819 struct nesting_info
*info
)
821 gimple_seq body
= gimple_body (info
->context
);
822 walk_body (callback_stmt
, callback_op
, info
, &body
);
823 gimple_set_body (info
->context
, body
);
826 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
829 walk_gimple_omp_for (gomp_for
*for_stmt
,
830 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
831 struct nesting_info
*info
)
833 struct walk_stmt_info wi
;
838 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
841 memset (&wi
, 0, sizeof (wi
));
843 wi
.gsi
= gsi_last (seq
);
845 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
848 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
852 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
857 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
860 t
= gimple_omp_for_incr (for_stmt
, i
);
861 gcc_assert (BINARY_CLASS_P (t
));
863 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
866 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
869 seq
= gsi_seq (wi
.gsi
);
870 if (!gimple_seq_empty_p (seq
))
872 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
873 annotate_all_with_location (seq
, gimple_location (for_stmt
));
874 gimple_seq_add_seq (&pre_body
, seq
);
875 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
879 /* Similarly for ROOT and all functions nested underneath, depth first. */
882 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
883 struct nesting_info
*root
)
885 struct nesting_info
*n
;
886 FOR_EACH_NEST_INFO (n
, root
)
887 walk_function (callback_stmt
, callback_op
, n
);
891 /* We have to check for a fairly pathological case. The operands of function
892 nested function are to be interpreted in the context of the enclosing
893 function. So if any are variably-sized, they will get remapped when the
894 enclosing function is inlined. But that remapping would also have to be
895 done in the types of the PARM_DECLs of the nested function, meaning the
896 argument types of that function will disagree with the arguments in the
897 calls to that function. So we'd either have to make a copy of the nested
898 function corresponding to each time the enclosing function was inlined or
899 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
900 function. The former is not practical. The latter would still require
901 detecting this case to know when to add the conversions. So, for now at
902 least, we don't inline such an enclosing function.
904 We have to do that check recursively, so here return indicating whether
905 FNDECL has such a nested function. ORIG_FN is the function we were
906 trying to inline to use for checking whether any argument is variably
907 modified by anything in it.
909 It would be better to do this in tree-inline.c so that we could give
910 the appropriate warning for why a function can't be inlined, but that's
911 too late since the nesting structure has already been flattened and
912 adding a flag just to record this fact seems a waste of a flag. */
915 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
917 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
920 for (cgn
= first_nested_function (cgn
); cgn
;
921 cgn
= next_nested_function (cgn
))
923 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
924 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
927 if (check_for_nested_with_variably_modified (cgn
->decl
,
935 /* Construct our local datastructure describing the function nesting
936 tree rooted by CGN. */
938 static struct nesting_info
*
939 create_nesting_tree (struct cgraph_node
*cgn
)
941 struct nesting_info
*info
= XCNEW (struct nesting_info
);
942 info
->field_map
= new hash_map
<tree
, tree
>;
943 info
->var_map
= new hash_map
<tree
, tree
>;
944 info
->mem_refs
= new hash_set
<tree
*>;
945 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
946 info
->context
= cgn
->decl
;
947 info
->thunk_p
= cgn
->thunk
;
949 for (cgn
= first_nested_function (cgn
); cgn
;
950 cgn
= next_nested_function (cgn
))
952 struct nesting_info
*sub
= create_nesting_tree (cgn
);
954 sub
->next
= info
->inner
;
958 /* See discussion at check_for_nested_with_variably_modified for a
959 discussion of why this has to be here. */
960 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
961 DECL_UNINLINABLE (info
->context
) = true;
966 /* Return an expression computing the static chain for TARGET_CONTEXT
967 from INFO->CONTEXT. Insert any necessary computations before TSI. */
970 get_static_chain (struct nesting_info
*info
, tree target_context
,
971 gimple_stmt_iterator
*gsi
)
973 struct nesting_info
*i
;
976 if (info
->context
== target_context
)
978 x
= build_addr (info
->frame_decl
);
979 info
->static_chain_added
|= 1;
983 x
= get_chain_decl (info
);
984 info
->static_chain_added
|= 2;
986 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
988 tree field
= get_chain_field (i
);
990 x
= build_simple_mem_ref_notrap (x
);
991 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
992 x
= init_tmp_var (info
, x
, gsi
);
1000 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
1001 frame as seen from INFO->CONTEXT. Insert any necessary computations
1005 get_frame_field (struct nesting_info
*info
, tree target_context
,
1006 tree field
, gimple_stmt_iterator
*gsi
)
1008 struct nesting_info
*i
;
1011 if (info
->context
== target_context
)
1013 /* Make sure frame_decl gets created. */
1014 (void) get_frame_type (info
);
1015 x
= info
->frame_decl
;
1016 info
->static_chain_added
|= 1;
1020 x
= get_chain_decl (info
);
1021 info
->static_chain_added
|= 2;
1023 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
1025 tree field
= get_chain_field (i
);
1027 x
= build_simple_mem_ref_notrap (x
);
1028 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1029 x
= init_tmp_var (info
, x
, gsi
);
1032 x
= build_simple_mem_ref_notrap (x
);
1035 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1039 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
1041 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
1042 in the nested function with DECL_VALUE_EXPR set to reference the true
1043 variable in the parent function. This is used both for debug info
1044 and in OMP lowering. */
1047 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
1049 tree target_context
;
1050 struct nesting_info
*i
;
1051 tree x
, field
, new_decl
;
1053 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1058 target_context
= decl_function_context (decl
);
1060 /* A copy of the code in get_frame_field, but without the temporaries. */
1061 if (info
->context
== target_context
)
1063 /* Make sure frame_decl gets created. */
1064 (void) get_frame_type (info
);
1065 x
= info
->frame_decl
;
1067 info
->static_chain_added
|= 1;
1071 x
= get_chain_decl (info
);
1072 info
->static_chain_added
|= 2;
1073 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
1075 field
= get_chain_field (i
);
1076 x
= build_simple_mem_ref_notrap (x
);
1077 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1079 x
= build_simple_mem_ref_notrap (x
);
1082 field
= lookup_field_for_decl (i
, decl
, INSERT
);
1083 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1084 if (use_pointer_in_frame (decl
))
1085 x
= build_simple_mem_ref_notrap (x
);
1087 /* ??? We should be remapping types as well, surely. */
1088 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1089 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1090 DECL_CONTEXT (new_decl
) = info
->context
;
1091 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1092 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1093 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1094 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1095 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1096 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1097 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1098 if ((TREE_CODE (decl
) == PARM_DECL
1099 || TREE_CODE (decl
) == RESULT_DECL
1101 && DECL_BY_REFERENCE (decl
))
1102 DECL_BY_REFERENCE (new_decl
) = 1;
1104 SET_DECL_VALUE_EXPR (new_decl
, x
);
1105 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1108 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1109 info
->debug_var_chain
= new_decl
;
1112 && info
->context
!= target_context
1113 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
1114 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
1120 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1121 and PARM_DECLs that belong to outer functions.
1123 The rewrite will involve some number of structure accesses back up
1124 the static chain. E.g. for a variable FOO up one nesting level it'll
1125 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1126 indirections apply to decls for which use_pointer_in_frame is true. */
1129 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1131 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1132 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1136 switch (TREE_CODE (t
))
1139 /* Non-automatic variables are never processed. */
1140 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1146 tree x
, target_context
= decl_function_context (t
);
1148 if (info
->context
== target_context
)
1153 if (bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1154 x
= get_nonlocal_debug_decl (info
, t
);
1157 struct nesting_info
*i
= info
;
1158 while (i
&& i
->context
!= target_context
)
1160 /* If none of the outer contexts is the target context, this means
1161 that the VAR or PARM_DECL is referenced in a wrong context. */
1163 internal_error ("%s from %s referenced in %s",
1164 IDENTIFIER_POINTER (DECL_NAME (t
)),
1165 IDENTIFIER_POINTER (DECL_NAME (target_context
)),
1166 IDENTIFIER_POINTER (DECL_NAME (info
->context
)));
1168 x
= lookup_field_for_decl (i
, t
, INSERT
);
1169 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1170 if (use_pointer_in_frame (t
))
1172 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1173 x
= build_simple_mem_ref_notrap (x
);
1180 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1182 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1190 /* We're taking the address of a label from a parent function, but
1191 this is not itself a non-local goto. Mark the label such that it
1192 will not be deleted, much as we would with a label address in
1194 if (decl_function_context (t
) != info
->context
)
1195 FORCED_LABEL (t
) = 1;
1200 bool save_val_only
= wi
->val_only
;
1202 wi
->val_only
= false;
1204 wi
->changed
= false;
1205 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
1206 wi
->val_only
= true;
1212 /* If we changed anything, we might no longer be directly
1213 referencing a decl. */
1214 save_context
= current_function_decl
;
1215 current_function_decl
= info
->context
;
1216 recompute_tree_invariant_for_addr_expr (t
);
1217 current_function_decl
= save_context
;
1219 /* If the callback converted the address argument in a context
1220 where we only accept variables (and min_invariant, presumably),
1221 then compute the address into a temporary. */
1223 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1233 case ARRAY_RANGE_REF
:
1235 /* Go down this entire nest and just look at the final prefix and
1236 anything that describes the references. Otherwise, we lose track
1237 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1238 wi
->val_only
= true;
1240 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1242 if (TREE_CODE (t
) == COMPONENT_REF
)
1243 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1245 else if (TREE_CODE (t
) == ARRAY_REF
1246 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1248 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1250 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1252 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1256 wi
->val_only
= false;
1257 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1260 case VIEW_CONVERT_EXPR
:
1261 /* Just request to look at the subtrees, leaving val_only and lhs
1262 untouched. This might actually be for !val_only + lhs, in which
1263 case we don't want to force a replacement by a temporary. */
1268 if (!IS_TYPE_OR_DECL_P (t
))
1271 wi
->val_only
= true;
1280 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1281 struct walk_stmt_info
*);
1283 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1284 and PARM_DECLs that belong to outer functions. */
1287 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1289 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1290 bool need_chain
= false, need_stmts
= false;
1291 tree clause
, decl
, *pdecl
;
1293 bitmap new_suppress
;
1295 new_suppress
= BITMAP_GGC_ALLOC ();
1296 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1298 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1301 switch (OMP_CLAUSE_CODE (clause
))
1303 case OMP_CLAUSE_REDUCTION
:
1304 case OMP_CLAUSE_IN_REDUCTION
:
1305 case OMP_CLAUSE_TASK_REDUCTION
:
1306 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1308 if (TREE_CODE (OMP_CLAUSE_DECL (clause
)) == MEM_REF
)
1310 pdecl
= &TREE_OPERAND (OMP_CLAUSE_DECL (clause
), 0);
1311 if (TREE_CODE (*pdecl
) == POINTER_PLUS_EXPR
)
1312 pdecl
= &TREE_OPERAND (*pdecl
, 0);
1313 if (TREE_CODE (*pdecl
) == INDIRECT_REF
1314 || TREE_CODE (*pdecl
) == ADDR_EXPR
)
1315 pdecl
= &TREE_OPERAND (*pdecl
, 0);
1317 goto do_decl_clause
;
1319 case OMP_CLAUSE_LASTPRIVATE
:
1320 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1322 goto do_decl_clause
;
1324 case OMP_CLAUSE_LINEAR
:
1325 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1327 wi
->val_only
= true;
1329 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
),
1331 goto do_decl_clause
;
1333 case OMP_CLAUSE_PRIVATE
:
1334 case OMP_CLAUSE_FIRSTPRIVATE
:
1335 case OMP_CLAUSE_COPYPRIVATE
:
1336 case OMP_CLAUSE_SHARED
:
1337 case OMP_CLAUSE_TO_DECLARE
:
1338 case OMP_CLAUSE_LINK
:
1339 case OMP_CLAUSE_USE_DEVICE_PTR
:
1340 case OMP_CLAUSE_USE_DEVICE_ADDR
:
1341 case OMP_CLAUSE_IS_DEVICE_PTR
:
1344 pdecl
= &OMP_CLAUSE_DECL (clause
);
1347 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1349 if (decl_function_context (decl
) != info
->context
)
1351 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1352 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1353 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1354 *pdecl
= get_nonlocal_debug_decl (info
, decl
);
1355 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1360 case OMP_CLAUSE_SCHEDULE
:
1361 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1364 case OMP_CLAUSE_FINAL
:
1366 case OMP_CLAUSE_NUM_THREADS
:
1367 case OMP_CLAUSE_DEPEND
:
1368 case OMP_CLAUSE_DEVICE
:
1369 case OMP_CLAUSE_NUM_TEAMS
:
1370 case OMP_CLAUSE_THREAD_LIMIT
:
1371 case OMP_CLAUSE_SAFELEN
:
1372 case OMP_CLAUSE_SIMDLEN
:
1373 case OMP_CLAUSE_PRIORITY
:
1374 case OMP_CLAUSE_GRAINSIZE
:
1375 case OMP_CLAUSE_NUM_TASKS
:
1376 case OMP_CLAUSE_HINT
:
1377 case OMP_CLAUSE_NUM_GANGS
:
1378 case OMP_CLAUSE_NUM_WORKERS
:
1379 case OMP_CLAUSE_VECTOR_LENGTH
:
1380 case OMP_CLAUSE_GANG
:
1381 case OMP_CLAUSE_WORKER
:
1382 case OMP_CLAUSE_VECTOR
:
1383 case OMP_CLAUSE_ASYNC
:
1384 case OMP_CLAUSE_WAIT
:
1385 /* Several OpenACC clauses have optional arguments. Check if they
1387 if (OMP_CLAUSE_OPERAND (clause
, 0))
1389 wi
->val_only
= true;
1391 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1395 /* The gang clause accepts two arguments. */
1396 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
1397 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
1399 wi
->val_only
= true;
1401 convert_nonlocal_reference_op
1402 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
1406 case OMP_CLAUSE_DIST_SCHEDULE
:
1407 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1409 wi
->val_only
= true;
1411 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1416 case OMP_CLAUSE_MAP
:
1418 case OMP_CLAUSE_FROM
:
1419 if (OMP_CLAUSE_SIZE (clause
))
1421 wi
->val_only
= true;
1423 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause
),
1426 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1427 goto do_decl_clause
;
1428 wi
->val_only
= true;
1430 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_nonlocal_reference_op
,
1434 case OMP_CLAUSE_ALIGNED
:
1435 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1437 wi
->val_only
= true;
1439 convert_nonlocal_reference_op
1440 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1443 case OMP_CLAUSE_NONTEMPORAL
:
1444 /* Like do_decl_clause, but don't add any suppression. */
1445 decl
= OMP_CLAUSE_DECL (clause
);
1447 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1449 if (decl_function_context (decl
) != info
->context
)
1451 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1456 case OMP_CLAUSE_NOWAIT
:
1457 case OMP_CLAUSE_ORDERED
:
1458 case OMP_CLAUSE_DEFAULT
:
1459 case OMP_CLAUSE_COPYIN
:
1460 case OMP_CLAUSE_COLLAPSE
:
1461 case OMP_CLAUSE_TILE
:
1462 case OMP_CLAUSE_UNTIED
:
1463 case OMP_CLAUSE_MERGEABLE
:
1464 case OMP_CLAUSE_PROC_BIND
:
1465 case OMP_CLAUSE_NOGROUP
:
1466 case OMP_CLAUSE_THREADS
:
1467 case OMP_CLAUSE_SIMD
:
1468 case OMP_CLAUSE_DEFAULTMAP
:
1469 case OMP_CLAUSE_ORDER
:
1470 case OMP_CLAUSE_SEQ
:
1471 case OMP_CLAUSE_INDEPENDENT
:
1472 case OMP_CLAUSE_AUTO
:
1473 case OMP_CLAUSE_IF_PRESENT
:
1474 case OMP_CLAUSE_FINALIZE
:
1475 case OMP_CLAUSE__CONDTEMP_
:
1476 case OMP_CLAUSE__SCANTEMP_
:
1479 /* The following clause belongs to the OpenACC cache directive, which
1480 is discarded during gimplification. */
1481 case OMP_CLAUSE__CACHE_
:
1482 /* The following clauses are only allowed in the OpenMP declare simd
1483 directive, so not seen here. */
1484 case OMP_CLAUSE_UNIFORM
:
1485 case OMP_CLAUSE_INBRANCH
:
1486 case OMP_CLAUSE_NOTINBRANCH
:
1487 /* The following clauses are only allowed on OpenMP cancel and
1488 cancellation point directives, which at this point have already
1489 been lowered into a function call. */
1490 case OMP_CLAUSE_FOR
:
1491 case OMP_CLAUSE_PARALLEL
:
1492 case OMP_CLAUSE_SECTIONS
:
1493 case OMP_CLAUSE_TASKGROUP
:
1494 /* The following clauses are only added during OMP lowering; nested
1495 function decomposition happens before that. */
1496 case OMP_CLAUSE__LOOPTEMP_
:
1497 case OMP_CLAUSE__REDUCTEMP_
:
1498 case OMP_CLAUSE__SIMDUID_
:
1499 case OMP_CLAUSE__SIMT_
:
1500 /* Anything else. */
1506 info
->suppress_expansion
= new_suppress
;
1509 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1510 switch (OMP_CLAUSE_CODE (clause
))
1512 case OMP_CLAUSE_REDUCTION
:
1513 case OMP_CLAUSE_IN_REDUCTION
:
1514 case OMP_CLAUSE_TASK_REDUCTION
:
1515 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1518 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1519 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1521 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1522 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1524 tree save_local_var_chain
= info
->new_local_var_chain
;
1525 info
->new_local_var_chain
= NULL
;
1526 gimple_seq
*seq
= &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
);
1527 walk_body (convert_nonlocal_reference_stmt
,
1528 convert_nonlocal_reference_op
, info
, seq
);
1529 if (info
->new_local_var_chain
)
1530 declare_vars (info
->new_local_var_chain
,
1531 gimple_seq_first_stmt (*seq
), false);
1532 info
->new_local_var_chain
= NULL
;
1533 seq
= &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
);
1534 walk_body (convert_nonlocal_reference_stmt
,
1535 convert_nonlocal_reference_op
, info
, seq
);
1536 if (info
->new_local_var_chain
)
1537 declare_vars (info
->new_local_var_chain
,
1538 gimple_seq_first_stmt (*seq
), false);
1539 info
->new_local_var_chain
= save_local_var_chain
;
1540 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1542 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1543 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1548 case OMP_CLAUSE_LASTPRIVATE
:
1550 tree save_local_var_chain
= info
->new_local_var_chain
;
1551 info
->new_local_var_chain
= NULL
;
1552 gimple_seq
*seq
= &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
);
1553 walk_body (convert_nonlocal_reference_stmt
,
1554 convert_nonlocal_reference_op
, info
, seq
);
1555 if (info
->new_local_var_chain
)
1556 declare_vars (info
->new_local_var_chain
,
1557 gimple_seq_first_stmt (*seq
), false);
1558 info
->new_local_var_chain
= save_local_var_chain
;
1562 case OMP_CLAUSE_LINEAR
:
1564 tree save_local_var_chain
= info
->new_local_var_chain
;
1565 info
->new_local_var_chain
= NULL
;
1566 gimple_seq
*seq
= &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
);
1567 walk_body (convert_nonlocal_reference_stmt
,
1568 convert_nonlocal_reference_op
, info
, seq
);
1569 if (info
->new_local_var_chain
)
1570 declare_vars (info
->new_local_var_chain
,
1571 gimple_seq_first_stmt (*seq
), false);
1572 info
->new_local_var_chain
= save_local_var_chain
;
1583 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1586 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1588 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1589 type
= TREE_TYPE (type
);
1591 if (TYPE_NAME (type
)
1592 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1593 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1594 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1596 while (POINTER_TYPE_P (type
)
1597 || TREE_CODE (type
) == VECTOR_TYPE
1598 || TREE_CODE (type
) == FUNCTION_TYPE
1599 || TREE_CODE (type
) == METHOD_TYPE
)
1600 type
= TREE_TYPE (type
);
1602 if (TREE_CODE (type
) == ARRAY_TYPE
)
1606 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1607 domain
= TYPE_DOMAIN (type
);
1610 t
= TYPE_MIN_VALUE (domain
);
1611 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1612 && decl_function_context (t
) != info
->context
)
1613 get_nonlocal_debug_decl (info
, t
);
1614 t
= TYPE_MAX_VALUE (domain
);
1615 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1616 && decl_function_context (t
) != info
->context
)
1617 get_nonlocal_debug_decl (info
, t
);
1622 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1623 PARM_DECLs that belong to outer functions. This handles statements
1624 that are not handled via the standard recursion done in
1625 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1626 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1627 operands of STMT have been handled by this function. */
1630 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1631 struct walk_stmt_info
*wi
)
1633 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1634 tree save_local_var_chain
;
1635 bitmap save_suppress
;
1636 gimple
*stmt
= gsi_stmt (*gsi
);
1638 switch (gimple_code (stmt
))
1641 /* Don't walk non-local gotos for now. */
1642 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1644 wi
->val_only
= true;
1646 *handled_ops_p
= false;
1651 case GIMPLE_OMP_TEAMS
:
1652 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
1654 save_suppress
= info
->suppress_expansion
;
1655 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
),
1657 walk_body (convert_nonlocal_reference_stmt
,
1658 convert_nonlocal_reference_op
, info
,
1659 gimple_omp_body_ptr (stmt
));
1660 info
->suppress_expansion
= save_suppress
;
1665 case GIMPLE_OMP_PARALLEL
:
1666 case GIMPLE_OMP_TASK
:
1667 save_suppress
= info
->suppress_expansion
;
1668 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1672 decl
= get_chain_decl (info
);
1673 c
= build_omp_clause (gimple_location (stmt
),
1674 OMP_CLAUSE_FIRSTPRIVATE
);
1675 OMP_CLAUSE_DECL (c
) = decl
;
1676 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1677 gimple_omp_taskreg_set_clauses (stmt
, c
);
1680 save_local_var_chain
= info
->new_local_var_chain
;
1681 info
->new_local_var_chain
= NULL
;
1683 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1684 info
, gimple_omp_body_ptr (stmt
));
1686 if (info
->new_local_var_chain
)
1687 declare_vars (info
->new_local_var_chain
,
1688 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1690 info
->new_local_var_chain
= save_local_var_chain
;
1691 info
->suppress_expansion
= save_suppress
;
1694 case GIMPLE_OMP_FOR
:
1695 save_suppress
= info
->suppress_expansion
;
1696 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1697 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
1698 convert_nonlocal_reference_stmt
,
1699 convert_nonlocal_reference_op
, info
);
1700 walk_body (convert_nonlocal_reference_stmt
,
1701 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1702 info
->suppress_expansion
= save_suppress
;
1705 case GIMPLE_OMP_SECTIONS
:
1706 save_suppress
= info
->suppress_expansion
;
1707 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1708 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1709 info
, gimple_omp_body_ptr (stmt
));
1710 info
->suppress_expansion
= save_suppress
;
1713 case GIMPLE_OMP_SINGLE
:
1714 save_suppress
= info
->suppress_expansion
;
1715 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1716 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1717 info
, gimple_omp_body_ptr (stmt
));
1718 info
->suppress_expansion
= save_suppress
;
1721 case GIMPLE_OMP_TASKGROUP
:
1722 save_suppress
= info
->suppress_expansion
;
1723 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt
), wi
);
1724 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1725 info
, gimple_omp_body_ptr (stmt
));
1726 info
->suppress_expansion
= save_suppress
;
1729 case GIMPLE_OMP_TARGET
:
1730 if (!is_gimple_omp_offloaded (stmt
))
1732 save_suppress
= info
->suppress_expansion
;
1733 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1735 info
->suppress_expansion
= save_suppress
;
1736 walk_body (convert_nonlocal_reference_stmt
,
1737 convert_nonlocal_reference_op
, info
,
1738 gimple_omp_body_ptr (stmt
));
1741 save_suppress
= info
->suppress_expansion
;
1742 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1746 decl
= get_chain_decl (info
);
1747 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1748 OMP_CLAUSE_DECL (c
) = decl
;
1749 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TO
);
1750 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
1751 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1752 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
1755 save_local_var_chain
= info
->new_local_var_chain
;
1756 info
->new_local_var_chain
= NULL
;
1758 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1759 info
, gimple_omp_body_ptr (stmt
));
1761 if (info
->new_local_var_chain
)
1762 declare_vars (info
->new_local_var_chain
,
1763 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1765 info
->new_local_var_chain
= save_local_var_chain
;
1766 info
->suppress_expansion
= save_suppress
;
1769 case GIMPLE_OMP_SECTION
:
1770 case GIMPLE_OMP_MASTER
:
1771 case GIMPLE_OMP_ORDERED
:
1772 case GIMPLE_OMP_SCAN
:
1773 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1774 info
, gimple_omp_body_ptr (stmt
));
1779 gbind
*bind_stmt
= as_a
<gbind
*> (stmt
);
1781 for (tree var
= gimple_bind_vars (bind_stmt
); var
; var
= DECL_CHAIN (var
))
1782 if (TREE_CODE (var
) == NAMELIST_DECL
)
1784 /* Adjust decls mentioned in NAMELIST_DECL. */
1785 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
1789 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
1792 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1794 if (decl_function_context (decl
) != info
->context
)
1795 CONSTRUCTOR_ELT (decls
, i
)->value
1796 = get_nonlocal_debug_decl (info
, decl
);
1800 *handled_ops_p
= false;
1804 wi
->val_only
= true;
1806 *handled_ops_p
= false;
1810 if (gimple_clobber_p (stmt
))
1812 tree lhs
= gimple_assign_lhs (stmt
);
1814 && !(TREE_STATIC (lhs
) || DECL_EXTERNAL (lhs
))
1815 && decl_function_context (lhs
) != info
->context
)
1817 gsi_replace (gsi
, gimple_build_nop (), true);
1821 *handled_ops_p
= false;
1825 /* For every other statement that we are not interested in
1826 handling here, let the walker traverse the operands. */
1827 *handled_ops_p
= false;
1831 /* We have handled all of STMT operands, no need to traverse the operands. */
1832 *handled_ops_p
= true;
1837 /* A subroutine of convert_local_reference. Create a local variable
1838 in the parent function with DECL_VALUE_EXPR set to reference the
1839 field in FRAME. This is used both for debug info and in OMP
1843 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1847 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1851 /* Make sure frame_decl gets created. */
1852 (void) get_frame_type (info
);
1853 x
= info
->frame_decl
;
1854 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1856 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1857 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1858 DECL_CONTEXT (new_decl
) = info
->context
;
1859 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1860 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1861 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1862 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1863 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1864 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1865 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1866 if ((TREE_CODE (decl
) == PARM_DECL
1867 || TREE_CODE (decl
) == RESULT_DECL
1869 && DECL_BY_REFERENCE (decl
))
1870 DECL_BY_REFERENCE (new_decl
) = 1;
1872 SET_DECL_VALUE_EXPR (new_decl
, x
);
1873 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1876 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1877 info
->debug_var_chain
= new_decl
;
1879 /* Do not emit debug info twice. */
1880 DECL_IGNORED_P (decl
) = 1;
1886 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1887 and PARM_DECLs that were referenced by inner nested functions.
1888 The rewrite will be a structure reference to the local frame variable. */
1890 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1893 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1895 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1896 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1897 tree t
= *tp
, field
, x
;
1901 switch (TREE_CODE (t
))
1904 /* Non-automatic variables are never processed. */
1905 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1910 if (t
!= info
->frame_decl
&& decl_function_context (t
) == info
->context
)
1912 /* If we copied a pointer to the frame, then the original decl
1913 is used unchanged in the parent function. */
1914 if (use_pointer_in_frame (t
))
1917 /* No need to transform anything if no child references the
1919 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1924 if (bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1925 x
= get_local_debug_decl (info
, t
, field
);
1927 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1932 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1934 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1942 save_val_only
= wi
->val_only
;
1943 wi
->val_only
= false;
1945 wi
->changed
= false;
1946 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1947 wi
->val_only
= save_val_only
;
1949 /* If we converted anything ... */
1954 /* Then the frame decl is now addressable. */
1955 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1957 save_context
= current_function_decl
;
1958 current_function_decl
= info
->context
;
1959 recompute_tree_invariant_for_addr_expr (t
);
1960 current_function_decl
= save_context
;
1962 /* If we are in a context where we only accept values, then
1963 compute the address into a temporary. */
1965 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1974 case ARRAY_RANGE_REF
:
1976 /* Go down this entire nest and just look at the final prefix and
1977 anything that describes the references. Otherwise, we lose track
1978 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1979 save_val_only
= wi
->val_only
;
1980 wi
->val_only
= true;
1982 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1984 if (TREE_CODE (t
) == COMPONENT_REF
)
1985 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1987 else if (TREE_CODE (t
) == ARRAY_REF
1988 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1990 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1992 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1994 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1998 wi
->val_only
= false;
1999 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
2000 wi
->val_only
= save_val_only
;
2004 save_val_only
= wi
->val_only
;
2005 wi
->val_only
= true;
2007 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
2009 /* We need to re-fold the MEM_REF as component references as
2010 part of a ADDR_EXPR address are not allowed. But we cannot
2011 fold here, as the chain record type is not yet finalized. */
2012 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
2013 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
2014 info
->mem_refs
->add (tp
);
2015 wi
->val_only
= save_val_only
;
2018 case VIEW_CONVERT_EXPR
:
2019 /* Just request to look at the subtrees, leaving val_only and lhs
2020 untouched. This might actually be for !val_only + lhs, in which
2021 case we don't want to force a replacement by a temporary. */
2026 if (!IS_TYPE_OR_DECL_P (t
))
2029 wi
->val_only
= true;
2038 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
2039 struct walk_stmt_info
*);
2041 /* Helper for convert_local_reference. Convert all the references in
2042 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
2045 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
2047 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2048 bool need_frame
= false, need_stmts
= false;
2049 tree clause
, decl
, *pdecl
;
2051 bitmap new_suppress
;
2053 new_suppress
= BITMAP_GGC_ALLOC ();
2054 bitmap_copy (new_suppress
, info
->suppress_expansion
);
2056 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
2059 switch (OMP_CLAUSE_CODE (clause
))
2061 case OMP_CLAUSE_REDUCTION
:
2062 case OMP_CLAUSE_IN_REDUCTION
:
2063 case OMP_CLAUSE_TASK_REDUCTION
:
2064 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2066 if (TREE_CODE (OMP_CLAUSE_DECL (clause
)) == MEM_REF
)
2068 pdecl
= &TREE_OPERAND (OMP_CLAUSE_DECL (clause
), 0);
2069 if (TREE_CODE (*pdecl
) == POINTER_PLUS_EXPR
)
2070 pdecl
= &TREE_OPERAND (*pdecl
, 0);
2071 if (TREE_CODE (*pdecl
) == INDIRECT_REF
2072 || TREE_CODE (*pdecl
) == ADDR_EXPR
)
2073 pdecl
= &TREE_OPERAND (*pdecl
, 0);
2075 goto do_decl_clause
;
2077 case OMP_CLAUSE_LASTPRIVATE
:
2078 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
2080 goto do_decl_clause
;
2082 case OMP_CLAUSE_LINEAR
:
2083 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
2085 wi
->val_only
= true;
2087 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
), &dummy
,
2089 goto do_decl_clause
;
2091 case OMP_CLAUSE_PRIVATE
:
2092 case OMP_CLAUSE_FIRSTPRIVATE
:
2093 case OMP_CLAUSE_COPYPRIVATE
:
2094 case OMP_CLAUSE_SHARED
:
2095 case OMP_CLAUSE_TO_DECLARE
:
2096 case OMP_CLAUSE_LINK
:
2097 case OMP_CLAUSE_USE_DEVICE_PTR
:
2098 case OMP_CLAUSE_USE_DEVICE_ADDR
:
2099 case OMP_CLAUSE_IS_DEVICE_PTR
:
2102 pdecl
= &OMP_CLAUSE_DECL (clause
);
2105 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2107 if (decl_function_context (decl
) == info
->context
2108 && !use_pointer_in_frame (decl
))
2110 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2113 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
2114 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
2115 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
2116 *pdecl
= get_local_debug_decl (info
, decl
, field
);
2122 case OMP_CLAUSE_SCHEDULE
:
2123 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
2126 case OMP_CLAUSE_FINAL
:
2128 case OMP_CLAUSE_NUM_THREADS
:
2129 case OMP_CLAUSE_DEPEND
:
2130 case OMP_CLAUSE_DEVICE
:
2131 case OMP_CLAUSE_NUM_TEAMS
:
2132 case OMP_CLAUSE_THREAD_LIMIT
:
2133 case OMP_CLAUSE_SAFELEN
:
2134 case OMP_CLAUSE_SIMDLEN
:
2135 case OMP_CLAUSE_PRIORITY
:
2136 case OMP_CLAUSE_GRAINSIZE
:
2137 case OMP_CLAUSE_NUM_TASKS
:
2138 case OMP_CLAUSE_HINT
:
2139 case OMP_CLAUSE_NUM_GANGS
:
2140 case OMP_CLAUSE_NUM_WORKERS
:
2141 case OMP_CLAUSE_VECTOR_LENGTH
:
2142 case OMP_CLAUSE_GANG
:
2143 case OMP_CLAUSE_WORKER
:
2144 case OMP_CLAUSE_VECTOR
:
2145 case OMP_CLAUSE_ASYNC
:
2146 case OMP_CLAUSE_WAIT
:
2147 /* Several OpenACC clauses have optional arguments. Check if they
2149 if (OMP_CLAUSE_OPERAND (clause
, 0))
2151 wi
->val_only
= true;
2153 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
2157 /* The gang clause accepts two arguments. */
2158 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
2159 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
2161 wi
->val_only
= true;
2163 convert_nonlocal_reference_op
2164 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
2168 case OMP_CLAUSE_DIST_SCHEDULE
:
2169 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
2171 wi
->val_only
= true;
2173 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
2178 case OMP_CLAUSE_MAP
:
2180 case OMP_CLAUSE_FROM
:
2181 if (OMP_CLAUSE_SIZE (clause
))
2183 wi
->val_only
= true;
2185 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause
),
2188 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
2189 goto do_decl_clause
;
2190 wi
->val_only
= true;
2192 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_local_reference_op
,
2196 case OMP_CLAUSE_ALIGNED
:
2197 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
2199 wi
->val_only
= true;
2201 convert_local_reference_op
2202 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
2205 case OMP_CLAUSE_NONTEMPORAL
:
2206 /* Like do_decl_clause, but don't add any suppression. */
2207 decl
= OMP_CLAUSE_DECL (clause
);
2209 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2211 if (decl_function_context (decl
) == info
->context
2212 && !use_pointer_in_frame (decl
))
2214 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2217 OMP_CLAUSE_DECL (clause
)
2218 = get_local_debug_decl (info
, decl
, field
);
2224 case OMP_CLAUSE_NOWAIT
:
2225 case OMP_CLAUSE_ORDERED
:
2226 case OMP_CLAUSE_DEFAULT
:
2227 case OMP_CLAUSE_COPYIN
:
2228 case OMP_CLAUSE_COLLAPSE
:
2229 case OMP_CLAUSE_TILE
:
2230 case OMP_CLAUSE_UNTIED
:
2231 case OMP_CLAUSE_MERGEABLE
:
2232 case OMP_CLAUSE_PROC_BIND
:
2233 case OMP_CLAUSE_NOGROUP
:
2234 case OMP_CLAUSE_THREADS
:
2235 case OMP_CLAUSE_SIMD
:
2236 case OMP_CLAUSE_DEFAULTMAP
:
2237 case OMP_CLAUSE_ORDER
:
2238 case OMP_CLAUSE_SEQ
:
2239 case OMP_CLAUSE_INDEPENDENT
:
2240 case OMP_CLAUSE_AUTO
:
2241 case OMP_CLAUSE_IF_PRESENT
:
2242 case OMP_CLAUSE_FINALIZE
:
2243 case OMP_CLAUSE__CONDTEMP_
:
2244 case OMP_CLAUSE__SCANTEMP_
:
2247 /* The following clause belongs to the OpenACC cache directive, which
2248 is discarded during gimplification. */
2249 case OMP_CLAUSE__CACHE_
:
2250 /* The following clauses are only allowed in the OpenMP declare simd
2251 directive, so not seen here. */
2252 case OMP_CLAUSE_UNIFORM
:
2253 case OMP_CLAUSE_INBRANCH
:
2254 case OMP_CLAUSE_NOTINBRANCH
:
2255 /* The following clauses are only allowed on OpenMP cancel and
2256 cancellation point directives, which at this point have already
2257 been lowered into a function call. */
2258 case OMP_CLAUSE_FOR
:
2259 case OMP_CLAUSE_PARALLEL
:
2260 case OMP_CLAUSE_SECTIONS
:
2261 case OMP_CLAUSE_TASKGROUP
:
2262 /* The following clauses are only added during OMP lowering; nested
2263 function decomposition happens before that. */
2264 case OMP_CLAUSE__LOOPTEMP_
:
2265 case OMP_CLAUSE__REDUCTEMP_
:
2266 case OMP_CLAUSE__SIMDUID_
:
2267 case OMP_CLAUSE__SIMT_
:
2268 /* Anything else. */
2274 info
->suppress_expansion
= new_suppress
;
2277 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
2278 switch (OMP_CLAUSE_CODE (clause
))
2280 case OMP_CLAUSE_REDUCTION
:
2281 case OMP_CLAUSE_IN_REDUCTION
:
2282 case OMP_CLAUSE_TASK_REDUCTION
:
2283 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2286 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
2287 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2289 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2290 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2292 walk_body (convert_local_reference_stmt
,
2293 convert_local_reference_op
, info
,
2294 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
2295 walk_body (convert_local_reference_stmt
,
2296 convert_local_reference_op
, info
,
2297 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
2298 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2300 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2301 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2306 case OMP_CLAUSE_LASTPRIVATE
:
2307 walk_body (convert_local_reference_stmt
,
2308 convert_local_reference_op
, info
,
2309 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
2312 case OMP_CLAUSE_LINEAR
:
2313 walk_body (convert_local_reference_stmt
,
2314 convert_local_reference_op
, info
,
2315 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
2326 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2327 and PARM_DECLs that were referenced by inner nested functions.
2328 The rewrite will be a structure reference to the local frame variable. */
2331 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2332 struct walk_stmt_info
*wi
)
2334 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2335 tree save_local_var_chain
;
2336 bitmap save_suppress
;
2337 char save_static_chain_added
;
2338 bool frame_decl_added
;
2339 gimple
*stmt
= gsi_stmt (*gsi
);
2341 switch (gimple_code (stmt
))
2343 case GIMPLE_OMP_TEAMS
:
2344 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
2346 save_suppress
= info
->suppress_expansion
;
2347 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
2348 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2349 info
, gimple_omp_body_ptr (stmt
));
2350 info
->suppress_expansion
= save_suppress
;
2355 case GIMPLE_OMP_PARALLEL
:
2356 case GIMPLE_OMP_TASK
:
2357 save_suppress
= info
->suppress_expansion
;
2358 frame_decl_added
= false;
2359 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
2362 tree c
= build_omp_clause (gimple_location (stmt
),
2364 (void) get_frame_type (info
);
2365 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2366 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2367 gimple_omp_taskreg_set_clauses (stmt
, c
);
2368 info
->static_chain_added
|= 4;
2369 frame_decl_added
= true;
2372 save_local_var_chain
= info
->new_local_var_chain
;
2373 save_static_chain_added
= info
->static_chain_added
;
2374 info
->new_local_var_chain
= NULL
;
2375 info
->static_chain_added
= 0;
2377 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2378 gimple_omp_body_ptr (stmt
));
2380 if ((info
->static_chain_added
& 4) != 0 && !frame_decl_added
)
2382 tree c
= build_omp_clause (gimple_location (stmt
),
2384 (void) get_frame_type (info
);
2385 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2386 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2387 info
->static_chain_added
|= 4;
2388 gimple_omp_taskreg_set_clauses (stmt
, c
);
2390 if (info
->new_local_var_chain
)
2391 declare_vars (info
->new_local_var_chain
,
2392 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2393 info
->new_local_var_chain
= save_local_var_chain
;
2394 info
->suppress_expansion
= save_suppress
;
2395 info
->static_chain_added
|= save_static_chain_added
;
2398 case GIMPLE_OMP_FOR
:
2399 save_suppress
= info
->suppress_expansion
;
2400 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
2401 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
2402 convert_local_reference_stmt
,
2403 convert_local_reference_op
, info
);
2404 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2405 info
, gimple_omp_body_ptr (stmt
));
2406 info
->suppress_expansion
= save_suppress
;
2409 case GIMPLE_OMP_SECTIONS
:
2410 save_suppress
= info
->suppress_expansion
;
2411 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
2412 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2413 info
, gimple_omp_body_ptr (stmt
));
2414 info
->suppress_expansion
= save_suppress
;
2417 case GIMPLE_OMP_SINGLE
:
2418 save_suppress
= info
->suppress_expansion
;
2419 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
2420 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2421 info
, gimple_omp_body_ptr (stmt
));
2422 info
->suppress_expansion
= save_suppress
;
2425 case GIMPLE_OMP_TASKGROUP
:
2426 save_suppress
= info
->suppress_expansion
;
2427 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt
), wi
);
2428 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2429 info
, gimple_omp_body_ptr (stmt
));
2430 info
->suppress_expansion
= save_suppress
;
2433 case GIMPLE_OMP_TARGET
:
2434 if (!is_gimple_omp_offloaded (stmt
))
2436 save_suppress
= info
->suppress_expansion
;
2437 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
2438 info
->suppress_expansion
= save_suppress
;
2439 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2440 info
, gimple_omp_body_ptr (stmt
));
2443 save_suppress
= info
->suppress_expansion
;
2444 frame_decl_added
= false;
2445 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
))
2447 tree c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2448 (void) get_frame_type (info
);
2449 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2450 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2451 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2452 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2453 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2454 info
->static_chain_added
|= 4;
2455 frame_decl_added
= true;
2458 save_local_var_chain
= info
->new_local_var_chain
;
2459 save_static_chain_added
= info
->static_chain_added
;
2460 info
->new_local_var_chain
= NULL
;
2461 info
->static_chain_added
= 0;
2463 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2464 gimple_omp_body_ptr (stmt
));
2466 if ((info
->static_chain_added
& 4) != 0 && !frame_decl_added
)
2468 tree c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2469 (void) get_frame_type (info
);
2470 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2471 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2472 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2473 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2474 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2475 info
->static_chain_added
|= 4;
2478 if (info
->new_local_var_chain
)
2479 declare_vars (info
->new_local_var_chain
,
2480 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2481 info
->new_local_var_chain
= save_local_var_chain
;
2482 info
->suppress_expansion
= save_suppress
;
2483 info
->static_chain_added
|= save_static_chain_added
;
2486 case GIMPLE_OMP_SECTION
:
2487 case GIMPLE_OMP_MASTER
:
2488 case GIMPLE_OMP_ORDERED
:
2489 case GIMPLE_OMP_SCAN
:
2490 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2491 info
, gimple_omp_body_ptr (stmt
));
2495 wi
->val_only
= true;
2497 *handled_ops_p
= false;
2501 if (gimple_clobber_p (stmt
))
2503 tree lhs
= gimple_assign_lhs (stmt
);
2505 && !use_pointer_in_frame (lhs
)
2506 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
2508 gsi_replace (gsi
, gimple_build_nop (), true);
2512 *handled_ops_p
= false;
2516 for (tree var
= gimple_bind_vars (as_a
<gbind
*> (stmt
));
2518 var
= DECL_CHAIN (var
))
2519 if (TREE_CODE (var
) == NAMELIST_DECL
)
2521 /* Adjust decls mentioned in NAMELIST_DECL. */
2522 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
2526 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
2529 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2531 if (decl_function_context (decl
) == info
->context
2532 && !use_pointer_in_frame (decl
))
2534 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2537 CONSTRUCTOR_ELT (decls
, i
)->value
2538 = get_local_debug_decl (info
, decl
, field
);
2544 *handled_ops_p
= false;
2548 /* For every other statement that we are not interested in
2549 handling here, let the walker traverse the operands. */
2550 *handled_ops_p
= false;
2554 /* Indicate that we have handled all the operands ourselves. */
2555 *handled_ops_p
= true;
2560 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2561 that reference labels from outer functions. The rewrite will be a
2562 call to __builtin_nonlocal_goto. */
2565 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2566 struct walk_stmt_info
*wi
)
2568 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2569 tree label
, new_label
, target_context
, x
, field
;
2571 gimple
*stmt
= gsi_stmt (*gsi
);
2573 if (gimple_code (stmt
) != GIMPLE_GOTO
)
2575 *handled_ops_p
= false;
2579 label
= gimple_goto_dest (stmt
);
2580 if (TREE_CODE (label
) != LABEL_DECL
)
2582 *handled_ops_p
= false;
2586 target_context
= decl_function_context (label
);
2587 if (target_context
== info
->context
)
2589 *handled_ops_p
= false;
2593 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
2596 /* The original user label may also be use for a normal goto, therefore
2597 we must create a new label that will actually receive the abnormal
2598 control transfer. This new label will be marked LABEL_NONLOCAL; this
2599 mark will trigger proper behavior in the cfg, as well as cause the
2600 (hairy target-specific) non-local goto receiver code to be generated
2601 when we expand rtl. Enter this association into var_map so that we
2602 can insert the new label into the IL during a second pass. */
2603 tree
*slot
= &i
->var_map
->get_or_insert (label
);
2606 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
2607 DECL_NONLOCAL (new_label
) = 1;
2613 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2614 field
= get_nl_goto_field (i
);
2615 x
= get_frame_field (info
, target_context
, field
, gsi
);
2617 x
= gsi_gimplify_val (info
, x
, gsi
);
2618 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
2619 2, build_addr (new_label
), x
);
2620 gsi_replace (gsi
, call
, false);
2622 /* We have handled all of STMT's operands, no need to keep going. */
2623 *handled_ops_p
= true;
2628 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2629 are referenced via nonlocal goto from a nested function. The rewrite
2630 will involve installing a newly generated DECL_NONLOCAL label, and
2631 (potentially) a branch around the rtl gunk that is assumed to be
2632 attached to such a label. */
2635 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2636 struct walk_stmt_info
*wi
)
2638 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2639 tree label
, new_label
;
2640 gimple_stmt_iterator tmp_gsi
;
2641 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (*gsi
));
2645 *handled_ops_p
= false;
2649 label
= gimple_label_label (stmt
);
2651 tree
*slot
= info
->var_map
->get (label
);
2654 *handled_ops_p
= false;
2658 /* If there's any possibility that the previous statement falls through,
2659 then we must branch around the new non-local label. */
2661 gsi_prev (&tmp_gsi
);
2662 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
2664 gimple
*stmt
= gimple_build_goto (label
);
2665 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2668 new_label
= (tree
) *slot
;
2669 stmt
= gimple_build_label (new_label
);
2670 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2672 *handled_ops_p
= true;
2677 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2678 of nested functions that require the use of trampolines. The rewrite
2679 will involve a reference a trampoline generated for the occasion. */
2682 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
2684 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2685 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2686 tree t
= *tp
, decl
, target_context
, x
, builtin
;
2691 switch (TREE_CODE (t
))
2695 T.1 = &CHAIN->tramp;
2696 T.2 = __builtin_adjust_trampoline (T.1);
2697 T.3 = (func_type)T.2;
2700 decl
= TREE_OPERAND (t
, 0);
2701 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2704 /* Only need to process nested functions. */
2705 target_context
= decl_function_context (decl
);
2706 if (!target_context
)
2709 /* If the nested function doesn't use a static chain, then
2710 it doesn't need a trampoline. */
2711 if (!DECL_STATIC_CHAIN (decl
))
2714 /* If we don't want a trampoline, then don't build one. */
2715 if (TREE_NO_TRAMPOLINE (t
))
2718 /* Lookup the immediate parent of the callee, as that's where
2719 we need to insert the trampoline. */
2720 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
2723 /* Decide whether to generate a descriptor or a trampoline. */
2724 descr
= FUNC_ADDR_BY_DESCRIPTOR (t
) && !flag_trampolines
;
2727 x
= lookup_descr_for_decl (i
, decl
, INSERT
);
2729 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
2731 /* Compute the address of the field holding the trampoline. */
2732 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
2734 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
2736 /* Do machine-specific ugliness. Normally this will involve
2737 computing extra alignment, but it can really be anything. */
2739 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR
);
2741 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
2742 call
= gimple_build_call (builtin
, 1, x
);
2743 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
2745 /* Cast back to the proper function type. */
2746 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
2747 x
= init_tmp_var (info
, x
, &wi
->gsi
);
2753 if (!IS_TYPE_OR_DECL_P (t
))
2762 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2763 to addresses of nested functions that require the use of
2764 trampolines. The rewrite will involve a reference a trampoline
2765 generated for the occasion. */
2768 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2769 struct walk_stmt_info
*wi
)
2771 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2772 gimple
*stmt
= gsi_stmt (*gsi
);
2774 switch (gimple_code (stmt
))
2778 /* Only walk call arguments, lest we generate trampolines for
2780 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2781 for (i
= 0; i
< nargs
; i
++)
2782 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2787 case GIMPLE_OMP_TEAMS
:
2788 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
2790 *handled_ops_p
= false;
2795 case GIMPLE_OMP_TARGET
:
2796 if (!is_gimple_omp_offloaded (stmt
))
2798 *handled_ops_p
= false;
2802 case GIMPLE_OMP_PARALLEL
:
2803 case GIMPLE_OMP_TASK
:
2806 tree save_local_var_chain
= info
->new_local_var_chain
;
2807 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2808 info
->new_local_var_chain
= NULL
;
2809 char save_static_chain_added
= info
->static_chain_added
;
2810 info
->static_chain_added
= 0;
2811 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2812 info
, gimple_omp_body_ptr (stmt
));
2813 if (info
->new_local_var_chain
)
2814 declare_vars (info
->new_local_var_chain
,
2815 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2817 for (int i
= 0; i
< 2; i
++)
2820 if ((info
->static_chain_added
& (1 << i
)) == 0)
2822 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2823 /* Don't add CHAIN.* or FRAME.* twice. */
2824 for (c
= gimple_omp_taskreg_clauses (stmt
);
2826 c
= OMP_CLAUSE_CHAIN (c
))
2827 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2828 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2829 && OMP_CLAUSE_DECL (c
) == decl
)
2831 if (c
== NULL
&& gimple_code (stmt
) != GIMPLE_OMP_TARGET
)
2833 c
= build_omp_clause (gimple_location (stmt
),
2834 i
? OMP_CLAUSE_FIRSTPRIVATE
2835 : OMP_CLAUSE_SHARED
);
2836 OMP_CLAUSE_DECL (c
) = decl
;
2837 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2838 gimple_omp_taskreg_set_clauses (stmt
, c
);
2842 c
= build_omp_clause (gimple_location (stmt
),
2844 OMP_CLAUSE_DECL (c
) = decl
;
2845 OMP_CLAUSE_SET_MAP_KIND (c
,
2846 i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2847 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2848 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2849 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2853 info
->new_local_var_chain
= save_local_var_chain
;
2854 info
->static_chain_added
|= save_static_chain_added
;
2859 *handled_ops_p
= false;
2863 *handled_ops_p
= true;
2869 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2870 that reference nested functions to make sure that the static chain
2871 is set up properly for the call. */
2874 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2875 struct walk_stmt_info
*wi
)
2877 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2878 tree decl
, target_context
;
2879 char save_static_chain_added
;
2881 gimple
*stmt
= gsi_stmt (*gsi
);
2883 switch (gimple_code (stmt
))
2886 if (gimple_call_chain (stmt
))
2888 decl
= gimple_call_fndecl (stmt
);
2891 target_context
= decl_function_context (decl
);
2892 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2894 struct nesting_info
*i
= info
;
2895 while (i
&& i
->context
!= target_context
)
2897 /* If none of the outer contexts is the target context, this means
2898 that the function is called in a wrong context. */
2900 internal_error ("%s from %s called in %s",
2901 IDENTIFIER_POINTER (DECL_NAME (decl
)),
2902 IDENTIFIER_POINTER (DECL_NAME (target_context
)),
2903 IDENTIFIER_POINTER (DECL_NAME (info
->context
)));
2905 gimple_call_set_chain (as_a
<gcall
*> (stmt
),
2906 get_static_chain (info
, target_context
,
2908 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2912 case GIMPLE_OMP_TEAMS
:
2913 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
2915 walk_body (convert_gimple_call
, NULL
, info
,
2916 gimple_omp_body_ptr (stmt
));
2921 case GIMPLE_OMP_PARALLEL
:
2922 case GIMPLE_OMP_TASK
:
2923 save_static_chain_added
= info
->static_chain_added
;
2924 info
->static_chain_added
= 0;
2925 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2926 for (i
= 0; i
< 2; i
++)
2929 if ((info
->static_chain_added
& (1 << i
)) == 0)
2931 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2932 /* Don't add CHAIN.* or FRAME.* twice. */
2933 for (c
= gimple_omp_taskreg_clauses (stmt
);
2935 c
= OMP_CLAUSE_CHAIN (c
))
2936 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2937 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2938 && OMP_CLAUSE_DECL (c
) == decl
)
2942 c
= build_omp_clause (gimple_location (stmt
),
2943 i
? OMP_CLAUSE_FIRSTPRIVATE
2944 : OMP_CLAUSE_SHARED
);
2945 OMP_CLAUSE_DECL (c
) = decl
;
2946 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2947 gimple_omp_taskreg_set_clauses (stmt
, c
);
2950 info
->static_chain_added
|= save_static_chain_added
;
2953 case GIMPLE_OMP_TARGET
:
2954 if (!is_gimple_omp_offloaded (stmt
))
2956 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2959 save_static_chain_added
= info
->static_chain_added
;
2960 info
->static_chain_added
= 0;
2961 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2962 for (i
= 0; i
< 2; i
++)
2965 if ((info
->static_chain_added
& (1 << i
)) == 0)
2967 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2968 /* Don't add CHAIN.* or FRAME.* twice. */
2969 for (c
= gimple_omp_target_clauses (stmt
);
2971 c
= OMP_CLAUSE_CHAIN (c
))
2972 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
2973 && OMP_CLAUSE_DECL (c
) == decl
)
2977 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2978 OMP_CLAUSE_DECL (c
) = decl
;
2979 OMP_CLAUSE_SET_MAP_KIND (c
, i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2980 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2981 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2982 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2986 info
->static_chain_added
|= save_static_chain_added
;
2989 case GIMPLE_OMP_FOR
:
2990 walk_body (convert_gimple_call
, NULL
, info
,
2991 gimple_omp_for_pre_body_ptr (stmt
));
2993 case GIMPLE_OMP_SECTIONS
:
2994 case GIMPLE_OMP_SECTION
:
2995 case GIMPLE_OMP_SINGLE
:
2996 case GIMPLE_OMP_MASTER
:
2997 case GIMPLE_OMP_TASKGROUP
:
2998 case GIMPLE_OMP_ORDERED
:
2999 case GIMPLE_OMP_SCAN
:
3000 case GIMPLE_OMP_CRITICAL
:
3001 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
3005 /* Keep looking for other operands. */
3006 *handled_ops_p
= false;
3010 *handled_ops_p
= true;
3014 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
3015 call expressions. At the same time, determine if a nested function
3016 actually uses its static chain; if not, remember that. */
3019 convert_all_function_calls (struct nesting_info
*root
)
3021 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
3022 struct nesting_info
*n
;
3024 /* First, optimistically clear static_chain for all decls that haven't
3025 used the static chain already for variable access. But always create
3026 it if not optimizing. This makes it possible to reconstruct the static
3027 nesting tree at run time and thus to resolve up-level references from
3028 within the debugger. */
3029 FOR_EACH_NEST_INFO (n
, root
)
3033 tree decl
= n
->context
;
3037 (void) get_frame_type (n
);
3039 (void) get_chain_decl (n
);
3041 else if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
3043 DECL_STATIC_CHAIN (decl
) = 0;
3044 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3045 fprintf (dump_file
, "Guessing no static-chain for %s\n",
3046 lang_hooks
.decl_printable_name (decl
, 2));
3049 DECL_STATIC_CHAIN (decl
) = 1;
3050 chain_count
+= DECL_STATIC_CHAIN (decl
);
3053 FOR_EACH_NEST_INFO (n
, root
)
3056 tree decl
= n
->context
;
3057 tree alias
= thunk_info::get (cgraph_node::get (decl
))->alias
;
3058 DECL_STATIC_CHAIN (decl
) = DECL_STATIC_CHAIN (alias
);
3061 /* Walk the functions and perform transformations. Note that these
3062 transformations can induce new uses of the static chain, which in turn
3063 require re-examining all users of the decl. */
3064 /* ??? It would make sense to try to use the call graph to speed this up,
3065 but the call graph hasn't really been built yet. Even if it did, we
3066 would still need to iterate in this loop since address-of references
3067 wouldn't show up in the callgraph anyway. */
3071 old_chain_count
= chain_count
;
3075 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3076 fputc ('\n', dump_file
);
3078 FOR_EACH_NEST_INFO (n
, root
)
3082 tree decl
= n
->context
;
3083 walk_function (convert_tramp_reference_stmt
,
3084 convert_tramp_reference_op
, n
);
3085 walk_function (convert_gimple_call
, NULL
, n
);
3086 chain_count
+= DECL_STATIC_CHAIN (decl
);
3089 FOR_EACH_NEST_INFO (n
, root
)
3092 tree decl
= n
->context
;
3093 tree alias
= thunk_info::get (cgraph_node::get (decl
))->alias
;
3094 DECL_STATIC_CHAIN (decl
) = DECL_STATIC_CHAIN (alias
);
3097 while (chain_count
!= old_chain_count
);
3099 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3100 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
3104 struct nesting_copy_body_data
3107 struct nesting_info
*root
;
3110 /* A helper subroutine for debug_var_chain type remapping. */
3113 nesting_copy_decl (tree decl
, copy_body_data
*id
)
3115 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
3116 tree
*slot
= nid
->root
->var_map
->get (decl
);
3119 return (tree
) *slot
;
3121 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
3123 tree new_decl
= copy_decl_no_change (decl
, id
);
3124 DECL_ORIGINAL_TYPE (new_decl
)
3125 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
3130 || TREE_CODE (decl
) == PARM_DECL
3131 || TREE_CODE (decl
) == RESULT_DECL
)
3134 return copy_decl_no_change (decl
, id
);
3137 /* A helper function for remap_vla_decls. See if *TP contains
3138 some remapped variables. */
3141 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
3143 struct nesting_info
*root
= (struct nesting_info
*) data
;
3149 tree
*slot
= root
->var_map
->get (t
);
3157 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
3161 remap_vla_decls (tree block
, struct nesting_info
*root
)
3163 tree var
, subblock
, val
, type
;
3164 struct nesting_copy_body_data id
;
3166 for (subblock
= BLOCK_SUBBLOCKS (block
);
3168 subblock
= BLOCK_CHAIN (subblock
))
3169 remap_vla_decls (subblock
, root
);
3171 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
3172 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
3174 val
= DECL_VALUE_EXPR (var
);
3175 type
= TREE_TYPE (var
);
3177 if (!(TREE_CODE (val
) == INDIRECT_REF
3178 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
3179 && variably_modified_type_p (type
, NULL
)))
3182 if (root
->var_map
->get (TREE_OPERAND (val
, 0))
3183 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
3187 if (var
== NULL_TREE
)
3190 memset (&id
, 0, sizeof (id
));
3191 id
.cb
.copy_decl
= nesting_copy_decl
;
3192 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
3195 for (; var
; var
= DECL_CHAIN (var
))
3196 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
3198 struct nesting_info
*i
;
3201 val
= DECL_VALUE_EXPR (var
);
3202 type
= TREE_TYPE (var
);
3204 if (!(TREE_CODE (val
) == INDIRECT_REF
3205 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
3206 && variably_modified_type_p (type
, NULL
)))
3209 tree
*slot
= root
->var_map
->get (TREE_OPERAND (val
, 0));
3210 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
3213 context
= decl_function_context (var
);
3214 for (i
= root
; i
; i
= i
->outer
)
3215 if (i
->context
== context
)
3221 /* Fully expand value expressions. This avoids having debug variables
3222 only referenced from them and that can be swept during GC. */
3225 tree t
= (tree
) *slot
;
3226 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
3227 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
3230 id
.cb
.src_fn
= i
->context
;
3231 id
.cb
.dst_fn
= i
->context
;
3232 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
3234 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
3235 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
3237 newt
= TREE_TYPE (newt
);
3238 type
= TREE_TYPE (type
);
3240 if (TYPE_NAME (newt
)
3241 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
3242 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
3244 && TYPE_NAME (newt
) == TYPE_NAME (type
))
3245 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
3247 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
3248 if (val
!= DECL_VALUE_EXPR (var
))
3249 SET_DECL_VALUE_EXPR (var
, val
);
3252 delete id
.cb
.decl_map
;
3255 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3259 fixup_vla_decls (tree block
)
3261 for (tree var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
3262 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
3264 tree val
= DECL_VALUE_EXPR (var
);
3266 if (!(TREE_CODE (val
) == INDIRECT_REF
3267 && VAR_P (TREE_OPERAND (val
, 0))
3268 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val
, 0))))
3271 /* Fully expand value expressions. This avoids having debug variables
3272 only referenced from them and that can be swept during GC. */
3273 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
),
3274 DECL_VALUE_EXPR (TREE_OPERAND (val
, 0)));
3275 SET_DECL_VALUE_EXPR (var
, val
);
3278 for (tree sub
= BLOCK_SUBBLOCKS (block
); sub
; sub
= BLOCK_CHAIN (sub
))
3279 fixup_vla_decls (sub
);
3282 /* Fold the MEM_REF *E. */
3284 fold_mem_refs (tree
*const &e
, void *data ATTRIBUTE_UNUSED
)
3286 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
3287 *ref_p
= fold (*ref_p
);
3291 /* Given DECL, a nested function, build an initialization call for FIELD,
3292 the trampoline or descriptor for DECL, using FUNC as the function. */
3295 build_init_call_stmt (struct nesting_info
*info
, tree decl
, tree field
,
3298 tree arg1
, arg2
, arg3
, x
;
3300 gcc_assert (DECL_STATIC_CHAIN (decl
));
3301 arg3
= build_addr (info
->frame_decl
);
3303 arg2
= build_addr (decl
);
3305 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3306 info
->frame_decl
, field
, NULL_TREE
);
3307 arg1
= build_addr (x
);
3309 return gimple_build_call (func
, 3, arg1
, arg2
, arg3
);
3312 /* Do "everything else" to clean up or complete state collected by the various
3313 walking passes -- create a field to hold the frame base address, lay out the
3314 types and decls, generate code to initialize the frame decl, store critical
3315 expressions in the struct function for rtl to find. */
3318 finalize_nesting_tree_1 (struct nesting_info
*root
)
3320 gimple_seq stmt_list
= NULL
;
3322 tree context
= root
->context
;
3323 struct function
*sf
;
3328 /* If we created a non-local frame type or decl, we need to lay them
3329 out at this time. */
3330 if (root
->frame_type
)
3332 /* Debugging information needs to compute the frame base address of the
3333 parent frame out of the static chain from the nested frame.
3335 The static chain is the address of the FRAME record, so one could
3336 imagine it would be possible to compute the frame base address just
3337 adding a constant offset to this address. Unfortunately, this is not
3338 possible: if the FRAME object has alignment constraints that are
3339 stronger than the stack, then the offset between the frame base and
3340 the FRAME object will be dynamic.
3342 What we do instead is to append a field to the FRAME object that holds
3343 the frame base address: then debug info just has to fetch this
3346 /* Debugging information will refer to the CFA as the frame base
3347 address: we will do the same here. */
3348 const tree frame_addr_fndecl
3349 = builtin_decl_explicit (BUILT_IN_DWARF_CFA
);
3351 /* Create a field in the FRAME record to hold the frame base address for
3352 this stack frame. Since it will be used only by the debugger, put it
3353 at the end of the record in order not to shift all other offsets. */
3354 tree fb_decl
= make_node (FIELD_DECL
);
3356 DECL_NAME (fb_decl
) = get_identifier ("FRAME_BASE.PARENT");
3357 TREE_TYPE (fb_decl
) = ptr_type_node
;
3358 TREE_ADDRESSABLE (fb_decl
) = 1;
3359 DECL_CONTEXT (fb_decl
) = root
->frame_type
;
3360 TYPE_FIELDS (root
->frame_type
) = chainon (TYPE_FIELDS (root
->frame_type
),
3363 /* In some cases the frame type will trigger the -Wpadded warning.
3364 This is not helpful; suppress it. */
3365 int save_warn_padded
= warn_padded
;
3367 layout_type (root
->frame_type
);
3368 warn_padded
= save_warn_padded
;
3369 layout_decl (root
->frame_decl
, 0);
3371 /* Initialize the frame base address field. If the builtin we need is
3372 not available, set it to NULL so that debugging information does not
3374 tree fb_ref
= build3 (COMPONENT_REF
, TREE_TYPE (fb_decl
),
3375 root
->frame_decl
, fb_decl
, NULL_TREE
);
3378 if (frame_addr_fndecl
!= NULL_TREE
)
3380 gcall
*fb_gimple
= gimple_build_call (frame_addr_fndecl
, 1,
3382 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3384 fb_tmp
= init_tmp_var_with_call (root
, &gsi
, fb_gimple
);
3387 fb_tmp
= build_int_cst (TREE_TYPE (fb_ref
), 0);
3388 gimple_seq_add_stmt (&stmt_list
,
3389 gimple_build_assign (fb_ref
, fb_tmp
));
3391 declare_vars (root
->frame_decl
,
3392 gimple_seq_first_stmt (gimple_body (context
)), true);
3395 /* If any parameters were referenced non-locally, then we need to insert
3396 a copy or a pointer. */
3397 if (root
->any_parm_remapped
)
3400 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
3404 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
3408 if (use_pointer_in_frame (p
))
3413 /* If the assignment is from a non-register the stmt is
3414 not valid gimple. Make it so by using a temporary instead. */
3415 if (!is_gimple_reg (x
)
3416 && is_gimple_reg_type (TREE_TYPE (x
)))
3418 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3419 x
= init_tmp_var (root
, x
, &gsi
);
3422 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3423 root
->frame_decl
, field
, NULL_TREE
);
3424 stmt
= gimple_build_assign (y
, x
);
3425 gimple_seq_add_stmt (&stmt_list
, stmt
);
3429 /* If a chain_field was created, then it needs to be initialized
3431 if (root
->chain_field
)
3433 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
3434 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
3435 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
3436 gimple_seq_add_stmt (&stmt_list
, stmt
);
3439 /* If trampolines were created, then we need to initialize them. */
3440 if (root
->any_tramp_created
)
3442 struct nesting_info
*i
;
3443 for (i
= root
->inner
; i
; i
= i
->next
)
3447 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
3451 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
3452 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3453 gimple_seq_add_stmt (&stmt_list
, stmt
);
3457 /* If descriptors were created, then we need to initialize them. */
3458 if (root
->any_descr_created
)
3460 struct nesting_info
*i
;
3461 for (i
= root
->inner
; i
; i
= i
->next
)
3465 field
= lookup_descr_for_decl (root
, i
->context
, NO_INSERT
);
3469 x
= builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR
);
3470 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3471 gimple_seq_add_stmt (&stmt_list
, stmt
);
3475 /* If we created initialization statements, insert them. */
3479 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
3480 bind
= gimple_seq_first_stmt_as_a_bind (gimple_body (context
));
3481 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
3482 gimple_bind_set_body (bind
, stmt_list
);
3485 /* If a chain_decl was created, then it needs to be registered with
3486 struct function so that it gets initialized from the static chain
3487 register at the beginning of the function. */
3488 sf
= DECL_STRUCT_FUNCTION (root
->context
);
3489 sf
->static_chain_decl
= root
->chain_decl
;
3491 /* Similarly for the non-local goto save area. */
3492 if (root
->nl_goto_field
)
3494 sf
->nonlocal_goto_save_area
3495 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
3496 sf
->has_nonlocal_label
= 1;
3499 /* Make sure all new local variables get inserted into the
3500 proper BIND_EXPR. */
3501 if (root
->new_local_var_chain
)
3502 declare_vars (root
->new_local_var_chain
,
3503 gimple_seq_first_stmt (gimple_body (root
->context
)),
3506 if (root
->debug_var_chain
)
3511 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
3513 for (debug_var
= root
->debug_var_chain
; debug_var
;
3514 debug_var
= DECL_CHAIN (debug_var
))
3515 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3518 /* If there are any debug decls with variable length types,
3519 remap those types using other debug_var_chain variables. */
3522 struct nesting_copy_body_data id
;
3524 memset (&id
, 0, sizeof (id
));
3525 id
.cb
.copy_decl
= nesting_copy_decl
;
3526 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
3529 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
3530 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3532 tree type
= TREE_TYPE (debug_var
);
3533 tree newt
, t
= type
;
3534 struct nesting_info
*i
;
3536 for (i
= root
; i
; i
= i
->outer
)
3537 if (variably_modified_type_p (type
, i
->context
))
3543 id
.cb
.src_fn
= i
->context
;
3544 id
.cb
.dst_fn
= i
->context
;
3545 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
3547 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
3548 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
3550 newt
= TREE_TYPE (newt
);
3553 if (TYPE_NAME (newt
)
3554 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
3555 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
3557 && TYPE_NAME (newt
) == TYPE_NAME (t
))
3558 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
3561 delete id
.cb
.decl_map
;
3564 scope
= gimple_seq_first_stmt_as_a_bind (gimple_body (root
->context
));
3565 if (gimple_bind_block (scope
))
3566 declare_vars (root
->debug_var_chain
, scope
, true);
3568 BLOCK_VARS (DECL_INITIAL (root
->context
))
3569 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
3570 root
->debug_var_chain
);
3573 fixup_vla_decls (DECL_INITIAL (root
->context
));
3575 /* Fold the rewritten MEM_REF trees. */
3576 root
->mem_refs
->traverse
<void *, fold_mem_refs
> (NULL
);
3578 /* Dump the translated tree function. */
3581 fputs ("\n\n", dump_file
);
3582 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
3587 finalize_nesting_tree (struct nesting_info
*root
)
3589 struct nesting_info
*n
;
3590 FOR_EACH_NEST_INFO (n
, root
)
3591 finalize_nesting_tree_1 (n
);
3594 /* Unnest the nodes and pass them to cgraph. */
3597 unnest_nesting_tree_1 (struct nesting_info
*root
)
3599 struct cgraph_node
*node
= cgraph_node::get (root
->context
);
3601 /* For nested functions update the cgraph to reflect unnesting.
3602 We also delay finalizing of these functions up to this point. */
3603 if (nested_function_info::get (node
)->origin
)
3605 unnest_function (node
);
3607 cgraph_node::finalize_function (root
->context
, true);
3612 unnest_nesting_tree (struct nesting_info
*root
)
3614 struct nesting_info
*n
;
3615 FOR_EACH_NEST_INFO (n
, root
)
3616 unnest_nesting_tree_1 (n
);
3619 /* Free the data structures allocated during this pass. */
3622 free_nesting_tree (struct nesting_info
*root
)
3624 struct nesting_info
*node
, *next
;
3626 node
= iter_nestinfo_start (root
);
3629 next
= iter_nestinfo_next (node
);
3630 delete node
->var_map
;
3631 delete node
->field_map
;
3632 delete node
->mem_refs
;
3639 /* Gimplify a function and all its nested functions. */
3641 gimplify_all_functions (struct cgraph_node
*root
)
3643 struct cgraph_node
*iter
;
3644 if (!gimple_body (root
->decl
))
3645 gimplify_function_tree (root
->decl
);
3646 for (iter
= first_nested_function (root
); iter
;
3647 iter
= next_nested_function (iter
))
3649 gimplify_all_functions (iter
);
3652 /* Main entry point for this pass. Process FNDECL and all of its nested
3653 subroutines and turn them into something less tightly bound. */
3656 lower_nested_functions (tree fndecl
)
3658 struct cgraph_node
*cgn
;
3659 struct nesting_info
*root
;
3661 /* If there are no nested functions, there's nothing to do. */
3662 cgn
= cgraph_node::get (fndecl
);
3663 if (!first_nested_function (cgn
))
3666 gimplify_all_functions (cgn
);
3668 set_dump_file (dump_begin (TDI_nested
, &dump_flags
));
3670 fprintf (dump_file
, "\n;; Function %s\n\n",
3671 lang_hooks
.decl_printable_name (fndecl
, 2));
3673 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
3674 root
= create_nesting_tree (cgn
);
3676 walk_all_functions (convert_nonlocal_reference_stmt
,
3677 convert_nonlocal_reference_op
,
3679 walk_all_functions (convert_local_reference_stmt
,
3680 convert_local_reference_op
,
3682 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
3683 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
3685 convert_all_function_calls (root
);
3686 finalize_nesting_tree (root
);
3687 unnest_nesting_tree (root
);
3689 free_nesting_tree (root
);
3690 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
3694 dump_end (TDI_nested
, dump_file
);
3695 set_dump_file (NULL
);
3699 #include "gt-tree-nested.h"