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