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"
47 /* The object of this pass is to lower the representation of a set of nested
48 functions in order to expose all of the gory details of the various
49 nonlocal references. We want to do this sooner rather than later, in
50 order to give us more freedom in emitting all of the functions in question.
52 Back in olden times, when gcc was young, we developed an insanely
53 complicated scheme whereby variables which were referenced nonlocally
54 were forced to live in the stack of the declaring function, and then
55 the nested functions magically discovered where these variables were
56 placed. In order for this scheme to function properly, it required
57 that the outer function be partially expanded, then we switch to
58 compiling the inner function, and once done with those we switch back
59 to compiling the outer function. Such delicate ordering requirements
60 makes it difficult to do whole translation unit optimizations
61 involving such functions.
63 The implementation here is much more direct. Everything that can be
64 referenced by an inner function is a member of an explicitly created
65 structure herein called the "nonlocal frame struct". The incoming
66 static chain for a nested function is a pointer to this struct in
67 the parent. In this way, we settle on known offsets from a known
68 base, and so are decoupled from the logic that places objects in the
69 function's stack frame. More importantly, we don't have to wait for
70 that to happen -- since the compilation of the inner function is no
71 longer tied to a real stack frame, the nonlocal frame struct can be
72 allocated anywhere. Which means that the outer function is now
75 Theory of operation here is very simple. Iterate over all the
76 statements in all the functions (depth first) several times,
77 allocating structures and fields on demand. In general we want to
78 examine inner functions first, so that we can avoid making changes
79 to outer functions which are unnecessary.
81 The order of the passes matters a bit, in that later passes will be
82 skipped if it is discovered that the functions don't actually interact
83 at all. That is, they're nested in the lexical sense but could have
84 been written as independent functions without change. */
89 struct nesting_info
*outer
;
90 struct nesting_info
*inner
;
91 struct nesting_info
*next
;
93 hash_map
<tree
, tree
> *field_map
;
94 hash_map
<tree
, tree
> *var_map
;
95 hash_set
<tree
*> *mem_refs
;
96 bitmap suppress_expansion
;
99 tree new_local_var_chain
;
100 tree debug_var_chain
;
108 bool any_parm_remapped
;
109 bool any_tramp_created
;
110 bool any_descr_created
;
111 char static_chain_added
;
115 /* Iterate over the nesting tree, starting with ROOT, depth first. */
117 static inline struct nesting_info
*
118 iter_nestinfo_start (struct nesting_info
*root
)
125 static inline struct nesting_info
*
126 iter_nestinfo_next (struct nesting_info
*node
)
129 return iter_nestinfo_start (node
->next
);
133 #define FOR_EACH_NEST_INFO(I, ROOT) \
134 for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
136 /* Obstack used for the bitmaps in the struct above. */
137 static struct bitmap_obstack nesting_info_bitmap_obstack
;
140 /* We're working in so many different function contexts simultaneously,
141 that create_tmp_var is dangerous. Prevent mishap. */
142 #define create_tmp_var cant_use_create_tmp_var_here_dummy
144 /* Like create_tmp_var, except record the variable for registration at
145 the given nesting level. */
148 create_tmp_var_for (struct nesting_info
*info
, tree type
, const char *prefix
)
152 /* If the type is of variable size or a type which must be created by the
153 frontend, something is wrong. Note that we explicitly allow
154 incomplete types here, since we create them ourselves here. */
155 gcc_assert (!TREE_ADDRESSABLE (type
));
156 gcc_assert (!TYPE_SIZE_UNIT (type
)
157 || TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
);
159 tmp_var
= create_tmp_var_raw (type
, prefix
);
160 DECL_CONTEXT (tmp_var
) = info
->context
;
161 DECL_CHAIN (tmp_var
) = info
->new_local_var_chain
;
162 DECL_SEEN_IN_BIND_EXPR_P (tmp_var
) = 1;
164 info
->new_local_var_chain
= tmp_var
;
169 /* Like build_simple_mem_ref, but set TREE_THIS_NOTRAP on the result. */
172 build_simple_mem_ref_notrap (tree ptr
)
174 tree t
= build_simple_mem_ref (ptr
);
175 TREE_THIS_NOTRAP (t
) = 1;
179 /* Take the address of EXP to be used within function CONTEXT.
180 Mark it for addressability as necessary. */
183 build_addr (tree exp
)
185 mark_addressable (exp
);
186 return build_fold_addr_expr (exp
);
189 /* Insert FIELD into TYPE, sorted by alignment requirements. */
192 insert_field_into_struct (tree type
, tree field
)
196 DECL_CONTEXT (field
) = type
;
198 for (p
= &TYPE_FIELDS (type
); *p
; p
= &DECL_CHAIN (*p
))
199 if (DECL_ALIGN (field
) >= DECL_ALIGN (*p
))
202 DECL_CHAIN (field
) = *p
;
205 /* Set correct alignment for frame struct type. */
206 if (TYPE_ALIGN (type
) < DECL_ALIGN (field
))
207 SET_TYPE_ALIGN (type
, DECL_ALIGN (field
));
210 /* Build or return the RECORD_TYPE that describes the frame state that is
211 shared between INFO->CONTEXT and its nested functions. This record will
212 not be complete until finalize_nesting_tree; up until that point we'll
213 be adding fields as necessary.
215 We also build the DECL that represents this frame in the function. */
218 get_frame_type (struct nesting_info
*info
)
220 tree type
= info
->frame_type
;
225 type
= make_node (RECORD_TYPE
);
227 name
= concat ("FRAME.",
228 IDENTIFIER_POINTER (DECL_NAME (info
->context
)),
230 TYPE_NAME (type
) = get_identifier (name
);
233 info
->frame_type
= type
;
235 /* Do not put info->frame_decl on info->new_local_var_chain,
236 so that we can declare it in the lexical blocks, which
237 makes sure virtual regs that end up appearing in its RTL
238 expression get substituted in instantiate_virtual_regs. */
239 info
->frame_decl
= create_tmp_var_raw (type
, "FRAME");
240 DECL_CONTEXT (info
->frame_decl
) = info
->context
;
241 DECL_NONLOCAL_FRAME (info
->frame_decl
) = 1;
242 DECL_SEEN_IN_BIND_EXPR_P (info
->frame_decl
) = 1;
244 /* ??? Always make it addressable for now, since it is meant to
245 be pointed to by the static chain pointer. This pessimizes
246 when it turns out that no static chains are needed because
247 the nested functions referencing non-local variables are not
248 reachable, but the true pessimization is to create the non-
249 local frame structure in the first place. */
250 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
256 /* Return true if DECL should be referenced by pointer in the non-local frame
260 use_pointer_in_frame (tree decl
)
262 if (TREE_CODE (decl
) == PARM_DECL
)
264 /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
265 sized DECLs, and inefficient to copy large aggregates. Don't bother
266 moving anything but scalar parameters. */
267 return AGGREGATE_TYPE_P (TREE_TYPE (decl
));
271 /* Variable-sized DECLs can only come from OMP clauses at this point
272 since the gimplifier has already turned the regular variables into
273 pointers. Do the same as the gimplifier. */
274 return !DECL_SIZE (decl
) || TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
;
278 /* Given DECL, a non-locally accessed variable, find or create a field
279 in the non-local frame structure for the given nesting context. */
282 lookup_field_for_decl (struct nesting_info
*info
, tree decl
,
283 enum insert_option insert
)
285 gcc_checking_assert (decl_function_context (decl
) == info
->context
);
287 if (insert
== NO_INSERT
)
289 tree
*slot
= info
->field_map
->get (decl
);
290 return slot
? *slot
: NULL_TREE
;
293 tree
*slot
= &info
->field_map
->get_or_insert (decl
);
296 tree type
= get_frame_type (info
);
297 tree field
= make_node (FIELD_DECL
);
298 DECL_NAME (field
) = DECL_NAME (decl
);
300 if (use_pointer_in_frame (decl
))
302 TREE_TYPE (field
) = build_pointer_type (TREE_TYPE (decl
));
303 SET_DECL_ALIGN (field
, TYPE_ALIGN (TREE_TYPE (field
)));
304 DECL_NONADDRESSABLE_P (field
) = 1;
308 TREE_TYPE (field
) = TREE_TYPE (decl
);
309 DECL_SOURCE_LOCATION (field
) = DECL_SOURCE_LOCATION (decl
);
310 SET_DECL_ALIGN (field
, DECL_ALIGN (decl
));
311 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (decl
);
312 TREE_ADDRESSABLE (field
) = TREE_ADDRESSABLE (decl
);
313 DECL_NONADDRESSABLE_P (field
) = !TREE_ADDRESSABLE (decl
);
314 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (decl
);
316 /* Declare the transformation and adjust the original DECL. For a
317 variable or for a parameter when not optimizing, we make it point
318 to the field in the frame directly. For a parameter, we don't do
319 it when optimizing because the variable tracking pass will already
321 if (VAR_P (decl
) || !optimize
)
324 = build3 (COMPONENT_REF
, TREE_TYPE (field
), info
->frame_decl
,
327 /* If the next declaration is a PARM_DECL pointing to the DECL,
328 we need to adjust its VALUE_EXPR directly, since chains of
329 VALUE_EXPRs run afoul of garbage collection. This occurs
330 in Ada for Out parameters that aren't copied in. */
331 tree next
= DECL_CHAIN (decl
);
333 && TREE_CODE (next
) == PARM_DECL
334 && DECL_HAS_VALUE_EXPR_P (next
)
335 && DECL_VALUE_EXPR (next
) == decl
)
336 SET_DECL_VALUE_EXPR (next
, x
);
338 SET_DECL_VALUE_EXPR (decl
, x
);
339 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
343 insert_field_into_struct (type
, field
);
346 if (TREE_CODE (decl
) == PARM_DECL
)
347 info
->any_parm_remapped
= true;
353 /* Build or return the variable that holds the static chain within
354 INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
357 get_chain_decl (struct nesting_info
*info
)
359 tree decl
= info
->chain_decl
;
365 type
= get_frame_type (info
->outer
);
366 type
= build_pointer_type (type
);
368 /* Note that this variable is *not* entered into any BIND_EXPR;
369 the construction of this variable is handled specially in
370 expand_function_start and initialize_inlined_parameters.
371 Note also that it's represented as a parameter. This is more
372 close to the truth, since the initial value does come from
374 decl
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
375 PARM_DECL
, create_tmp_var_name ("CHAIN"), type
);
376 DECL_ARTIFICIAL (decl
) = 1;
377 DECL_IGNORED_P (decl
) = 1;
378 TREE_USED (decl
) = 1;
379 DECL_CONTEXT (decl
) = info
->context
;
380 DECL_ARG_TYPE (decl
) = type
;
382 /* Tell tree-inline.c that we never write to this variable, so
383 it can copy-prop the replacement value immediately. */
384 TREE_READONLY (decl
) = 1;
386 info
->chain_decl
= decl
;
389 && (dump_flags
& TDF_DETAILS
)
390 && !DECL_STATIC_CHAIN (info
->context
))
391 fprintf (dump_file
, "Setting static-chain for %s\n",
392 lang_hooks
.decl_printable_name (info
->context
, 2));
394 DECL_STATIC_CHAIN (info
->context
) = 1;
399 /* Build or return the field within the non-local frame state that holds
400 the static chain for INFO->CONTEXT. This is the way to walk back up
401 multiple nesting levels. */
404 get_chain_field (struct nesting_info
*info
)
406 tree field
= info
->chain_field
;
410 tree type
= build_pointer_type (get_frame_type (info
->outer
));
412 field
= make_node (FIELD_DECL
);
413 DECL_NAME (field
) = get_identifier ("__chain");
414 TREE_TYPE (field
) = type
;
415 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
416 DECL_NONADDRESSABLE_P (field
) = 1;
418 insert_field_into_struct (get_frame_type (info
), field
);
420 info
->chain_field
= field
;
423 && (dump_flags
& TDF_DETAILS
)
424 && !DECL_STATIC_CHAIN (info
->context
))
425 fprintf (dump_file
, "Setting static-chain for %s\n",
426 lang_hooks
.decl_printable_name (info
->context
, 2));
428 DECL_STATIC_CHAIN (info
->context
) = 1;
433 /* Initialize a new temporary with the GIMPLE_CALL STMT. */
436 init_tmp_var_with_call (struct nesting_info
*info
, gimple_stmt_iterator
*gsi
,
441 t
= create_tmp_var_for (info
, gimple_call_return_type (call
), NULL
);
442 gimple_call_set_lhs (call
, t
);
443 if (! gsi_end_p (*gsi
))
444 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
445 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
451 /* Copy EXP into a temporary. Allocate the temporary in the context of
452 INFO and insert the initialization statement before GSI. */
455 init_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
460 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
461 stmt
= gimple_build_assign (t
, exp
);
462 if (! gsi_end_p (*gsi
))
463 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
464 gsi_insert_before_without_update (gsi
, stmt
, GSI_SAME_STMT
);
470 /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
473 gsi_gimplify_val (struct nesting_info
*info
, tree exp
,
474 gimple_stmt_iterator
*gsi
)
476 if (is_gimple_val (exp
))
479 return init_tmp_var (info
, exp
, gsi
);
482 /* Similarly, but copy from the temporary and insert the statement
483 after the iterator. */
486 save_tmp_var (struct nesting_info
*info
, tree exp
, gimple_stmt_iterator
*gsi
)
491 t
= create_tmp_var_for (info
, TREE_TYPE (exp
), NULL
);
492 stmt
= gimple_build_assign (exp
, t
);
493 if (! gsi_end_p (*gsi
))
494 gimple_set_location (stmt
, gimple_location (gsi_stmt (*gsi
)));
495 gsi_insert_after_without_update (gsi
, stmt
, GSI_SAME_STMT
);
500 /* Build or return the type used to represent a nested function trampoline. */
502 static GTY(()) tree trampoline_type
;
505 get_trampoline_type (struct nesting_info
*info
)
507 unsigned align
, size
;
511 return trampoline_type
;
513 align
= TRAMPOLINE_ALIGNMENT
;
514 size
= TRAMPOLINE_SIZE
;
516 /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
517 then allocate extra space so that we can do dynamic alignment. */
518 if (align
> STACK_BOUNDARY
)
520 size
+= ((align
/BITS_PER_UNIT
) - 1) & -(STACK_BOUNDARY
/BITS_PER_UNIT
);
521 align
= STACK_BOUNDARY
;
524 t
= build_index_type (size_int (size
- 1));
525 t
= build_array_type (char_type_node
, t
);
526 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
527 FIELD_DECL
, get_identifier ("__data"), t
);
528 SET_DECL_ALIGN (t
, align
);
529 DECL_USER_ALIGN (t
) = 1;
531 trampoline_type
= make_node (RECORD_TYPE
);
532 TYPE_NAME (trampoline_type
) = get_identifier ("__builtin_trampoline");
533 TYPE_FIELDS (trampoline_type
) = t
;
534 layout_type (trampoline_type
);
535 DECL_CONTEXT (t
) = trampoline_type
;
537 return trampoline_type
;
540 /* Build or return the type used to represent a nested function descriptor. */
542 static GTY(()) tree descriptor_type
;
545 get_descriptor_type (struct nesting_info
*info
)
547 /* The base alignment is that of a function. */
548 const unsigned align
= FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY
);
552 return descriptor_type
;
554 t
= build_index_type (integer_one_node
);
555 t
= build_array_type (ptr_type_node
, t
);
556 t
= build_decl (DECL_SOURCE_LOCATION (info
->context
),
557 FIELD_DECL
, get_identifier ("__data"), t
);
558 SET_DECL_ALIGN (t
, MAX (TYPE_ALIGN (ptr_type_node
), align
));
559 DECL_USER_ALIGN (t
) = 1;
561 descriptor_type
= make_node (RECORD_TYPE
);
562 TYPE_NAME (descriptor_type
) = get_identifier ("__builtin_descriptor");
563 TYPE_FIELDS (descriptor_type
) = t
;
564 layout_type (descriptor_type
);
565 DECL_CONTEXT (t
) = descriptor_type
;
567 return descriptor_type
;
570 /* Given DECL, a nested function, find or create an element in the
571 var map for this function. */
574 lookup_element_for_decl (struct nesting_info
*info
, tree decl
,
575 enum insert_option insert
)
577 if (insert
== NO_INSERT
)
579 tree
*slot
= info
->var_map
->get (decl
);
580 return slot
? *slot
: NULL_TREE
;
583 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
585 *slot
= build_tree_list (NULL_TREE
, NULL_TREE
);
590 /* Given DECL, a nested function, create a field in the non-local
591 frame structure for this function. */
594 create_field_for_decl (struct nesting_info
*info
, tree decl
, tree type
)
596 tree field
= make_node (FIELD_DECL
);
597 DECL_NAME (field
) = DECL_NAME (decl
);
598 TREE_TYPE (field
) = type
;
599 TREE_ADDRESSABLE (field
) = 1;
600 insert_field_into_struct (get_frame_type (info
), field
);
604 /* Given DECL, a nested function, find or create a field in the non-local
605 frame structure for a trampoline for this function. */
608 lookup_tramp_for_decl (struct nesting_info
*info
, tree decl
,
609 enum insert_option insert
)
613 elt
= lookup_element_for_decl (info
, decl
, insert
);
617 field
= TREE_PURPOSE (elt
);
619 if (!field
&& insert
== INSERT
)
621 field
= create_field_for_decl (info
, decl
, get_trampoline_type (info
));
622 TREE_PURPOSE (elt
) = field
;
623 info
->any_tramp_created
= true;
629 /* Given DECL, a nested function, find or create a field in the non-local
630 frame structure for a descriptor for this function. */
633 lookup_descr_for_decl (struct nesting_info
*info
, tree decl
,
634 enum insert_option insert
)
638 elt
= lookup_element_for_decl (info
, decl
, insert
);
642 field
= TREE_VALUE (elt
);
644 if (!field
&& insert
== INSERT
)
646 field
= create_field_for_decl (info
, decl
, get_descriptor_type (info
));
647 TREE_VALUE (elt
) = field
;
648 info
->any_descr_created
= true;
654 /* Build or return the field within the non-local frame state that holds
655 the non-local goto "jmp_buf". The buffer itself is maintained by the
656 rtl middle-end as dynamic stack space is allocated. */
659 get_nl_goto_field (struct nesting_info
*info
)
661 tree field
= info
->nl_goto_field
;
667 /* For __builtin_nonlocal_goto, we need N words. The first is the
668 frame pointer, the rest is for the target's stack pointer save
669 area. The number of words is controlled by STACK_SAVEAREA_MODE;
670 not the best interface, but it'll do for now. */
671 if (Pmode
== ptr_mode
)
672 type
= ptr_type_node
;
674 type
= lang_hooks
.types
.type_for_mode (Pmode
, 1);
677 = as_a
<scalar_int_mode
> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
));
678 size
= GET_MODE_SIZE (mode
);
679 size
= size
/ GET_MODE_SIZE (Pmode
);
682 type
= build_array_type
683 (type
, build_index_type (size_int (size
)));
685 field
= make_node (FIELD_DECL
);
686 DECL_NAME (field
) = get_identifier ("__nl_goto_buf");
687 TREE_TYPE (field
) = type
;
688 SET_DECL_ALIGN (field
, TYPE_ALIGN (type
));
689 TREE_ADDRESSABLE (field
) = 1;
691 insert_field_into_struct (get_frame_type (info
), field
);
693 info
->nl_goto_field
= field
;
699 /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
702 walk_body (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
703 struct nesting_info
*info
, gimple_seq
*pseq
)
705 struct walk_stmt_info wi
;
707 memset (&wi
, 0, sizeof (wi
));
710 walk_gimple_seq_mod (pseq
, callback_stmt
, callback_op
, &wi
);
714 /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
717 walk_function (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
718 struct nesting_info
*info
)
720 gimple_seq body
= gimple_body (info
->context
);
721 walk_body (callback_stmt
, callback_op
, info
, &body
);
722 gimple_set_body (info
->context
, body
);
725 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
728 walk_gimple_omp_for (gomp_for
*for_stmt
,
729 walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
730 struct nesting_info
*info
)
732 struct walk_stmt_info wi
;
737 walk_body (callback_stmt
, callback_op
, info
, gimple_omp_for_pre_body_ptr (for_stmt
));
740 memset (&wi
, 0, sizeof (wi
));
742 wi
.gsi
= gsi_last (seq
);
744 for (i
= 0; i
< gimple_omp_for_collapse (for_stmt
); i
++)
747 walk_tree (gimple_omp_for_index_ptr (for_stmt
, i
), callback_op
,
751 walk_tree (gimple_omp_for_initial_ptr (for_stmt
, i
), callback_op
,
756 walk_tree (gimple_omp_for_final_ptr (for_stmt
, i
), callback_op
,
759 t
= gimple_omp_for_incr (for_stmt
, i
);
760 gcc_assert (BINARY_CLASS_P (t
));
762 walk_tree (&TREE_OPERAND (t
, 0), callback_op
, &wi
, NULL
);
765 walk_tree (&TREE_OPERAND (t
, 1), callback_op
, &wi
, NULL
);
768 seq
= gsi_seq (wi
.gsi
);
769 if (!gimple_seq_empty_p (seq
))
771 gimple_seq pre_body
= gimple_omp_for_pre_body (for_stmt
);
772 annotate_all_with_location (seq
, gimple_location (for_stmt
));
773 gimple_seq_add_seq (&pre_body
, seq
);
774 gimple_omp_for_set_pre_body (for_stmt
, pre_body
);
778 /* Similarly for ROOT and all functions nested underneath, depth first. */
781 walk_all_functions (walk_stmt_fn callback_stmt
, walk_tree_fn callback_op
,
782 struct nesting_info
*root
)
784 struct nesting_info
*n
;
785 FOR_EACH_NEST_INFO (n
, root
)
786 walk_function (callback_stmt
, callback_op
, n
);
790 /* We have to check for a fairly pathological case. The operands of function
791 nested function are to be interpreted in the context of the enclosing
792 function. So if any are variably-sized, they will get remapped when the
793 enclosing function is inlined. But that remapping would also have to be
794 done in the types of the PARM_DECLs of the nested function, meaning the
795 argument types of that function will disagree with the arguments in the
796 calls to that function. So we'd either have to make a copy of the nested
797 function corresponding to each time the enclosing function was inlined or
798 add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
799 function. The former is not practical. The latter would still require
800 detecting this case to know when to add the conversions. So, for now at
801 least, we don't inline such an enclosing function.
803 We have to do that check recursively, so here return indicating whether
804 FNDECL has such a nested function. ORIG_FN is the function we were
805 trying to inline to use for checking whether any argument is variably
806 modified by anything in it.
808 It would be better to do this in tree-inline.c so that we could give
809 the appropriate warning for why a function can't be inlined, but that's
810 too late since the nesting structure has already been flattened and
811 adding a flag just to record this fact seems a waste of a flag. */
814 check_for_nested_with_variably_modified (tree fndecl
, tree orig_fndecl
)
816 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
819 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
821 for (arg
= DECL_ARGUMENTS (cgn
->decl
); arg
; arg
= DECL_CHAIN (arg
))
822 if (variably_modified_type_p (TREE_TYPE (arg
), orig_fndecl
))
825 if (check_for_nested_with_variably_modified (cgn
->decl
,
833 /* Construct our local datastructure describing the function nesting
834 tree rooted by CGN. */
836 static struct nesting_info
*
837 create_nesting_tree (struct cgraph_node
*cgn
)
839 struct nesting_info
*info
= XCNEW (struct nesting_info
);
840 info
->field_map
= new hash_map
<tree
, tree
>;
841 info
->var_map
= new hash_map
<tree
, tree
>;
842 info
->mem_refs
= new hash_set
<tree
*>;
843 info
->suppress_expansion
= BITMAP_ALLOC (&nesting_info_bitmap_obstack
);
844 info
->context
= cgn
->decl
;
845 info
->thunk_p
= cgn
->thunk
.thunk_p
;
847 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
849 struct nesting_info
*sub
= create_nesting_tree (cgn
);
851 sub
->next
= info
->inner
;
855 /* See discussion at check_for_nested_with_variably_modified for a
856 discussion of why this has to be here. */
857 if (check_for_nested_with_variably_modified (info
->context
, info
->context
))
858 DECL_UNINLINABLE (info
->context
) = true;
863 /* Return an expression computing the static chain for TARGET_CONTEXT
864 from INFO->CONTEXT. Insert any necessary computations before TSI. */
867 get_static_chain (struct nesting_info
*info
, tree target_context
,
868 gimple_stmt_iterator
*gsi
)
870 struct nesting_info
*i
;
873 if (info
->context
== target_context
)
875 x
= build_addr (info
->frame_decl
);
876 info
->static_chain_added
|= 1;
880 x
= get_chain_decl (info
);
881 info
->static_chain_added
|= 2;
883 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
885 tree field
= get_chain_field (i
);
887 x
= build_simple_mem_ref_notrap (x
);
888 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
889 x
= init_tmp_var (info
, x
, gsi
);
897 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
898 frame as seen from INFO->CONTEXT. Insert any necessary computations
902 get_frame_field (struct nesting_info
*info
, tree target_context
,
903 tree field
, gimple_stmt_iterator
*gsi
)
905 struct nesting_info
*i
;
908 if (info
->context
== target_context
)
910 /* Make sure frame_decl gets created. */
911 (void) get_frame_type (info
);
912 x
= info
->frame_decl
;
913 info
->static_chain_added
|= 1;
917 x
= get_chain_decl (info
);
918 info
->static_chain_added
|= 2;
920 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
922 tree field
= get_chain_field (i
);
924 x
= build_simple_mem_ref_notrap (x
);
925 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
926 x
= init_tmp_var (info
, x
, gsi
);
929 x
= build_simple_mem_ref_notrap (x
);
932 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
936 static void note_nonlocal_vla_type (struct nesting_info
*info
, tree type
);
938 /* A subroutine of convert_nonlocal_reference_op. Create a local variable
939 in the nested function with DECL_VALUE_EXPR set to reference the true
940 variable in the parent function. This is used both for debug info
941 and in OMP lowering. */
944 get_nonlocal_debug_decl (struct nesting_info
*info
, tree decl
)
947 struct nesting_info
*i
;
948 tree x
, field
, new_decl
;
950 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
955 target_context
= decl_function_context (decl
);
957 /* A copy of the code in get_frame_field, but without the temporaries. */
958 if (info
->context
== target_context
)
960 /* Make sure frame_decl gets created. */
961 (void) get_frame_type (info
);
962 x
= info
->frame_decl
;
964 info
->static_chain_added
|= 1;
968 x
= get_chain_decl (info
);
969 info
->static_chain_added
|= 2;
970 for (i
= info
->outer
; i
->context
!= target_context
; i
= i
->outer
)
972 field
= get_chain_field (i
);
973 x
= build_simple_mem_ref_notrap (x
);
974 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
976 x
= build_simple_mem_ref_notrap (x
);
979 field
= lookup_field_for_decl (i
, decl
, INSERT
);
980 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
981 if (use_pointer_in_frame (decl
))
982 x
= build_simple_mem_ref_notrap (x
);
984 /* ??? We should be remapping types as well, surely. */
985 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
986 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
987 DECL_CONTEXT (new_decl
) = info
->context
;
988 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
989 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
990 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
991 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
992 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
993 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
994 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
995 if ((TREE_CODE (decl
) == PARM_DECL
996 || TREE_CODE (decl
) == RESULT_DECL
998 && DECL_BY_REFERENCE (decl
))
999 DECL_BY_REFERENCE (new_decl
) = 1;
1001 SET_DECL_VALUE_EXPR (new_decl
, x
);
1002 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1005 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1006 info
->debug_var_chain
= new_decl
;
1009 && info
->context
!= target_context
1010 && variably_modified_type_p (TREE_TYPE (decl
), NULL
))
1011 note_nonlocal_vla_type (info
, TREE_TYPE (decl
));
1017 /* Callback for walk_gimple_stmt, rewrite all references to VAR
1018 and PARM_DECLs that belong to outer functions.
1020 The rewrite will involve some number of structure accesses back up
1021 the static chain. E.g. for a variable FOO up one nesting level it'll
1022 be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1023 indirections apply to decls for which use_pointer_in_frame is true. */
1026 convert_nonlocal_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1028 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1029 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1033 switch (TREE_CODE (t
))
1036 /* Non-automatic variables are never processed. */
1037 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1043 tree x
, target_context
= decl_function_context (t
);
1045 if (info
->context
== target_context
)
1050 if (bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1051 x
= get_nonlocal_debug_decl (info
, t
);
1054 struct nesting_info
*i
= info
;
1055 while (i
&& i
->context
!= target_context
)
1057 /* If none of the outer contexts is the target context, this means
1058 that the VAR or PARM_DECL is referenced in a wrong context. */
1060 internal_error ("%s from %s referenced in %s",
1061 IDENTIFIER_POINTER (DECL_NAME (t
)),
1062 IDENTIFIER_POINTER (DECL_NAME (target_context
)),
1063 IDENTIFIER_POINTER (DECL_NAME (info
->context
)));
1065 x
= lookup_field_for_decl (i
, t
, INSERT
);
1066 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
1067 if (use_pointer_in_frame (t
))
1069 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1070 x
= build_simple_mem_ref_notrap (x
);
1077 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1079 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1087 /* We're taking the address of a label from a parent function, but
1088 this is not itself a non-local goto. Mark the label such that it
1089 will not be deleted, much as we would with a label address in
1091 if (decl_function_context (t
) != info
->context
)
1092 FORCED_LABEL (t
) = 1;
1097 bool save_val_only
= wi
->val_only
;
1099 wi
->val_only
= false;
1101 wi
->changed
= false;
1102 walk_tree (&TREE_OPERAND (t
, 0), convert_nonlocal_reference_op
, wi
, 0);
1103 wi
->val_only
= true;
1109 /* If we changed anything, we might no longer be directly
1110 referencing a decl. */
1111 save_context
= current_function_decl
;
1112 current_function_decl
= info
->context
;
1113 recompute_tree_invariant_for_addr_expr (t
);
1114 current_function_decl
= save_context
;
1116 /* If the callback converted the address argument in a context
1117 where we only accept variables (and min_invariant, presumably),
1118 then compute the address into a temporary. */
1120 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1130 case ARRAY_RANGE_REF
:
1132 /* Go down this entire nest and just look at the final prefix and
1133 anything that describes the references. Otherwise, we lose track
1134 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1135 wi
->val_only
= true;
1137 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1139 if (TREE_CODE (t
) == COMPONENT_REF
)
1140 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
, wi
,
1142 else if (TREE_CODE (t
) == ARRAY_REF
1143 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1145 walk_tree (&TREE_OPERAND (t
, 1), convert_nonlocal_reference_op
,
1147 walk_tree (&TREE_OPERAND (t
, 2), convert_nonlocal_reference_op
,
1149 walk_tree (&TREE_OPERAND (t
, 3), convert_nonlocal_reference_op
,
1153 wi
->val_only
= false;
1154 walk_tree (tp
, convert_nonlocal_reference_op
, wi
, NULL
);
1157 case VIEW_CONVERT_EXPR
:
1158 /* Just request to look at the subtrees, leaving val_only and lhs
1159 untouched. This might actually be for !val_only + lhs, in which
1160 case we don't want to force a replacement by a temporary. */
1165 if (!IS_TYPE_OR_DECL_P (t
))
1168 wi
->val_only
= true;
1177 static tree
convert_nonlocal_reference_stmt (gimple_stmt_iterator
*, bool *,
1178 struct walk_stmt_info
*);
1180 /* Helper for convert_nonlocal_references, rewrite all references to VAR
1181 and PARM_DECLs that belong to outer functions. */
1184 convert_nonlocal_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1186 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1187 bool need_chain
= false, need_stmts
= false;
1188 tree clause
, decl
, *pdecl
;
1190 bitmap new_suppress
;
1192 new_suppress
= BITMAP_GGC_ALLOC ();
1193 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1195 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1198 switch (OMP_CLAUSE_CODE (clause
))
1200 case OMP_CLAUSE_REDUCTION
:
1201 case OMP_CLAUSE_IN_REDUCTION
:
1202 case OMP_CLAUSE_TASK_REDUCTION
:
1203 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1205 if (TREE_CODE (OMP_CLAUSE_DECL (clause
)) == MEM_REF
)
1207 pdecl
= &TREE_OPERAND (OMP_CLAUSE_DECL (clause
), 0);
1208 if (TREE_CODE (*pdecl
) == POINTER_PLUS_EXPR
)
1209 pdecl
= &TREE_OPERAND (*pdecl
, 0);
1210 if (TREE_CODE (*pdecl
) == INDIRECT_REF
1211 || TREE_CODE (*pdecl
) == ADDR_EXPR
)
1212 pdecl
= &TREE_OPERAND (*pdecl
, 0);
1214 goto do_decl_clause
;
1216 case OMP_CLAUSE_LASTPRIVATE
:
1217 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1219 goto do_decl_clause
;
1221 case OMP_CLAUSE_LINEAR
:
1222 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1224 wi
->val_only
= true;
1226 convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
),
1228 goto do_decl_clause
;
1230 case OMP_CLAUSE_PRIVATE
:
1231 case OMP_CLAUSE_FIRSTPRIVATE
:
1232 case OMP_CLAUSE_COPYPRIVATE
:
1233 case OMP_CLAUSE_SHARED
:
1234 case OMP_CLAUSE_TO_DECLARE
:
1235 case OMP_CLAUSE_LINK
:
1236 case OMP_CLAUSE_USE_DEVICE_PTR
:
1237 case OMP_CLAUSE_USE_DEVICE_ADDR
:
1238 case OMP_CLAUSE_IS_DEVICE_PTR
:
1241 pdecl
= &OMP_CLAUSE_DECL (clause
);
1244 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1246 if (decl_function_context (decl
) != info
->context
)
1248 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1249 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1250 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1251 *pdecl
= get_nonlocal_debug_decl (info
, decl
);
1252 if (OMP_CLAUSE_CODE (clause
) != OMP_CLAUSE_PRIVATE
)
1257 case OMP_CLAUSE_SCHEDULE
:
1258 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1261 case OMP_CLAUSE_FINAL
:
1263 case OMP_CLAUSE_NUM_THREADS
:
1264 case OMP_CLAUSE_DEPEND
:
1265 case OMP_CLAUSE_DEVICE
:
1266 case OMP_CLAUSE_NUM_TEAMS
:
1267 case OMP_CLAUSE_THREAD_LIMIT
:
1268 case OMP_CLAUSE_SAFELEN
:
1269 case OMP_CLAUSE_SIMDLEN
:
1270 case OMP_CLAUSE_PRIORITY
:
1271 case OMP_CLAUSE_GRAINSIZE
:
1272 case OMP_CLAUSE_NUM_TASKS
:
1273 case OMP_CLAUSE_HINT
:
1274 case OMP_CLAUSE_NUM_GANGS
:
1275 case OMP_CLAUSE_NUM_WORKERS
:
1276 case OMP_CLAUSE_VECTOR_LENGTH
:
1277 case OMP_CLAUSE_GANG
:
1278 case OMP_CLAUSE_WORKER
:
1279 case OMP_CLAUSE_VECTOR
:
1280 case OMP_CLAUSE_ASYNC
:
1281 case OMP_CLAUSE_WAIT
:
1282 /* Several OpenACC clauses have optional arguments. Check if they
1284 if (OMP_CLAUSE_OPERAND (clause
, 0))
1286 wi
->val_only
= true;
1288 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1292 /* The gang clause accepts two arguments. */
1293 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
1294 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
1296 wi
->val_only
= true;
1298 convert_nonlocal_reference_op
1299 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
1303 case OMP_CLAUSE_DIST_SCHEDULE
:
1304 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
1306 wi
->val_only
= true;
1308 convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
1313 case OMP_CLAUSE_MAP
:
1315 case OMP_CLAUSE_FROM
:
1316 if (OMP_CLAUSE_SIZE (clause
))
1318 wi
->val_only
= true;
1320 convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause
),
1323 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
1324 goto do_decl_clause
;
1325 wi
->val_only
= true;
1327 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_nonlocal_reference_op
,
1331 case OMP_CLAUSE_ALIGNED
:
1332 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
1334 wi
->val_only
= true;
1336 convert_nonlocal_reference_op
1337 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
1340 case OMP_CLAUSE_NONTEMPORAL
:
1341 /* Like do_decl_clause, but don't add any suppression. */
1342 decl
= OMP_CLAUSE_DECL (clause
);
1344 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1346 if (decl_function_context (decl
) != info
->context
)
1348 OMP_CLAUSE_DECL (clause
) = get_nonlocal_debug_decl (info
, decl
);
1353 case OMP_CLAUSE_NOWAIT
:
1354 case OMP_CLAUSE_ORDERED
:
1355 case OMP_CLAUSE_DEFAULT
:
1356 case OMP_CLAUSE_COPYIN
:
1357 case OMP_CLAUSE_COLLAPSE
:
1358 case OMP_CLAUSE_TILE
:
1359 case OMP_CLAUSE_UNTIED
:
1360 case OMP_CLAUSE_MERGEABLE
:
1361 case OMP_CLAUSE_PROC_BIND
:
1362 case OMP_CLAUSE_NOGROUP
:
1363 case OMP_CLAUSE_THREADS
:
1364 case OMP_CLAUSE_SIMD
:
1365 case OMP_CLAUSE_DEFAULTMAP
:
1366 case OMP_CLAUSE_ORDER
:
1367 case OMP_CLAUSE_SEQ
:
1368 case OMP_CLAUSE_INDEPENDENT
:
1369 case OMP_CLAUSE_AUTO
:
1370 case OMP_CLAUSE_IF_PRESENT
:
1371 case OMP_CLAUSE_FINALIZE
:
1372 case OMP_CLAUSE__CONDTEMP_
:
1373 case OMP_CLAUSE__SCANTEMP_
:
1376 /* The following clause belongs to the OpenACC cache directive, which
1377 is discarded during gimplification. */
1378 case OMP_CLAUSE__CACHE_
:
1379 /* The following clauses are only allowed in the OpenMP declare simd
1380 directive, so not seen here. */
1381 case OMP_CLAUSE_UNIFORM
:
1382 case OMP_CLAUSE_INBRANCH
:
1383 case OMP_CLAUSE_NOTINBRANCH
:
1384 /* The following clauses are only allowed on OpenMP cancel and
1385 cancellation point directives, which at this point have already
1386 been lowered into a function call. */
1387 case OMP_CLAUSE_FOR
:
1388 case OMP_CLAUSE_PARALLEL
:
1389 case OMP_CLAUSE_SECTIONS
:
1390 case OMP_CLAUSE_TASKGROUP
:
1391 /* The following clauses are only added during OMP lowering; nested
1392 function decomposition happens before that. */
1393 case OMP_CLAUSE__LOOPTEMP_
:
1394 case OMP_CLAUSE__REDUCTEMP_
:
1395 case OMP_CLAUSE__SIMDUID_
:
1396 case OMP_CLAUSE__GRIDDIM_
:
1397 case OMP_CLAUSE__SIMT_
:
1398 /* Anything else. */
1404 info
->suppress_expansion
= new_suppress
;
1407 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1408 switch (OMP_CLAUSE_CODE (clause
))
1410 case OMP_CLAUSE_REDUCTION
:
1411 case OMP_CLAUSE_IN_REDUCTION
:
1412 case OMP_CLAUSE_TASK_REDUCTION
:
1413 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1416 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
1417 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1419 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1420 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1422 walk_body (convert_nonlocal_reference_stmt
,
1423 convert_nonlocal_reference_op
, info
,
1424 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
1425 walk_body (convert_nonlocal_reference_stmt
,
1426 convert_nonlocal_reference_op
, info
,
1427 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
1428 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1430 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1431 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
1436 case OMP_CLAUSE_LASTPRIVATE
:
1437 walk_body (convert_nonlocal_reference_stmt
,
1438 convert_nonlocal_reference_op
, info
,
1439 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
1442 case OMP_CLAUSE_LINEAR
:
1443 walk_body (convert_nonlocal_reference_stmt
,
1444 convert_nonlocal_reference_op
, info
,
1445 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
1455 /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1458 note_nonlocal_vla_type (struct nesting_info
*info
, tree type
)
1460 while (POINTER_TYPE_P (type
) && !TYPE_NAME (type
))
1461 type
= TREE_TYPE (type
);
1463 if (TYPE_NAME (type
)
1464 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1465 && DECL_ORIGINAL_TYPE (TYPE_NAME (type
)))
1466 type
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
1468 while (POINTER_TYPE_P (type
)
1469 || TREE_CODE (type
) == VECTOR_TYPE
1470 || TREE_CODE (type
) == FUNCTION_TYPE
1471 || TREE_CODE (type
) == METHOD_TYPE
)
1472 type
= TREE_TYPE (type
);
1474 if (TREE_CODE (type
) == ARRAY_TYPE
)
1478 note_nonlocal_vla_type (info
, TREE_TYPE (type
));
1479 domain
= TYPE_DOMAIN (type
);
1482 t
= TYPE_MIN_VALUE (domain
);
1483 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1484 && decl_function_context (t
) != info
->context
)
1485 get_nonlocal_debug_decl (info
, t
);
1486 t
= TYPE_MAX_VALUE (domain
);
1487 if (t
&& (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
1488 && decl_function_context (t
) != info
->context
)
1489 get_nonlocal_debug_decl (info
, t
);
1494 /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1495 PARM_DECLs that belong to outer functions. This handles statements
1496 that are not handled via the standard recursion done in
1497 walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1498 convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1499 operands of STMT have been handled by this function. */
1502 convert_nonlocal_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1503 struct walk_stmt_info
*wi
)
1505 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
1506 tree save_local_var_chain
;
1507 bitmap save_suppress
;
1508 gimple
*stmt
= gsi_stmt (*gsi
);
1510 switch (gimple_code (stmt
))
1513 /* Don't walk non-local gotos for now. */
1514 if (TREE_CODE (gimple_goto_dest (stmt
)) != LABEL_DECL
)
1516 wi
->val_only
= true;
1518 *handled_ops_p
= false;
1523 case GIMPLE_OMP_TEAMS
:
1524 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
1526 save_suppress
= info
->suppress_expansion
;
1527 convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
),
1529 walk_body (convert_nonlocal_reference_stmt
,
1530 convert_nonlocal_reference_op
, info
,
1531 gimple_omp_body_ptr (stmt
));
1532 info
->suppress_expansion
= save_suppress
;
1537 case GIMPLE_OMP_PARALLEL
:
1538 case GIMPLE_OMP_TASK
:
1539 save_suppress
= info
->suppress_expansion
;
1540 if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
1544 decl
= get_chain_decl (info
);
1545 c
= build_omp_clause (gimple_location (stmt
),
1546 OMP_CLAUSE_FIRSTPRIVATE
);
1547 OMP_CLAUSE_DECL (c
) = decl
;
1548 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
1549 gimple_omp_taskreg_set_clauses (stmt
, c
);
1552 save_local_var_chain
= info
->new_local_var_chain
;
1553 info
->new_local_var_chain
= NULL
;
1555 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1556 info
, gimple_omp_body_ptr (stmt
));
1558 if (info
->new_local_var_chain
)
1559 declare_vars (info
->new_local_var_chain
,
1560 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1562 info
->new_local_var_chain
= save_local_var_chain
;
1563 info
->suppress_expansion
= save_suppress
;
1566 case GIMPLE_OMP_FOR
:
1567 save_suppress
= info
->suppress_expansion
;
1568 convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
1569 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
1570 convert_nonlocal_reference_stmt
,
1571 convert_nonlocal_reference_op
, info
);
1572 walk_body (convert_nonlocal_reference_stmt
,
1573 convert_nonlocal_reference_op
, info
, gimple_omp_body_ptr (stmt
));
1574 info
->suppress_expansion
= save_suppress
;
1577 case GIMPLE_OMP_SECTIONS
:
1578 save_suppress
= info
->suppress_expansion
;
1579 convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
1580 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1581 info
, gimple_omp_body_ptr (stmt
));
1582 info
->suppress_expansion
= save_suppress
;
1585 case GIMPLE_OMP_SINGLE
:
1586 save_suppress
= info
->suppress_expansion
;
1587 convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
1588 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1589 info
, gimple_omp_body_ptr (stmt
));
1590 info
->suppress_expansion
= save_suppress
;
1593 case GIMPLE_OMP_TASKGROUP
:
1594 save_suppress
= info
->suppress_expansion
;
1595 convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt
), wi
);
1596 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1597 info
, gimple_omp_body_ptr (stmt
));
1598 info
->suppress_expansion
= save_suppress
;
1601 case GIMPLE_OMP_TARGET
:
1602 if (!is_gimple_omp_offloaded (stmt
))
1604 save_suppress
= info
->suppress_expansion
;
1605 convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1607 info
->suppress_expansion
= save_suppress
;
1608 walk_body (convert_nonlocal_reference_stmt
,
1609 convert_nonlocal_reference_op
, info
,
1610 gimple_omp_body_ptr (stmt
));
1613 save_suppress
= info
->suppress_expansion
;
1614 if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt
),
1618 decl
= get_chain_decl (info
);
1619 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
1620 OMP_CLAUSE_DECL (c
) = decl
;
1621 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TO
);
1622 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
1623 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
1624 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
1627 save_local_var_chain
= info
->new_local_var_chain
;
1628 info
->new_local_var_chain
= NULL
;
1630 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1631 info
, gimple_omp_body_ptr (stmt
));
1633 if (info
->new_local_var_chain
)
1634 declare_vars (info
->new_local_var_chain
,
1635 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
1637 info
->new_local_var_chain
= save_local_var_chain
;
1638 info
->suppress_expansion
= save_suppress
;
1641 case GIMPLE_OMP_SECTION
:
1642 case GIMPLE_OMP_MASTER
:
1643 case GIMPLE_OMP_ORDERED
:
1644 case GIMPLE_OMP_SCAN
:
1645 walk_body (convert_nonlocal_reference_stmt
, convert_nonlocal_reference_op
,
1646 info
, gimple_omp_body_ptr (stmt
));
1651 gbind
*bind_stmt
= as_a
<gbind
*> (stmt
);
1653 for (tree var
= gimple_bind_vars (bind_stmt
); var
; var
= DECL_CHAIN (var
))
1654 if (TREE_CODE (var
) == NAMELIST_DECL
)
1656 /* Adjust decls mentioned in NAMELIST_DECL. */
1657 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
1661 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
1664 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1666 if (decl_function_context (decl
) != info
->context
)
1667 CONSTRUCTOR_ELT (decls
, i
)->value
1668 = get_nonlocal_debug_decl (info
, decl
);
1672 *handled_ops_p
= false;
1676 wi
->val_only
= true;
1678 *handled_ops_p
= false;
1682 if (gimple_clobber_p (stmt
))
1684 tree lhs
= gimple_assign_lhs (stmt
);
1686 && !(TREE_STATIC (lhs
) || DECL_EXTERNAL (lhs
))
1687 && decl_function_context (lhs
) != info
->context
)
1689 gsi_replace (gsi
, gimple_build_nop (), true);
1693 *handled_ops_p
= false;
1697 /* For every other statement that we are not interested in
1698 handling here, let the walker traverse the operands. */
1699 *handled_ops_p
= false;
1703 /* We have handled all of STMT operands, no need to traverse the operands. */
1704 *handled_ops_p
= true;
1709 /* A subroutine of convert_local_reference. Create a local variable
1710 in the parent function with DECL_VALUE_EXPR set to reference the
1711 field in FRAME. This is used both for debug info and in OMP
1715 get_local_debug_decl (struct nesting_info
*info
, tree decl
, tree field
)
1719 tree
*slot
= &info
->var_map
->get_or_insert (decl
);
1723 /* Make sure frame_decl gets created. */
1724 (void) get_frame_type (info
);
1725 x
= info
->frame_decl
;
1726 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
), x
, field
, NULL_TREE
);
1728 new_decl
= build_decl (DECL_SOURCE_LOCATION (decl
),
1729 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
1730 DECL_CONTEXT (new_decl
) = info
->context
;
1731 DECL_ARTIFICIAL (new_decl
) = DECL_ARTIFICIAL (decl
);
1732 DECL_IGNORED_P (new_decl
) = DECL_IGNORED_P (decl
);
1733 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (decl
);
1734 TREE_SIDE_EFFECTS (new_decl
) = TREE_SIDE_EFFECTS (decl
);
1735 TREE_READONLY (new_decl
) = TREE_READONLY (decl
);
1736 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (decl
);
1737 DECL_SEEN_IN_BIND_EXPR_P (new_decl
) = 1;
1738 if ((TREE_CODE (decl
) == PARM_DECL
1739 || TREE_CODE (decl
) == RESULT_DECL
1741 && DECL_BY_REFERENCE (decl
))
1742 DECL_BY_REFERENCE (new_decl
) = 1;
1744 SET_DECL_VALUE_EXPR (new_decl
, x
);
1745 DECL_HAS_VALUE_EXPR_P (new_decl
) = 1;
1748 DECL_CHAIN (new_decl
) = info
->debug_var_chain
;
1749 info
->debug_var_chain
= new_decl
;
1751 /* Do not emit debug info twice. */
1752 DECL_IGNORED_P (decl
) = 1;
1758 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1759 and PARM_DECLs that were referenced by inner nested functions.
1760 The rewrite will be a structure reference to the local frame variable. */
1762 static bool convert_local_omp_clauses (tree
*, struct walk_stmt_info
*);
1765 convert_local_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
1767 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
1768 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1769 tree t
= *tp
, field
, x
;
1773 switch (TREE_CODE (t
))
1776 /* Non-automatic variables are never processed. */
1777 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
1782 if (t
!= info
->frame_decl
&& decl_function_context (t
) == info
->context
)
1784 /* If we copied a pointer to the frame, then the original decl
1785 is used unchanged in the parent function. */
1786 if (use_pointer_in_frame (t
))
1789 /* No need to transform anything if no child references the
1791 field
= lookup_field_for_decl (info
, t
, NO_INSERT
);
1796 if (bitmap_bit_p (info
->suppress_expansion
, DECL_UID (t
)))
1797 x
= get_local_debug_decl (info
, t
, field
);
1799 x
= get_frame_field (info
, info
->context
, field
, &wi
->gsi
);
1804 x
= save_tmp_var (info
, x
, &wi
->gsi
);
1806 x
= init_tmp_var (info
, x
, &wi
->gsi
);
1814 save_val_only
= wi
->val_only
;
1815 wi
->val_only
= false;
1817 wi
->changed
= false;
1818 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
, wi
, NULL
);
1819 wi
->val_only
= save_val_only
;
1821 /* If we converted anything ... */
1826 /* Then the frame decl is now addressable. */
1827 TREE_ADDRESSABLE (info
->frame_decl
) = 1;
1829 save_context
= current_function_decl
;
1830 current_function_decl
= info
->context
;
1831 recompute_tree_invariant_for_addr_expr (t
);
1832 current_function_decl
= save_context
;
1834 /* If we are in a context where we only accept values, then
1835 compute the address into a temporary. */
1837 *tp
= gsi_gimplify_val ((struct nesting_info
*) wi
->info
,
1846 case ARRAY_RANGE_REF
:
1848 /* Go down this entire nest and just look at the final prefix and
1849 anything that describes the references. Otherwise, we lose track
1850 of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1851 save_val_only
= wi
->val_only
;
1852 wi
->val_only
= true;
1854 for (; handled_component_p (t
); tp
= &TREE_OPERAND (t
, 0), t
= *tp
)
1856 if (TREE_CODE (t
) == COMPONENT_REF
)
1857 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1859 else if (TREE_CODE (t
) == ARRAY_REF
1860 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
1862 walk_tree (&TREE_OPERAND (t
, 1), convert_local_reference_op
, wi
,
1864 walk_tree (&TREE_OPERAND (t
, 2), convert_local_reference_op
, wi
,
1866 walk_tree (&TREE_OPERAND (t
, 3), convert_local_reference_op
, wi
,
1870 wi
->val_only
= false;
1871 walk_tree (tp
, convert_local_reference_op
, wi
, NULL
);
1872 wi
->val_only
= save_val_only
;
1876 save_val_only
= wi
->val_only
;
1877 wi
->val_only
= true;
1879 walk_tree (&TREE_OPERAND (t
, 0), convert_local_reference_op
,
1881 /* We need to re-fold the MEM_REF as component references as
1882 part of a ADDR_EXPR address are not allowed. But we cannot
1883 fold here, as the chain record type is not yet finalized. */
1884 if (TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
1885 && !DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
1886 info
->mem_refs
->add (tp
);
1887 wi
->val_only
= save_val_only
;
1890 case VIEW_CONVERT_EXPR
:
1891 /* Just request to look at the subtrees, leaving val_only and lhs
1892 untouched. This might actually be for !val_only + lhs, in which
1893 case we don't want to force a replacement by a temporary. */
1898 if (!IS_TYPE_OR_DECL_P (t
))
1901 wi
->val_only
= true;
1910 static tree
convert_local_reference_stmt (gimple_stmt_iterator
*, bool *,
1911 struct walk_stmt_info
*);
1913 /* Helper for convert_local_reference. Convert all the references in
1914 the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
1917 convert_local_omp_clauses (tree
*pclauses
, struct walk_stmt_info
*wi
)
1919 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
1920 bool need_frame
= false, need_stmts
= false;
1921 tree clause
, decl
, *pdecl
;
1923 bitmap new_suppress
;
1925 new_suppress
= BITMAP_GGC_ALLOC ();
1926 bitmap_copy (new_suppress
, info
->suppress_expansion
);
1928 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
1931 switch (OMP_CLAUSE_CODE (clause
))
1933 case OMP_CLAUSE_REDUCTION
:
1934 case OMP_CLAUSE_IN_REDUCTION
:
1935 case OMP_CLAUSE_TASK_REDUCTION
:
1936 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
1938 if (TREE_CODE (OMP_CLAUSE_DECL (clause
)) == MEM_REF
)
1940 pdecl
= &TREE_OPERAND (OMP_CLAUSE_DECL (clause
), 0);
1941 if (TREE_CODE (*pdecl
) == POINTER_PLUS_EXPR
)
1942 pdecl
= &TREE_OPERAND (*pdecl
, 0);
1943 if (TREE_CODE (*pdecl
) == INDIRECT_REF
1944 || TREE_CODE (*pdecl
) == ADDR_EXPR
)
1945 pdecl
= &TREE_OPERAND (*pdecl
, 0);
1947 goto do_decl_clause
;
1949 case OMP_CLAUSE_LASTPRIVATE
:
1950 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
))
1952 goto do_decl_clause
;
1954 case OMP_CLAUSE_LINEAR
:
1955 if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
))
1957 wi
->val_only
= true;
1959 convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause
), &dummy
,
1961 goto do_decl_clause
;
1963 case OMP_CLAUSE_PRIVATE
:
1964 case OMP_CLAUSE_FIRSTPRIVATE
:
1965 case OMP_CLAUSE_COPYPRIVATE
:
1966 case OMP_CLAUSE_SHARED
:
1967 case OMP_CLAUSE_TO_DECLARE
:
1968 case OMP_CLAUSE_LINK
:
1969 case OMP_CLAUSE_USE_DEVICE_PTR
:
1970 case OMP_CLAUSE_USE_DEVICE_ADDR
:
1971 case OMP_CLAUSE_IS_DEVICE_PTR
:
1974 pdecl
= &OMP_CLAUSE_DECL (clause
);
1977 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
1979 if (decl_function_context (decl
) == info
->context
1980 && !use_pointer_in_frame (decl
))
1982 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
1985 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_SHARED
)
1986 OMP_CLAUSE_SHARED_READONLY (clause
) = 0;
1987 bitmap_set_bit (new_suppress
, DECL_UID (decl
));
1988 *pdecl
= get_local_debug_decl (info
, decl
, field
);
1994 case OMP_CLAUSE_SCHEDULE
:
1995 if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause
) == NULL
)
1998 case OMP_CLAUSE_FINAL
:
2000 case OMP_CLAUSE_NUM_THREADS
:
2001 case OMP_CLAUSE_DEPEND
:
2002 case OMP_CLAUSE_DEVICE
:
2003 case OMP_CLAUSE_NUM_TEAMS
:
2004 case OMP_CLAUSE_THREAD_LIMIT
:
2005 case OMP_CLAUSE_SAFELEN
:
2006 case OMP_CLAUSE_SIMDLEN
:
2007 case OMP_CLAUSE_PRIORITY
:
2008 case OMP_CLAUSE_GRAINSIZE
:
2009 case OMP_CLAUSE_NUM_TASKS
:
2010 case OMP_CLAUSE_HINT
:
2011 case OMP_CLAUSE_NUM_GANGS
:
2012 case OMP_CLAUSE_NUM_WORKERS
:
2013 case OMP_CLAUSE_VECTOR_LENGTH
:
2014 case OMP_CLAUSE_GANG
:
2015 case OMP_CLAUSE_WORKER
:
2016 case OMP_CLAUSE_VECTOR
:
2017 case OMP_CLAUSE_ASYNC
:
2018 case OMP_CLAUSE_WAIT
:
2019 /* Several OpenACC clauses have optional arguments. Check if they
2021 if (OMP_CLAUSE_OPERAND (clause
, 0))
2023 wi
->val_only
= true;
2025 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
2029 /* The gang clause accepts two arguments. */
2030 if (OMP_CLAUSE_CODE (clause
) == OMP_CLAUSE_GANG
2031 && OMP_CLAUSE_GANG_STATIC_EXPR (clause
))
2033 wi
->val_only
= true;
2035 convert_nonlocal_reference_op
2036 (&OMP_CLAUSE_GANG_STATIC_EXPR (clause
), &dummy
, wi
);
2040 case OMP_CLAUSE_DIST_SCHEDULE
:
2041 if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause
) != NULL
)
2043 wi
->val_only
= true;
2045 convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause
, 0),
2050 case OMP_CLAUSE_MAP
:
2052 case OMP_CLAUSE_FROM
:
2053 if (OMP_CLAUSE_SIZE (clause
))
2055 wi
->val_only
= true;
2057 convert_local_reference_op (&OMP_CLAUSE_SIZE (clause
),
2060 if (DECL_P (OMP_CLAUSE_DECL (clause
)))
2061 goto do_decl_clause
;
2062 wi
->val_only
= true;
2064 walk_tree (&OMP_CLAUSE_DECL (clause
), convert_local_reference_op
,
2068 case OMP_CLAUSE_ALIGNED
:
2069 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
))
2071 wi
->val_only
= true;
2073 convert_local_reference_op
2074 (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause
), &dummy
, wi
);
2077 case OMP_CLAUSE_NONTEMPORAL
:
2078 /* Like do_decl_clause, but don't add any suppression. */
2079 decl
= OMP_CLAUSE_DECL (clause
);
2081 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2083 if (decl_function_context (decl
) == info
->context
2084 && !use_pointer_in_frame (decl
))
2086 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2089 OMP_CLAUSE_DECL (clause
)
2090 = get_local_debug_decl (info
, decl
, field
);
2096 case OMP_CLAUSE_NOWAIT
:
2097 case OMP_CLAUSE_ORDERED
:
2098 case OMP_CLAUSE_DEFAULT
:
2099 case OMP_CLAUSE_COPYIN
:
2100 case OMP_CLAUSE_COLLAPSE
:
2101 case OMP_CLAUSE_TILE
:
2102 case OMP_CLAUSE_UNTIED
:
2103 case OMP_CLAUSE_MERGEABLE
:
2104 case OMP_CLAUSE_PROC_BIND
:
2105 case OMP_CLAUSE_NOGROUP
:
2106 case OMP_CLAUSE_THREADS
:
2107 case OMP_CLAUSE_SIMD
:
2108 case OMP_CLAUSE_DEFAULTMAP
:
2109 case OMP_CLAUSE_ORDER
:
2110 case OMP_CLAUSE_SEQ
:
2111 case OMP_CLAUSE_INDEPENDENT
:
2112 case OMP_CLAUSE_AUTO
:
2113 case OMP_CLAUSE_IF_PRESENT
:
2114 case OMP_CLAUSE_FINALIZE
:
2115 case OMP_CLAUSE__CONDTEMP_
:
2116 case OMP_CLAUSE__SCANTEMP_
:
2119 /* The following clause belongs to the OpenACC cache directive, which
2120 is discarded during gimplification. */
2121 case OMP_CLAUSE__CACHE_
:
2122 /* The following clauses are only allowed in the OpenMP declare simd
2123 directive, so not seen here. */
2124 case OMP_CLAUSE_UNIFORM
:
2125 case OMP_CLAUSE_INBRANCH
:
2126 case OMP_CLAUSE_NOTINBRANCH
:
2127 /* The following clauses are only allowed on OpenMP cancel and
2128 cancellation point directives, which at this point have already
2129 been lowered into a function call. */
2130 case OMP_CLAUSE_FOR
:
2131 case OMP_CLAUSE_PARALLEL
:
2132 case OMP_CLAUSE_SECTIONS
:
2133 case OMP_CLAUSE_TASKGROUP
:
2134 /* The following clauses are only added during OMP lowering; nested
2135 function decomposition happens before that. */
2136 case OMP_CLAUSE__LOOPTEMP_
:
2137 case OMP_CLAUSE__REDUCTEMP_
:
2138 case OMP_CLAUSE__SIMDUID_
:
2139 case OMP_CLAUSE__GRIDDIM_
:
2140 case OMP_CLAUSE__SIMT_
:
2141 /* Anything else. */
2147 info
->suppress_expansion
= new_suppress
;
2150 for (clause
= *pclauses
; clause
; clause
= OMP_CLAUSE_CHAIN (clause
))
2151 switch (OMP_CLAUSE_CODE (clause
))
2153 case OMP_CLAUSE_REDUCTION
:
2154 case OMP_CLAUSE_IN_REDUCTION
:
2155 case OMP_CLAUSE_TASK_REDUCTION
:
2156 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2159 = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
));
2160 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2162 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2163 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2165 walk_body (convert_local_reference_stmt
,
2166 convert_local_reference_op
, info
,
2167 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause
));
2168 walk_body (convert_local_reference_stmt
,
2169 convert_local_reference_op
, info
,
2170 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause
));
2171 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause
))
2173 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2174 DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause
))
2179 case OMP_CLAUSE_LASTPRIVATE
:
2180 walk_body (convert_local_reference_stmt
,
2181 convert_local_reference_op
, info
,
2182 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause
));
2185 case OMP_CLAUSE_LINEAR
:
2186 walk_body (convert_local_reference_stmt
,
2187 convert_local_reference_op
, info
,
2188 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause
));
2199 /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2200 and PARM_DECLs that were referenced by inner nested functions.
2201 The rewrite will be a structure reference to the local frame variable. */
2204 convert_local_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2205 struct walk_stmt_info
*wi
)
2207 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2208 tree save_local_var_chain
;
2209 bitmap save_suppress
;
2210 char save_static_chain_added
;
2211 bool frame_decl_added
;
2212 gimple
*stmt
= gsi_stmt (*gsi
);
2214 switch (gimple_code (stmt
))
2216 case GIMPLE_OMP_TEAMS
:
2217 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
2219 save_suppress
= info
->suppress_expansion
;
2220 convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt
), wi
);
2221 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2222 info
, gimple_omp_body_ptr (stmt
));
2223 info
->suppress_expansion
= save_suppress
;
2228 case GIMPLE_OMP_PARALLEL
:
2229 case GIMPLE_OMP_TASK
:
2230 save_suppress
= info
->suppress_expansion
;
2231 frame_decl_added
= false;
2232 if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt
),
2235 tree c
= build_omp_clause (gimple_location (stmt
),
2237 (void) get_frame_type (info
);
2238 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2239 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2240 gimple_omp_taskreg_set_clauses (stmt
, c
);
2241 info
->static_chain_added
|= 4;
2242 frame_decl_added
= true;
2245 save_local_var_chain
= info
->new_local_var_chain
;
2246 save_static_chain_added
= info
->static_chain_added
;
2247 info
->new_local_var_chain
= NULL
;
2248 info
->static_chain_added
= 0;
2250 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2251 gimple_omp_body_ptr (stmt
));
2253 if ((info
->static_chain_added
& 4) != 0 && !frame_decl_added
)
2255 tree c
= build_omp_clause (gimple_location (stmt
),
2257 (void) get_frame_type (info
);
2258 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2259 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2260 info
->static_chain_added
|= 4;
2261 gimple_omp_taskreg_set_clauses (stmt
, c
);
2263 if (info
->new_local_var_chain
)
2264 declare_vars (info
->new_local_var_chain
,
2265 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2266 info
->new_local_var_chain
= save_local_var_chain
;
2267 info
->suppress_expansion
= save_suppress
;
2268 info
->static_chain_added
|= save_static_chain_added
;
2271 case GIMPLE_OMP_FOR
:
2272 save_suppress
= info
->suppress_expansion
;
2273 convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt
), wi
);
2274 walk_gimple_omp_for (as_a
<gomp_for
*> (stmt
),
2275 convert_local_reference_stmt
,
2276 convert_local_reference_op
, info
);
2277 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2278 info
, gimple_omp_body_ptr (stmt
));
2279 info
->suppress_expansion
= save_suppress
;
2282 case GIMPLE_OMP_SECTIONS
:
2283 save_suppress
= info
->suppress_expansion
;
2284 convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt
), wi
);
2285 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2286 info
, gimple_omp_body_ptr (stmt
));
2287 info
->suppress_expansion
= save_suppress
;
2290 case GIMPLE_OMP_SINGLE
:
2291 save_suppress
= info
->suppress_expansion
;
2292 convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt
), wi
);
2293 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2294 info
, gimple_omp_body_ptr (stmt
));
2295 info
->suppress_expansion
= save_suppress
;
2298 case GIMPLE_OMP_TASKGROUP
:
2299 save_suppress
= info
->suppress_expansion
;
2300 convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt
), wi
);
2301 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2302 info
, gimple_omp_body_ptr (stmt
));
2303 info
->suppress_expansion
= save_suppress
;
2306 case GIMPLE_OMP_TARGET
:
2307 if (!is_gimple_omp_offloaded (stmt
))
2309 save_suppress
= info
->suppress_expansion
;
2310 convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
);
2311 info
->suppress_expansion
= save_suppress
;
2312 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2313 info
, gimple_omp_body_ptr (stmt
));
2316 save_suppress
= info
->suppress_expansion
;
2317 frame_decl_added
= false;
2318 if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt
), wi
))
2320 tree c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2321 (void) get_frame_type (info
);
2322 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2323 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2324 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2325 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2326 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2327 info
->static_chain_added
|= 4;
2328 frame_decl_added
= true;
2331 save_local_var_chain
= info
->new_local_var_chain
;
2332 save_static_chain_added
= info
->static_chain_added
;
2333 info
->new_local_var_chain
= NULL
;
2334 info
->static_chain_added
= 0;
2336 walk_body (convert_local_reference_stmt
, convert_local_reference_op
, info
,
2337 gimple_omp_body_ptr (stmt
));
2339 if ((info
->static_chain_added
& 4) != 0 && !frame_decl_added
)
2341 tree c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2342 (void) get_frame_type (info
);
2343 OMP_CLAUSE_DECL (c
) = info
->frame_decl
;
2344 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
2345 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (info
->frame_decl
);
2346 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2347 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
), c
);
2348 info
->static_chain_added
|= 4;
2351 if (info
->new_local_var_chain
)
2352 declare_vars (info
->new_local_var_chain
,
2353 gimple_seq_first_stmt (gimple_omp_body (stmt
)), false);
2354 info
->new_local_var_chain
= save_local_var_chain
;
2355 info
->suppress_expansion
= save_suppress
;
2356 info
->static_chain_added
|= save_static_chain_added
;
2359 case GIMPLE_OMP_SECTION
:
2360 case GIMPLE_OMP_MASTER
:
2361 case GIMPLE_OMP_ORDERED
:
2362 case GIMPLE_OMP_SCAN
:
2363 walk_body (convert_local_reference_stmt
, convert_local_reference_op
,
2364 info
, gimple_omp_body_ptr (stmt
));
2368 wi
->val_only
= true;
2370 *handled_ops_p
= false;
2374 if (gimple_clobber_p (stmt
))
2376 tree lhs
= gimple_assign_lhs (stmt
);
2378 && !use_pointer_in_frame (lhs
)
2379 && lookup_field_for_decl (info
, lhs
, NO_INSERT
))
2381 gsi_replace (gsi
, gimple_build_nop (), true);
2385 *handled_ops_p
= false;
2389 for (tree var
= gimple_bind_vars (as_a
<gbind
*> (stmt
));
2391 var
= DECL_CHAIN (var
))
2392 if (TREE_CODE (var
) == NAMELIST_DECL
)
2394 /* Adjust decls mentioned in NAMELIST_DECL. */
2395 tree decls
= NAMELIST_DECL_ASSOCIATED_DECL (var
);
2399 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls
), i
, decl
)
2402 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
2404 if (decl_function_context (decl
) == info
->context
2405 && !use_pointer_in_frame (decl
))
2407 tree field
= lookup_field_for_decl (info
, decl
, NO_INSERT
);
2410 CONSTRUCTOR_ELT (decls
, i
)->value
2411 = get_local_debug_decl (info
, decl
, field
);
2417 *handled_ops_p
= false;
2421 /* For every other statement that we are not interested in
2422 handling here, let the walker traverse the operands. */
2423 *handled_ops_p
= false;
2427 /* Indicate that we have handled all the operands ourselves. */
2428 *handled_ops_p
= true;
2433 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2434 that reference labels from outer functions. The rewrite will be a
2435 call to __builtin_nonlocal_goto. */
2438 convert_nl_goto_reference (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2439 struct walk_stmt_info
*wi
)
2441 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2442 tree label
, new_label
, target_context
, x
, field
;
2444 gimple
*stmt
= gsi_stmt (*gsi
);
2446 if (gimple_code (stmt
) != GIMPLE_GOTO
)
2448 *handled_ops_p
= false;
2452 label
= gimple_goto_dest (stmt
);
2453 if (TREE_CODE (label
) != LABEL_DECL
)
2455 *handled_ops_p
= false;
2459 target_context
= decl_function_context (label
);
2460 if (target_context
== info
->context
)
2462 *handled_ops_p
= false;
2466 for (i
= info
->outer
; target_context
!= i
->context
; i
= i
->outer
)
2469 /* The original user label may also be use for a normal goto, therefore
2470 we must create a new label that will actually receive the abnormal
2471 control transfer. This new label will be marked LABEL_NONLOCAL; this
2472 mark will trigger proper behavior in the cfg, as well as cause the
2473 (hairy target-specific) non-local goto receiver code to be generated
2474 when we expand rtl. Enter this association into var_map so that we
2475 can insert the new label into the IL during a second pass. */
2476 tree
*slot
= &i
->var_map
->get_or_insert (label
);
2479 new_label
= create_artificial_label (UNKNOWN_LOCATION
);
2480 DECL_NONLOCAL (new_label
) = 1;
2486 /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2487 field
= get_nl_goto_field (i
);
2488 x
= get_frame_field (info
, target_context
, field
, gsi
);
2490 x
= gsi_gimplify_val (info
, x
, gsi
);
2491 call
= gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO
),
2492 2, build_addr (new_label
), x
);
2493 gsi_replace (gsi
, call
, false);
2495 /* We have handled all of STMT's operands, no need to keep going. */
2496 *handled_ops_p
= true;
2501 /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2502 are referenced via nonlocal goto from a nested function. The rewrite
2503 will involve installing a newly generated DECL_NONLOCAL label, and
2504 (potentially) a branch around the rtl gunk that is assumed to be
2505 attached to such a label. */
2508 convert_nl_goto_receiver (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2509 struct walk_stmt_info
*wi
)
2511 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2512 tree label
, new_label
;
2513 gimple_stmt_iterator tmp_gsi
;
2514 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (*gsi
));
2518 *handled_ops_p
= false;
2522 label
= gimple_label_label (stmt
);
2524 tree
*slot
= info
->var_map
->get (label
);
2527 *handled_ops_p
= false;
2531 /* If there's any possibility that the previous statement falls through,
2532 then we must branch around the new non-local label. */
2534 gsi_prev (&tmp_gsi
);
2535 if (gsi_end_p (tmp_gsi
) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi
)))
2537 gimple
*stmt
= gimple_build_goto (label
);
2538 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2541 new_label
= (tree
) *slot
;
2542 stmt
= gimple_build_label (new_label
);
2543 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
2545 *handled_ops_p
= true;
2550 /* Called via walk_function+walk_stmt, rewrite all references to addresses
2551 of nested functions that require the use of trampolines. The rewrite
2552 will involve a reference a trampoline generated for the occasion. */
2555 convert_tramp_reference_op (tree
*tp
, int *walk_subtrees
, void *data
)
2557 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2558 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
, *i
;
2559 tree t
= *tp
, decl
, target_context
, x
, builtin
;
2564 switch (TREE_CODE (t
))
2568 T.1 = &CHAIN->tramp;
2569 T.2 = __builtin_adjust_trampoline (T.1);
2570 T.3 = (func_type)T.2;
2573 decl
= TREE_OPERAND (t
, 0);
2574 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2577 /* Only need to process nested functions. */
2578 target_context
= decl_function_context (decl
);
2579 if (!target_context
)
2582 /* If the nested function doesn't use a static chain, then
2583 it doesn't need a trampoline. */
2584 if (!DECL_STATIC_CHAIN (decl
))
2587 /* If we don't want a trampoline, then don't build one. */
2588 if (TREE_NO_TRAMPOLINE (t
))
2591 /* Lookup the immediate parent of the callee, as that's where
2592 we need to insert the trampoline. */
2593 for (i
= info
; i
->context
!= target_context
; i
= i
->outer
)
2596 /* Decide whether to generate a descriptor or a trampoline. */
2597 descr
= FUNC_ADDR_BY_DESCRIPTOR (t
) && !flag_trampolines
;
2600 x
= lookup_descr_for_decl (i
, decl
, INSERT
);
2602 x
= lookup_tramp_for_decl (i
, decl
, INSERT
);
2604 /* Compute the address of the field holding the trampoline. */
2605 x
= get_frame_field (info
, target_context
, x
, &wi
->gsi
);
2607 x
= gsi_gimplify_val (info
, x
, &wi
->gsi
);
2609 /* Do machine-specific ugliness. Normally this will involve
2610 computing extra alignment, but it can really be anything. */
2612 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR
);
2614 builtin
= builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE
);
2615 call
= gimple_build_call (builtin
, 1, x
);
2616 x
= init_tmp_var_with_call (info
, &wi
->gsi
, call
);
2618 /* Cast back to the proper function type. */
2619 x
= build1 (NOP_EXPR
, TREE_TYPE (t
), x
);
2620 x
= init_tmp_var (info
, x
, &wi
->gsi
);
2626 if (!IS_TYPE_OR_DECL_P (t
))
2635 /* Called via walk_function+walk_gimple_stmt, rewrite all references
2636 to addresses of nested functions that require the use of
2637 trampolines. The rewrite will involve a reference a trampoline
2638 generated for the occasion. */
2641 convert_tramp_reference_stmt (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2642 struct walk_stmt_info
*wi
)
2644 struct nesting_info
*info
= (struct nesting_info
*) wi
->info
;
2645 gimple
*stmt
= gsi_stmt (*gsi
);
2647 switch (gimple_code (stmt
))
2651 /* Only walk call arguments, lest we generate trampolines for
2653 unsigned long i
, nargs
= gimple_call_num_args (stmt
);
2654 for (i
= 0; i
< nargs
; i
++)
2655 walk_tree (gimple_call_arg_ptr (stmt
, i
), convert_tramp_reference_op
,
2660 case GIMPLE_OMP_TEAMS
:
2661 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
2663 *handled_ops_p
= false;
2668 case GIMPLE_OMP_TARGET
:
2669 if (!is_gimple_omp_offloaded (stmt
))
2671 *handled_ops_p
= false;
2675 case GIMPLE_OMP_PARALLEL
:
2676 case GIMPLE_OMP_TASK
:
2679 tree save_local_var_chain
= info
->new_local_var_chain
;
2680 walk_gimple_op (stmt
, convert_tramp_reference_op
, wi
);
2681 info
->new_local_var_chain
= NULL
;
2682 char save_static_chain_added
= info
->static_chain_added
;
2683 info
->static_chain_added
= 0;
2684 walk_body (convert_tramp_reference_stmt
, convert_tramp_reference_op
,
2685 info
, gimple_omp_body_ptr (stmt
));
2686 if (info
->new_local_var_chain
)
2687 declare_vars (info
->new_local_var_chain
,
2688 gimple_seq_first_stmt (gimple_omp_body (stmt
)),
2690 for (int i
= 0; i
< 2; i
++)
2693 if ((info
->static_chain_added
& (1 << i
)) == 0)
2695 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2696 /* Don't add CHAIN.* or FRAME.* twice. */
2697 for (c
= gimple_omp_taskreg_clauses (stmt
);
2699 c
= OMP_CLAUSE_CHAIN (c
))
2700 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2701 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2702 && OMP_CLAUSE_DECL (c
) == decl
)
2704 if (c
== NULL
&& gimple_code (stmt
) != GIMPLE_OMP_TARGET
)
2706 c
= build_omp_clause (gimple_location (stmt
),
2707 i
? OMP_CLAUSE_FIRSTPRIVATE
2708 : OMP_CLAUSE_SHARED
);
2709 OMP_CLAUSE_DECL (c
) = decl
;
2710 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2711 gimple_omp_taskreg_set_clauses (stmt
, c
);
2715 c
= build_omp_clause (gimple_location (stmt
),
2717 OMP_CLAUSE_DECL (c
) = decl
;
2718 OMP_CLAUSE_SET_MAP_KIND (c
,
2719 i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2720 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2721 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2722 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2726 info
->new_local_var_chain
= save_local_var_chain
;
2727 info
->static_chain_added
|= save_static_chain_added
;
2732 *handled_ops_p
= false;
2736 *handled_ops_p
= true;
2742 /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2743 that reference nested functions to make sure that the static chain
2744 is set up properly for the call. */
2747 convert_gimple_call (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
2748 struct walk_stmt_info
*wi
)
2750 struct nesting_info
*const info
= (struct nesting_info
*) wi
->info
;
2751 tree decl
, target_context
;
2752 char save_static_chain_added
;
2754 gimple
*stmt
= gsi_stmt (*gsi
);
2756 switch (gimple_code (stmt
))
2759 if (gimple_call_chain (stmt
))
2761 decl
= gimple_call_fndecl (stmt
);
2764 target_context
= decl_function_context (decl
);
2765 if (target_context
&& DECL_STATIC_CHAIN (decl
))
2767 struct nesting_info
*i
= info
;
2768 while (i
&& i
->context
!= target_context
)
2770 /* If none of the outer contexts is the target context, this means
2771 that the function is called in a wrong context. */
2773 internal_error ("%s from %s called in %s",
2774 IDENTIFIER_POINTER (DECL_NAME (decl
)),
2775 IDENTIFIER_POINTER (DECL_NAME (target_context
)),
2776 IDENTIFIER_POINTER (DECL_NAME (info
->context
)));
2778 gimple_call_set_chain (as_a
<gcall
*> (stmt
),
2779 get_static_chain (info
, target_context
,
2781 info
->static_chain_added
|= (1 << (info
->context
!= target_context
));
2785 case GIMPLE_OMP_TEAMS
:
2786 if (!gimple_omp_teams_host (as_a
<gomp_teams
*> (stmt
)))
2788 walk_body (convert_gimple_call
, NULL
, info
,
2789 gimple_omp_body_ptr (stmt
));
2794 case GIMPLE_OMP_PARALLEL
:
2795 case GIMPLE_OMP_TASK
:
2796 save_static_chain_added
= info
->static_chain_added
;
2797 info
->static_chain_added
= 0;
2798 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2799 for (i
= 0; i
< 2; i
++)
2802 if ((info
->static_chain_added
& (1 << i
)) == 0)
2804 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2805 /* Don't add CHAIN.* or FRAME.* twice. */
2806 for (c
= gimple_omp_taskreg_clauses (stmt
);
2808 c
= OMP_CLAUSE_CHAIN (c
))
2809 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
2810 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
)
2811 && OMP_CLAUSE_DECL (c
) == decl
)
2815 c
= build_omp_clause (gimple_location (stmt
),
2816 i
? OMP_CLAUSE_FIRSTPRIVATE
2817 : OMP_CLAUSE_SHARED
);
2818 OMP_CLAUSE_DECL (c
) = decl
;
2819 OMP_CLAUSE_CHAIN (c
) = gimple_omp_taskreg_clauses (stmt
);
2820 gimple_omp_taskreg_set_clauses (stmt
, c
);
2823 info
->static_chain_added
|= save_static_chain_added
;
2826 case GIMPLE_OMP_TARGET
:
2827 if (!is_gimple_omp_offloaded (stmt
))
2829 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2832 save_static_chain_added
= info
->static_chain_added
;
2833 info
->static_chain_added
= 0;
2834 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2835 for (i
= 0; i
< 2; i
++)
2838 if ((info
->static_chain_added
& (1 << i
)) == 0)
2840 decl
= i
? get_chain_decl (info
) : info
->frame_decl
;
2841 /* Don't add CHAIN.* or FRAME.* twice. */
2842 for (c
= gimple_omp_target_clauses (stmt
);
2844 c
= OMP_CLAUSE_CHAIN (c
))
2845 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
2846 && OMP_CLAUSE_DECL (c
) == decl
)
2850 c
= build_omp_clause (gimple_location (stmt
), OMP_CLAUSE_MAP
);
2851 OMP_CLAUSE_DECL (c
) = decl
;
2852 OMP_CLAUSE_SET_MAP_KIND (c
, i
? GOMP_MAP_TO
: GOMP_MAP_TOFROM
);
2853 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
2854 OMP_CLAUSE_CHAIN (c
) = gimple_omp_target_clauses (stmt
);
2855 gimple_omp_target_set_clauses (as_a
<gomp_target
*> (stmt
),
2859 info
->static_chain_added
|= save_static_chain_added
;
2862 case GIMPLE_OMP_FOR
:
2863 walk_body (convert_gimple_call
, NULL
, info
,
2864 gimple_omp_for_pre_body_ptr (stmt
));
2866 case GIMPLE_OMP_SECTIONS
:
2867 case GIMPLE_OMP_SECTION
:
2868 case GIMPLE_OMP_SINGLE
:
2869 case GIMPLE_OMP_MASTER
:
2870 case GIMPLE_OMP_TASKGROUP
:
2871 case GIMPLE_OMP_ORDERED
:
2872 case GIMPLE_OMP_SCAN
:
2873 case GIMPLE_OMP_CRITICAL
:
2874 walk_body (convert_gimple_call
, NULL
, info
, gimple_omp_body_ptr (stmt
));
2878 /* Keep looking for other operands. */
2879 *handled_ops_p
= false;
2883 *handled_ops_p
= true;
2887 /* Walk the nesting tree starting with ROOT. Convert all trampolines and
2888 call expressions. At the same time, determine if a nested function
2889 actually uses its static chain; if not, remember that. */
2892 convert_all_function_calls (struct nesting_info
*root
)
2894 unsigned int chain_count
= 0, old_chain_count
, iter_count
;
2895 struct nesting_info
*n
;
2897 /* First, optimistically clear static_chain for all decls that haven't
2898 used the static chain already for variable access. But always create
2899 it if not optimizing. This makes it possible to reconstruct the static
2900 nesting tree at run time and thus to resolve up-level references from
2901 within the debugger. */
2902 FOR_EACH_NEST_INFO (n
, root
)
2906 tree decl
= n
->context
;
2910 (void) get_frame_type (n
);
2912 (void) get_chain_decl (n
);
2914 else if (!n
->outer
|| (!n
->chain_decl
&& !n
->chain_field
))
2916 DECL_STATIC_CHAIN (decl
) = 0;
2917 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2918 fprintf (dump_file
, "Guessing no static-chain for %s\n",
2919 lang_hooks
.decl_printable_name (decl
, 2));
2922 DECL_STATIC_CHAIN (decl
) = 1;
2923 chain_count
+= DECL_STATIC_CHAIN (decl
);
2926 FOR_EACH_NEST_INFO (n
, root
)
2929 tree decl
= n
->context
;
2930 tree alias
= cgraph_node::get (decl
)->thunk
.alias
;
2931 DECL_STATIC_CHAIN (decl
) = DECL_STATIC_CHAIN (alias
);
2934 /* Walk the functions and perform transformations. Note that these
2935 transformations can induce new uses of the static chain, which in turn
2936 require re-examining all users of the decl. */
2937 /* ??? It would make sense to try to use the call graph to speed this up,
2938 but the call graph hasn't really been built yet. Even if it did, we
2939 would still need to iterate in this loop since address-of references
2940 wouldn't show up in the callgraph anyway. */
2944 old_chain_count
= chain_count
;
2948 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2949 fputc ('\n', dump_file
);
2951 FOR_EACH_NEST_INFO (n
, root
)
2955 tree decl
= n
->context
;
2956 walk_function (convert_tramp_reference_stmt
,
2957 convert_tramp_reference_op
, n
);
2958 walk_function (convert_gimple_call
, NULL
, n
);
2959 chain_count
+= DECL_STATIC_CHAIN (decl
);
2962 FOR_EACH_NEST_INFO (n
, root
)
2965 tree decl
= n
->context
;
2966 tree alias
= cgraph_node::get (decl
)->thunk
.alias
;
2967 DECL_STATIC_CHAIN (decl
) = DECL_STATIC_CHAIN (alias
);
2970 while (chain_count
!= old_chain_count
);
2972 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2973 fprintf (dump_file
, "convert_all_function_calls iterations: %u\n\n",
2977 struct nesting_copy_body_data
2980 struct nesting_info
*root
;
2983 /* A helper subroutine for debug_var_chain type remapping. */
2986 nesting_copy_decl (tree decl
, copy_body_data
*id
)
2988 struct nesting_copy_body_data
*nid
= (struct nesting_copy_body_data
*) id
;
2989 tree
*slot
= nid
->root
->var_map
->get (decl
);
2992 return (tree
) *slot
;
2994 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (decl
))
2996 tree new_decl
= copy_decl_no_change (decl
, id
);
2997 DECL_ORIGINAL_TYPE (new_decl
)
2998 = remap_type (DECL_ORIGINAL_TYPE (decl
), id
);
3003 || TREE_CODE (decl
) == PARM_DECL
3004 || TREE_CODE (decl
) == RESULT_DECL
)
3007 return copy_decl_no_change (decl
, id
);
3010 /* A helper function for remap_vla_decls. See if *TP contains
3011 some remapped variables. */
3014 contains_remapped_vars (tree
*tp
, int *walk_subtrees
, void *data
)
3016 struct nesting_info
*root
= (struct nesting_info
*) data
;
3022 tree
*slot
= root
->var_map
->get (t
);
3030 /* Remap VLA decls in BLOCK and subblocks if remapped variables are
3034 remap_vla_decls (tree block
, struct nesting_info
*root
)
3036 tree var
, subblock
, val
, type
;
3037 struct nesting_copy_body_data id
;
3039 for (subblock
= BLOCK_SUBBLOCKS (block
);
3041 subblock
= BLOCK_CHAIN (subblock
))
3042 remap_vla_decls (subblock
, root
);
3044 for (var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
3045 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
3047 val
= DECL_VALUE_EXPR (var
);
3048 type
= TREE_TYPE (var
);
3050 if (!(TREE_CODE (val
) == INDIRECT_REF
3051 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
3052 && variably_modified_type_p (type
, NULL
)))
3055 if (root
->var_map
->get (TREE_OPERAND (val
, 0))
3056 || walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
3060 if (var
== NULL_TREE
)
3063 memset (&id
, 0, sizeof (id
));
3064 id
.cb
.copy_decl
= nesting_copy_decl
;
3065 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
3068 for (; var
; var
= DECL_CHAIN (var
))
3069 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
3071 struct nesting_info
*i
;
3074 val
= DECL_VALUE_EXPR (var
);
3075 type
= TREE_TYPE (var
);
3077 if (!(TREE_CODE (val
) == INDIRECT_REF
3078 && TREE_CODE (TREE_OPERAND (val
, 0)) == VAR_DECL
3079 && variably_modified_type_p (type
, NULL
)))
3082 tree
*slot
= root
->var_map
->get (TREE_OPERAND (val
, 0));
3083 if (!slot
&& !walk_tree (&type
, contains_remapped_vars
, root
, NULL
))
3086 context
= decl_function_context (var
);
3087 for (i
= root
; i
; i
= i
->outer
)
3088 if (i
->context
== context
)
3094 /* Fully expand value expressions. This avoids having debug variables
3095 only referenced from them and that can be swept during GC. */
3098 tree t
= (tree
) *slot
;
3099 gcc_assert (DECL_P (t
) && DECL_HAS_VALUE_EXPR_P (t
));
3100 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
), DECL_VALUE_EXPR (t
));
3103 id
.cb
.src_fn
= i
->context
;
3104 id
.cb
.dst_fn
= i
->context
;
3105 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
3107 TREE_TYPE (var
) = newt
= remap_type (type
, &id
.cb
);
3108 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
3110 newt
= TREE_TYPE (newt
);
3111 type
= TREE_TYPE (type
);
3113 if (TYPE_NAME (newt
)
3114 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
3115 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
3117 && TYPE_NAME (newt
) == TYPE_NAME (type
))
3118 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
3120 walk_tree (&val
, copy_tree_body_r
, &id
.cb
, NULL
);
3121 if (val
!= DECL_VALUE_EXPR (var
))
3122 SET_DECL_VALUE_EXPR (var
, val
);
3125 delete id
.cb
.decl_map
;
3128 /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3132 fixup_vla_decls (tree block
)
3134 for (tree var
= BLOCK_VARS (block
); var
; var
= DECL_CHAIN (var
))
3135 if (VAR_P (var
) && DECL_HAS_VALUE_EXPR_P (var
))
3137 tree val
= DECL_VALUE_EXPR (var
);
3139 if (!(TREE_CODE (val
) == INDIRECT_REF
3140 && VAR_P (TREE_OPERAND (val
, 0))
3141 && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val
, 0))))
3144 /* Fully expand value expressions. This avoids having debug variables
3145 only referenced from them and that can be swept during GC. */
3146 val
= build1 (INDIRECT_REF
, TREE_TYPE (val
),
3147 DECL_VALUE_EXPR (TREE_OPERAND (val
, 0)));
3148 SET_DECL_VALUE_EXPR (var
, val
);
3151 for (tree sub
= BLOCK_SUBBLOCKS (block
); sub
; sub
= BLOCK_CHAIN (sub
))
3152 fixup_vla_decls (sub
);
3155 /* Fold the MEM_REF *E. */
3157 fold_mem_refs (tree
*const &e
, void *data ATTRIBUTE_UNUSED
)
3159 tree
*ref_p
= CONST_CAST2 (tree
*, const tree
*, (const tree
*)e
);
3160 *ref_p
= fold (*ref_p
);
3164 /* Given DECL, a nested function, build an initialization call for FIELD,
3165 the trampoline or descriptor for DECL, using FUNC as the function. */
3168 build_init_call_stmt (struct nesting_info
*info
, tree decl
, tree field
,
3171 tree arg1
, arg2
, arg3
, x
;
3173 gcc_assert (DECL_STATIC_CHAIN (decl
));
3174 arg3
= build_addr (info
->frame_decl
);
3176 arg2
= build_addr (decl
);
3178 x
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3179 info
->frame_decl
, field
, NULL_TREE
);
3180 arg1
= build_addr (x
);
3182 return gimple_build_call (func
, 3, arg1
, arg2
, arg3
);
3185 /* Do "everything else" to clean up or complete state collected by the various
3186 walking passes -- create a field to hold the frame base address, lay out the
3187 types and decls, generate code to initialize the frame decl, store critical
3188 expressions in the struct function for rtl to find. */
3191 finalize_nesting_tree_1 (struct nesting_info
*root
)
3193 gimple_seq stmt_list
= NULL
;
3195 tree context
= root
->context
;
3196 struct function
*sf
;
3201 /* If we created a non-local frame type or decl, we need to lay them
3202 out at this time. */
3203 if (root
->frame_type
)
3205 /* Debugging information needs to compute the frame base address of the
3206 parent frame out of the static chain from the nested frame.
3208 The static chain is the address of the FRAME record, so one could
3209 imagine it would be possible to compute the frame base address just
3210 adding a constant offset to this address. Unfortunately, this is not
3211 possible: if the FRAME object has alignment constraints that are
3212 stronger than the stack, then the offset between the frame base and
3213 the FRAME object will be dynamic.
3215 What we do instead is to append a field to the FRAME object that holds
3216 the frame base address: then debug info just has to fetch this
3219 /* Debugging information will refer to the CFA as the frame base
3220 address: we will do the same here. */
3221 const tree frame_addr_fndecl
3222 = builtin_decl_explicit (BUILT_IN_DWARF_CFA
);
3224 /* Create a field in the FRAME record to hold the frame base address for
3225 this stack frame. Since it will be used only by the debugger, put it
3226 at the end of the record in order not to shift all other offsets. */
3227 tree fb_decl
= make_node (FIELD_DECL
);
3229 DECL_NAME (fb_decl
) = get_identifier ("FRAME_BASE.PARENT");
3230 TREE_TYPE (fb_decl
) = ptr_type_node
;
3231 TREE_ADDRESSABLE (fb_decl
) = 1;
3232 DECL_CONTEXT (fb_decl
) = root
->frame_type
;
3233 TYPE_FIELDS (root
->frame_type
) = chainon (TYPE_FIELDS (root
->frame_type
),
3236 /* In some cases the frame type will trigger the -Wpadded warning.
3237 This is not helpful; suppress it. */
3238 int save_warn_padded
= warn_padded
;
3240 layout_type (root
->frame_type
);
3241 warn_padded
= save_warn_padded
;
3242 layout_decl (root
->frame_decl
, 0);
3244 /* Initialize the frame base address field. If the builtin we need is
3245 not available, set it to NULL so that debugging information does not
3247 tree fb_ref
= build3 (COMPONENT_REF
, TREE_TYPE (fb_decl
),
3248 root
->frame_decl
, fb_decl
, NULL_TREE
);
3251 if (frame_addr_fndecl
!= NULL_TREE
)
3253 gcall
*fb_gimple
= gimple_build_call (frame_addr_fndecl
, 1,
3255 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3257 fb_tmp
= init_tmp_var_with_call (root
, &gsi
, fb_gimple
);
3260 fb_tmp
= build_int_cst (TREE_TYPE (fb_ref
), 0);
3261 gimple_seq_add_stmt (&stmt_list
,
3262 gimple_build_assign (fb_ref
, fb_tmp
));
3264 declare_vars (root
->frame_decl
,
3265 gimple_seq_first_stmt (gimple_body (context
)), true);
3268 /* If any parameters were referenced non-locally, then we need to insert
3269 a copy or a pointer. */
3270 if (root
->any_parm_remapped
)
3273 for (p
= DECL_ARGUMENTS (context
); p
; p
= DECL_CHAIN (p
))
3277 field
= lookup_field_for_decl (root
, p
, NO_INSERT
);
3281 if (use_pointer_in_frame (p
))
3286 /* If the assignment is from a non-register the stmt is
3287 not valid gimple. Make it so by using a temporary instead. */
3288 if (!is_gimple_reg (x
)
3289 && is_gimple_reg_type (TREE_TYPE (x
)))
3291 gimple_stmt_iterator gsi
= gsi_last (stmt_list
);
3292 x
= init_tmp_var (root
, x
, &gsi
);
3295 y
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3296 root
->frame_decl
, field
, NULL_TREE
);
3297 stmt
= gimple_build_assign (y
, x
);
3298 gimple_seq_add_stmt (&stmt_list
, stmt
);
3302 /* If a chain_field was created, then it needs to be initialized
3304 if (root
->chain_field
)
3306 tree x
= build3 (COMPONENT_REF
, TREE_TYPE (root
->chain_field
),
3307 root
->frame_decl
, root
->chain_field
, NULL_TREE
);
3308 stmt
= gimple_build_assign (x
, get_chain_decl (root
));
3309 gimple_seq_add_stmt (&stmt_list
, stmt
);
3312 /* If trampolines were created, then we need to initialize them. */
3313 if (root
->any_tramp_created
)
3315 struct nesting_info
*i
;
3316 for (i
= root
->inner
; i
; i
= i
->next
)
3320 field
= lookup_tramp_for_decl (root
, i
->context
, NO_INSERT
);
3324 x
= builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE
);
3325 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3326 gimple_seq_add_stmt (&stmt_list
, stmt
);
3330 /* If descriptors were created, then we need to initialize them. */
3331 if (root
->any_descr_created
)
3333 struct nesting_info
*i
;
3334 for (i
= root
->inner
; i
; i
= i
->next
)
3338 field
= lookup_descr_for_decl (root
, i
->context
, NO_INSERT
);
3342 x
= builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR
);
3343 stmt
= build_init_call_stmt (root
, i
->context
, field
, x
);
3344 gimple_seq_add_stmt (&stmt_list
, stmt
);
3348 /* If we created initialization statements, insert them. */
3352 annotate_all_with_location (stmt_list
, DECL_SOURCE_LOCATION (context
));
3353 bind
= gimple_seq_first_stmt_as_a_bind (gimple_body (context
));
3354 gimple_seq_add_seq (&stmt_list
, gimple_bind_body (bind
));
3355 gimple_bind_set_body (bind
, stmt_list
);
3358 /* If a chain_decl was created, then it needs to be registered with
3359 struct function so that it gets initialized from the static chain
3360 register at the beginning of the function. */
3361 sf
= DECL_STRUCT_FUNCTION (root
->context
);
3362 sf
->static_chain_decl
= root
->chain_decl
;
3364 /* Similarly for the non-local goto save area. */
3365 if (root
->nl_goto_field
)
3367 sf
->nonlocal_goto_save_area
3368 = get_frame_field (root
, context
, root
->nl_goto_field
, NULL
);
3369 sf
->has_nonlocal_label
= 1;
3372 /* Make sure all new local variables get inserted into the
3373 proper BIND_EXPR. */
3374 if (root
->new_local_var_chain
)
3375 declare_vars (root
->new_local_var_chain
,
3376 gimple_seq_first_stmt (gimple_body (root
->context
)),
3379 if (root
->debug_var_chain
)
3384 remap_vla_decls (DECL_INITIAL (root
->context
), root
);
3386 for (debug_var
= root
->debug_var_chain
; debug_var
;
3387 debug_var
= DECL_CHAIN (debug_var
))
3388 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3391 /* If there are any debug decls with variable length types,
3392 remap those types using other debug_var_chain variables. */
3395 struct nesting_copy_body_data id
;
3397 memset (&id
, 0, sizeof (id
));
3398 id
.cb
.copy_decl
= nesting_copy_decl
;
3399 id
.cb
.decl_map
= new hash_map
<tree
, tree
>;
3402 for (; debug_var
; debug_var
= DECL_CHAIN (debug_var
))
3403 if (variably_modified_type_p (TREE_TYPE (debug_var
), NULL
))
3405 tree type
= TREE_TYPE (debug_var
);
3406 tree newt
, t
= type
;
3407 struct nesting_info
*i
;
3409 for (i
= root
; i
; i
= i
->outer
)
3410 if (variably_modified_type_p (type
, i
->context
))
3416 id
.cb
.src_fn
= i
->context
;
3417 id
.cb
.dst_fn
= i
->context
;
3418 id
.cb
.src_cfun
= DECL_STRUCT_FUNCTION (root
->context
);
3420 TREE_TYPE (debug_var
) = newt
= remap_type (type
, &id
.cb
);
3421 while (POINTER_TYPE_P (newt
) && !TYPE_NAME (newt
))
3423 newt
= TREE_TYPE (newt
);
3426 if (TYPE_NAME (newt
)
3427 && TREE_CODE (TYPE_NAME (newt
)) == TYPE_DECL
3428 && DECL_ORIGINAL_TYPE (TYPE_NAME (newt
))
3430 && TYPE_NAME (newt
) == TYPE_NAME (t
))
3431 TYPE_NAME (newt
) = remap_decl (TYPE_NAME (newt
), &id
.cb
);
3434 delete id
.cb
.decl_map
;
3437 scope
= gimple_seq_first_stmt_as_a_bind (gimple_body (root
->context
));
3438 if (gimple_bind_block (scope
))
3439 declare_vars (root
->debug_var_chain
, scope
, true);
3441 BLOCK_VARS (DECL_INITIAL (root
->context
))
3442 = chainon (BLOCK_VARS (DECL_INITIAL (root
->context
)),
3443 root
->debug_var_chain
);
3446 fixup_vla_decls (DECL_INITIAL (root
->context
));
3448 /* Fold the rewritten MEM_REF trees. */
3449 root
->mem_refs
->traverse
<void *, fold_mem_refs
> (NULL
);
3451 /* Dump the translated tree function. */
3454 fputs ("\n\n", dump_file
);
3455 dump_function_to_file (root
->context
, dump_file
, dump_flags
);
3460 finalize_nesting_tree (struct nesting_info
*root
)
3462 struct nesting_info
*n
;
3463 FOR_EACH_NEST_INFO (n
, root
)
3464 finalize_nesting_tree_1 (n
);
3467 /* Unnest the nodes and pass them to cgraph. */
3470 unnest_nesting_tree_1 (struct nesting_info
*root
)
3472 struct cgraph_node
*node
= cgraph_node::get (root
->context
);
3474 /* For nested functions update the cgraph to reflect unnesting.
3475 We also delay finalizing of these functions up to this point. */
3480 cgraph_node::finalize_function (root
->context
, true);
3485 unnest_nesting_tree (struct nesting_info
*root
)
3487 struct nesting_info
*n
;
3488 FOR_EACH_NEST_INFO (n
, root
)
3489 unnest_nesting_tree_1 (n
);
3492 /* Free the data structures allocated during this pass. */
3495 free_nesting_tree (struct nesting_info
*root
)
3497 struct nesting_info
*node
, *next
;
3499 node
= iter_nestinfo_start (root
);
3502 next
= iter_nestinfo_next (node
);
3503 delete node
->var_map
;
3504 delete node
->field_map
;
3505 delete node
->mem_refs
;
3512 /* Gimplify a function and all its nested functions. */
3514 gimplify_all_functions (struct cgraph_node
*root
)
3516 struct cgraph_node
*iter
;
3517 if (!gimple_body (root
->decl
))
3518 gimplify_function_tree (root
->decl
);
3519 for (iter
= root
->nested
; iter
; iter
= iter
->next_nested
)
3520 if (!iter
->thunk
.thunk_p
)
3521 gimplify_all_functions (iter
);
3524 /* Main entry point for this pass. Process FNDECL and all of its nested
3525 subroutines and turn them into something less tightly bound. */
3528 lower_nested_functions (tree fndecl
)
3530 struct cgraph_node
*cgn
;
3531 struct nesting_info
*root
;
3533 /* If there are no nested functions, there's nothing to do. */
3534 cgn
= cgraph_node::get (fndecl
);
3538 gimplify_all_functions (cgn
);
3540 set_dump_file (dump_begin (TDI_nested
, &dump_flags
));
3542 fprintf (dump_file
, "\n;; Function %s\n\n",
3543 lang_hooks
.decl_printable_name (fndecl
, 2));
3545 bitmap_obstack_initialize (&nesting_info_bitmap_obstack
);
3546 root
= create_nesting_tree (cgn
);
3548 walk_all_functions (convert_nonlocal_reference_stmt
,
3549 convert_nonlocal_reference_op
,
3551 walk_all_functions (convert_local_reference_stmt
,
3552 convert_local_reference_op
,
3554 walk_all_functions (convert_nl_goto_reference
, NULL
, root
);
3555 walk_all_functions (convert_nl_goto_receiver
, NULL
, root
);
3557 convert_all_function_calls (root
);
3558 finalize_nesting_tree (root
);
3559 unnest_nesting_tree (root
);
3561 free_nesting_tree (root
);
3562 bitmap_obstack_release (&nesting_info_bitmap_obstack
);
3566 dump_end (TDI_nested
, dump_file
);
3567 set_dump_file (NULL
);
3571 #include "gt-tree-nested.h"