Line data Source code
1 : /* Nested function decomposition for GIMPLE.
2 : Copyright (C) 2004-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3, or (at your option)
9 : any later version.
10 :
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.
15 :
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/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "target.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "memmodel.h"
29 : #include "tm_p.h"
30 : #include "stringpool.h"
31 : #include "cgraph.h"
32 : #include "fold-const.h"
33 : #include "stor-layout.h"
34 : #include "dumpfile.h"
35 : #include "tree-inline.h"
36 : #include "gimplify.h"
37 : #include "gimple-iterator.h"
38 : #include "gimple-walk.h"
39 : #include "tree-cfg.h"
40 : #include "explow.h"
41 : #include "langhooks.h"
42 : #include "gimple-low.h"
43 : #include "gomp-constants.h"
44 : #include "diagnostic.h"
45 : #include "alloc-pool.h"
46 : #include "tree-nested.h"
47 : #include "symbol-summary.h"
48 : #include "symtab-thunks.h"
49 : #include "attribs.h"
50 :
51 : /* Summary of nested functions. */
52 : static function_summary <nested_function_info *>
53 : *nested_function_sum = NULL;
54 :
55 : /* Return nested_function_info, if available. */
56 : nested_function_info *
57 152212942 : nested_function_info::get (cgraph_node *node)
58 : {
59 152212942 : if (!nested_function_sum)
60 : return NULL;
61 700642 : return nested_function_sum->get (node);
62 : }
63 :
64 : /* Return nested_function_info possibly creating new one. */
65 : nested_function_info *
66 51288 : nested_function_info::get_create (cgraph_node *node)
67 : {
68 51288 : if (!nested_function_sum)
69 : {
70 17842 : nested_function_sum = new function_summary <nested_function_info *>
71 8921 : (symtab);
72 8921 : nested_function_sum->disable_insertion_hook ();
73 : }
74 51288 : return nested_function_sum->get_create (node);
75 : }
76 :
77 : /* cgraph_node is no longer nested function; update cgraph accordingly. */
78 : void
79 25393 : unnest_function (cgraph_node *node)
80 : {
81 25393 : nested_function_info *info = nested_function_info::get (node);
82 25393 : cgraph_node **node2 = &nested_function_info::get
83 25393 : (nested_function_origin (node))->nested;
84 :
85 25393 : gcc_checking_assert (info->origin);
86 131727 : while (*node2 != node)
87 106334 : node2 = &nested_function_info::get (*node2)->next_nested;
88 25393 : *node2 = info->next_nested;
89 25393 : info->next_nested = NULL;
90 25393 : info->origin = NULL;
91 25393 : nested_function_sum->remove (node);
92 25393 : }
93 :
94 : /* Destructor: unlink function from nested function lists. */
95 35143 : nested_function_info::~nested_function_info ()
96 : {
97 35143 : cgraph_node *next;
98 35247 : for (cgraph_node *n = nested; n; n = next)
99 : {
100 104 : nested_function_info *info = nested_function_info::get (n);
101 104 : next = info->next_nested;
102 104 : info->origin = NULL;
103 104 : info->next_nested = NULL;
104 : }
105 35143 : nested = NULL;
106 35143 : if (origin)
107 : {
108 147 : cgraph_node **node2
109 147 : = &nested_function_info::get (origin)->nested;
110 :
111 147 : nested_function_info *info;
112 147 : while ((info = nested_function_info::get (*node2)) != this && info)
113 0 : node2 = &info->next_nested;
114 147 : *node2 = next_nested;
115 : }
116 35143 : }
117 :
118 : /* Free nested function info summaries. */
119 : void
120 512305 : nested_function_info::release ()
121 : {
122 512305 : if (nested_function_sum)
123 8917 : delete (nested_function_sum);
124 512305 : nested_function_sum = NULL;
125 512305 : }
126 :
127 : /* If NODE is nested function, record it. */
128 : void
129 112818789 : maybe_record_nested_function (cgraph_node *node)
130 : {
131 : /* All nested functions gets lowered during the construction of symtab. */
132 112818789 : if (symtab->state > CONSTRUCTION)
133 : return;
134 112703871 : if (DECL_CONTEXT (node->decl)
135 112703871 : && TREE_CODE (DECL_CONTEXT (node->decl)) == FUNCTION_DECL)
136 : {
137 25644 : cgraph_node *origin = cgraph_node::get_create (DECL_CONTEXT (node->decl));
138 25644 : nested_function_info *info = nested_function_info::get_create (node);
139 25644 : nested_function_info *origin_info
140 25644 : = nested_function_info::get_create (origin);
141 :
142 25644 : info->origin = origin;
143 25644 : info->next_nested = origin_info->nested;
144 25644 : origin_info->nested = node;
145 : }
146 : }
147 :
148 : /* The object of this pass is to lower the representation of a set of nested
149 : functions in order to expose all of the gory details of the various
150 : nonlocal references. We want to do this sooner rather than later, in
151 : order to give us more freedom in emitting all of the functions in question.
152 :
153 : Back in olden times, when gcc was young, we developed an insanely
154 : complicated scheme whereby variables which were referenced nonlocally
155 : were forced to live in the stack of the declaring function, and then
156 : the nested functions magically discovered where these variables were
157 : placed. In order for this scheme to function properly, it required
158 : that the outer function be partially expanded, then we switch to
159 : compiling the inner function, and once done with those we switch back
160 : to compiling the outer function. Such delicate ordering requirements
161 : makes it difficult to do whole translation unit optimizations
162 : involving such functions.
163 :
164 : The implementation here is much more direct. Everything that can be
165 : referenced by an inner function is a member of an explicitly created
166 : structure herein called the "nonlocal frame struct". The incoming
167 : static chain for a nested function is a pointer to this struct in
168 : the parent. In this way, we settle on known offsets from a known
169 : base, and so are decoupled from the logic that places objects in the
170 : function's stack frame. More importantly, we don't have to wait for
171 : that to happen -- since the compilation of the inner function is no
172 : longer tied to a real stack frame, the nonlocal frame struct can be
173 : allocated anywhere. Which means that the outer function is now
174 : inlinable.
175 :
176 : Theory of operation here is very simple. Iterate over all the
177 : statements in all the functions (depth first) several times,
178 : allocating structures and fields on demand. In general we want to
179 : examine inner functions first, so that we can avoid making changes
180 : to outer functions which are unnecessary.
181 :
182 : The order of the passes matters a bit, in that later passes will be
183 : skipped if it is discovered that the functions don't actually interact
184 : at all. That is, they're nested in the lexical sense but could have
185 : been written as independent functions without change. */
186 :
187 :
188 : struct nesting_info
189 : {
190 : struct nesting_info *outer;
191 : struct nesting_info *inner;
192 : struct nesting_info *next;
193 :
194 : hash_map<tree, tree> *field_map;
195 : hash_map<tree, tree> *var_map;
196 : hash_set<tree *> *mem_refs;
197 : bitmap suppress_expansion;
198 :
199 : tree context;
200 : tree new_local_var_chain;
201 : tree debug_var_chain;
202 : tree frame_type;
203 : tree frame_decl;
204 : tree chain_field;
205 : tree chain_decl;
206 : tree nl_goto_field;
207 :
208 : bool thunk_p;
209 : bool any_parm_remapped;
210 : bool any_tramp_created;
211 : bool any_descr_created;
212 : char static_chain_added;
213 : };
214 :
215 :
216 : /* Iterate over the nesting tree, starting with ROOT, depth first. */
217 :
218 : static inline struct nesting_info *
219 84827 : iter_nestinfo_start (struct nesting_info *root)
220 : {
221 383470 : while (root->inner)
222 : root = root->inner;
223 : return root;
224 : }
225 :
226 : static inline struct nesting_info *
227 383470 : iter_nestinfo_next (struct nesting_info *node)
228 : {
229 383470 : if (node->next)
230 : return iter_nestinfo_start (node->next);
231 210974 : return node->outer;
232 : }
233 :
234 : #define FOR_EACH_NEST_INFO(I, ROOT) \
235 : for ((I) = iter_nestinfo_start (ROOT); (I); (I) = iter_nestinfo_next (I))
236 :
237 : /* Obstack used for the bitmaps in the struct above. */
238 : static struct bitmap_obstack nesting_info_bitmap_obstack;
239 :
240 :
241 : /* We're working in so many different function contexts simultaneously,
242 : that create_tmp_var is dangerous. Prevent mishap. */
243 : #define create_tmp_var cant_use_create_tmp_var_here_dummy
244 :
245 : /* Like create_tmp_var, except record the variable for registration at
246 : the given nesting level. */
247 :
248 : static tree
249 19202 : create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
250 : {
251 19202 : tree tmp_var;
252 :
253 : /* If the type is of variable size or a type which must be created by the
254 : frontend, something is wrong. Note that we explicitly allow
255 : incomplete types here, since we create them ourselves here. */
256 19202 : gcc_assert (!TREE_ADDRESSABLE (type));
257 19202 : gcc_assert (!TYPE_SIZE_UNIT (type)
258 : || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
259 :
260 19202 : tmp_var = create_tmp_var_raw (type, prefix);
261 19202 : DECL_CONTEXT (tmp_var) = info->context;
262 19202 : DECL_CHAIN (tmp_var) = info->new_local_var_chain;
263 19202 : DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
264 :
265 19202 : info->new_local_var_chain = tmp_var;
266 :
267 19202 : return tmp_var;
268 : }
269 :
270 : /* Like build_simple_mem_ref, but set TREE_THIS_NOTRAP on the result. */
271 :
272 : static tree
273 21586 : build_simple_mem_ref_notrap (tree ptr)
274 : {
275 21586 : tree t = build_simple_mem_ref (ptr);
276 21586 : TREE_THIS_NOTRAP (t) = 1;
277 21586 : return t;
278 : }
279 :
280 : /* Take the address of EXP to be used within function CONTEXT.
281 : Mark it for addressability as necessary. */
282 :
283 : tree
284 56126 : build_addr (tree exp)
285 : {
286 56126 : mark_addressable (exp);
287 56126 : return build_fold_addr_expr (exp);
288 : }
289 :
290 : /* Insert FIELD into TYPE, sorted by alignment requirements. */
291 :
292 : void
293 174102 : insert_field_into_struct (tree type, tree field)
294 : {
295 174102 : tree *p;
296 :
297 174102 : DECL_CONTEXT (field) = type;
298 :
299 214758 : for (p = &TYPE_FIELDS (type); *p ; p = &DECL_CHAIN (*p))
300 155295 : if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
301 : break;
302 :
303 174102 : DECL_CHAIN (field) = *p;
304 174102 : *p = field;
305 :
306 : /* Set correct alignment for frame struct type. */
307 174102 : if (TYPE_ALIGN (type) < DECL_ALIGN (field))
308 57300 : SET_TYPE_ALIGN (type, DECL_ALIGN (field));
309 174102 : }
310 :
311 : /* Build or return the RECORD_TYPE that describes the frame state that is
312 : shared between INFO->CONTEXT and its nested functions. This record will
313 : not be complete until finalize_nesting_tree; up until that point we'll
314 : be adding fields as necessary.
315 :
316 : We also build the DECL that represents this frame in the function. */
317 :
318 : static tree
319 35097 : get_frame_type (struct nesting_info *info)
320 : {
321 35097 : tree type = info->frame_type;
322 35097 : if (!type)
323 : {
324 3469 : char *name;
325 :
326 3469 : type = make_node (RECORD_TYPE);
327 :
328 10407 : name = concat ("FRAME.",
329 3469 : IDENTIFIER_POINTER (DECL_NAME (info->context)),
330 : NULL);
331 3469 : TYPE_NAME (type) = get_identifier (name);
332 3469 : free (name);
333 :
334 3469 : info->frame_type = type;
335 :
336 : /* Do not put info->frame_decl on info->new_local_var_chain,
337 : so that we can declare it in the lexical blocks, which
338 : makes sure virtual regs that end up appearing in its RTL
339 : expression get substituted in instantiate_virtual_regs. */
340 3469 : info->frame_decl = create_tmp_var_raw (type, "FRAME");
341 3469 : DECL_CONTEXT (info->frame_decl) = info->context;
342 3469 : DECL_NONLOCAL_FRAME (info->frame_decl) = 1;
343 3469 : DECL_SEEN_IN_BIND_EXPR_P (info->frame_decl) = 1;
344 :
345 : /* ??? Always make it addressable for now, since it is meant to
346 : be pointed to by the static chain pointer. This pessimizes
347 : when it turns out that no static chains are needed because
348 : the nested functions referencing non-local variables are not
349 : reachable, but the true pessimization is to create the non-
350 : local frame structure in the first place. */
351 3469 : TREE_ADDRESSABLE (info->frame_decl) = 1;
352 : }
353 :
354 35097 : return type;
355 : }
356 :
357 : /* Return true if DECL should be referenced by pointer in the non-local frame
358 : structure. */
359 :
360 : static bool
361 4867291 : use_pointer_in_frame (tree decl)
362 : {
363 4867291 : if (TREE_CODE (decl) == PARM_DECL)
364 : {
365 : /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable-
366 : sized DECLs, and inefficient to copy large aggregates. Don't bother
367 : moving anything but scalar parameters. */
368 197645 : return AGGREGATE_TYPE_P (TREE_TYPE (decl));
369 : }
370 : else
371 : {
372 : /* Variable-sized DECLs can only come from OMP clauses at this point
373 : since the gimplifier has already turned the regular variables into
374 : pointers. Do the same as the gimplifier. */
375 4669646 : return !DECL_SIZE (decl) || TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST;
376 : }
377 : }
378 :
379 : /* Given DECL, a non-locally accessed variable, find or create a field
380 : in the non-local frame structure for the given nesting context. */
381 :
382 : static tree
383 4863744 : lookup_field_for_decl (struct nesting_info *info, tree decl,
384 : enum insert_option insert)
385 : {
386 4863744 : gcc_checking_assert (decl_function_context (decl) == info->context);
387 :
388 4863744 : if (insert == NO_INSERT)
389 : {
390 4842878 : tree *slot = info->field_map->get (decl);
391 4842878 : return slot ? *slot : NULL_TREE;
392 : }
393 :
394 20866 : tree *slot = &info->field_map->get_or_insert (decl);
395 20866 : if (!*slot)
396 : {
397 3367 : tree type = get_frame_type (info);
398 3367 : tree field = make_node (FIELD_DECL);
399 3367 : DECL_NAME (field) = DECL_NAME (decl);
400 :
401 3367 : if (use_pointer_in_frame (decl))
402 : {
403 27 : TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
404 27 : SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
405 27 : DECL_NONADDRESSABLE_P (field) = 1;
406 : }
407 : else
408 : {
409 3340 : TREE_TYPE (field) = TREE_TYPE (decl);
410 3340 : DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
411 3340 : SET_DECL_ALIGN (field, DECL_ALIGN (decl));
412 3340 : DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
413 3340 : DECL_IGNORED_P (field) = DECL_IGNORED_P (decl);
414 3340 : DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
415 3340 : TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
416 3340 : copy_warning (field, decl);
417 :
418 : /* Declare the transformation and adjust the original DECL. For a
419 : variable or for a parameter when not optimizing, we make it point
420 : to the field in the frame directly. For a parameter, we don't do
421 : it when optimizing because the variable tracking pass will already
422 : do the job, */
423 3340 : if (VAR_P (decl) || !optimize)
424 : {
425 3148 : tree x
426 3148 : = build3 (COMPONENT_REF, TREE_TYPE (field), info->frame_decl,
427 : field, NULL_TREE);
428 :
429 : /* If the next declaration is a PARM_DECL pointing to the DECL,
430 : we need to adjust its VALUE_EXPR directly, since chains of
431 : VALUE_EXPRs run afoul of garbage collection. This occurs
432 : in Ada for Out parameters that aren't copied in. */
433 3148 : tree next = DECL_CHAIN (decl);
434 3148 : if (next
435 2647 : && TREE_CODE (next) == PARM_DECL
436 9 : && DECL_HAS_VALUE_EXPR_P (next)
437 3152 : && DECL_VALUE_EXPR (next) == decl)
438 0 : SET_DECL_VALUE_EXPR (next, x);
439 :
440 3148 : SET_DECL_VALUE_EXPR (decl, x);
441 3148 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
442 : }
443 : }
444 :
445 3367 : insert_field_into_struct (type, field);
446 3367 : *slot = field;
447 :
448 3367 : if (TREE_CODE (decl) == PARM_DECL)
449 265 : info->any_parm_remapped = true;
450 : }
451 :
452 20866 : return *slot;
453 : }
454 :
455 : /* Build or return the variable that holds the static chain within
456 : INFO->CONTEXT. This variable may only be used within INFO->CONTEXT. */
457 :
458 : static tree
459 28042 : get_chain_decl (struct nesting_info *info)
460 : {
461 28042 : tree decl = info->chain_decl;
462 :
463 28042 : if (!decl)
464 : {
465 6986 : tree type;
466 :
467 6986 : type = get_frame_type (info->outer);
468 6986 : type = build_pointer_type (type);
469 :
470 : /* Note that this variable is *not* entered into any BIND_EXPR;
471 : the construction of this variable is handled specially in
472 : expand_function_start and initialize_inlined_parameters.
473 : Note also that it's represented as a parameter. This is more
474 : close to the truth, since the initial value does come from
475 : the caller. */
476 6986 : decl = build_decl (DECL_SOURCE_LOCATION (info->context),
477 : PARM_DECL, create_tmp_var_name ("CHAIN"), type);
478 6986 : DECL_ARTIFICIAL (decl) = 1;
479 6986 : DECL_IGNORED_P (decl) = 1;
480 6986 : TREE_USED (decl) = 1;
481 6986 : DECL_CONTEXT (decl) = info->context;
482 6986 : DECL_ARG_TYPE (decl) = type;
483 :
484 : /* Tell tree-inline.cc that we never write to this variable, so
485 : it can copy-prop the replacement value immediately. */
486 6986 : TREE_READONLY (decl) = 1;
487 :
488 6986 : info->chain_decl = decl;
489 :
490 6986 : if (dump_file
491 0 : && (dump_flags & TDF_DETAILS)
492 6986 : && !DECL_STATIC_CHAIN (info->context))
493 0 : fprintf (dump_file, "Setting static-chain for %s\n",
494 0 : lang_hooks.decl_printable_name (info->context, 2));
495 :
496 6986 : DECL_STATIC_CHAIN (info->context) = 1;
497 : }
498 28042 : return decl;
499 : }
500 :
501 : /* Build or return the field within the non-local frame state that holds
502 : the static chain for INFO->CONTEXT. This is the way to walk back up
503 : multiple nesting levels. */
504 :
505 : static tree
506 148 : get_chain_field (struct nesting_info *info)
507 : {
508 148 : tree field = info->chain_field;
509 :
510 148 : if (!field)
511 : {
512 79 : tree type = build_pointer_type (get_frame_type (info->outer));
513 :
514 79 : field = make_node (FIELD_DECL);
515 79 : DECL_NAME (field) = get_identifier ("__chain");
516 79 : TREE_TYPE (field) = type;
517 79 : SET_DECL_ALIGN (field, TYPE_ALIGN (type));
518 79 : DECL_NONADDRESSABLE_P (field) = 1;
519 :
520 79 : insert_field_into_struct (get_frame_type (info), field);
521 :
522 79 : info->chain_field = field;
523 :
524 79 : if (dump_file
525 0 : && (dump_flags & TDF_DETAILS)
526 79 : && !DECL_STATIC_CHAIN (info->context))
527 0 : fprintf (dump_file, "Setting static-chain for %s\n",
528 0 : lang_hooks.decl_printable_name (info->context, 2));
529 :
530 79 : DECL_STATIC_CHAIN (info->context) = 1;
531 : }
532 148 : return field;
533 : }
534 :
535 : /* Initialize a new temporary with the GIMPLE_CALL STMT. */
536 :
537 : static tree
538 1408 : init_tmp_var_with_call (struct nesting_info *info, gimple_stmt_iterator *gsi,
539 : gcall *call)
540 : {
541 1408 : tree t;
542 :
543 1408 : t = create_tmp_var_for (info, gimple_call_return_type (call), NULL);
544 1408 : gimple_call_set_lhs (call, t);
545 1408 : if (! gsi_end_p (*gsi))
546 324 : gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
547 1408 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
548 :
549 1408 : return t;
550 : }
551 :
552 :
553 : /* Copy EXP into a temporary. Allocate the temporary in the context of
554 : INFO and insert the initialization statement before GSI. */
555 :
556 : static tree
557 12997 : init_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
558 : {
559 12997 : tree t;
560 12997 : gimple *stmt;
561 :
562 12997 : t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
563 12997 : stmt = gimple_build_assign (t, exp);
564 12997 : if (! gsi_end_p (*gsi))
565 12979 : gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
566 12997 : gsi_insert_before_without_update (gsi, stmt, GSI_SAME_STMT);
567 :
568 12997 : return t;
569 : }
570 :
571 :
572 : /* Similarly, but only do so to force EXP to satisfy is_gimple_val. */
573 :
574 : static tree
575 3837 : gsi_gimplify_val (struct nesting_info *info, tree exp,
576 : gimple_stmt_iterator *gsi)
577 : {
578 3837 : if (is_gimple_val (exp))
579 : return exp;
580 : else
581 561 : return init_tmp_var (info, exp, gsi);
582 : }
583 :
584 : /* Similarly, but copy from the temporary and insert the statement
585 : after the iterator. */
586 :
587 : static tree
588 4797 : save_tmp_var (struct nesting_info *info, tree exp, gimple_stmt_iterator *gsi)
589 : {
590 4797 : tree t;
591 4797 : gimple *stmt;
592 :
593 4797 : t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
594 4797 : stmt = gimple_build_assign (exp, t);
595 4797 : if (! gsi_end_p (*gsi))
596 4797 : gimple_set_location (stmt, gimple_location (gsi_stmt (*gsi)));
597 4797 : gsi_insert_after_without_update (gsi, stmt, GSI_SAME_STMT);
598 :
599 4797 : return t;
600 : }
601 :
602 : /* Build or return the type used to represent a nested function trampoline. */
603 :
604 : static GTY(()) tree trampoline_type;
605 :
606 : static tree
607 280 : get_trampoline_type (struct nesting_info *info)
608 : {
609 280 : unsigned align, size;
610 280 : tree t;
611 :
612 280 : if (trampoline_type)
613 : return trampoline_type;
614 :
615 : /* When trampolines are created off-stack then the only thing we need in the
616 : local frame is a single pointer. */
617 225 : if (flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
618 : {
619 1 : trampoline_type = build_pointer_type (void_type_node);
620 1 : return trampoline_type;
621 : }
622 :
623 224 : align = TRAMPOLINE_ALIGNMENT;
624 224 : size = TRAMPOLINE_SIZE;
625 :
626 : /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
627 : then allocate extra space so that we can do dynamic alignment. */
628 224 : if (align > STACK_BOUNDARY)
629 : {
630 0 : size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
631 0 : align = STACK_BOUNDARY;
632 : }
633 :
634 224 : t = build_index_type (size_int (size - 1));
635 224 : t = build_array_type (char_type_node, t);
636 224 : t = build_decl (DECL_SOURCE_LOCATION (info->context),
637 : FIELD_DECL, get_identifier ("__data"), t);
638 224 : SET_DECL_ALIGN (t, align);
639 224 : DECL_USER_ALIGN (t) = 1;
640 :
641 224 : trampoline_type = make_node (RECORD_TYPE);
642 224 : TYPE_NAME (trampoline_type) = get_identifier ("__builtin_trampoline");
643 224 : TYPE_FIELDS (trampoline_type) = t;
644 224 : layout_type (trampoline_type);
645 224 : DECL_CONTEXT (t) = trampoline_type;
646 :
647 224 : return trampoline_type;
648 : }
649 :
650 : /* Build or return the type used to represent a nested function descriptor. */
651 :
652 : static GTY(()) tree descriptor_type;
653 :
654 : static tree
655 0 : get_descriptor_type (struct nesting_info *info)
656 : {
657 : /* The base alignment is that of a function. */
658 0 : const unsigned align = FUNCTION_ALIGNMENT (FUNCTION_BOUNDARY);
659 0 : tree t;
660 :
661 0 : if (descriptor_type)
662 : return descriptor_type;
663 :
664 0 : t = build_index_type (integer_one_node);
665 0 : t = build_array_type (ptr_type_node, t);
666 0 : t = build_decl (DECL_SOURCE_LOCATION (info->context),
667 : FIELD_DECL, get_identifier ("__data"), t);
668 0 : SET_DECL_ALIGN (t, MAX (TYPE_ALIGN (ptr_type_node), align));
669 0 : DECL_USER_ALIGN (t) = 1;
670 :
671 0 : descriptor_type = make_node (RECORD_TYPE);
672 0 : TYPE_NAME (descriptor_type) = get_identifier ("__builtin_descriptor");
673 0 : TYPE_FIELDS (descriptor_type) = t;
674 0 : layout_type (descriptor_type);
675 0 : DECL_CONTEXT (t) = descriptor_type;
676 :
677 0 : return descriptor_type;
678 : }
679 :
680 : /* Given DECL, a nested function, find or create an element in the
681 : var map for this function. */
682 :
683 : static tree
684 688 : lookup_element_for_decl (struct nesting_info *info, tree decl,
685 : enum insert_option insert)
686 : {
687 688 : if (insert == NO_INSERT)
688 : {
689 361 : tree *slot = info->var_map->get (decl);
690 361 : return slot ? *slot : NULL_TREE;
691 : }
692 :
693 327 : tree *slot = &info->var_map->get_or_insert (decl);
694 327 : if (!*slot)
695 280 : *slot = build_tree_list (NULL_TREE, NULL_TREE);
696 :
697 327 : return (tree) *slot;
698 : }
699 :
700 : /* Given DECL, a nested function, create a field in the non-local
701 : frame structure for this function. */
702 :
703 : static tree
704 280 : create_field_for_decl (struct nesting_info *info, tree decl, tree type)
705 : {
706 280 : tree field = make_node (FIELD_DECL);
707 280 : DECL_NAME (field) = DECL_NAME (decl);
708 280 : TREE_TYPE (field) = type;
709 280 : TREE_ADDRESSABLE (field) = 1;
710 280 : insert_field_into_struct (get_frame_type (info), field);
711 280 : return field;
712 : }
713 :
714 : /* Given DECL, a nested function, find or create a field in the non-local
715 : frame structure for a trampoline for this function. */
716 :
717 : static tree
718 688 : lookup_tramp_for_decl (struct nesting_info *info, tree decl,
719 : enum insert_option insert)
720 : {
721 688 : tree elt, field;
722 :
723 688 : elt = lookup_element_for_decl (info, decl, insert);
724 688 : if (!elt)
725 : return NULL_TREE;
726 :
727 607 : field = TREE_PURPOSE (elt);
728 :
729 607 : if (!field && insert == INSERT)
730 : {
731 280 : field = create_field_for_decl (info, decl, get_trampoline_type (info));
732 280 : TREE_PURPOSE (elt) = field;
733 280 : info->any_tramp_created = true;
734 : }
735 :
736 : return field;
737 : }
738 :
739 : /* Given DECL, a nested function, find or create a field in the non-local
740 : frame structure for a descriptor for this function. */
741 :
742 : static tree
743 0 : lookup_descr_for_decl (struct nesting_info *info, tree decl,
744 : enum insert_option insert)
745 : {
746 0 : tree elt, field;
747 :
748 0 : elt = lookup_element_for_decl (info, decl, insert);
749 0 : if (!elt)
750 : return NULL_TREE;
751 :
752 0 : field = TREE_VALUE (elt);
753 :
754 0 : if (!field && insert == INSERT)
755 : {
756 0 : field = create_field_for_decl (info, decl, get_descriptor_type (info));
757 0 : TREE_VALUE (elt) = field;
758 0 : info->any_descr_created = true;
759 : }
760 :
761 : return field;
762 : }
763 :
764 : /* Build or return the field within the non-local frame state that holds
765 : the non-local goto "jmp_buf". The buffer itself is maintained by the
766 : rtl middle-end as dynamic stack space is allocated. */
767 :
768 : static tree
769 522 : get_nl_goto_field (struct nesting_info *info)
770 : {
771 522 : tree field = info->nl_goto_field;
772 522 : if (!field)
773 : {
774 375 : unsigned size;
775 375 : tree type;
776 :
777 : /* For __builtin_nonlocal_goto, we need N words. The first is the
778 : frame pointer, the rest is for the target's stack pointer save
779 : area. The number of words is controlled by STACK_SAVEAREA_MODE;
780 : not the best interface, but it'll do for now. */
781 375 : if (Pmode == ptr_mode)
782 375 : type = ptr_type_node;
783 : else
784 0 : type = lang_hooks.types.type_for_mode (Pmode, 1);
785 :
786 375 : fixed_size_mode mode
787 375 : = as_a <fixed_size_mode> (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
788 375 : size = GET_MODE_SIZE (mode);
789 375 : size = size / GET_MODE_SIZE (Pmode);
790 375 : size = size + 1;
791 :
792 375 : type = build_array_type
793 375 : (type, build_index_type (size_int (size)));
794 :
795 375 : field = make_node (FIELD_DECL);
796 375 : DECL_NAME (field) = get_identifier ("__nl_goto_buf");
797 375 : TREE_TYPE (field) = type;
798 375 : SET_DECL_ALIGN (field, TYPE_ALIGN (type));
799 375 : TREE_ADDRESSABLE (field) = 1;
800 :
801 375 : insert_field_into_struct (get_frame_type (info), field);
802 :
803 375 : info->nl_goto_field = field;
804 : }
805 :
806 522 : return field;
807 : }
808 :
809 : /* Invoke CALLBACK on all statements of GIMPLE sequence *PSEQ. */
810 :
811 : static void
812 240818 : walk_body (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
813 : struct nesting_info *info, gimple_seq *pseq)
814 : {
815 240818 : struct walk_stmt_info wi;
816 :
817 240818 : memset (&wi, 0, sizeof (wi));
818 240818 : wi.info = info;
819 240818 : wi.val_only = true;
820 240818 : walk_gimple_seq_mod (pseq, callback_stmt, callback_op, &wi);
821 240818 : }
822 :
823 :
824 : /* Invoke CALLBACK_STMT/CALLBACK_OP on all statements of INFO->CONTEXT. */
825 :
826 : static inline void
827 209410 : walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
828 : struct nesting_info *info)
829 : {
830 209410 : gimple_seq body = gimple_body (info->context);
831 209410 : walk_body (callback_stmt, callback_op, info, &body);
832 209410 : gimple_set_body (info->context, body);
833 209410 : }
834 :
835 : /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body. */
836 :
837 : static void
838 3194 : walk_gimple_omp_for (gomp_for *for_stmt,
839 : walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
840 : struct nesting_info *info)
841 : {
842 3194 : struct walk_stmt_info wi;
843 3194 : gimple_seq seq;
844 3194 : tree t;
845 3194 : size_t i;
846 :
847 3194 : walk_body (callback_stmt, callback_op, info, gimple_omp_for_pre_body_ptr (for_stmt));
848 :
849 3194 : seq = NULL;
850 3194 : memset (&wi, 0, sizeof (wi));
851 3194 : wi.info = info;
852 3194 : wi.gsi = gsi_last (seq);
853 :
854 7306 : for (i = 0; i < gimple_omp_for_collapse (for_stmt); i++)
855 : {
856 4112 : wi.val_only = false;
857 4112 : walk_tree (gimple_omp_for_index_ptr (for_stmt, i), callback_op,
858 : &wi, NULL);
859 4112 : wi.val_only = true;
860 4112 : wi.is_lhs = false;
861 4112 : walk_tree (gimple_omp_for_initial_ptr (for_stmt, i), callback_op,
862 : &wi, NULL);
863 :
864 4112 : wi.val_only = true;
865 4112 : wi.is_lhs = false;
866 4112 : walk_tree (gimple_omp_for_final_ptr (for_stmt, i), callback_op,
867 : &wi, NULL);
868 :
869 4112 : t = gimple_omp_for_incr (for_stmt, i);
870 4112 : gcc_assert (BINARY_CLASS_P (t));
871 4112 : wi.val_only = false;
872 4112 : walk_tree (&TREE_OPERAND (t, 0), callback_op, &wi, NULL);
873 4112 : wi.val_only = true;
874 4112 : wi.is_lhs = false;
875 4112 : walk_tree (&TREE_OPERAND (t, 1), callback_op, &wi, NULL);
876 : }
877 :
878 3194 : seq = gsi_seq (wi.gsi);
879 3194 : if (!gimple_seq_empty_p (seq))
880 : {
881 9 : gimple_seq pre_body = gimple_omp_for_pre_body (for_stmt);
882 9 : annotate_all_with_location (seq, gimple_location (for_stmt));
883 9 : gimple_seq_add_seq (&pre_body, seq);
884 9 : gimple_omp_for_set_pre_body (for_stmt, pre_body);
885 : }
886 3194 : }
887 :
888 : /* Similarly for ROOT and all functions nested underneath, depth first. */
889 :
890 : static void
891 37676 : walk_all_functions (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
892 : struct nesting_info *root)
893 : {
894 37676 : struct nesting_info *n;
895 353848 : FOR_EACH_NEST_INFO (n, root)
896 139248 : walk_function (callback_stmt, callback_op, n);
897 37676 : }
898 :
899 :
900 : /* We have to check for a fairly pathological case. The operands of function
901 : nested function are to be interpreted in the context of the enclosing
902 : function. So if any are variably-sized, they will get remapped when the
903 : enclosing function is inlined. But that remapping would also have to be
904 : done in the types of the PARM_DECLs of the nested function, meaning the
905 : argument types of that function will disagree with the arguments in the
906 : calls to that function. So we'd either have to make a copy of the nested
907 : function corresponding to each time the enclosing function was inlined or
908 : add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
909 : function. The former is not practical. The latter would still require
910 : detecting this case to know when to add the conversions. So, for now at
911 : least, we don't inline such an enclosing function. A similar issue
912 : applies if the nested function has a variably modified return type, and
913 : is not inlined, but the enclosing function is inlined and so the type of
914 : the return slot as used in the enclosing function is remapped, so also
915 : avoid inlining in that case.
916 :
917 : We have to do that check recursively, so here return indicating whether
918 : FNDECL has such a nested function. ORIG_FN is the function we were
919 : trying to inline to use for checking whether any argument is variably
920 : modified by anything in it.
921 :
922 : It would be better to do this in tree-inline.cc so that we could give
923 : the appropriate warning for why a function can't be inlined, but that's
924 : too late since the nesting structure has already been flattened and
925 : adding a flag just to record this fact seems a waste of a flag. */
926 :
927 : static bool
928 59444 : check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
929 : {
930 59444 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
931 59444 : tree arg;
932 :
933 168152 : for (cgn = first_nested_function (cgn); cgn;
934 24632 : cgn = next_nested_function (cgn))
935 : {
936 25003 : if (variably_modified_type_p (TREE_TYPE (TREE_TYPE (cgn->decl)),
937 : orig_fndecl))
938 : return true;
939 67659 : for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = DECL_CHAIN (arg))
940 43027 : if (variably_modified_type_p (TREE_TYPE (arg), orig_fndecl))
941 : return true;
942 :
943 24632 : if (check_for_nested_with_variably_modified (cgn->decl,
944 : orig_fndecl))
945 : return true;
946 : }
947 :
948 : return false;
949 : }
950 :
951 : /* Construct our local datastructure describing the function nesting
952 : tree rooted by CGN. */
953 :
954 : static struct nesting_info *
955 34812 : create_nesting_tree (struct cgraph_node *cgn)
956 : {
957 34812 : struct nesting_info *info = XCNEW (struct nesting_info);
958 34812 : info->field_map = new hash_map<tree, tree>;
959 34812 : info->var_map = new hash_map<tree, tree>;
960 34812 : info->mem_refs = new hash_set<tree *>;
961 34812 : info->suppress_expansion = BITMAP_ALLOC (&nesting_info_bitmap_obstack);
962 34812 : info->context = cgn->decl;
963 34812 : info->thunk_p = cgn->thunk;
964 :
965 95017 : for (cgn = first_nested_function (cgn); cgn;
966 25393 : cgn = next_nested_function (cgn))
967 : {
968 25393 : struct nesting_info *sub = create_nesting_tree (cgn);
969 25393 : sub->outer = info;
970 25393 : sub->next = info->inner;
971 25393 : info->inner = sub;
972 : }
973 :
974 : /* See discussion at check_for_nested_with_variably_modified for a
975 : discussion of why this has to be here. */
976 34812 : if (check_for_nested_with_variably_modified (info->context, info->context))
977 : {
978 371 : DECL_UNINLINABLE (info->context) = true;
979 371 : tree attrs = DECL_ATTRIBUTES (info->context);
980 371 : if (lookup_attribute ("noclone", attrs) == NULL)
981 370 : DECL_ATTRIBUTES (info->context)
982 740 : = tree_cons (get_identifier ("noclone"), NULL, attrs);
983 : }
984 :
985 34812 : return info;
986 : }
987 :
988 : /* Return an expression computing the static chain for TARGET_CONTEXT
989 : from INFO->CONTEXT. Insert any necessary computations before TSI. */
990 :
991 : static tree
992 16392 : get_static_chain (struct nesting_info *info, tree target_context,
993 : gimple_stmt_iterator *gsi)
994 : {
995 16392 : struct nesting_info *i;
996 16392 : tree x;
997 :
998 16392 : if (info->context == target_context)
999 : {
1000 14243 : x = build_addr (info->frame_decl);
1001 14243 : info->static_chain_added |= 1;
1002 : }
1003 : else
1004 : {
1005 2149 : x = get_chain_decl (info);
1006 2149 : info->static_chain_added |= 2;
1007 :
1008 2189 : for (i = info->outer; i->context != target_context; i = i->outer)
1009 : {
1010 40 : tree field = get_chain_field (i);
1011 :
1012 40 : x = build_simple_mem_ref_notrap (x);
1013 40 : x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1014 40 : x = init_tmp_var (info, x, gsi);
1015 : }
1016 : }
1017 :
1018 16392 : return x;
1019 : }
1020 :
1021 :
1022 : /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
1023 : frame as seen from INFO->CONTEXT. Insert any necessary computations
1024 : before GSI. */
1025 :
1026 : static tree
1027 42924 : get_frame_field (struct nesting_info *info, tree target_context,
1028 : tree field, gimple_stmt_iterator *gsi)
1029 : {
1030 42924 : struct nesting_info *i;
1031 42924 : tree x;
1032 :
1033 42924 : if (info->context == target_context)
1034 : {
1035 : /* Make sure frame_decl gets created. */
1036 22163 : (void) get_frame_type (info);
1037 22163 : x = info->frame_decl;
1038 22163 : info->static_chain_added |= 1;
1039 : }
1040 : else
1041 : {
1042 20761 : x = get_chain_decl (info);
1043 20761 : info->static_chain_added |= 2;
1044 :
1045 20869 : for (i = info->outer; i->context != target_context; i = i->outer)
1046 : {
1047 108 : tree field = get_chain_field (i);
1048 :
1049 108 : x = build_simple_mem_ref_notrap (x);
1050 108 : x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1051 108 : x = init_tmp_var (info, x, gsi);
1052 : }
1053 :
1054 20761 : x = build_simple_mem_ref_notrap (x);
1055 : }
1056 :
1057 42924 : x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1058 42924 : TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (field);
1059 42924 : return x;
1060 : }
1061 :
1062 : static void note_nonlocal_vla_type (struct nesting_info *info, tree type);
1063 :
1064 : /* Helper for get_nonlocal_debug_decl and get_local_debug_decl. */
1065 :
1066 : static tree
1067 716 : get_debug_decl (tree decl)
1068 : {
1069 716 : tree new_decl
1070 716 : = build_decl (DECL_SOURCE_LOCATION (decl),
1071 716 : VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
1072 716 : DECL_ARTIFICIAL (new_decl) = DECL_ARTIFICIAL (decl);
1073 716 : DECL_IGNORED_P (new_decl) = DECL_IGNORED_P (decl);
1074 716 : TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (decl);
1075 716 : TREE_SIDE_EFFECTS (new_decl) = TREE_SIDE_EFFECTS (decl);
1076 716 : TREE_READONLY (new_decl) = TREE_READONLY (decl);
1077 716 : TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (decl);
1078 716 : DECL_SEEN_IN_BIND_EXPR_P (new_decl) = 1;
1079 716 : if ((TREE_CODE (decl) == PARM_DECL
1080 : || TREE_CODE (decl) == RESULT_DECL
1081 : || VAR_P (decl))
1082 710 : && DECL_BY_REFERENCE (decl))
1083 8 : DECL_BY_REFERENCE (new_decl) = 1;
1084 : /* Copy DECL_LANG_SPECIFIC and DECL_LANG_FLAG_* for OpenMP langhook
1085 : purposes. */
1086 716 : DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (decl);
1087 : #define COPY_DLF(n) DECL_LANG_FLAG_##n (new_decl) = DECL_LANG_FLAG_##n (decl)
1088 716 : COPY_DLF (0); COPY_DLF (1); COPY_DLF (2); COPY_DLF (3);
1089 716 : COPY_DLF (4); COPY_DLF (5); COPY_DLF (6); COPY_DLF (7);
1090 716 : COPY_DLF (8);
1091 : #undef COPY_DLF
1092 716 : return new_decl;
1093 : }
1094 :
1095 : /* A subroutine of convert_nonlocal_reference_op. Create a local variable
1096 : in the nested function with DECL_VALUE_EXPR set to reference the true
1097 : variable in the parent function. This is used both for debug info
1098 : and in OMP lowering. */
1099 :
1100 : static tree
1101 3681 : get_nonlocal_debug_decl (struct nesting_info *info, tree decl)
1102 : {
1103 3681 : tree target_context;
1104 3681 : struct nesting_info *i;
1105 3681 : tree x, field, new_decl;
1106 :
1107 3681 : tree *slot = &info->var_map->get_or_insert (decl);
1108 :
1109 3681 : if (*slot)
1110 : return *slot;
1111 :
1112 650 : target_context = decl_function_context (decl);
1113 :
1114 : /* A copy of the code in get_frame_field, but without the temporaries. */
1115 650 : if (info->context == target_context)
1116 : {
1117 : /* Make sure frame_decl gets created. */
1118 0 : (void) get_frame_type (info);
1119 0 : x = info->frame_decl;
1120 0 : i = info;
1121 0 : info->static_chain_added |= 1;
1122 : }
1123 : else
1124 : {
1125 650 : x = get_chain_decl (info);
1126 650 : info->static_chain_added |= 2;
1127 650 : for (i = info->outer; i->context != target_context; i = i->outer)
1128 : {
1129 0 : field = get_chain_field (i);
1130 0 : x = build_simple_mem_ref_notrap (x);
1131 0 : x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1132 : }
1133 650 : x = build_simple_mem_ref_notrap (x);
1134 : }
1135 :
1136 650 : field = lookup_field_for_decl (i, decl, INSERT);
1137 650 : x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1138 650 : if (use_pointer_in_frame (decl))
1139 6 : x = build_simple_mem_ref_notrap (x);
1140 :
1141 : /* ??? We should be remapping types as well, surely. */
1142 650 : new_decl = get_debug_decl (decl);
1143 650 : DECL_CONTEXT (new_decl) = info->context;
1144 :
1145 650 : SET_DECL_VALUE_EXPR (new_decl, x);
1146 650 : DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1147 :
1148 650 : *slot = new_decl;
1149 650 : DECL_CHAIN (new_decl) = info->debug_var_chain;
1150 650 : info->debug_var_chain = new_decl;
1151 :
1152 650 : if (!optimize
1153 121 : && info->context != target_context
1154 771 : && variably_modified_type_p (TREE_TYPE (decl), NULL))
1155 1 : note_nonlocal_vla_type (info, TREE_TYPE (decl));
1156 :
1157 : return new_decl;
1158 : }
1159 :
1160 :
1161 : /* Callback for walk_gimple_stmt, rewrite all references to VAR
1162 : and PARM_DECLs that belong to outer functions.
1163 :
1164 : The rewrite will involve some number of structure accesses back up
1165 : the static chain. E.g. for a variable FOO up one nesting level it'll
1166 : be CHAIN->FOO. For two levels it'll be CHAIN->__chain->FOO. Further
1167 : indirections apply to decls for which use_pointer_in_frame is true. */
1168 :
1169 : static tree
1170 16895957 : convert_nonlocal_reference_op (tree *tp, int *walk_subtrees, void *data)
1171 : {
1172 16895957 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1173 16895957 : struct nesting_info *const info = (struct nesting_info *) wi->info;
1174 16895957 : tree t = *tp;
1175 :
1176 16895957 : *walk_subtrees = 0;
1177 16895957 : switch (TREE_CODE (t))
1178 : {
1179 4693336 : case VAR_DECL:
1180 : /* Non-automatic variables are never processed. */
1181 4693336 : if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1182 : break;
1183 : /* FALLTHRU */
1184 :
1185 4684438 : case PARM_DECL:
1186 4684438 : {
1187 4684438 : tree x, target_context = decl_function_context (t);
1188 :
1189 4684438 : if (info->context == target_context)
1190 : break;
1191 :
1192 22689 : wi->changed = true;
1193 :
1194 22689 : if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1195 2473 : x = get_nonlocal_debug_decl (info, t);
1196 : else
1197 : {
1198 : struct nesting_info *i = info;
1199 40540 : while (i && i->context != target_context)
1200 20324 : i = i->outer;
1201 : /* If none of the outer contexts is the target context, this means
1202 : that the VAR or PARM_DECL is referenced in a wrong context. */
1203 20216 : if (!i)
1204 0 : internal_error ("%s from %s referenced in %s",
1205 0 : IDENTIFIER_POINTER (DECL_NAME (t)),
1206 0 : IDENTIFIER_POINTER (DECL_NAME (target_context)),
1207 0 : IDENTIFIER_POINTER (DECL_NAME (info->context)));
1208 :
1209 20216 : x = lookup_field_for_decl (i, t, INSERT);
1210 20216 : x = get_frame_field (info, target_context, x, &wi->gsi);
1211 20216 : if (use_pointer_in_frame (t))
1212 : {
1213 21 : x = init_tmp_var (info, x, &wi->gsi);
1214 21 : x = build_simple_mem_ref_notrap (x);
1215 : }
1216 : }
1217 :
1218 22689 : if (wi->val_only)
1219 : {
1220 8009 : if (wi->is_lhs)
1221 1962 : x = save_tmp_var (info, x, &wi->gsi);
1222 : else
1223 6047 : x = init_tmp_var (info, x, &wi->gsi);
1224 : }
1225 :
1226 22689 : *tp = x;
1227 : }
1228 22689 : break;
1229 :
1230 1881623 : case LABEL_DECL:
1231 : /* We're taking the address of a label from a parent function, but
1232 : this is not itself a non-local goto. Mark the label such that it
1233 : will not be deleted, much as we would with a label address in
1234 : static storage. */
1235 1881623 : if (decl_function_context (t) != info->context)
1236 79 : FORCED_LABEL (t) = 1;
1237 : break;
1238 :
1239 740824 : case ADDR_EXPR:
1240 740824 : {
1241 740824 : bool save_val_only = wi->val_only;
1242 :
1243 740824 : wi->val_only = false;
1244 740824 : wi->is_lhs = false;
1245 740824 : wi->changed = false;
1246 740824 : walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference_op, wi, 0);
1247 740824 : wi->val_only = true;
1248 :
1249 740824 : if (wi->changed)
1250 : {
1251 2803 : tree save_context;
1252 :
1253 : /* If we changed anything, we might no longer be directly
1254 : referencing a decl. */
1255 2803 : save_context = current_function_decl;
1256 2803 : current_function_decl = info->context;
1257 2803 : recompute_tree_invariant_for_addr_expr (t);
1258 :
1259 : /* If the callback converted the address argument in a context
1260 : where we only accept variables (and min_invariant, presumably),
1261 : then compute the address into a temporary. */
1262 2803 : if (save_val_only)
1263 12 : *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
1264 : t, &wi->gsi);
1265 2803 : current_function_decl = save_context;
1266 : }
1267 : }
1268 : break;
1269 :
1270 1829264 : case REALPART_EXPR:
1271 1829264 : case IMAGPART_EXPR:
1272 1829264 : case COMPONENT_REF:
1273 1829264 : case ARRAY_REF:
1274 1829264 : case ARRAY_RANGE_REF:
1275 1829264 : case BIT_FIELD_REF:
1276 : /* Go down this entire nest and just look at the final prefix and
1277 : anything that describes the references. Otherwise, we lose track
1278 : of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
1279 1829264 : wi->val_only = true;
1280 1829264 : wi->is_lhs = false;
1281 5358727 : for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1282 : {
1283 3529463 : if (TREE_CODE (t) == COMPONENT_REF)
1284 2644554 : walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op, wi,
1285 : NULL);
1286 884909 : else if (TREE_CODE (t) == ARRAY_REF
1287 884909 : || TREE_CODE (t) == ARRAY_RANGE_REF)
1288 : {
1289 884223 : walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference_op,
1290 : wi, NULL);
1291 884223 : walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference_op,
1292 : wi, NULL);
1293 884223 : walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference_op,
1294 : wi, NULL);
1295 : }
1296 : }
1297 1829264 : wi->val_only = false;
1298 1829264 : walk_tree (tp, convert_nonlocal_reference_op, wi, NULL);
1299 1829264 : break;
1300 :
1301 882 : case VIEW_CONVERT_EXPR:
1302 : /* Just request to look at the subtrees, leaving val_only and lhs
1303 : untouched. This might actually be for !val_only + lhs, in which
1304 : case we don't want to force a replacement by a temporary. */
1305 882 : *walk_subtrees = 1;
1306 882 : break;
1307 :
1308 7575168 : default:
1309 7575168 : if (!IS_TYPE_OR_DECL_P (t))
1310 : {
1311 7165423 : *walk_subtrees = 1;
1312 7165423 : wi->val_only = true;
1313 7165423 : wi->is_lhs = false;
1314 : }
1315 : break;
1316 : }
1317 :
1318 16895957 : return NULL_TREE;
1319 : }
1320 :
1321 : static tree convert_nonlocal_reference_stmt (gimple_stmt_iterator *, bool *,
1322 : struct walk_stmt_info *);
1323 :
1324 : /* Helper for convert_nonlocal_references, rewrite all references to VAR
1325 : and PARM_DECLs that belong to outer functions. */
1326 :
1327 : static bool
1328 5200 : convert_nonlocal_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
1329 : {
1330 5200 : struct nesting_info *const info = (struct nesting_info *) wi->info;
1331 5200 : bool need_chain = false, need_stmts = false;
1332 5200 : tree clause, decl, *pdecl;
1333 5200 : int dummy;
1334 5200 : bitmap new_suppress;
1335 :
1336 5200 : new_suppress = BITMAP_GGC_ALLOC ();
1337 5200 : bitmap_copy (new_suppress, info->suppress_expansion);
1338 :
1339 30624 : for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1340 : {
1341 25424 : pdecl = NULL;
1342 25424 : switch (OMP_CLAUSE_CODE (clause))
1343 : {
1344 663 : case OMP_CLAUSE_REDUCTION:
1345 663 : case OMP_CLAUSE_IN_REDUCTION:
1346 663 : case OMP_CLAUSE_TASK_REDUCTION:
1347 663 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1348 37 : need_stmts = true;
1349 663 : if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
1350 : {
1351 6 : pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
1352 6 : if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
1353 0 : pdecl = &TREE_OPERAND (*pdecl, 0);
1354 6 : if (INDIRECT_REF_P (*pdecl)
1355 6 : || TREE_CODE (*pdecl) == ADDR_EXPR)
1356 4 : pdecl = &TREE_OPERAND (*pdecl, 0);
1357 : }
1358 663 : goto do_decl_clause;
1359 :
1360 1176 : case OMP_CLAUSE_LASTPRIVATE:
1361 1176 : if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
1362 250 : need_stmts = true;
1363 1176 : goto do_decl_clause;
1364 :
1365 391 : case OMP_CLAUSE_LINEAR:
1366 391 : if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
1367 74 : need_stmts = true;
1368 391 : wi->val_only = true;
1369 391 : wi->is_lhs = false;
1370 391 : convert_nonlocal_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause),
1371 : &dummy, wi);
1372 391 : goto do_decl_clause;
1373 :
1374 5829 : case OMP_CLAUSE_PRIVATE:
1375 5829 : case OMP_CLAUSE_FIRSTPRIVATE:
1376 5829 : case OMP_CLAUSE_COPYPRIVATE:
1377 5829 : case OMP_CLAUSE_SHARED:
1378 5829 : case OMP_CLAUSE_ENTER:
1379 5829 : case OMP_CLAUSE_LINK:
1380 5829 : case OMP_CLAUSE_USE_DEVICE_PTR:
1381 5829 : case OMP_CLAUSE_USE_DEVICE_ADDR:
1382 5829 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
1383 5829 : case OMP_CLAUSE_IS_DEVICE_PTR:
1384 5829 : case OMP_CLAUSE_DETACH:
1385 5829 : do_decl_clause:
1386 2230 : if (pdecl == NULL)
1387 17235 : pdecl = &OMP_CLAUSE_DECL (clause);
1388 17241 : decl = *pdecl;
1389 17241 : if (VAR_P (decl)
1390 17241 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1391 : break;
1392 16747 : if (decl_function_context (decl) != info->context)
1393 : {
1394 1194 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
1395 356 : OMP_CLAUSE_SHARED_READONLY (clause) = 0;
1396 1194 : bitmap_set_bit (new_suppress, DECL_UID (decl));
1397 1194 : *pdecl = get_nonlocal_debug_decl (info, decl);
1398 1194 : if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_PRIVATE)
1399 25424 : need_chain = true;
1400 : }
1401 : break;
1402 :
1403 177 : case OMP_CLAUSE_SCHEDULE:
1404 177 : if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
1405 : break;
1406 : /* FALLTHRU */
1407 2747 : case OMP_CLAUSE_FINAL:
1408 2747 : case OMP_CLAUSE_IF:
1409 2747 : case OMP_CLAUSE_SELF:
1410 2747 : case OMP_CLAUSE_NUM_THREADS:
1411 2747 : case OMP_CLAUSE_DEPEND:
1412 2747 : case OMP_CLAUSE_DOACROSS:
1413 2747 : case OMP_CLAUSE_DEVICE:
1414 2747 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
1415 2747 : case OMP_CLAUSE_NUM_TEAMS:
1416 2747 : case OMP_CLAUSE_THREAD_LIMIT:
1417 2747 : case OMP_CLAUSE_SAFELEN:
1418 2747 : case OMP_CLAUSE_SIMDLEN:
1419 2747 : case OMP_CLAUSE_PRIORITY:
1420 2747 : case OMP_CLAUSE_GRAINSIZE:
1421 2747 : case OMP_CLAUSE_NUM_TASKS:
1422 2747 : case OMP_CLAUSE_HINT:
1423 2747 : case OMP_CLAUSE_FILTER:
1424 2747 : case OMP_CLAUSE_NUM_GANGS:
1425 2747 : case OMP_CLAUSE_NUM_WORKERS:
1426 2747 : case OMP_CLAUSE_VECTOR_LENGTH:
1427 2747 : case OMP_CLAUSE_GANG:
1428 2747 : case OMP_CLAUSE_WORKER:
1429 2747 : case OMP_CLAUSE_VECTOR:
1430 2747 : case OMP_CLAUSE_ASYNC:
1431 2747 : case OMP_CLAUSE_WAIT:
1432 : /* Several OpenACC clauses have optional arguments. Check if they
1433 : are present. */
1434 2747 : if (OMP_CLAUSE_OPERAND (clause, 0))
1435 : {
1436 2491 : wi->val_only = true;
1437 2491 : wi->is_lhs = false;
1438 2491 : convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1439 : &dummy, wi);
1440 : }
1441 :
1442 : /* The gang clause accepts two arguments. */
1443 2747 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
1444 2747 : && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
1445 : {
1446 28 : wi->val_only = true;
1447 28 : wi->is_lhs = false;
1448 28 : convert_nonlocal_reference_op
1449 28 : (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
1450 : }
1451 : break;
1452 :
1453 7 : case OMP_CLAUSE_DIST_SCHEDULE:
1454 7 : if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
1455 : {
1456 7 : wi->val_only = true;
1457 7 : wi->is_lhs = false;
1458 7 : convert_nonlocal_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
1459 : &dummy, wi);
1460 : }
1461 : break;
1462 :
1463 7715 : case OMP_CLAUSE_MAP:
1464 7715 : case OMP_CLAUSE_TO:
1465 7715 : case OMP_CLAUSE_FROM:
1466 7715 : if (OMP_CLAUSE_SIZE (clause))
1467 : {
1468 7715 : wi->val_only = true;
1469 7715 : wi->is_lhs = false;
1470 7715 : convert_nonlocal_reference_op (&OMP_CLAUSE_SIZE (clause),
1471 : &dummy, wi);
1472 : }
1473 7715 : if (DECL_P (OMP_CLAUSE_DECL (clause)))
1474 3599 : goto do_decl_clause;
1475 4116 : wi->val_only = true;
1476 4116 : wi->is_lhs = false;
1477 4116 : walk_tree (&OMP_CLAUSE_DECL (clause), convert_nonlocal_reference_op,
1478 : wi, NULL);
1479 4116 : break;
1480 :
1481 6 : case OMP_CLAUSE_ALIGNED:
1482 6 : if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
1483 : {
1484 6 : wi->val_only = true;
1485 6 : wi->is_lhs = false;
1486 6 : convert_nonlocal_reference_op
1487 6 : (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
1488 : }
1489 : /* FALLTHRU */
1490 42 : case OMP_CLAUSE_NONTEMPORAL:
1491 0 : do_decl_clause_no_supp:
1492 : /* Like do_decl_clause, but don't add any suppression. */
1493 42 : decl = OMP_CLAUSE_DECL (clause);
1494 42 : if (VAR_P (decl)
1495 42 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1496 : break;
1497 30 : if (decl_function_context (decl) != info->context)
1498 : {
1499 6 : OMP_CLAUSE_DECL (clause) = get_nonlocal_debug_decl (info, decl);
1500 6 : need_chain = true;
1501 : }
1502 : break;
1503 :
1504 36 : case OMP_CLAUSE_ALLOCATE:
1505 36 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause))
1506 : {
1507 36 : wi->val_only = true;
1508 36 : wi->is_lhs = false;
1509 36 : convert_nonlocal_reference_op
1510 36 : (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause), &dummy, wi);
1511 : }
1512 36 : goto do_decl_clause_no_supp;
1513 :
1514 : case OMP_CLAUSE_NOWAIT:
1515 : case OMP_CLAUSE_ORDERED:
1516 : case OMP_CLAUSE_DEFAULT:
1517 : case OMP_CLAUSE_COPYIN:
1518 : case OMP_CLAUSE_COLLAPSE:
1519 : case OMP_CLAUSE_TILE:
1520 : case OMP_CLAUSE_UNTIED:
1521 : case OMP_CLAUSE_MERGEABLE:
1522 : case OMP_CLAUSE_PROC_BIND:
1523 : case OMP_CLAUSE_NOGROUP:
1524 : case OMP_CLAUSE_THREADS:
1525 : case OMP_CLAUSE_SIMD:
1526 : case OMP_CLAUSE_DEFAULTMAP:
1527 : case OMP_CLAUSE_ORDER:
1528 : case OMP_CLAUSE_SEQ:
1529 : case OMP_CLAUSE_INDEPENDENT:
1530 : case OMP_CLAUSE_AUTO:
1531 : case OMP_CLAUSE_IF_PRESENT:
1532 : case OMP_CLAUSE_FINALIZE:
1533 : case OMP_CLAUSE_BIND:
1534 : case OMP_CLAUSE__CONDTEMP_:
1535 : case OMP_CLAUSE__SCANTEMP_:
1536 : break;
1537 :
1538 : /* The following clause belongs to the OpenACC cache directive, which
1539 : is discarded during gimplification. */
1540 0 : case OMP_CLAUSE__CACHE_:
1541 : /* The following clauses are only allowed in the OpenMP declare simd
1542 : directive, so not seen here. */
1543 0 : case OMP_CLAUSE_UNIFORM:
1544 0 : case OMP_CLAUSE_INBRANCH:
1545 0 : case OMP_CLAUSE_NOTINBRANCH:
1546 : /* The following clauses are only allowed on OpenMP cancel and
1547 : cancellation point directives, which at this point have already
1548 : been lowered into a function call. */
1549 0 : case OMP_CLAUSE_FOR:
1550 0 : case OMP_CLAUSE_PARALLEL:
1551 0 : case OMP_CLAUSE_SECTIONS:
1552 0 : case OMP_CLAUSE_TASKGROUP:
1553 : /* The following clauses are only added during OMP lowering; nested
1554 : function decomposition happens before that. */
1555 0 : case OMP_CLAUSE__LOOPTEMP_:
1556 0 : case OMP_CLAUSE__REDUCTEMP_:
1557 0 : case OMP_CLAUSE__SIMDUID_:
1558 0 : case OMP_CLAUSE__SIMT_:
1559 : /* The following clauses are only allowed on OpenACC 'routine'
1560 : directives, not seen here. */
1561 0 : case OMP_CLAUSE_NOHOST:
1562 : /* Anything else. */
1563 0 : default:
1564 0 : gcc_unreachable ();
1565 : }
1566 : }
1567 :
1568 5200 : info->suppress_expansion = new_suppress;
1569 :
1570 5200 : if (need_stmts)
1571 1540 : for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
1572 1255 : switch (OMP_CLAUSE_CODE (clause))
1573 : {
1574 103 : case OMP_CLAUSE_REDUCTION:
1575 103 : case OMP_CLAUSE_IN_REDUCTION:
1576 103 : case OMP_CLAUSE_TASK_REDUCTION:
1577 103 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1578 : {
1579 37 : tree old_context
1580 37 : = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
1581 37 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1582 37 : = info->context;
1583 37 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1584 0 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1585 0 : = info->context;
1586 37 : tree save_local_var_chain = info->new_local_var_chain;
1587 37 : info->new_local_var_chain = NULL;
1588 37 : gimple_seq *seq = &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause);
1589 37 : walk_body (convert_nonlocal_reference_stmt,
1590 : convert_nonlocal_reference_op, info, seq);
1591 37 : if (info->new_local_var_chain)
1592 1 : declare_vars (info->new_local_var_chain,
1593 : gimple_seq_first_stmt (*seq), false);
1594 37 : info->new_local_var_chain = NULL;
1595 37 : seq = &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause);
1596 37 : walk_body (convert_nonlocal_reference_stmt,
1597 : convert_nonlocal_reference_op, info, seq);
1598 37 : if (info->new_local_var_chain)
1599 1 : declare_vars (info->new_local_var_chain,
1600 : gimple_seq_first_stmt (*seq), false);
1601 37 : info->new_local_var_chain = save_local_var_chain;
1602 37 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
1603 37 : = old_context;
1604 37 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1605 0 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
1606 0 : = old_context;
1607 : }
1608 : break;
1609 :
1610 625 : case OMP_CLAUSE_LASTPRIVATE:
1611 625 : case OMP_CLAUSE_LINEAR:
1612 625 : {
1613 625 : tree save_local_var_chain = info->new_local_var_chain;
1614 625 : info->new_local_var_chain = NULL;
1615 625 : gimple_seq *seq;
1616 625 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LASTPRIVATE)
1617 410 : seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause);
1618 : else
1619 215 : seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause);
1620 625 : walk_body (convert_nonlocal_reference_stmt,
1621 : convert_nonlocal_reference_op, info, seq);
1622 625 : if (info->new_local_var_chain)
1623 : {
1624 8 : gimple *g = gimple_seq_first_stmt (*seq);
1625 8 : if (gimple_code (g) != GIMPLE_BIND)
1626 : {
1627 2 : g = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
1628 2 : *seq = NULL;
1629 2 : gimple_seq_add_stmt_without_update (seq, g);
1630 : }
1631 8 : declare_vars (info->new_local_var_chain,
1632 : gimple_seq_first_stmt (*seq), false);
1633 : }
1634 625 : info->new_local_var_chain = save_local_var_chain;
1635 : }
1636 625 : break;
1637 :
1638 : default:
1639 : break;
1640 : }
1641 :
1642 5200 : return need_chain;
1643 : }
1644 :
1645 : /* Create nonlocal debug decls for nonlocal VLA array bounds. */
1646 :
1647 : static void
1648 2 : note_nonlocal_vla_type (struct nesting_info *info, tree type)
1649 : {
1650 3 : while (POINTER_TYPE_P (type) && !TYPE_NAME (type))
1651 1 : type = TREE_TYPE (type);
1652 :
1653 2 : if (TYPE_NAME (type)
1654 2 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1655 4 : && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1656 1 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1657 :
1658 2 : while (POINTER_TYPE_P (type)
1659 : || VECTOR_TYPE_P (type)
1660 : || TREE_CODE (type) == FUNCTION_TYPE
1661 2 : || TREE_CODE (type) == METHOD_TYPE)
1662 0 : type = TREE_TYPE (type);
1663 :
1664 2 : if (TREE_CODE (type) == ARRAY_TYPE)
1665 : {
1666 1 : tree domain, t;
1667 :
1668 1 : note_nonlocal_vla_type (info, TREE_TYPE (type));
1669 1 : domain = TYPE_DOMAIN (type);
1670 1 : if (domain)
1671 : {
1672 1 : t = TYPE_MIN_VALUE (domain);
1673 1 : if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1674 1 : && decl_function_context (t) != info->context)
1675 0 : get_nonlocal_debug_decl (info, t);
1676 1 : t = TYPE_MAX_VALUE (domain);
1677 1 : if (t && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1678 2 : && decl_function_context (t) != info->context)
1679 1 : get_nonlocal_debug_decl (info, t);
1680 : }
1681 : }
1682 2 : }
1683 :
1684 : /* Callback for walk_gimple_stmt. Rewrite all references to VAR and
1685 : PARM_DECLs that belong to outer functions. This handles statements
1686 : that are not handled via the standard recursion done in
1687 : walk_gimple_stmt. STMT is the statement to examine, DATA is as in
1688 : convert_nonlocal_reference_op. Set *HANDLED_OPS_P to true if all the
1689 : operands of STMT have been handled by this function. */
1690 :
1691 : static tree
1692 6260347 : convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1693 : struct walk_stmt_info *wi)
1694 : {
1695 6260347 : struct nesting_info *info = (struct nesting_info *) wi->info;
1696 6260347 : tree save_local_var_chain;
1697 6260347 : bitmap save_suppress;
1698 6260347 : gimple *stmt = gsi_stmt (*gsi);
1699 :
1700 6260347 : switch (gimple_code (stmt))
1701 : {
1702 238766 : case GIMPLE_GOTO:
1703 : /* Don't walk non-local gotos for now. */
1704 238766 : if (TREE_CODE (gimple_goto_dest (stmt)) != LABEL_DECL)
1705 : {
1706 75 : wi->val_only = true;
1707 75 : wi->is_lhs = false;
1708 75 : *handled_ops_p = false;
1709 75 : return NULL_TREE;
1710 : }
1711 : break;
1712 :
1713 53 : case GIMPLE_OMP_TEAMS:
1714 53 : if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
1715 : {
1716 41 : save_suppress = info->suppress_expansion;
1717 41 : convert_nonlocal_omp_clauses (gimple_omp_teams_clauses_ptr (stmt),
1718 : wi);
1719 41 : walk_body (convert_nonlocal_reference_stmt,
1720 : convert_nonlocal_reference_op, info,
1721 : gimple_omp_body_ptr (stmt));
1722 41 : info->suppress_expansion = save_suppress;
1723 41 : break;
1724 : }
1725 : /* FALLTHRU */
1726 :
1727 1380 : case GIMPLE_OMP_PARALLEL:
1728 1380 : case GIMPLE_OMP_TASK:
1729 1380 : save_suppress = info->suppress_expansion;
1730 1380 : if (convert_nonlocal_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
1731 : wi))
1732 : {
1733 164 : tree c, decl;
1734 164 : decl = get_chain_decl (info);
1735 164 : c = build_omp_clause (gimple_location (stmt),
1736 : OMP_CLAUSE_FIRSTPRIVATE);
1737 164 : OMP_CLAUSE_DECL (c) = decl;
1738 164 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
1739 164 : gimple_omp_taskreg_set_clauses (stmt, c);
1740 : }
1741 :
1742 1380 : save_local_var_chain = info->new_local_var_chain;
1743 1380 : info->new_local_var_chain = NULL;
1744 :
1745 1380 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1746 : info, gimple_omp_body_ptr (stmt));
1747 :
1748 1380 : if (info->new_local_var_chain)
1749 122 : declare_vars (info->new_local_var_chain,
1750 : gimple_seq_first_stmt (gimple_omp_body (stmt)),
1751 : false);
1752 1380 : info->new_local_var_chain = save_local_var_chain;
1753 1380 : info->suppress_expansion = save_suppress;
1754 1380 : break;
1755 :
1756 1597 : case GIMPLE_OMP_FOR:
1757 1597 : save_suppress = info->suppress_expansion;
1758 1597 : convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
1759 1597 : walk_gimple_omp_for (as_a <gomp_for *> (stmt),
1760 : convert_nonlocal_reference_stmt,
1761 : convert_nonlocal_reference_op, info);
1762 1597 : walk_body (convert_nonlocal_reference_stmt,
1763 : convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
1764 1597 : info->suppress_expansion = save_suppress;
1765 1597 : break;
1766 :
1767 42 : case GIMPLE_OMP_SECTIONS:
1768 42 : save_suppress = info->suppress_expansion;
1769 42 : convert_nonlocal_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
1770 42 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1771 : info, gimple_omp_body_ptr (stmt));
1772 42 : info->suppress_expansion = save_suppress;
1773 42 : break;
1774 :
1775 185 : case GIMPLE_OMP_SINGLE:
1776 185 : save_suppress = info->suppress_expansion;
1777 185 : convert_nonlocal_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
1778 185 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1779 : info, gimple_omp_body_ptr (stmt));
1780 185 : info->suppress_expansion = save_suppress;
1781 185 : break;
1782 :
1783 0 : case GIMPLE_OMP_SCOPE:
1784 0 : save_suppress = info->suppress_expansion;
1785 0 : convert_nonlocal_omp_clauses (gimple_omp_scope_clauses_ptr (stmt), wi);
1786 0 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1787 : info, gimple_omp_body_ptr (stmt));
1788 0 : info->suppress_expansion = save_suppress;
1789 0 : break;
1790 :
1791 38 : case GIMPLE_OMP_TASKGROUP:
1792 38 : save_suppress = info->suppress_expansion;
1793 38 : convert_nonlocal_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
1794 38 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1795 : info, gimple_omp_body_ptr (stmt));
1796 38 : info->suppress_expansion = save_suppress;
1797 38 : break;
1798 :
1799 1917 : case GIMPLE_OMP_TARGET:
1800 1917 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1801 : info, gimple_omp_target_iterator_loops_ptr (stmt));
1802 1917 : if (!is_gimple_omp_offloaded (stmt))
1803 : {
1804 676 : save_suppress = info->suppress_expansion;
1805 676 : convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1806 : wi);
1807 676 : info->suppress_expansion = save_suppress;
1808 676 : walk_body (convert_nonlocal_reference_stmt,
1809 : convert_nonlocal_reference_op, info,
1810 : gimple_omp_body_ptr (stmt));
1811 676 : break;
1812 : }
1813 1241 : save_suppress = info->suppress_expansion;
1814 1241 : if (convert_nonlocal_omp_clauses (gimple_omp_target_clauses_ptr (stmt),
1815 : wi))
1816 : {
1817 67 : tree c, decl;
1818 67 : decl = get_chain_decl (info);
1819 67 : c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
1820 67 : OMP_CLAUSE_DECL (c) = decl;
1821 67 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
1822 67 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1823 67 : OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
1824 67 : gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
1825 : }
1826 :
1827 1241 : save_local_var_chain = info->new_local_var_chain;
1828 1241 : info->new_local_var_chain = NULL;
1829 :
1830 1241 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1831 : info, gimple_omp_body_ptr (stmt));
1832 :
1833 1241 : if (info->new_local_var_chain)
1834 217 : declare_vars (info->new_local_var_chain,
1835 : gimple_seq_first_stmt (gimple_omp_body (stmt)),
1836 : false);
1837 1241 : info->new_local_var_chain = save_local_var_chain;
1838 1241 : info->suppress_expansion = save_suppress;
1839 1241 : break;
1840 :
1841 600 : case GIMPLE_OMP_SECTION:
1842 600 : case GIMPLE_OMP_STRUCTURED_BLOCK:
1843 600 : case GIMPLE_OMP_MASTER:
1844 600 : case GIMPLE_OMP_MASKED:
1845 600 : case GIMPLE_OMP_ORDERED:
1846 600 : case GIMPLE_OMP_SCAN:
1847 600 : walk_body (convert_nonlocal_reference_stmt, convert_nonlocal_reference_op,
1848 : info, gimple_omp_body_ptr (stmt));
1849 600 : break;
1850 :
1851 290849 : case GIMPLE_BIND:
1852 290849 : {
1853 290849 : gbind *bind_stmt = as_a <gbind *> (stmt);
1854 :
1855 1355652 : for (tree var = gimple_bind_vars (bind_stmt); var; var = DECL_CHAIN (var))
1856 1064803 : if (TREE_CODE (var) == NAMELIST_DECL)
1857 : {
1858 : /* Adjust decls mentioned in NAMELIST_DECL. */
1859 131 : tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
1860 131 : tree decl;
1861 131 : unsigned int i;
1862 :
1863 1065480 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
1864 : {
1865 771 : if (VAR_P (decl)
1866 677 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1867 94 : continue;
1868 583 : if (decl_function_context (decl) != info->context)
1869 7 : CONSTRUCTOR_ELT (decls, i)->value
1870 14 : = get_nonlocal_debug_decl (info, decl);
1871 : }
1872 : }
1873 :
1874 290849 : *handled_ops_p = false;
1875 290849 : return NULL_TREE;
1876 : }
1877 389600 : case GIMPLE_COND:
1878 389600 : wi->val_only = true;
1879 389600 : wi->is_lhs = false;
1880 389600 : *handled_ops_p = false;
1881 389600 : return NULL_TREE;
1882 :
1883 3752791 : case GIMPLE_ASSIGN:
1884 3752791 : if (gimple_clobber_p (stmt))
1885 : {
1886 127200 : tree lhs = gimple_assign_lhs (stmt);
1887 127200 : if (DECL_P (lhs)
1888 127171 : && !(TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
1889 254302 : && decl_function_context (lhs) != info->context)
1890 : {
1891 11 : gsi_replace (gsi, gimple_build_nop (), true);
1892 11 : break;
1893 : }
1894 : }
1895 3752780 : *handled_ops_p = false;
1896 3752780 : return NULL_TREE;
1897 :
1898 1582541 : default:
1899 : /* For every other statement that we are not interested in
1900 : handling here, let the walker traverse the operands. */
1901 1582541 : *handled_ops_p = false;
1902 1582541 : return NULL_TREE;
1903 : }
1904 :
1905 : /* We have handled all of STMT operands, no need to traverse the operands. */
1906 244502 : *handled_ops_p = true;
1907 244502 : return NULL_TREE;
1908 : }
1909 :
1910 :
1911 : /* A subroutine of convert_local_reference. Create a local variable
1912 : in the parent function with DECL_VALUE_EXPR set to reference the
1913 : field in FRAME. This is used both for debug info and in OMP
1914 : lowering. */
1915 :
1916 : static tree
1917 222 : get_local_debug_decl (struct nesting_info *info, tree decl, tree field)
1918 : {
1919 222 : tree x, new_decl;
1920 :
1921 222 : tree *slot = &info->var_map->get_or_insert (decl);
1922 222 : if (*slot)
1923 : return *slot;
1924 :
1925 : /* Make sure frame_decl gets created. */
1926 66 : (void) get_frame_type (info);
1927 66 : x = info->frame_decl;
1928 66 : x = build3 (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
1929 :
1930 66 : new_decl = get_debug_decl (decl);
1931 66 : DECL_CONTEXT (new_decl) = info->context;
1932 :
1933 66 : SET_DECL_VALUE_EXPR (new_decl, x);
1934 66 : DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
1935 66 : *slot = new_decl;
1936 :
1937 66 : DECL_CHAIN (new_decl) = info->debug_var_chain;
1938 66 : info->debug_var_chain = new_decl;
1939 :
1940 : /* Do not emit debug info twice. */
1941 66 : DECL_IGNORED_P (decl) = 1;
1942 :
1943 66 : return new_decl;
1944 : }
1945 :
1946 :
1947 : /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
1948 : and PARM_DECLs that were referenced by inner nested functions.
1949 : The rewrite will be a structure reference to the local frame variable. */
1950 :
1951 : static bool convert_local_omp_clauses (tree *, struct walk_stmt_info *);
1952 :
1953 : static tree
1954 16813750 : convert_local_reference_op (tree *tp, int *walk_subtrees, void *data)
1955 : {
1956 16813750 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1957 16813750 : struct nesting_info *const info = (struct nesting_info *) wi->info;
1958 16813750 : tree t = *tp, field, x;
1959 16813750 : bool save_val_only;
1960 :
1961 16813750 : *walk_subtrees = 0;
1962 16813750 : switch (TREE_CODE (t))
1963 : {
1964 4691642 : case VAR_DECL:
1965 : /* Non-automatic variables are never processed. */
1966 4691642 : if (TREE_STATIC (t) || DECL_EXTERNAL (t))
1967 : break;
1968 : /* FALLTHRU */
1969 :
1970 4700856 : case PARM_DECL:
1971 4700856 : if (t != info->frame_decl && decl_function_context (t) == info->context)
1972 : {
1973 : /* If we copied a pointer to the frame, then the original decl
1974 : is used unchanged in the parent function. */
1975 4698044 : if (use_pointer_in_frame (t))
1976 : break;
1977 :
1978 : /* No need to transform anything if no child references the
1979 : variable. */
1980 4697852 : field = lookup_field_for_decl (info, t, NO_INSERT);
1981 4697852 : if (!field)
1982 : break;
1983 21617 : wi->changed = true;
1984 :
1985 21617 : if (bitmap_bit_p (info->suppress_expansion, DECL_UID (t)))
1986 133 : x = get_local_debug_decl (info, t, field);
1987 : else
1988 21484 : x = get_frame_field (info, info->context, field, &wi->gsi);
1989 :
1990 21617 : if (wi->val_only)
1991 : {
1992 8684 : if (wi->is_lhs)
1993 2835 : x = save_tmp_var (info, x, &wi->gsi);
1994 : else
1995 5849 : x = init_tmp_var (info, x, &wi->gsi);
1996 : }
1997 :
1998 21617 : *tp = x;
1999 : }
2000 : break;
2001 :
2002 740824 : case ADDR_EXPR:
2003 740824 : save_val_only = wi->val_only;
2004 740824 : wi->val_only = false;
2005 740824 : wi->is_lhs = false;
2006 740824 : wi->changed = false;
2007 740824 : walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op, wi, NULL);
2008 740824 : wi->val_only = save_val_only;
2009 :
2010 : /* If we converted anything ... */
2011 740824 : if (wi->changed)
2012 : {
2013 3568 : tree save_context;
2014 :
2015 : /* Then the frame decl is now addressable. */
2016 3568 : TREE_ADDRESSABLE (info->frame_decl) = 1;
2017 :
2018 3568 : save_context = current_function_decl;
2019 3568 : current_function_decl = info->context;
2020 3568 : recompute_tree_invariant_for_addr_expr (t);
2021 :
2022 : /* If we are in a context where we only accept values, then
2023 : compute the address into a temporary. */
2024 3568 : if (save_val_only)
2025 2976 : *tp = gsi_gimplify_val ((struct nesting_info *) wi->info,
2026 : t, &wi->gsi);
2027 3568 : current_function_decl = save_context;
2028 : }
2029 : break;
2030 :
2031 1840680 : case REALPART_EXPR:
2032 1840680 : case IMAGPART_EXPR:
2033 1840680 : case COMPONENT_REF:
2034 1840680 : case ARRAY_REF:
2035 1840680 : case ARRAY_RANGE_REF:
2036 1840680 : case BIT_FIELD_REF:
2037 : /* Go down this entire nest and just look at the final prefix and
2038 : anything that describes the references. Otherwise, we lose track
2039 : of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value. */
2040 1840680 : save_val_only = wi->val_only;
2041 1840680 : wi->val_only = true;
2042 1840680 : wi->is_lhs = false;
2043 5391544 : for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
2044 : {
2045 3550864 : if (TREE_CODE (t) == COMPONENT_REF)
2046 2665955 : walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
2047 : NULL);
2048 884909 : else if (TREE_CODE (t) == ARRAY_REF
2049 884909 : || TREE_CODE (t) == ARRAY_RANGE_REF)
2050 : {
2051 884223 : walk_tree (&TREE_OPERAND (t, 1), convert_local_reference_op, wi,
2052 : NULL);
2053 884223 : walk_tree (&TREE_OPERAND (t, 2), convert_local_reference_op, wi,
2054 : NULL);
2055 884223 : walk_tree (&TREE_OPERAND (t, 3), convert_local_reference_op, wi,
2056 : NULL);
2057 : }
2058 : }
2059 1840680 : wi->val_only = false;
2060 1840680 : walk_tree (tp, convert_local_reference_op, wi, NULL);
2061 1840680 : wi->val_only = save_val_only;
2062 1840680 : break;
2063 :
2064 384351 : case MEM_REF:
2065 384351 : save_val_only = wi->val_only;
2066 384351 : wi->val_only = true;
2067 384351 : wi->is_lhs = false;
2068 384351 : walk_tree (&TREE_OPERAND (t, 0), convert_local_reference_op,
2069 : wi, NULL);
2070 : /* We need to re-fold the MEM_REF as component references as
2071 : part of a ADDR_EXPR address are not allowed. But we cannot
2072 : fold here, as the chain record type is not yet finalized. */
2073 384351 : if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2074 384351 : && !DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
2075 0 : info->mem_refs->add (tp);
2076 384351 : wi->val_only = save_val_only;
2077 384351 : break;
2078 :
2079 882 : case VIEW_CONVERT_EXPR:
2080 : /* Just request to look at the subtrees, leaving val_only and lhs
2081 : untouched. This might actually be for !val_only + lhs, in which
2082 : case we don't want to force a replacement by a temporary. */
2083 882 : *walk_subtrees = 1;
2084 882 : break;
2085 :
2086 8962399 : default:
2087 8962399 : if (!IS_TYPE_OR_DECL_P (t))
2088 : {
2089 6432340 : *walk_subtrees = 1;
2090 6432340 : wi->val_only = true;
2091 6432340 : wi->is_lhs = false;
2092 : }
2093 : break;
2094 : }
2095 :
2096 16813750 : return NULL_TREE;
2097 : }
2098 :
2099 : static tree convert_local_reference_stmt (gimple_stmt_iterator *, bool *,
2100 : struct walk_stmt_info *);
2101 :
2102 : /* Helper for convert_local_reference. Convert all the references in
2103 : the chain of clauses at *PCLAUSES. WI is as in convert_local_reference. */
2104 :
2105 : static bool
2106 5200 : convert_local_omp_clauses (tree *pclauses, struct walk_stmt_info *wi)
2107 : {
2108 5200 : struct nesting_info *const info = (struct nesting_info *) wi->info;
2109 5200 : bool need_frame = false, need_stmts = false;
2110 5200 : tree clause, decl, *pdecl;
2111 5200 : int dummy;
2112 5200 : bitmap new_suppress;
2113 :
2114 5200 : new_suppress = BITMAP_GGC_ALLOC ();
2115 5200 : bitmap_copy (new_suppress, info->suppress_expansion);
2116 :
2117 30855 : for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2118 : {
2119 25655 : pdecl = NULL;
2120 25655 : switch (OMP_CLAUSE_CODE (clause))
2121 : {
2122 663 : case OMP_CLAUSE_REDUCTION:
2123 663 : case OMP_CLAUSE_IN_REDUCTION:
2124 663 : case OMP_CLAUSE_TASK_REDUCTION:
2125 663 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2126 37 : need_stmts = true;
2127 663 : if (TREE_CODE (OMP_CLAUSE_DECL (clause)) == MEM_REF)
2128 : {
2129 6 : pdecl = &TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0);
2130 6 : if (TREE_CODE (*pdecl) == POINTER_PLUS_EXPR)
2131 0 : pdecl = &TREE_OPERAND (*pdecl, 0);
2132 6 : if (INDIRECT_REF_P (*pdecl)
2133 6 : || TREE_CODE (*pdecl) == ADDR_EXPR)
2134 4 : pdecl = &TREE_OPERAND (*pdecl, 0);
2135 : }
2136 663 : goto do_decl_clause;
2137 :
2138 1176 : case OMP_CLAUSE_LASTPRIVATE:
2139 1176 : if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause))
2140 250 : need_stmts = true;
2141 1176 : goto do_decl_clause;
2142 :
2143 391 : case OMP_CLAUSE_LINEAR:
2144 391 : if (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause))
2145 74 : need_stmts = true;
2146 391 : wi->val_only = true;
2147 391 : wi->is_lhs = false;
2148 391 : convert_local_reference_op (&OMP_CLAUSE_LINEAR_STEP (clause), &dummy,
2149 : wi);
2150 391 : goto do_decl_clause;
2151 :
2152 5896 : case OMP_CLAUSE_PRIVATE:
2153 5896 : case OMP_CLAUSE_FIRSTPRIVATE:
2154 5896 : case OMP_CLAUSE_COPYPRIVATE:
2155 5896 : case OMP_CLAUSE_SHARED:
2156 5896 : case OMP_CLAUSE_ENTER:
2157 5896 : case OMP_CLAUSE_LINK:
2158 5896 : case OMP_CLAUSE_USE_DEVICE_PTR:
2159 5896 : case OMP_CLAUSE_USE_DEVICE_ADDR:
2160 5896 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
2161 5896 : case OMP_CLAUSE_IS_DEVICE_PTR:
2162 5896 : case OMP_CLAUSE_DETACH:
2163 5896 : do_decl_clause:
2164 2230 : if (pdecl == NULL)
2165 17466 : pdecl = &OMP_CLAUSE_DECL (clause);
2166 17472 : decl = *pdecl;
2167 17472 : if (VAR_P (decl)
2168 17472 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2169 : break;
2170 16978 : if (decl_function_context (decl) == info->context
2171 16978 : && !use_pointer_in_frame (decl))
2172 : {
2173 16852 : tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2174 16852 : if (field)
2175 : {
2176 87 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_SHARED)
2177 37 : OMP_CLAUSE_SHARED_READONLY (clause) = 0;
2178 87 : bitmap_set_bit (new_suppress, DECL_UID (decl));
2179 87 : *pdecl = get_local_debug_decl (info, decl, field);
2180 87 : need_frame = true;
2181 : }
2182 : }
2183 : break;
2184 :
2185 177 : case OMP_CLAUSE_SCHEDULE:
2186 177 : if (OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clause) == NULL)
2187 : break;
2188 : /* FALLTHRU */
2189 2747 : case OMP_CLAUSE_FINAL:
2190 2747 : case OMP_CLAUSE_IF:
2191 2747 : case OMP_CLAUSE_SELF:
2192 2747 : case OMP_CLAUSE_NUM_THREADS:
2193 2747 : case OMP_CLAUSE_DEPEND:
2194 2747 : case OMP_CLAUSE_DOACROSS:
2195 2747 : case OMP_CLAUSE_DEVICE:
2196 2747 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
2197 2747 : case OMP_CLAUSE_NUM_TEAMS:
2198 2747 : case OMP_CLAUSE_THREAD_LIMIT:
2199 2747 : case OMP_CLAUSE_SAFELEN:
2200 2747 : case OMP_CLAUSE_SIMDLEN:
2201 2747 : case OMP_CLAUSE_PRIORITY:
2202 2747 : case OMP_CLAUSE_GRAINSIZE:
2203 2747 : case OMP_CLAUSE_NUM_TASKS:
2204 2747 : case OMP_CLAUSE_HINT:
2205 2747 : case OMP_CLAUSE_FILTER:
2206 2747 : case OMP_CLAUSE_NUM_GANGS:
2207 2747 : case OMP_CLAUSE_NUM_WORKERS:
2208 2747 : case OMP_CLAUSE_VECTOR_LENGTH:
2209 2747 : case OMP_CLAUSE_GANG:
2210 2747 : case OMP_CLAUSE_WORKER:
2211 2747 : case OMP_CLAUSE_VECTOR:
2212 2747 : case OMP_CLAUSE_ASYNC:
2213 2747 : case OMP_CLAUSE_WAIT:
2214 : /* Several OpenACC clauses have optional arguments. Check if they
2215 : are present. */
2216 2747 : if (OMP_CLAUSE_OPERAND (clause, 0))
2217 : {
2218 2491 : wi->val_only = true;
2219 2491 : wi->is_lhs = false;
2220 2491 : convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2221 : &dummy, wi);
2222 : }
2223 :
2224 : /* The gang clause accepts two arguments. */
2225 2747 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_GANG
2226 2747 : && OMP_CLAUSE_GANG_STATIC_EXPR (clause))
2227 : {
2228 28 : wi->val_only = true;
2229 28 : wi->is_lhs = false;
2230 28 : convert_nonlocal_reference_op
2231 28 : (&OMP_CLAUSE_GANG_STATIC_EXPR (clause), &dummy, wi);
2232 : }
2233 : break;
2234 :
2235 7 : case OMP_CLAUSE_DIST_SCHEDULE:
2236 7 : if (OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (clause) != NULL)
2237 : {
2238 7 : wi->val_only = true;
2239 7 : wi->is_lhs = false;
2240 7 : convert_local_reference_op (&OMP_CLAUSE_OPERAND (clause, 0),
2241 : &dummy, wi);
2242 : }
2243 : break;
2244 :
2245 7782 : case OMP_CLAUSE_MAP:
2246 7782 : case OMP_CLAUSE_TO:
2247 7782 : case OMP_CLAUSE_FROM:
2248 7782 : if (OMP_CLAUSE_SIZE (clause))
2249 : {
2250 7782 : wi->val_only = true;
2251 7782 : wi->is_lhs = false;
2252 7782 : convert_local_reference_op (&OMP_CLAUSE_SIZE (clause),
2253 : &dummy, wi);
2254 : }
2255 7782 : if (DECL_P (OMP_CLAUSE_DECL (clause)))
2256 3666 : goto do_decl_clause;
2257 4116 : wi->val_only = true;
2258 4116 : wi->is_lhs = false;
2259 4116 : walk_tree (&OMP_CLAUSE_DECL (clause), convert_local_reference_op,
2260 : wi, NULL);
2261 4116 : break;
2262 :
2263 6 : case OMP_CLAUSE_ALIGNED:
2264 6 : if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
2265 : {
2266 6 : wi->val_only = true;
2267 6 : wi->is_lhs = false;
2268 6 : convert_local_reference_op
2269 6 : (&OMP_CLAUSE_ALIGNED_ALIGNMENT (clause), &dummy, wi);
2270 : }
2271 : /* FALLTHRU */
2272 42 : case OMP_CLAUSE_NONTEMPORAL:
2273 0 : do_decl_clause_no_supp:
2274 : /* Like do_decl_clause, but don't add any suppression. */
2275 42 : decl = OMP_CLAUSE_DECL (clause);
2276 42 : if (VAR_P (decl)
2277 42 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2278 : break;
2279 30 : if (decl_function_context (decl) == info->context
2280 30 : && !use_pointer_in_frame (decl))
2281 : {
2282 30 : tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2283 30 : if (field)
2284 : {
2285 0 : OMP_CLAUSE_DECL (clause)
2286 0 : = get_local_debug_decl (info, decl, field);
2287 0 : need_frame = true;
2288 : }
2289 : }
2290 : break;
2291 :
2292 36 : case OMP_CLAUSE_ALLOCATE:
2293 36 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause))
2294 : {
2295 36 : wi->val_only = true;
2296 36 : wi->is_lhs = false;
2297 36 : convert_local_reference_op
2298 36 : (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (clause), &dummy, wi);
2299 : }
2300 36 : goto do_decl_clause_no_supp;
2301 :
2302 : case OMP_CLAUSE_NOWAIT:
2303 : case OMP_CLAUSE_ORDERED:
2304 : case OMP_CLAUSE_DEFAULT:
2305 : case OMP_CLAUSE_COPYIN:
2306 : case OMP_CLAUSE_COLLAPSE:
2307 : case OMP_CLAUSE_TILE:
2308 : case OMP_CLAUSE_UNTIED:
2309 : case OMP_CLAUSE_MERGEABLE:
2310 : case OMP_CLAUSE_PROC_BIND:
2311 : case OMP_CLAUSE_NOGROUP:
2312 : case OMP_CLAUSE_THREADS:
2313 : case OMP_CLAUSE_SIMD:
2314 : case OMP_CLAUSE_DEFAULTMAP:
2315 : case OMP_CLAUSE_ORDER:
2316 : case OMP_CLAUSE_SEQ:
2317 : case OMP_CLAUSE_INDEPENDENT:
2318 : case OMP_CLAUSE_AUTO:
2319 : case OMP_CLAUSE_IF_PRESENT:
2320 : case OMP_CLAUSE_FINALIZE:
2321 : case OMP_CLAUSE_BIND:
2322 : case OMP_CLAUSE__CONDTEMP_:
2323 : case OMP_CLAUSE__SCANTEMP_:
2324 : break;
2325 :
2326 : /* The following clause belongs to the OpenACC cache directive, which
2327 : is discarded during gimplification. */
2328 0 : case OMP_CLAUSE__CACHE_:
2329 : /* The following clauses are only allowed in the OpenMP declare simd
2330 : directive, so not seen here. */
2331 0 : case OMP_CLAUSE_UNIFORM:
2332 0 : case OMP_CLAUSE_INBRANCH:
2333 0 : case OMP_CLAUSE_NOTINBRANCH:
2334 : /* The following clauses are only allowed on OpenMP cancel and
2335 : cancellation point directives, which at this point have already
2336 : been lowered into a function call. */
2337 0 : case OMP_CLAUSE_FOR:
2338 0 : case OMP_CLAUSE_PARALLEL:
2339 0 : case OMP_CLAUSE_SECTIONS:
2340 0 : case OMP_CLAUSE_TASKGROUP:
2341 : /* The following clauses are only added during OMP lowering; nested
2342 : function decomposition happens before that. */
2343 0 : case OMP_CLAUSE__LOOPTEMP_:
2344 0 : case OMP_CLAUSE__REDUCTEMP_:
2345 0 : case OMP_CLAUSE__SIMDUID_:
2346 0 : case OMP_CLAUSE__SIMT_:
2347 : /* The following clauses are only allowed on OpenACC 'routine'
2348 : directives, not seen here. */
2349 0 : case OMP_CLAUSE_NOHOST:
2350 : /* Anything else. */
2351 0 : default:
2352 0 : gcc_unreachable ();
2353 : }
2354 : }
2355 :
2356 5200 : info->suppress_expansion = new_suppress;
2357 :
2358 5200 : if (need_stmts)
2359 1547 : for (clause = *pclauses; clause ; clause = OMP_CLAUSE_CHAIN (clause))
2360 1262 : switch (OMP_CLAUSE_CODE (clause))
2361 : {
2362 103 : case OMP_CLAUSE_REDUCTION:
2363 103 : case OMP_CLAUSE_IN_REDUCTION:
2364 103 : case OMP_CLAUSE_TASK_REDUCTION:
2365 103 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2366 : {
2367 37 : tree old_context
2368 37 : = DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause));
2369 37 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2370 37 : = info->context;
2371 37 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2372 0 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2373 0 : = info->context;
2374 37 : walk_body (convert_local_reference_stmt,
2375 : convert_local_reference_op, info,
2376 37 : &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (clause));
2377 37 : walk_body (convert_local_reference_stmt,
2378 : convert_local_reference_op, info,
2379 37 : &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (clause));
2380 37 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_PLACEHOLDER (clause))
2381 37 : = old_context;
2382 37 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2383 0 : DECL_CONTEXT (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clause))
2384 0 : = old_context;
2385 : }
2386 : break;
2387 :
2388 410 : case OMP_CLAUSE_LASTPRIVATE:
2389 410 : walk_body (convert_local_reference_stmt,
2390 : convert_local_reference_op, info,
2391 410 : &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (clause));
2392 410 : break;
2393 :
2394 215 : case OMP_CLAUSE_LINEAR:
2395 215 : walk_body (convert_local_reference_stmt,
2396 : convert_local_reference_op, info,
2397 215 : &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (clause));
2398 215 : break;
2399 :
2400 : default:
2401 : break;
2402 : }
2403 :
2404 5200 : return need_frame;
2405 : }
2406 :
2407 :
2408 : /* Called via walk_function+walk_gimple_stmt, rewrite all references to VAR
2409 : and PARM_DECLs that were referenced by inner nested functions.
2410 : The rewrite will be a structure reference to the local frame variable. */
2411 :
2412 : static tree
2413 6269372 : convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2414 : struct walk_stmt_info *wi)
2415 : {
2416 6269372 : struct nesting_info *info = (struct nesting_info *) wi->info;
2417 6269372 : tree save_local_var_chain;
2418 6269372 : bitmap save_suppress;
2419 6269372 : char save_static_chain_added;
2420 6269372 : bool frame_decl_added;
2421 6269372 : gimple *stmt = gsi_stmt (*gsi);
2422 :
2423 6269372 : switch (gimple_code (stmt))
2424 : {
2425 53 : case GIMPLE_OMP_TEAMS:
2426 53 : if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2427 : {
2428 41 : save_suppress = info->suppress_expansion;
2429 41 : convert_local_omp_clauses (gimple_omp_teams_clauses_ptr (stmt), wi);
2430 41 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2431 : info, gimple_omp_body_ptr (stmt));
2432 41 : info->suppress_expansion = save_suppress;
2433 41 : break;
2434 : }
2435 : /* FALLTHRU */
2436 :
2437 1380 : case GIMPLE_OMP_PARALLEL:
2438 1380 : case GIMPLE_OMP_TASK:
2439 1380 : save_suppress = info->suppress_expansion;
2440 1380 : frame_decl_added = false;
2441 1380 : if (convert_local_omp_clauses (gimple_omp_taskreg_clauses_ptr (stmt),
2442 : wi))
2443 : {
2444 41 : tree c = build_omp_clause (gimple_location (stmt),
2445 : OMP_CLAUSE_SHARED);
2446 41 : (void) get_frame_type (info);
2447 41 : OMP_CLAUSE_DECL (c) = info->frame_decl;
2448 41 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2449 41 : gimple_omp_taskreg_set_clauses (stmt, c);
2450 41 : info->static_chain_added |= 4;
2451 41 : frame_decl_added = true;
2452 : }
2453 :
2454 1380 : save_local_var_chain = info->new_local_var_chain;
2455 1380 : save_static_chain_added = info->static_chain_added;
2456 1380 : info->new_local_var_chain = NULL;
2457 1380 : info->static_chain_added = 0;
2458 :
2459 1380 : walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2460 : gimple_omp_body_ptr (stmt));
2461 :
2462 1380 : if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2463 : {
2464 1 : tree c = build_omp_clause (gimple_location (stmt),
2465 : OMP_CLAUSE_SHARED);
2466 1 : (void) get_frame_type (info);
2467 1 : OMP_CLAUSE_DECL (c) = info->frame_decl;
2468 1 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2469 1 : info->static_chain_added |= 4;
2470 1 : gimple_omp_taskreg_set_clauses (stmt, c);
2471 : }
2472 1380 : if (info->new_local_var_chain)
2473 20 : declare_vars (info->new_local_var_chain,
2474 : gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2475 1380 : info->new_local_var_chain = save_local_var_chain;
2476 1380 : info->suppress_expansion = save_suppress;
2477 1380 : info->static_chain_added |= save_static_chain_added;
2478 1380 : break;
2479 :
2480 1597 : case GIMPLE_OMP_FOR:
2481 1597 : save_suppress = info->suppress_expansion;
2482 1597 : convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
2483 1597 : walk_gimple_omp_for (as_a <gomp_for *> (stmt),
2484 : convert_local_reference_stmt,
2485 : convert_local_reference_op, info);
2486 1597 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2487 : info, gimple_omp_body_ptr (stmt));
2488 1597 : info->suppress_expansion = save_suppress;
2489 1597 : break;
2490 :
2491 42 : case GIMPLE_OMP_SECTIONS:
2492 42 : save_suppress = info->suppress_expansion;
2493 42 : convert_local_omp_clauses (gimple_omp_sections_clauses_ptr (stmt), wi);
2494 42 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2495 : info, gimple_omp_body_ptr (stmt));
2496 42 : info->suppress_expansion = save_suppress;
2497 42 : break;
2498 :
2499 185 : case GIMPLE_OMP_SINGLE:
2500 185 : save_suppress = info->suppress_expansion;
2501 185 : convert_local_omp_clauses (gimple_omp_single_clauses_ptr (stmt), wi);
2502 185 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2503 : info, gimple_omp_body_ptr (stmt));
2504 185 : info->suppress_expansion = save_suppress;
2505 185 : break;
2506 :
2507 0 : case GIMPLE_OMP_SCOPE:
2508 0 : save_suppress = info->suppress_expansion;
2509 0 : convert_local_omp_clauses (gimple_omp_scope_clauses_ptr (stmt), wi);
2510 0 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2511 : info, gimple_omp_body_ptr (stmt));
2512 0 : info->suppress_expansion = save_suppress;
2513 0 : break;
2514 :
2515 38 : case GIMPLE_OMP_TASKGROUP:
2516 38 : save_suppress = info->suppress_expansion;
2517 38 : convert_local_omp_clauses (gimple_omp_taskgroup_clauses_ptr (stmt), wi);
2518 38 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2519 : info, gimple_omp_body_ptr (stmt));
2520 38 : info->suppress_expansion = save_suppress;
2521 38 : break;
2522 :
2523 1917 : case GIMPLE_OMP_TARGET:
2524 1917 : walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2525 : gimple_omp_target_iterator_loops_ptr (stmt));
2526 :
2527 1917 : if (!is_gimple_omp_offloaded (stmt))
2528 : {
2529 676 : save_suppress = info->suppress_expansion;
2530 676 : convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi);
2531 676 : info->suppress_expansion = save_suppress;
2532 676 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2533 : info, gimple_omp_body_ptr (stmt));
2534 676 : break;
2535 : }
2536 1241 : save_suppress = info->suppress_expansion;
2537 1241 : frame_decl_added = false;
2538 1241 : if (convert_local_omp_clauses (gimple_omp_target_clauses_ptr (stmt), wi))
2539 : {
2540 0 : tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2541 0 : (void) get_frame_type (info);
2542 0 : OMP_CLAUSE_DECL (c) = info->frame_decl;
2543 0 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2544 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2545 0 : OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2546 0 : gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2547 0 : info->static_chain_added |= 4;
2548 0 : frame_decl_added = true;
2549 : }
2550 :
2551 1241 : save_local_var_chain = info->new_local_var_chain;
2552 1241 : save_static_chain_added = info->static_chain_added;
2553 1241 : info->new_local_var_chain = NULL;
2554 1241 : info->static_chain_added = 0;
2555 :
2556 1241 : walk_body (convert_local_reference_stmt, convert_local_reference_op, info,
2557 : gimple_omp_body_ptr (stmt));
2558 :
2559 1241 : if ((info->static_chain_added & 4) != 0 && !frame_decl_added)
2560 : {
2561 0 : tree c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
2562 0 : (void) get_frame_type (info);
2563 0 : OMP_CLAUSE_DECL (c) = info->frame_decl;
2564 0 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2565 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (info->frame_decl);
2566 0 : OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2567 0 : gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt), c);
2568 0 : info->static_chain_added |= 4;
2569 : }
2570 :
2571 1241 : if (info->new_local_var_chain)
2572 2 : declare_vars (info->new_local_var_chain,
2573 : gimple_seq_first_stmt (gimple_omp_body (stmt)), false);
2574 1241 : info->new_local_var_chain = save_local_var_chain;
2575 1241 : info->suppress_expansion = save_suppress;
2576 1241 : info->static_chain_added |= save_static_chain_added;
2577 1241 : break;
2578 :
2579 600 : case GIMPLE_OMP_SECTION:
2580 600 : case GIMPLE_OMP_STRUCTURED_BLOCK:
2581 600 : case GIMPLE_OMP_MASTER:
2582 600 : case GIMPLE_OMP_MASKED:
2583 600 : case GIMPLE_OMP_ORDERED:
2584 600 : case GIMPLE_OMP_SCAN:
2585 600 : walk_body (convert_local_reference_stmt, convert_local_reference_op,
2586 : info, gimple_omp_body_ptr (stmt));
2587 600 : break;
2588 :
2589 389600 : case GIMPLE_COND:
2590 389600 : wi->val_only = true;
2591 389600 : wi->is_lhs = false;
2592 389600 : *handled_ops_p = false;
2593 389600 : return NULL_TREE;
2594 :
2595 3761803 : case GIMPLE_ASSIGN:
2596 3761803 : if (gimple_clobber_p (stmt))
2597 : {
2598 127189 : tree lhs = gimple_assign_lhs (stmt);
2599 127189 : if (DECL_P (lhs)
2600 127160 : && decl_function_context (lhs) == info->context
2601 127158 : && !use_pointer_in_frame (lhs)
2602 254347 : && lookup_field_for_decl (info, lhs, NO_INSERT))
2603 : {
2604 1620 : gsi_replace (gsi, gimple_build_nop (), true);
2605 1620 : break;
2606 : }
2607 : }
2608 3760183 : *handled_ops_p = false;
2609 3760183 : return NULL_TREE;
2610 :
2611 290851 : case GIMPLE_BIND:
2612 290851 : for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
2613 1356940 : var;
2614 1066089 : var = DECL_CHAIN (var))
2615 1066089 : if (TREE_CODE (var) == NAMELIST_DECL)
2616 : {
2617 : /* Adjust decls mentioned in NAMELIST_DECL. */
2618 131 : tree decls = NAMELIST_DECL_ASSOCIATED_DECL (var);
2619 131 : tree decl;
2620 131 : unsigned int i;
2621 :
2622 1066766 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (decls), i, decl)
2623 : {
2624 771 : if (VAR_P (decl)
2625 677 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2626 94 : continue;
2627 583 : if (decl_function_context (decl) == info->context
2628 583 : && !use_pointer_in_frame (decl))
2629 : {
2630 583 : tree field = lookup_field_for_decl (info, decl, NO_INSERT);
2631 583 : if (field)
2632 : {
2633 2 : CONSTRUCTOR_ELT (decls, i)->value
2634 4 : = get_local_debug_decl (info, decl, field);
2635 : }
2636 : }
2637 : }
2638 : }
2639 :
2640 290851 : *handled_ops_p = false;
2641 290851 : return NULL_TREE;
2642 :
2643 1821318 : default:
2644 : /* For every other statement that we are not interested in
2645 : handling here, let the walker traverse the operands. */
2646 1821318 : *handled_ops_p = false;
2647 1821318 : return NULL_TREE;
2648 : }
2649 :
2650 : /* Indicate that we have handled all the operands ourselves. */
2651 7420 : *handled_ops_p = true;
2652 7420 : return NULL_TREE;
2653 : }
2654 :
2655 :
2656 : /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_GOTOs
2657 : that reference labels from outer functions. The rewrite will be a
2658 : call to __builtin_nonlocal_goto. */
2659 :
2660 : static tree
2661 6272446 : convert_nl_goto_reference (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2662 : struct walk_stmt_info *wi)
2663 : {
2664 6272446 : struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2665 6272446 : tree label, new_label, target_context, x, field;
2666 6272446 : gcall *call;
2667 6272446 : gimple *stmt = gsi_stmt (*gsi);
2668 :
2669 6272446 : if (gimple_code (stmt) != GIMPLE_GOTO)
2670 : {
2671 6033778 : *handled_ops_p = false;
2672 6033778 : return NULL_TREE;
2673 : }
2674 :
2675 238668 : label = gimple_goto_dest (stmt);
2676 238668 : if (TREE_CODE (label) != LABEL_DECL)
2677 : {
2678 75 : *handled_ops_p = false;
2679 75 : return NULL_TREE;
2680 : }
2681 :
2682 238593 : target_context = decl_function_context (label);
2683 238593 : if (target_context == info->context)
2684 : {
2685 238071 : *handled_ops_p = false;
2686 238071 : return NULL_TREE;
2687 : }
2688 :
2689 522 : for (i = info->outer; target_context != i->context; i = i->outer)
2690 0 : continue;
2691 :
2692 : /* The original user label may also be use for a normal goto, therefore
2693 : we must create a new label that will actually receive the abnormal
2694 : control transfer. This new label will be marked LABEL_NONLOCAL; this
2695 : mark will trigger proper behavior in the cfg, as well as cause the
2696 : (hairy target-specific) non-local goto receiver code to be generated
2697 : when we expand rtl. Enter this association into var_map so that we
2698 : can insert the new label into the IL during a second pass. */
2699 522 : tree *slot = &i->var_map->get_or_insert (label);
2700 522 : if (*slot == NULL)
2701 : {
2702 479 : new_label = create_artificial_label (UNKNOWN_LOCATION);
2703 479 : DECL_NONLOCAL (new_label) = 1;
2704 479 : DECL_CONTEXT (new_label) = target_context;
2705 479 : *slot = new_label;
2706 : }
2707 : else
2708 : new_label = *slot;
2709 :
2710 : /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field). */
2711 522 : field = get_nl_goto_field (i);
2712 522 : x = get_frame_field (info, target_context, field, gsi);
2713 522 : x = build_addr (x);
2714 522 : x = gsi_gimplify_val (info, x, gsi);
2715 1044 : call = gimple_build_call (builtin_decl_implicit (BUILT_IN_NONLOCAL_GOTO),
2716 : 2, build_addr (new_label), x);
2717 522 : gsi_replace (gsi, call, false);
2718 :
2719 : /* We have handled all of STMT's operands, no need to keep going. */
2720 522 : *handled_ops_p = true;
2721 522 : return NULL_TREE;
2722 0 : }
2723 :
2724 :
2725 : /* Called via walk_function+walk_tree, rewrite all GIMPLE_LABELs whose labels
2726 : are referenced via nonlocal goto from a nested function. The rewrite
2727 : will involve installing a newly generated DECL_NONLOCAL label, and
2728 : (potentially) a branch around the rtl gunk that is assumed to be
2729 : attached to such a label. */
2730 :
2731 : static tree
2732 6272968 : convert_nl_goto_receiver (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2733 : struct walk_stmt_info *wi)
2734 : {
2735 6272968 : struct nesting_info *const info = (struct nesting_info *) wi->info;
2736 6272968 : tree label, new_label;
2737 6272968 : gimple_stmt_iterator tmp_gsi;
2738 6272968 : glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsi));
2739 :
2740 6272968 : if (!stmt)
2741 : {
2742 5175551 : *handled_ops_p = false;
2743 5175551 : return NULL_TREE;
2744 : }
2745 :
2746 1097417 : label = gimple_label_label (stmt);
2747 :
2748 1097417 : tree *slot = info->var_map->get (label);
2749 1097417 : if (!slot)
2750 : {
2751 1096938 : *handled_ops_p = false;
2752 1096938 : return NULL_TREE;
2753 : }
2754 :
2755 : /* If there's any possibility that the previous statement falls through,
2756 : then we must branch around the new non-local label. */
2757 479 : tmp_gsi = wi->gsi;
2758 479 : gsi_prev (&tmp_gsi);
2759 479 : if (gsi_end_p (tmp_gsi) || gimple_stmt_may_fallthru (gsi_stmt (tmp_gsi)))
2760 : {
2761 268 : gimple *stmt = gimple_build_goto (label);
2762 268 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2763 : }
2764 :
2765 479 : new_label = (tree) *slot;
2766 479 : stmt = gimple_build_label (new_label);
2767 479 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2768 :
2769 479 : *handled_ops_p = true;
2770 479 : return NULL_TREE;
2771 : }
2772 :
2773 :
2774 : /* Called via walk_function+walk_stmt, rewrite all references to addresses
2775 : of nested functions that require the use of trampolines. The rewrite
2776 : will involve a reference a trampoline generated for the occasion. */
2777 :
2778 : static tree
2779 20085615 : convert_tramp_reference_op (tree *tp, int *walk_subtrees, void *data)
2780 : {
2781 20085615 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
2782 20085615 : struct nesting_info *const info = (struct nesting_info *) wi->info, *i;
2783 20085615 : tree t = *tp, decl, target_context, x, builtin;
2784 20085615 : bool descr;
2785 20085615 : gcall *call;
2786 :
2787 20085615 : *walk_subtrees = 0;
2788 20085615 : switch (TREE_CODE (t))
2789 : {
2790 384756 : case ADDR_EXPR:
2791 : /* Build
2792 : T.1 = &CHAIN->tramp;
2793 : T.2 = __builtin_adjust_trampoline (T.1);
2794 : T.3 = (func_type)T.2;
2795 : */
2796 :
2797 384756 : decl = TREE_OPERAND (t, 0);
2798 384756 : if (TREE_CODE (decl) != FUNCTION_DECL)
2799 : break;
2800 :
2801 : /* Only need to process nested functions. */
2802 2793 : target_context = decl_function_context (decl);
2803 2793 : if (!target_context)
2804 : break;
2805 :
2806 : /* If the nested function doesn't use a static chain, then
2807 : it doesn't need a trampoline. */
2808 2318 : if (!DECL_STATIC_CHAIN (decl))
2809 : break;
2810 :
2811 : /* If we don't want a trampoline, then don't build one. */
2812 508 : if (TREE_NO_TRAMPOLINE (t))
2813 : break;
2814 :
2815 : /* Lookup the immediate parent of the callee, as that's where
2816 : we need to insert the trampoline. */
2817 350 : for (i = info; i->context != target_context; i = i->outer)
2818 23 : continue;
2819 :
2820 : /* Decide whether to generate a descriptor or a trampoline. */
2821 327 : descr = FUNC_ADDR_BY_DESCRIPTOR (t) && !flag_trampolines;
2822 :
2823 327 : if (descr)
2824 0 : x = lookup_descr_for_decl (i, decl, INSERT);
2825 : else
2826 327 : x = lookup_tramp_for_decl (i, decl, INSERT);
2827 :
2828 : /* Compute the address of the field holding the trampoline. */
2829 327 : x = get_frame_field (info, target_context, x, &wi->gsi);
2830 :
2831 : /* APB: We don't need to do the adjustment calls when using off-stack
2832 : trampolines, any such adjustment will be done when the off-stack
2833 : trampoline is created. */
2834 327 : if (!descr && flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
2835 3 : x = gsi_gimplify_val (info, x, &wi->gsi);
2836 : else
2837 : {
2838 324 : x = build_addr (x);
2839 :
2840 324 : x = gsi_gimplify_val (info, x, &wi->gsi);
2841 :
2842 : /* Do machine-specific ugliness. Normally this will involve
2843 : computing extra alignment, but it can really be anything. */
2844 324 : if (descr)
2845 0 : builtin = builtin_decl_implicit (BUILT_IN_ADJUST_DESCRIPTOR);
2846 : else
2847 324 : builtin = builtin_decl_implicit (BUILT_IN_ADJUST_TRAMPOLINE);
2848 324 : call = gimple_build_call (builtin, 1, x);
2849 324 : x = init_tmp_var_with_call (info, &wi->gsi, call);
2850 : }
2851 :
2852 : /* Cast back to the proper function type. */
2853 327 : x = build1 (NOP_EXPR, TREE_TYPE (t), x);
2854 327 : x = init_tmp_var (info, x, &wi->gsi);
2855 :
2856 327 : *tp = x;
2857 327 : break;
2858 :
2859 19700859 : default:
2860 19700859 : if (!IS_TYPE_OR_DECL_P (t))
2861 10214732 : *walk_subtrees = 1;
2862 : break;
2863 : }
2864 :
2865 20085615 : return NULL_TREE;
2866 : }
2867 :
2868 :
2869 : /* Called via walk_function+walk_gimple_stmt, rewrite all references
2870 : to addresses of nested functions that require the use of
2871 : trampolines. The rewrite will involve a reference a trampoline
2872 : generated for the occasion. */
2873 :
2874 : static tree
2875 6304293 : convert_tramp_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2876 : struct walk_stmt_info *wi)
2877 : {
2878 6304293 : struct nesting_info *info = (struct nesting_info *) wi->info;
2879 6304293 : gimple *stmt = gsi_stmt (*gsi);
2880 :
2881 6304293 : switch (gimple_code (stmt))
2882 : {
2883 397273 : case GIMPLE_CALL:
2884 397273 : {
2885 : /* Only walk call arguments, lest we generate trampolines for
2886 : direct calls. */
2887 397273 : unsigned long i, nargs = gimple_call_num_args (stmt);
2888 1349004 : for (i = 0; i < nargs; i++)
2889 951731 : walk_tree (gimple_call_arg_ptr (stmt, i), convert_tramp_reference_op,
2890 : wi, NULL);
2891 : break;
2892 : }
2893 :
2894 53 : case GIMPLE_OMP_TEAMS:
2895 53 : if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
2896 : {
2897 41 : *handled_ops_p = false;
2898 41 : return NULL_TREE;
2899 : }
2900 12 : goto do_parallel;
2901 :
2902 1917 : case GIMPLE_OMP_TARGET:
2903 1917 : if (!is_gimple_omp_offloaded (stmt))
2904 : {
2905 676 : *handled_ops_p = false;
2906 676 : return NULL_TREE;
2907 : }
2908 1241 : walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2909 : info, gimple_omp_target_iterator_loops_ptr (stmt));
2910 : /* FALLTHRU */
2911 2637 : case GIMPLE_OMP_PARALLEL:
2912 2637 : case GIMPLE_OMP_TASK:
2913 2637 : do_parallel:
2914 2637 : {
2915 2637 : tree save_local_var_chain = info->new_local_var_chain;
2916 2637 : walk_gimple_op (stmt, convert_tramp_reference_op, wi);
2917 2637 : info->new_local_var_chain = NULL;
2918 2637 : char save_static_chain_added = info->static_chain_added;
2919 2637 : info->static_chain_added = 0;
2920 2637 : walk_body (convert_tramp_reference_stmt, convert_tramp_reference_op,
2921 : info, gimple_omp_body_ptr (stmt));
2922 2637 : if (info->new_local_var_chain)
2923 6 : declare_vars (info->new_local_var_chain,
2924 : gimple_seq_first_stmt (gimple_omp_body (stmt)),
2925 : false);
2926 7911 : for (int i = 0; i < 2; i++)
2927 : {
2928 5274 : tree c, decl;
2929 5274 : if ((info->static_chain_added & (1 << i)) == 0)
2930 5268 : continue;
2931 6 : decl = i ? get_chain_decl (info) : info->frame_decl;
2932 : /* Don't add CHAIN.* or FRAME.* twice. */
2933 6 : if (gimple_code (stmt) == GIMPLE_OMP_TARGET)
2934 0 : c = gimple_omp_target_clauses (stmt);
2935 : else
2936 6 : c = gimple_omp_taskreg_clauses (stmt);
2937 6 : for (; c; c = OMP_CLAUSE_CHAIN (c))
2938 1 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
2939 1 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
2940 2 : && OMP_CLAUSE_DECL (c) == decl)
2941 : break;
2942 6 : if (c == NULL && gimple_code (stmt) != GIMPLE_OMP_TARGET)
2943 : {
2944 8 : c = build_omp_clause (gimple_location (stmt),
2945 : i ? OMP_CLAUSE_FIRSTPRIVATE
2946 : : OMP_CLAUSE_SHARED);
2947 5 : OMP_CLAUSE_DECL (c) = decl;
2948 5 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2949 5 : gimple_omp_taskreg_set_clauses (stmt, c);
2950 : }
2951 1 : else if (c == NULL)
2952 : {
2953 0 : c = build_omp_clause (gimple_location (stmt),
2954 : OMP_CLAUSE_MAP);
2955 0 : OMP_CLAUSE_DECL (c) = decl;
2956 0 : OMP_CLAUSE_SET_MAP_KIND (c,
2957 : i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
2958 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
2959 0 : OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
2960 0 : gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
2961 : c);
2962 : }
2963 : }
2964 2637 : info->new_local_var_chain = save_local_var_chain;
2965 2637 : info->static_chain_added |= save_static_chain_added;
2966 : }
2967 2637 : break;
2968 :
2969 5903666 : default:
2970 5903666 : *handled_ops_p = false;
2971 5903666 : return NULL_TREE;
2972 : }
2973 :
2974 399910 : *handled_ops_p = true;
2975 399910 : return NULL_TREE;
2976 : }
2977 :
2978 :
2979 :
2980 : /* Called via walk_function+walk_gimple_stmt, rewrite all GIMPLE_CALLs
2981 : that reference nested functions to make sure that the static chain
2982 : is set up properly for the call. */
2983 :
2984 : static tree
2985 6304971 : convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2986 : struct walk_stmt_info *wi)
2987 : {
2988 6304971 : struct nesting_info *const info = (struct nesting_info *) wi->info;
2989 6304971 : tree decl, target_context;
2990 6304971 : char save_static_chain_added;
2991 6304971 : int i;
2992 6304971 : gimple *stmt = gsi_stmt (*gsi);
2993 :
2994 6304971 : switch (gimple_code (stmt))
2995 : {
2996 397597 : case GIMPLE_CALL:
2997 397597 : if (gimple_call_chain (stmt))
2998 : break;
2999 397389 : decl = gimple_call_fndecl (stmt);
3000 397389 : if (!decl)
3001 : break;
3002 360926 : target_context = decl_function_context (decl);
3003 413969 : if (target_context && DECL_STATIC_CHAIN (decl))
3004 : {
3005 : struct nesting_info *i = info;
3006 18581 : while (i && i->context != target_context)
3007 2189 : i = i->outer;
3008 : /* If none of the outer contexts is the target context, this means
3009 : that the function is called in a wrong context. */
3010 16392 : if (!i)
3011 0 : internal_error ("%s from %s called in %s",
3012 0 : IDENTIFIER_POINTER (DECL_NAME (decl)),
3013 0 : IDENTIFIER_POINTER (DECL_NAME (target_context)),
3014 0 : IDENTIFIER_POINTER (DECL_NAME (info->context)));
3015 :
3016 16392 : gimple_call_set_chain (as_a <gcall *> (stmt),
3017 : get_static_chain (info, target_context,
3018 : &wi->gsi));
3019 30635 : info->static_chain_added |= (1 << (info->context != target_context));
3020 : }
3021 : break;
3022 :
3023 53 : case GIMPLE_OMP_TEAMS:
3024 53 : if (!gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
3025 : {
3026 41 : walk_body (convert_gimple_call, NULL, info,
3027 : gimple_omp_body_ptr (stmt));
3028 41 : break;
3029 : }
3030 : /* FALLTHRU */
3031 :
3032 1396 : case GIMPLE_OMP_PARALLEL:
3033 1396 : case GIMPLE_OMP_TASK:
3034 1396 : save_static_chain_added = info->static_chain_added;
3035 1396 : info->static_chain_added = 0;
3036 1396 : walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3037 4188 : for (i = 0; i < 2; i++)
3038 : {
3039 2792 : tree c, decl;
3040 2792 : if ((info->static_chain_added & (1 << i)) == 0)
3041 2722 : continue;
3042 70 : decl = i ? get_chain_decl (info) : info->frame_decl;
3043 : /* Don't add CHAIN.* or FRAME.* twice. */
3044 70 : for (c = gimple_omp_taskreg_clauses (stmt);
3045 750 : c;
3046 680 : c = OMP_CLAUSE_CHAIN (c))
3047 682 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
3048 270 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
3049 742 : && OMP_CLAUSE_DECL (c) == decl)
3050 : break;
3051 70 : if (c == NULL)
3052 : {
3053 112 : c = build_omp_clause (gimple_location (stmt),
3054 : i ? OMP_CLAUSE_FIRSTPRIVATE
3055 : : OMP_CLAUSE_SHARED);
3056 68 : OMP_CLAUSE_DECL (c) = decl;
3057 68 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
3058 68 : gimple_omp_taskreg_set_clauses (stmt, c);
3059 : }
3060 : }
3061 1396 : info->static_chain_added |= save_static_chain_added;
3062 1396 : break;
3063 :
3064 1917 : case GIMPLE_OMP_TARGET:
3065 1917 : if (!is_gimple_omp_offloaded (stmt))
3066 : {
3067 676 : walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3068 676 : break;
3069 : }
3070 1241 : save_static_chain_added = info->static_chain_added;
3071 1241 : info->static_chain_added = 0;
3072 1241 : walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3073 3723 : for (i = 0; i < 2; i++)
3074 : {
3075 2482 : tree c, decl;
3076 2482 : if ((info->static_chain_added & (1 << i)) == 0)
3077 2461 : continue;
3078 21 : decl = i ? get_chain_decl (info) : info->frame_decl;
3079 : /* Don't add CHAIN.* or FRAME.* twice. */
3080 21 : for (c = gimple_omp_target_clauses (stmt);
3081 154 : c;
3082 133 : c = OMP_CLAUSE_CHAIN (c))
3083 133 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
3084 133 : && OMP_CLAUSE_DECL (c) == decl)
3085 : break;
3086 21 : if (c == NULL)
3087 : {
3088 21 : c = build_omp_clause (gimple_location (stmt), OMP_CLAUSE_MAP);
3089 21 : OMP_CLAUSE_DECL (c) = decl;
3090 32 : OMP_CLAUSE_SET_MAP_KIND (c, i ? GOMP_MAP_TO : GOMP_MAP_TOFROM);
3091 21 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
3092 21 : OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (stmt);
3093 21 : gimple_omp_target_set_clauses (as_a <gomp_target *> (stmt),
3094 : c);
3095 : }
3096 : }
3097 1241 : info->static_chain_added |= save_static_chain_added;
3098 1241 : break;
3099 :
3100 1602 : case GIMPLE_OMP_FOR:
3101 1602 : walk_body (convert_gimple_call, NULL, info,
3102 : gimple_omp_for_pre_body_ptr (stmt));
3103 : /* FALLTHRU */
3104 2548 : case GIMPLE_OMP_SECTIONS:
3105 2548 : case GIMPLE_OMP_SECTION:
3106 2548 : case GIMPLE_OMP_STRUCTURED_BLOCK:
3107 2548 : case GIMPLE_OMP_SINGLE:
3108 2548 : case GIMPLE_OMP_SCOPE:
3109 2548 : case GIMPLE_OMP_MASTER:
3110 2548 : case GIMPLE_OMP_MASKED:
3111 2548 : case GIMPLE_OMP_TASKGROUP:
3112 2548 : case GIMPLE_OMP_ORDERED:
3113 2548 : case GIMPLE_OMP_SCAN:
3114 2548 : case GIMPLE_OMP_CRITICAL:
3115 2548 : walk_body (convert_gimple_call, NULL, info, gimple_omp_body_ptr (stmt));
3116 2548 : break;
3117 :
3118 5901472 : default:
3119 : /* Keep looking for other operands. */
3120 5901472 : *handled_ops_p = false;
3121 5901472 : return NULL_TREE;
3122 : }
3123 :
3124 403499 : *handled_ops_p = true;
3125 403499 : return NULL_TREE;
3126 : }
3127 :
3128 : /* Walk the nesting tree starting with ROOT. Convert all trampolines and
3129 : call expressions. At the same time, determine if a nested function
3130 : actually uses its static chain; if not, remember that. */
3131 :
3132 : static void
3133 9419 : convert_all_function_calls (struct nesting_info *root)
3134 : {
3135 9419 : unsigned int chain_count = 0, old_chain_count, iter_count;
3136 9419 : struct nesting_info *n;
3137 :
3138 : /* First, optimistically clear static_chain for all decls that haven't
3139 : used the static chain already for variable access. But always create
3140 : it if not optimizing. This makes it possible to reconstruct the static
3141 : nesting tree at run time and thus to resolve up-level references from
3142 : within the debugger. */
3143 88462 : FOR_EACH_NEST_INFO (n, root)
3144 : {
3145 34812 : if (n->thunk_p)
3146 0 : continue;
3147 34812 : tree decl = n->context;
3148 34812 : if (!optimize)
3149 : {
3150 5725 : if (n->inner)
3151 1660 : (void) get_frame_type (n);
3152 5725 : if (n->outer)
3153 4135 : (void) get_chain_decl (n);
3154 : }
3155 29087 : else if (!n->outer || (!n->chain_decl && !n->chain_field))
3156 : {
3157 26297 : DECL_STATIC_CHAIN (decl) = 0;
3158 26297 : if (dump_file && (dump_flags & TDF_DETAILS))
3159 0 : fprintf (dump_file, "Guessing no static-chain for %s\n",
3160 0 : lang_hooks.decl_printable_name (decl, 2));
3161 : }
3162 : else
3163 2790 : DECL_STATIC_CHAIN (decl) = 1;
3164 34812 : chain_count += DECL_STATIC_CHAIN (decl);
3165 : }
3166 :
3167 79043 : FOR_EACH_NEST_INFO (n, root)
3168 34812 : if (n->thunk_p)
3169 : {
3170 0 : tree decl = n->context;
3171 0 : tree alias = thunk_info::get (cgraph_node::get (decl))->alias;
3172 0 : DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
3173 : }
3174 :
3175 : /* Walk the functions and perform transformations. Note that these
3176 : transformations can induce new uses of the static chain, which in turn
3177 : require re-examining all users of the decl. */
3178 : /* ??? It would make sense to try to use the call graph to speed this up,
3179 : but the call graph hasn't really been built yet. Even if it did, we
3180 : would still need to iterate in this loop since address-of references
3181 : wouldn't show up in the callgraph anyway. */
3182 : iter_count = 0;
3183 9475 : do
3184 : {
3185 9475 : old_chain_count = chain_count;
3186 9475 : chain_count = 0;
3187 9475 : iter_count++;
3188 :
3189 9475 : if (dump_file && (dump_flags & TDF_DETAILS))
3190 0 : fputc ('\n', dump_file);
3191 :
3192 89112 : FOR_EACH_NEST_INFO (n, root)
3193 : {
3194 35081 : if (n->thunk_p)
3195 0 : continue;
3196 35081 : tree decl = n->context;
3197 35081 : walk_function (convert_tramp_reference_stmt,
3198 : convert_tramp_reference_op, n);
3199 35081 : walk_function (convert_gimple_call, NULL, n);
3200 35081 : chain_count += DECL_STATIC_CHAIN (decl);
3201 : }
3202 :
3203 79637 : FOR_EACH_NEST_INFO (n, root)
3204 35081 : if (n->thunk_p)
3205 : {
3206 0 : tree decl = n->context;
3207 0 : tree alias = thunk_info::get (cgraph_node::get (decl))->alias;
3208 0 : DECL_STATIC_CHAIN (decl) = DECL_STATIC_CHAIN (alias);
3209 : }
3210 : }
3211 9475 : while (chain_count != old_chain_count);
3212 :
3213 9419 : if (dump_file && (dump_flags & TDF_DETAILS))
3214 0 : fprintf (dump_file, "convert_all_function_calls iterations: %u\n\n",
3215 : iter_count);
3216 9419 : }
3217 :
3218 : struct nesting_copy_body_data
3219 : {
3220 : copy_body_data cb;
3221 : struct nesting_info *root;
3222 : };
3223 :
3224 : /* A helper subroutine for debug_var_chain type remapping. */
3225 :
3226 : static tree
3227 30 : nesting_copy_decl (tree decl, copy_body_data *id)
3228 : {
3229 30 : struct nesting_copy_body_data *nid = (struct nesting_copy_body_data *) id;
3230 30 : tree *slot = nid->root->var_map->get (decl);
3231 :
3232 30 : if (slot)
3233 6 : return (tree) *slot;
3234 :
3235 24 : if (TREE_CODE (decl) == TYPE_DECL && DECL_ORIGINAL_TYPE (decl))
3236 : {
3237 6 : tree new_decl = copy_decl_no_change (decl, id);
3238 12 : DECL_ORIGINAL_TYPE (new_decl)
3239 6 : = remap_type (DECL_ORIGINAL_TYPE (decl), id);
3240 6 : return new_decl;
3241 : }
3242 :
3243 18 : if (VAR_P (decl)
3244 : || TREE_CODE (decl) == PARM_DECL
3245 : || TREE_CODE (decl) == RESULT_DECL)
3246 : return decl;
3247 :
3248 0 : return copy_decl_no_change (decl, id);
3249 : }
3250 :
3251 : /* A helper function for remap_vla_decls. See if *TP contains
3252 : some remapped variables. */
3253 :
3254 : static tree
3255 39 : contains_remapped_vars (tree *tp, int *walk_subtrees, void *data)
3256 : {
3257 39 : struct nesting_info *root = (struct nesting_info *) data;
3258 39 : tree t = *tp;
3259 :
3260 39 : if (DECL_P (t))
3261 : {
3262 0 : *walk_subtrees = 0;
3263 0 : tree *slot = root->var_map->get (t);
3264 :
3265 0 : if (slot)
3266 0 : return *slot;
3267 : }
3268 : return NULL;
3269 : }
3270 :
3271 : /* Remap VLA decls in BLOCK and subblocks if remapped variables are
3272 : involved. */
3273 :
3274 : static void
3275 2787 : remap_vla_decls (tree block, struct nesting_info *root)
3276 : {
3277 2787 : tree var, subblock, val, type;
3278 2787 : struct nesting_copy_body_data id;
3279 :
3280 5214 : for (subblock = BLOCK_SUBBLOCKS (block);
3281 5214 : subblock;
3282 2427 : subblock = BLOCK_CHAIN (subblock))
3283 2427 : remap_vla_decls (subblock, root);
3284 :
3285 8827 : for (var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3286 6040 : if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3287 : {
3288 101 : val = DECL_VALUE_EXPR (var);
3289 101 : type = TREE_TYPE (var);
3290 :
3291 114 : if (! (INDIRECT_REF_P (val)
3292 13 : && VAR_P (TREE_OPERAND (val, 0))
3293 13 : && variably_modified_type_p (type, NULL)))
3294 88 : continue;
3295 :
3296 13 : if (root->var_map->get (TREE_OPERAND (val, 0))
3297 13 : || walk_tree (&type, contains_remapped_vars, root, NULL))
3298 : break;
3299 : }
3300 :
3301 2787 : if (var == NULL_TREE)
3302 2787 : return;
3303 :
3304 0 : memset (&id, 0, sizeof (id));
3305 0 : id.cb.copy_decl = nesting_copy_decl;
3306 0 : id.cb.decl_map = new hash_map<tree, tree>;
3307 0 : id.root = root;
3308 :
3309 0 : for (; var; var = DECL_CHAIN (var))
3310 0 : if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3311 : {
3312 0 : struct nesting_info *i;
3313 0 : tree newt, context;
3314 :
3315 0 : val = DECL_VALUE_EXPR (var);
3316 0 : type = TREE_TYPE (var);
3317 :
3318 0 : if (! (INDIRECT_REF_P (val)
3319 0 : && VAR_P (TREE_OPERAND (val, 0))
3320 0 : && variably_modified_type_p (type, NULL)))
3321 0 : continue;
3322 :
3323 0 : tree *slot = root->var_map->get (TREE_OPERAND (val, 0));
3324 0 : if (!slot && !walk_tree (&type, contains_remapped_vars, root, NULL))
3325 0 : continue;
3326 :
3327 0 : context = decl_function_context (var);
3328 0 : for (i = root; i; i = i->outer)
3329 0 : if (i->context == context)
3330 : break;
3331 :
3332 0 : if (i == NULL)
3333 0 : continue;
3334 :
3335 : /* Fully expand value expressions. This avoids having debug variables
3336 : only referenced from them and that can be swept during GC. */
3337 0 : if (slot)
3338 : {
3339 0 : tree t = (tree) *slot;
3340 0 : gcc_assert (DECL_P (t) && DECL_HAS_VALUE_EXPR_P (t));
3341 0 : val = build1 (INDIRECT_REF, TREE_TYPE (val), DECL_VALUE_EXPR (t));
3342 : }
3343 :
3344 0 : id.cb.src_fn = i->context;
3345 0 : id.cb.dst_fn = i->context;
3346 0 : id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3347 :
3348 0 : TREE_TYPE (var) = newt = remap_type (type, &id.cb);
3349 0 : while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3350 : {
3351 0 : newt = TREE_TYPE (newt);
3352 0 : type = TREE_TYPE (type);
3353 : }
3354 0 : if (TYPE_NAME (newt)
3355 0 : && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3356 0 : && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3357 0 : && newt != type
3358 0 : && TYPE_NAME (newt) == TYPE_NAME (type))
3359 0 : TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3360 :
3361 0 : walk_tree (&val, copy_tree_body_r, &id.cb, NULL);
3362 0 : if (val != DECL_VALUE_EXPR (var))
3363 0 : SET_DECL_VALUE_EXPR (var, val);
3364 : }
3365 :
3366 0 : delete id.cb.decl_map;
3367 : }
3368 :
3369 : /* Fixup VLA decls in BLOCK and subblocks if remapped variables are
3370 : involved. */
3371 :
3372 : static void
3373 288763 : fixup_vla_decls (tree block)
3374 : {
3375 1258490 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
3376 969727 : if (VAR_P (var) && DECL_HAS_VALUE_EXPR_P (var))
3377 : {
3378 3819 : tree val = DECL_VALUE_EXPR (var);
3379 :
3380 3819 : if (! (INDIRECT_REF_P (val)
3381 347 : && VAR_P (TREE_OPERAND (val, 0))
3382 345 : && DECL_HAS_VALUE_EXPR_P (TREE_OPERAND (val, 0))))
3383 3786 : continue;
3384 :
3385 : /* Fully expand value expressions. This avoids having debug variables
3386 : only referenced from them and that can be swept during GC. */
3387 33 : val = build1 (INDIRECT_REF, TREE_TYPE (val),
3388 33 : DECL_VALUE_EXPR (TREE_OPERAND (val, 0)));
3389 33 : SET_DECL_VALUE_EXPR (var, val);
3390 : }
3391 :
3392 543074 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3393 254311 : fixup_vla_decls (sub);
3394 288763 : }
3395 :
3396 : /* Fold the MEM_REF *E. */
3397 : bool
3398 0 : fold_mem_refs (tree *const &e, void *data ATTRIBUTE_UNUSED)
3399 : {
3400 0 : tree *ref_p = const_cast<tree *> (e);
3401 0 : *ref_p = fold (*ref_p);
3402 0 : return true;
3403 : }
3404 :
3405 : /* Given DECL, a nested function, build an initialization call for FIELD,
3406 : the trampoline or descriptor for DECL, using FUNC as the function. */
3407 :
3408 : static gcall *
3409 279 : build_init_call_stmt (struct nesting_info *info, tree decl, tree field,
3410 : tree func)
3411 : {
3412 279 : tree arg1, arg2, arg3, x;
3413 :
3414 279 : gcc_assert (DECL_STATIC_CHAIN (decl));
3415 279 : arg3 = build_addr (info->frame_decl);
3416 :
3417 279 : arg2 = build_addr (decl);
3418 :
3419 279 : x = build3 (COMPONENT_REF, TREE_TYPE (field),
3420 : info->frame_decl, field, NULL_TREE);
3421 279 : arg1 = build_addr (x);
3422 :
3423 279 : return gimple_build_call (func, 3, arg1, arg2, arg3);
3424 : }
3425 :
3426 : /* Do "everything else" to clean up or complete state collected by the various
3427 : walking passes -- create a field to hold the frame base address, lay out the
3428 : types and decls, generate code to initialize the frame decl, store critical
3429 : expressions in the struct function for rtl to find. */
3430 :
3431 : static void
3432 34812 : finalize_nesting_tree_1 (struct nesting_info *root)
3433 : {
3434 34812 : gimple_seq cleanup_list = NULL;
3435 34812 : gimple_seq stmt_list = NULL;
3436 34812 : gimple *stmt;
3437 34812 : tree context = root->context;
3438 34812 : struct function *sf;
3439 :
3440 34812 : if (root->thunk_p)
3441 0 : return;
3442 :
3443 : /* If we created a non-local frame type or decl, we need to lay them
3444 : out at this time. */
3445 34812 : if (root->frame_type)
3446 : {
3447 : /* Debugging information needs to compute the frame base address of the
3448 : parent frame out of the static chain from the nested frame.
3449 :
3450 : The static chain is the address of the FRAME record, so one could
3451 : imagine it would be possible to compute the frame base address just
3452 : adding a constant offset to this address. Unfortunately, this is not
3453 : possible: if the FRAME object has alignment constraints that are
3454 : stronger than the stack, then the offset between the frame base and
3455 : the FRAME object will be dynamic.
3456 :
3457 : What we do instead is to append a field to the FRAME object that holds
3458 : the frame base address: then debug info just has to fetch this
3459 : field. */
3460 :
3461 : /* Debugging information will refer to the CFA as the frame base
3462 : address: we will do the same here. */
3463 3469 : const tree frame_addr_fndecl
3464 3469 : = builtin_decl_explicit (BUILT_IN_DWARF_CFA);
3465 :
3466 : /* Create a field in the FRAME record to hold the frame base address for
3467 : this stack frame. Since it will be used only by the debugger, put it
3468 : at the end of the record in order not to shift all other offsets. */
3469 3469 : tree fb_decl = make_node (FIELD_DECL);
3470 :
3471 3469 : DECL_NAME (fb_decl) = get_identifier ("FRAME_BASE.PARENT");
3472 3469 : TREE_TYPE (fb_decl) = ptr_type_node;
3473 3469 : TREE_ADDRESSABLE (fb_decl) = 1;
3474 3469 : DECL_CONTEXT (fb_decl) = root->frame_type;
3475 3469 : TYPE_FIELDS (root->frame_type) = chainon (TYPE_FIELDS (root->frame_type),
3476 : fb_decl);
3477 :
3478 : /* In some cases the frame type will trigger the -Wpadded warning.
3479 : This is not helpful; suppress it. */
3480 3469 : int save_warn_padded = warn_padded;
3481 3469 : warn_padded = 0;
3482 3469 : layout_type (root->frame_type);
3483 3469 : warn_padded = save_warn_padded;
3484 3469 : layout_decl (root->frame_decl, 0);
3485 :
3486 : /* Initialize the frame base address field. If the builtin we need is
3487 : not available, set it to NULL so that debugging information does not
3488 : reference junk. */
3489 3469 : tree fb_ref = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
3490 : root->frame_decl, fb_decl, NULL_TREE);
3491 3469 : tree fb_tmp;
3492 :
3493 3469 : if (frame_addr_fndecl != NULL_TREE)
3494 : {
3495 1084 : gcall *fb_gimple = gimple_build_call (frame_addr_fndecl, 1,
3496 : integer_zero_node);
3497 1084 : gimple_stmt_iterator gsi = gsi_last (stmt_list);
3498 :
3499 1084 : fb_tmp = init_tmp_var_with_call (root, &gsi, fb_gimple);
3500 : }
3501 : else
3502 2385 : fb_tmp = build_int_cst (TREE_TYPE (fb_ref), 0);
3503 3469 : gimple_seq_add_stmt (&stmt_list,
3504 3469 : gimple_build_assign (fb_ref, fb_tmp));
3505 :
3506 3469 : declare_vars (root->frame_decl,
3507 : gimple_seq_first_stmt (gimple_body (context)), true);
3508 : }
3509 :
3510 : /* If any parameters were referenced non-locally, then we need to insert
3511 : a copy or a pointer. */
3512 34812 : if (root->any_parm_remapped)
3513 : {
3514 226 : tree p;
3515 629 : for (p = DECL_ARGUMENTS (context); p ; p = DECL_CHAIN (p))
3516 : {
3517 403 : tree field, x, y;
3518 :
3519 403 : field = lookup_field_for_decl (root, p, NO_INSERT);
3520 403 : if (!field)
3521 138 : continue;
3522 :
3523 265 : if (use_pointer_in_frame (p))
3524 21 : x = build_addr (p);
3525 : else
3526 : x = p;
3527 :
3528 : /* If the assignment is from a non-register the stmt is
3529 : not valid gimple. Make it so by using a temporary instead. */
3530 265 : if (!is_gimple_reg (x)
3531 265 : && is_gimple_reg_type (TREE_TYPE (x)))
3532 : {
3533 44 : gimple_stmt_iterator gsi = gsi_last (stmt_list);
3534 44 : x = init_tmp_var (root, x, &gsi);
3535 : }
3536 :
3537 265 : y = build3 (COMPONENT_REF, TREE_TYPE (field),
3538 : root->frame_decl, field, NULL_TREE);
3539 265 : stmt = gimple_build_assign (y, x);
3540 265 : gimple_seq_add_stmt (&stmt_list, stmt);
3541 : }
3542 : }
3543 :
3544 : /* If a chain_field was created, then it needs to be initialized
3545 : from chain_decl. */
3546 34812 : if (root->chain_field)
3547 : {
3548 79 : tree x = build3 (COMPONENT_REF, TREE_TYPE (root->chain_field),
3549 : root->frame_decl, root->chain_field, NULL_TREE);
3550 79 : stmt = gimple_build_assign (x, get_chain_decl (root));
3551 79 : gimple_seq_add_stmt (&stmt_list, stmt);
3552 : }
3553 :
3554 : /* If trampolines were created, then we need to initialize them. */
3555 34812 : if (root->any_tramp_created)
3556 : {
3557 245 : struct nesting_info *i;
3558 606 : for (i = root->inner; i ; i = i->next)
3559 : {
3560 361 : tree field, x;
3561 :
3562 361 : field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
3563 361 : if (!field)
3564 81 : continue;
3565 :
3566 280 : if (flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
3567 : {
3568 : /* We pass a whole bunch of arguments to the builtin function that
3569 : creates the off-stack trampoline, these are
3570 : 1. The nested function chain value (that must be passed to the
3571 : nested function so it can find the function arguments).
3572 : 2. A pointer to the nested function implementation,
3573 : 3. The address in the local stack frame where we should write
3574 : the address of the trampoline.
3575 :
3576 : When this code was originally written I just kind of threw
3577 : everything at the builtin, figuring I'd work out what was
3578 : actually needed later, I think, the stack pointer could
3579 : certainly be dropped, arguments #2 and #4 are based off the
3580 : stack pointer anyway, so #1 doesn't seem to add much value. */
3581 1 : tree arg1, arg2, arg3;
3582 :
3583 1 : gcc_assert (DECL_STATIC_CHAIN (i->context));
3584 1 : arg1 = build_addr (root->frame_decl);
3585 1 : arg2 = build_addr (i->context);
3586 :
3587 1 : x = build3 (COMPONENT_REF, TREE_TYPE (field),
3588 : root->frame_decl, field, NULL_TREE);
3589 1 : arg3 = build_addr (x);
3590 :
3591 1 : x = builtin_decl_explicit (BUILT_IN_GCC_NESTED_PTR_CREATED);
3592 1 : stmt = gimple_build_call (x, 3, arg1, arg2, arg3);
3593 1 : gimple_seq_add_stmt (&stmt_list, stmt);
3594 :
3595 : /* This call to delete the nested function trampoline is added to
3596 : the cleanup list, and called when we exit the current scope. */
3597 1 : x = builtin_decl_explicit (BUILT_IN_GCC_NESTED_PTR_DELETED);
3598 1 : stmt = gimple_build_call (x, 0);
3599 1 : gimple_seq_add_stmt (&cleanup_list, stmt);
3600 : }
3601 : else
3602 : {
3603 : /* Original code to initialise the on stack trampoline. */
3604 279 : x = builtin_decl_implicit (BUILT_IN_INIT_TRAMPOLINE);
3605 279 : stmt = build_init_call_stmt (root, i->context, field, x);
3606 279 : gimple_seq_add_stmt (&stmt_list, stmt);
3607 : }
3608 : }
3609 : }
3610 :
3611 : /* If descriptors were created, then we need to initialize them. */
3612 34812 : if (root->any_descr_created)
3613 : {
3614 0 : struct nesting_info *i;
3615 0 : for (i = root->inner; i ; i = i->next)
3616 : {
3617 0 : tree field, x;
3618 :
3619 0 : field = lookup_descr_for_decl (root, i->context, NO_INSERT);
3620 0 : if (!field)
3621 0 : continue;
3622 :
3623 0 : x = builtin_decl_implicit (BUILT_IN_INIT_DESCRIPTOR);
3624 0 : stmt = build_init_call_stmt (root, i->context, field, x);
3625 0 : gimple_seq_add_stmt (&stmt_list, stmt);
3626 : }
3627 : }
3628 :
3629 : /* If we created initialization statements, insert them. */
3630 34812 : if (stmt_list)
3631 : {
3632 3469 : if (flag_trampoline_impl == TRAMPOLINE_IMPL_HEAP)
3633 : {
3634 : /* Handle off-stack trampolines. */
3635 1 : gbind *bind;
3636 1 : annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3637 1 : annotate_all_with_location (cleanup_list, DECL_SOURCE_LOCATION (context));
3638 1 : bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3639 1 : gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3640 :
3641 1 : gimple_seq xxx_list = NULL;
3642 :
3643 1 : if (cleanup_list != NULL)
3644 : {
3645 : /* Maybe we shouldn't be creating this try/finally if -fno-exceptions is
3646 : in use. If this is the case, then maybe we should, instead, be
3647 : inserting the cleanup code onto every path out of this function? Not
3648 : yet figured out how we would do this. */
3649 1 : gtry *t = gimple_build_try (stmt_list, cleanup_list, GIMPLE_TRY_FINALLY);
3650 1 : gimple_seq_add_stmt (&xxx_list, t);
3651 : }
3652 : else
3653 0 : xxx_list = stmt_list;
3654 :
3655 1 : gimple_bind_set_body (bind, xxx_list);
3656 : }
3657 : else
3658 : {
3659 : /* The traditional, on stack trampolines. */
3660 3468 : gbind *bind;
3661 3468 : annotate_all_with_location (stmt_list, DECL_SOURCE_LOCATION (context));
3662 3468 : bind = gimple_seq_first_stmt_as_a_bind (gimple_body (context));
3663 3468 : gimple_seq_add_seq (&stmt_list, gimple_bind_body (bind));
3664 3468 : gimple_bind_set_body (bind, stmt_list);
3665 : }
3666 : }
3667 :
3668 : /* If a chain_decl was created, then it needs to be registered with
3669 : struct function so that it gets initialized from the static chain
3670 : register at the beginning of the function. */
3671 34812 : sf = DECL_STRUCT_FUNCTION (root->context);
3672 34812 : sf->static_chain_decl = root->chain_decl;
3673 :
3674 : /* Similarly for the non-local goto save area. */
3675 34812 : if (root->nl_goto_field)
3676 : {
3677 375 : sf->nonlocal_goto_save_area
3678 375 : = get_frame_field (root, context, root->nl_goto_field, NULL);
3679 375 : sf->has_nonlocal_label = 1;
3680 : }
3681 :
3682 : /* Make sure all new local variables get inserted into the
3683 : proper BIND_EXPR. */
3684 34812 : if (root->new_local_var_chain)
3685 3638 : declare_vars (root->new_local_var_chain,
3686 : gimple_seq_first_stmt (gimple_body (root->context)),
3687 : false);
3688 :
3689 34812 : if (root->debug_var_chain)
3690 : {
3691 360 : tree debug_var;
3692 360 : gbind *scope;
3693 :
3694 360 : remap_vla_decls (DECL_INITIAL (root->context), root);
3695 :
3696 1046 : for (debug_var = root->debug_var_chain; debug_var;
3697 686 : debug_var = DECL_CHAIN (debug_var))
3698 692 : if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3699 : break;
3700 :
3701 : /* If there are any debug decls with variable length types,
3702 : remap those types using other debug_var_chain variables. */
3703 360 : if (debug_var)
3704 : {
3705 6 : struct nesting_copy_body_data id;
3706 :
3707 6 : memset (&id, 0, sizeof (id));
3708 6 : id.cb.copy_decl = nesting_copy_decl;
3709 6 : id.cb.decl_map = new hash_map<tree, tree>;
3710 6 : id.root = root;
3711 :
3712 36 : for (; debug_var; debug_var = DECL_CHAIN (debug_var))
3713 30 : if (variably_modified_type_p (TREE_TYPE (debug_var), NULL))
3714 : {
3715 6 : tree type = TREE_TYPE (debug_var);
3716 6 : tree newt, t = type;
3717 6 : struct nesting_info *i;
3718 :
3719 12 : for (i = root; i; i = i->outer)
3720 12 : if (variably_modified_type_p (type, i->context))
3721 : break;
3722 :
3723 6 : if (i == NULL)
3724 0 : continue;
3725 :
3726 6 : id.cb.src_fn = i->context;
3727 6 : id.cb.dst_fn = i->context;
3728 6 : id.cb.src_cfun = DECL_STRUCT_FUNCTION (root->context);
3729 :
3730 6 : TREE_TYPE (debug_var) = newt = remap_type (type, &id.cb);
3731 12 : while (POINTER_TYPE_P (newt) && !TYPE_NAME (newt))
3732 : {
3733 6 : newt = TREE_TYPE (newt);
3734 6 : t = TREE_TYPE (t);
3735 : }
3736 6 : if (TYPE_NAME (newt)
3737 6 : && TREE_CODE (TYPE_NAME (newt)) == TYPE_DECL
3738 6 : && DECL_ORIGINAL_TYPE (TYPE_NAME (newt))
3739 6 : && newt != t
3740 12 : && TYPE_NAME (newt) == TYPE_NAME (t))
3741 6 : TYPE_NAME (newt) = remap_decl (TYPE_NAME (newt), &id.cb);
3742 : }
3743 :
3744 12 : delete id.cb.decl_map;
3745 : }
3746 :
3747 360 : scope = gimple_seq_first_stmt_as_a_bind (gimple_body (root->context));
3748 360 : if (gimple_bind_block (scope))
3749 358 : declare_vars (root->debug_var_chain, scope, true);
3750 : else
3751 2 : BLOCK_VARS (DECL_INITIAL (root->context))
3752 4 : = chainon (BLOCK_VARS (DECL_INITIAL (root->context)),
3753 : root->debug_var_chain);
3754 : }
3755 : else
3756 34452 : fixup_vla_decls (DECL_INITIAL (root->context));
3757 :
3758 : /* Fold the rewritten MEM_REF trees. */
3759 34812 : root->mem_refs->traverse<void *, fold_mem_refs> (NULL);
3760 :
3761 : /* Dump the translated tree function. */
3762 34812 : if (dump_file)
3763 : {
3764 0 : fputs ("\n\n", dump_file);
3765 0 : dump_function_to_file (root->context, dump_file, dump_flags);
3766 : }
3767 : }
3768 :
3769 : static void
3770 9419 : finalize_nesting_tree (struct nesting_info *root)
3771 : {
3772 9419 : struct nesting_info *n;
3773 88462 : FOR_EACH_NEST_INFO (n, root)
3774 34812 : finalize_nesting_tree_1 (n);
3775 9419 : }
3776 :
3777 : /* Unnest the nodes and pass them to cgraph. */
3778 :
3779 : static void
3780 34812 : unnest_nesting_tree_1 (struct nesting_info *root)
3781 : {
3782 34812 : struct cgraph_node *node = cgraph_node::get (root->context);
3783 :
3784 : /* For nested functions update the cgraph to reflect unnesting.
3785 : We also delay finalizing of these functions up to this point. */
3786 34812 : if (nested_function_info::get (node)->origin)
3787 : {
3788 25393 : unnest_function (node);
3789 25393 : if (!root->thunk_p)
3790 25393 : cgraph_node::finalize_function (root->context, true);
3791 : }
3792 34812 : }
3793 :
3794 : static void
3795 9419 : unnest_nesting_tree (struct nesting_info *root)
3796 : {
3797 9419 : struct nesting_info *n;
3798 88462 : FOR_EACH_NEST_INFO (n, root)
3799 34812 : unnest_nesting_tree_1 (n);
3800 9419 : }
3801 :
3802 : /* Free the data structures allocated during this pass. */
3803 :
3804 : static void
3805 9419 : free_nesting_tree (struct nesting_info *root)
3806 : {
3807 9419 : struct nesting_info *node, *next;
3808 :
3809 9419 : node = iter_nestinfo_start (root);
3810 34812 : do
3811 : {
3812 34812 : next = iter_nestinfo_next (node);
3813 69624 : delete node->var_map;
3814 69624 : delete node->field_map;
3815 69624 : delete node->mem_refs;
3816 34812 : free (node);
3817 34812 : node = next;
3818 : }
3819 34812 : while (node);
3820 9419 : }
3821 :
3822 : /* Gimplify a function and all its nested functions. */
3823 : static void
3824 34812 : gimplify_all_functions (struct cgraph_node *root)
3825 : {
3826 34812 : struct cgraph_node *iter;
3827 34812 : if (!gimple_body (root->decl))
3828 25392 : gimplify_function_tree (root->decl);
3829 120410 : for (iter = first_nested_function (root); iter;
3830 25393 : iter = next_nested_function (iter))
3831 25393 : if (!iter->thunk)
3832 25393 : gimplify_all_functions (iter);
3833 34812 : }
3834 :
3835 : /* Main entry point for this pass. Process FNDECL and all of its nested
3836 : subroutines and turn them into something less tightly bound. */
3837 :
3838 : void
3839 9419 : lower_nested_functions (tree fndecl)
3840 : {
3841 9419 : struct cgraph_node *cgn;
3842 9419 : struct nesting_info *root;
3843 :
3844 : /* If there are no nested functions, there's nothing to do. */
3845 9419 : cgn = cgraph_node::get (fndecl);
3846 18838 : if (!first_nested_function (cgn))
3847 : return;
3848 :
3849 9419 : gimplify_all_functions (cgn);
3850 :
3851 9419 : set_dump_file (dump_begin (TDI_nested, &dump_flags));
3852 9419 : if (dump_file)
3853 0 : fprintf (dump_file, "\n;; Function %s\n\n",
3854 0 : lang_hooks.decl_printable_name (fndecl, 2));
3855 :
3856 9419 : bitmap_obstack_initialize (&nesting_info_bitmap_obstack);
3857 9419 : root = create_nesting_tree (cgn);
3858 :
3859 9419 : walk_all_functions (convert_nonlocal_reference_stmt,
3860 : convert_nonlocal_reference_op,
3861 : root);
3862 9419 : walk_all_functions (convert_local_reference_stmt,
3863 : convert_local_reference_op,
3864 : root);
3865 9419 : walk_all_functions (convert_nl_goto_reference, NULL, root);
3866 9419 : walk_all_functions (convert_nl_goto_receiver, NULL, root);
3867 :
3868 9419 : convert_all_function_calls (root);
3869 9419 : finalize_nesting_tree (root);
3870 9419 : unnest_nesting_tree (root);
3871 :
3872 9419 : free_nesting_tree (root);
3873 9419 : bitmap_obstack_release (&nesting_info_bitmap_obstack);
3874 :
3875 9419 : if (dump_file)
3876 : {
3877 0 : dump_end (TDI_nested, dump_file);
3878 0 : set_dump_file (NULL);
3879 : }
3880 : }
3881 :
3882 : #include "gt-tree-nested.h"
|