1 /* Control flow graph manipulation code for GNU compiler. 2 Copyright (C) 1987-2024 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 it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 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 /* This file contains low level functions to manipulate the CFG and 21 analyze it. All other modules should not transform the data structure 22 directly and use abstraction instead. The file is supposed to be 23 ordered bottom-up and should not contain any code dependent on a 24 particular intermediate language (RTL or trees). 25 26 Available functionality: 27 - Initialization/deallocation 28 init_flow, free_cfg 29 - Low level basic block manipulation 30 alloc_block, expunge_block 31 - Edge manipulation 32 make_edge, make_single_succ_edge, cached_make_edge, remove_edge 33 - Low level edge redirection (without updating instruction chain) 34 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred 35 - Dumping and debugging 36 dump_flow_info, debug_flow_info, dump_edge_info 37 - Allocation of AUX fields for basic blocks 38 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block 39 - clear_bb_flags 40 - Consistency checking 41 verify_flow_info 42 - Dumping and debugging 43 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n 44 45 TODO: Document these "Available functionality" functions in the files 46 that implement them. 47 */ 48 49 #include "config.h" 51 #include "system.h" 52 #include "coretypes.h" 53 #include "backend.h" 54 #include "hard-reg-set.h" 55 #include "tree.h" 56 #include "cfghooks.h" 57 #include "df.h" 58 #include "cfganal.h" 59 #include "cfgloop.h" /* FIXME: For struct loop. */ 60 #include "dumpfile.h" 61 62 63 65 /* Called once at initialization time. */ 66 67 void 68 init_flow (struct function *the_fun) 69 { 70 if (!the_fun->cfg) 71 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> (); 72 n_edges_for_fn (the_fun) = 0; 73 the_fun->cfg->count_max = profile_count::uninitialized (); 74 ENTRY_BLOCK_PTR_FOR_FN (the_fun) 75 = alloc_block (); 76 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK; 77 EXIT_BLOCK_PTR_FOR_FN (the_fun) 78 = alloc_block (); 79 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK; 80 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb 81 = EXIT_BLOCK_PTR_FOR_FN (the_fun); 82 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb 83 = ENTRY_BLOCK_PTR_FOR_FN (the_fun); 84 the_fun->cfg->edge_flags_allocated = EDGE_ALL_FLAGS; 85 the_fun->cfg->bb_flags_allocated = BB_ALL_FLAGS; 86 the_fun->cfg->full_profile = false; 87 } 88 89 /* Helper function for remove_edge and free_cffg. Frees edge structure 91 without actually removing it from the pred/succ arrays. */ 92 93 static void 94 free_edge (function *fn, edge e) 95 { 96 n_edges_for_fn (fn)--; 97 ggc_free (e); 98 } 99 100 /* Free basic block BB. */ 101 102 static void 103 free_block (basic_block bb) 104 { 105 vec_free (bb->succs); 106 bb->succs = NULL; 107 vec_free (bb->preds); 108 bb->preds = NULL; 109 ggc_free (bb); 110 } 111 112 /* Free the memory associated with the CFG in FN. */ 113 114 void 115 free_cfg (struct function *fn) 116 { 117 edge e; 118 edge_iterator ei; 119 basic_block next; 120 121 for (basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (fn); bb; bb = next) 122 { 123 next = bb->next_bb; 124 FOR_EACH_EDGE (e, ei, bb->succs) 125 free_edge (fn, e); 126 free_block (bb); 127 } 128 129 gcc_assert (!n_edges_for_fn (fn)); 130 /* Sanity check that dominance tree is freed. */ 131 gcc_assert (!fn->cfg->x_dom_computed[0] && !fn->cfg->x_dom_computed[1]); 132 133 vec_free (fn->cfg->x_label_to_block_map); 134 vec_free (basic_block_info_for_fn (fn)); 135 ggc_free (fn->cfg); 136 fn->cfg = NULL; 137 } 138 139 /* Allocate memory for basic_block. */ 141 142 basic_block 143 alloc_block (void) 144 { 145 basic_block bb; 146 bb = ggc_cleared_alloc<basic_block_def> (); 147 bb->count = profile_count::uninitialized (); 148 return bb; 149 } 150 151 /* Link block B to chain after AFTER. */ 152 void 153 link_block (basic_block b, basic_block after) 154 { 155 b->next_bb = after->next_bb; 156 b->prev_bb = after; 157 after->next_bb = b; 158 b->next_bb->prev_bb = b; 159 } 160 161 /* Unlink block B from chain. */ 162 void 163 unlink_block (basic_block b) 164 { 165 b->next_bb->prev_bb = b->prev_bb; 166 b->prev_bb->next_bb = b->next_bb; 167 b->prev_bb = NULL; 168 b->next_bb = NULL; 169 } 170 171 /* Sequentially order blocks and compact the arrays. */ 172 void 173 compact_blocks (void) 174 { 175 int i; 176 177 SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun)); 178 SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun)); 179 180 if (df) 181 df_compact_blocks (); 182 else 183 { 184 basic_block bb; 185 186 i = NUM_FIXED_BLOCKS; 187 FOR_EACH_BB_FN (bb, cfun) 188 { 189 SET_BASIC_BLOCK_FOR_FN (cfun, i, bb); 190 bb->index = i; 191 i++; 192 } 193 gcc_assert (i == n_basic_blocks_for_fn (cfun)); 194 195 for (; i < last_basic_block_for_fn (cfun); i++) 196 SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL); 197 } 198 last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun); 199 } 200 201 /* Remove block B from the basic block array. */ 202 203 void 204 expunge_block (basic_block b) 205 { 206 unlink_block (b); 207 SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL); 208 n_basic_blocks_for_fn (cfun)--; 209 /* We should be able to ggc_free here, but we are not. 210 The dead SSA_NAMES are left pointing to dead statements that are pointing 211 to dead basic blocks making garbage collector to die. 212 We should be able to release all dead SSA_NAMES and at the same time we 213 should clear out BB pointer of dead statements consistently. */ 214 } 215 216 /* Connect E to E->src. */ 218 219 static inline void 220 connect_src (edge e) 221 { 222 vec_safe_push (e->src->succs, e); 223 df_mark_solutions_dirty (); 224 } 225 226 /* Connect E to E->dest. */ 227 228 static inline void 229 connect_dest (edge e) 230 { 231 basic_block dest = e->dest; 232 vec_safe_push (dest->preds, e); 233 e->dest_idx = EDGE_COUNT (dest->preds) - 1; 234 df_mark_solutions_dirty (); 235 } 236 237 /* Disconnect edge E from E->src. */ 238 239 static inline void 240 disconnect_src (edge e) 241 { 242 basic_block src = e->src; 243 edge_iterator ei; 244 edge tmp; 245 246 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); ) 247 { 248 if (tmp == e) 249 { 250 src->succs->unordered_remove (ei.index); 251 df_mark_solutions_dirty (); 252 return; 253 } 254 else 255 ei_next (&ei); 256 } 257 258 gcc_unreachable (); 259 } 260 261 /* Disconnect edge E from E->dest. */ 262 263 static inline void 264 disconnect_dest (edge e) 265 { 266 basic_block dest = e->dest; 267 unsigned int dest_idx = e->dest_idx; 268 269 dest->preds->unordered_remove (dest_idx); 270 271 /* If we removed an edge in the middle of the edge vector, we need 272 to update dest_idx of the edge that moved into the "hole". */ 273 if (dest_idx < EDGE_COUNT (dest->preds)) 274 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx; 275 df_mark_solutions_dirty (); 276 } 277 278 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly 279 created edge. Use this only if you are sure that this edge can't 280 possibly already exist. */ 281 282 edge 283 unchecked_make_edge (basic_block src, basic_block dst, int flags) 284 { 285 edge e; 286 e = ggc_cleared_alloc<edge_def> (); 287 n_edges_for_fn (cfun)++; 288 289 e->probability = profile_probability::uninitialized (); 290 e->src = src; 291 e->dest = dst; 292 e->flags = flags; 293 294 connect_src (e); 295 connect_dest (e); 296 297 execute_on_growing_pred (e); 298 return e; 299 } 300 301 /* Create an edge connecting SRC and DST with FLAGS optionally using 302 edge cache CACHE. Return the new edge, NULL if already exist. */ 303 304 edge 305 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags) 306 { 307 if (edge_cache == NULL 308 || src == ENTRY_BLOCK_PTR_FOR_FN (cfun) 309 || dst == EXIT_BLOCK_PTR_FOR_FN (cfun)) 310 return make_edge (src, dst, flags); 311 312 /* Does the requested edge already exist? */ 313 if (! bitmap_bit_p (edge_cache, dst->index)) 314 { 315 /* The edge does not exist. Create one and update the 316 cache. */ 317 bitmap_set_bit (edge_cache, dst->index); 318 return unchecked_make_edge (src, dst, flags); 319 } 320 321 /* At this point, we know that the requested edge exists. Adjust 322 flags if necessary. */ 323 if (flags) 324 { 325 edge e = find_edge (src, dst); 326 e->flags |= flags; 327 } 328 329 return NULL; 330 } 331 332 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly 333 created edge or NULL if already exist. */ 334 335 edge 336 make_edge (basic_block src, basic_block dest, int flags) 337 { 338 edge e = find_edge (src, dest); 339 340 /* Make sure we don't add duplicate edges. */ 341 if (e) 342 { 343 e->flags |= flags; 344 return NULL; 345 } 346 347 return unchecked_make_edge (src, dest, flags); 348 } 349 350 /* Create an edge connecting SRC to DEST and set probability by knowing 351 that it is the single edge leaving SRC. */ 352 353 edge 354 make_single_succ_edge (basic_block src, basic_block dest, int flags) 355 { 356 edge e = make_edge (src, dest, flags); 357 358 e->probability = profile_probability::always (); 359 return e; 360 } 361 362 /* This function will remove an edge from the flow graph. */ 363 364 void 365 remove_edge_raw (edge e) 366 { 367 remove_predictions_associated_with_edge (e); 368 execute_on_shrinking_pred (e); 369 370 disconnect_src (e); 371 disconnect_dest (e); 372 373 free_edge (cfun, e); 374 } 375 376 /* Redirect an edge's successor from one block to another. */ 377 378 void 379 redirect_edge_succ (edge e, basic_block new_succ) 380 { 381 execute_on_shrinking_pred (e); 382 383 disconnect_dest (e); 384 385 e->dest = new_succ; 386 387 /* Reconnect the edge to the new successor block. */ 388 connect_dest (e); 389 390 execute_on_growing_pred (e); 391 } 392 393 /* Redirect an edge's predecessor from one block to another. */ 394 395 void 396 redirect_edge_pred (edge e, basic_block new_pred) 397 { 398 disconnect_src (e); 399 400 e->src = new_pred; 401 402 /* Reconnect the edge to the new predecessor block. */ 403 connect_src (e); 404 } 405 406 /* Clear all basic block flags that do not have to be preserved. */ 407 void 408 clear_bb_flags (void) 409 { 410 basic_block bb; 411 int flags_to_preserve = BB_FLAGS_TO_PRESERVE; 412 if (current_loops 413 && loops_state_satisfies_p (cfun, LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)) 414 flags_to_preserve |= BB_IRREDUCIBLE_LOOP; 415 416 FOR_ALL_BB_FN (bb, cfun) 417 bb->flags &= flags_to_preserve; 418 } 419 420 /* Check the consistency of profile information. We can't do that 422 in verify_flow_info, as the counts may get invalid for incompletely 423 solved graphs, later eliminating of conditionals or roundoff errors. 424 It is still practical to have them reported for debugging of simple 425 testcases. */ 426 static void 427 check_bb_profile (basic_block bb, FILE * file, int indent) 428 { 429 edge e; 430 edge_iterator ei; 431 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl); 432 char *s_indent = (char *) alloca ((size_t) indent + 1); 433 memset ((void *) s_indent, ' ', (size_t) indent); 434 s_indent[indent] = '\0'; 435 436 if (profile_status_for_fn (fun) == PROFILE_ABSENT) 437 return; 438 439 if (bb != EXIT_BLOCK_PTR_FOR_FN (fun)) 440 { 441 bool found = false; 442 profile_probability sum = profile_probability::never (); 443 int isum = 0; 444 445 FOR_EACH_EDGE (e, ei, bb->succs) 446 { 447 if (!(e->flags & (EDGE_EH | EDGE_FAKE))) 448 found = true; 449 sum += e->probability; 450 if (e->probability.initialized_p ()) 451 isum += e->probability.to_reg_br_prob_base (); 452 } 453 /* Only report mismatches for non-EH control flow. If there are only EH 454 edges it means that the BB ends by noreturn call. Here the control 455 flow may just terminate. */ 456 if (found) 457 { 458 if (sum.differs_from_p (profile_probability::always ())) 459 { 460 fprintf (file, 461 ";; %sInvalid sum of outgoing probabilities ", 462 s_indent); 463 sum.dump (file); 464 fprintf (file, "\n"); 465 } 466 /* Probabilities caps to 100% and thus the previous test will never 467 fire if the sum of probabilities is too large. */ 468 else if (isum > REG_BR_PROB_BASE + 100) 469 { 470 fprintf (file, 471 ";; %sInvalid sum of outgoing probabilities %.1f%%\n", 472 s_indent, isum * 100.0 / REG_BR_PROB_BASE); 473 } 474 } 475 } 476 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun)) 477 { 478 profile_count sum = profile_count::zero (); 479 FOR_EACH_EDGE (e, ei, bb->preds) 480 sum += e->count (); 481 if (sum.differs_from_p (bb->count)) 482 { 483 fprintf (file, ";; %sInvalid sum of incoming counts ", 484 s_indent); 485 sum.dump (file, fun); 486 fprintf (file, ", should be "); 487 bb->count.dump (file, fun); 488 fprintf (file, "\n"); 489 } 490 } 491 if (BB_PARTITION (bb) == BB_COLD_PARTITION) 492 { 493 /* Warn about inconsistencies in the partitioning that are 494 currently caused by profile insanities created via optimization. */ 495 if (!probably_never_executed_bb_p (fun, bb)) 496 fprintf (file, ";; %sBlock in cold partition with hot count\n", 497 s_indent); 498 FOR_EACH_EDGE (e, ei, bb->preds) 499 { 500 if (!probably_never_executed_edge_p (fun, e)) 501 fprintf (file, 502 ";; %sBlock in cold partition with incoming hot edge\n", 503 s_indent); 504 } 505 } 506 } 507 508 void 510 dump_edge_info (FILE *file, edge e, dump_flags_t flags, int do_succ) 511 { 512 basic_block side = (do_succ ? e->dest : e->src); 513 bool do_details = false; 514 515 if ((flags & TDF_DETAILS) != 0 516 && (flags & TDF_SLIM) == 0) 517 do_details = true; 518 519 if (side->index == ENTRY_BLOCK) 520 fputs (" ENTRY", file); 521 else if (side->index == EXIT_BLOCK) 522 fputs (" EXIT", file); 523 else 524 fprintf (file, " %d", side->index); 525 526 if (e->probability.initialized_p () && do_details) 527 { 528 fprintf (file, " ["); 529 e->probability.dump (file); 530 fprintf (file, "] "); 531 } 532 533 if (e->count ().initialized_p () && do_details) 534 { 535 fputs (" count:", file); 536 e->count ().dump (file, cfun); 537 } 538 539 if (e->flags && do_details) 540 { 541 static const char * const bitnames[] = 542 { 543 #define DEF_EDGE_FLAG(NAME,IDX) #NAME , 544 #include "cfg-flags.def" 545 NULL 546 #undef DEF_EDGE_FLAG 547 }; 548 bool comma = false; 549 int i, flags = e->flags; 550 551 gcc_assert (e->flags <= EDGE_ALL_FLAGS); 552 fputs (" (", file); 553 for (i = 0; flags; i++) 554 if (flags & (1 << i)) 555 { 556 flags &= ~(1 << i); 557 558 if (comma) 559 fputc (',', file); 560 fputs (bitnames[i], file); 561 comma = true; 562 } 563 564 fputc (')', file); 565 } 566 567 if (do_details && LOCATION_LOCUS (e->goto_locus) > BUILTINS_LOCATION) 568 fprintf (file, " %s:%d:%d", LOCATION_FILE (e->goto_locus), 569 LOCATION_LINE (e->goto_locus), LOCATION_COLUMN (e->goto_locus)); 570 } 571 572 DEBUG_FUNCTION void 573 debug (edge_def &ref) 574 { 575 fprintf (stderr, "<edge (%d -> %d)>\n", 576 ref.src->index, ref.dest->index); 577 dump_edge_info (stderr, &ref, TDF_DETAILS, false); 578 fprintf (stderr, "\n"); 579 } 580 581 DEBUG_FUNCTION void 582 debug (edge_def *ptr) 583 { 584 if (ptr) 585 debug (*ptr); 586 else 587 fprintf (stderr, "<nil>\n"); 588 } 589 590 static void 591 debug_slim (edge e) 592 { 593 fprintf (stderr, "<edge 0x%p (%d -> %d)>", (void *) e, 594 e->src->index, e->dest->index); 595 } 596 597 DEFINE_DEBUG_VEC (edge) 598 DEFINE_DEBUG_HASH_SET (edge) 599 600 /* Simple routines to easily allocate AUX fields of basic blocks. */ 602 603 static struct obstack block_aux_obstack; 604 static void *first_block_aux_obj = 0; 605 static struct obstack edge_aux_obstack; 606 static void *first_edge_aux_obj = 0; 607 608 /* Allocate a memory block of SIZE as BB->aux. The obstack must 609 be first initialized by alloc_aux_for_blocks. */ 610 611 static void 612 alloc_aux_for_block (basic_block bb, int size) 613 { 614 /* Verify that aux field is clear. */ 615 gcc_assert (!bb->aux && first_block_aux_obj); 616 bb->aux = obstack_alloc (&block_aux_obstack, size); 617 memset (bb->aux, 0, size); 618 } 619 620 /* Initialize the block_aux_obstack and if SIZE is nonzero, call 621 alloc_aux_for_block for each basic block. */ 622 623 void 624 alloc_aux_for_blocks (int size) 625 { 626 static int initialized; 627 628 if (!initialized) 629 { 630 gcc_obstack_init (&block_aux_obstack); 631 initialized = 1; 632 } 633 else 634 /* Check whether AUX data are still allocated. */ 635 gcc_assert (!first_block_aux_obj); 636 637 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0); 638 if (size) 639 { 640 basic_block bb; 641 642 FOR_ALL_BB_FN (bb, cfun) 643 alloc_aux_for_block (bb, size); 644 } 645 } 646 647 /* Clear AUX pointers of all blocks. */ 648 649 void 650 clear_aux_for_blocks (void) 651 { 652 basic_block bb; 653 654 FOR_ALL_BB_FN (bb, cfun) 655 bb->aux = NULL; 656 } 657 658 /* Free data allocated in block_aux_obstack and clear AUX pointers 659 of all blocks. */ 660 661 void 662 free_aux_for_blocks (void) 663 { 664 gcc_assert (first_block_aux_obj); 665 obstack_free (&block_aux_obstack, first_block_aux_obj); 666 first_block_aux_obj = NULL; 667 668 clear_aux_for_blocks (); 669 } 670 671 /* Allocate a memory edge of SIZE as E->aux. The obstack must 672 be first initialized by alloc_aux_for_edges. */ 673 674 void 675 alloc_aux_for_edge (edge e, int size) 676 { 677 /* Verify that aux field is clear. */ 678 gcc_assert (!e->aux && first_edge_aux_obj); 679 e->aux = obstack_alloc (&edge_aux_obstack, size); 680 memset (e->aux, 0, size); 681 } 682 683 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call 684 alloc_aux_for_edge for each basic edge. */ 685 686 void 687 alloc_aux_for_edges (int size) 688 { 689 static int initialized; 690 691 if (!initialized) 692 { 693 gcc_obstack_init (&edge_aux_obstack); 694 initialized = 1; 695 } 696 else 697 /* Check whether AUX data are still allocated. */ 698 gcc_assert (!first_edge_aux_obj); 699 700 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0); 701 if (size) 702 { 703 basic_block bb; 704 705 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), 706 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) 707 { 708 edge e; 709 edge_iterator ei; 710 711 FOR_EACH_EDGE (e, ei, bb->succs) 712 alloc_aux_for_edge (e, size); 713 } 714 } 715 } 716 717 /* Clear AUX pointers of all edges. */ 718 719 void 720 clear_aux_for_edges (void) 721 { 722 basic_block bb; 723 edge e; 724 725 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), 726 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) 727 { 728 edge_iterator ei; 729 FOR_EACH_EDGE (e, ei, bb->succs) 730 e->aux = NULL; 731 } 732 } 733 734 /* Free data allocated in edge_aux_obstack and clear AUX pointers 735 of all edges. */ 736 737 void 738 free_aux_for_edges (void) 739 { 740 gcc_assert (first_edge_aux_obj); 741 obstack_free (&edge_aux_obstack, first_edge_aux_obj); 742 first_edge_aux_obj = NULL; 743 744 clear_aux_for_edges (); 745 } 746 747 DEBUG_FUNCTION void 748 debug_bb (basic_block bb) 749 { 750 debug_bb (bb, dump_flags); 751 } 752 753 DEBUG_FUNCTION basic_block 754 debug_bb_n (int n) 755 { 756 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n); 757 debug_bb (bb); 758 return bb; 759 } 760 761 /* Print BB with specified FLAGS. */ 762 763 DEBUG_FUNCTION void 764 debug_bb (basic_block bb, dump_flags_t flags) 765 { 766 dump_bb (stderr, bb, 0, flags); 767 } 768 769 /* Print basic block numbered N with specified FLAGS. */ 770 771 DEBUG_FUNCTION basic_block 772 debug_bb_n (int n, dump_flags_t flags) 773 { 774 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n); 775 debug_bb (bb, flags); 776 return bb; 777 } 778 779 /* Dumps cfg related information about basic block BB to OUTF. 780 If HEADER is true, dump things that appear before the instructions 781 contained in BB. If FOOTER is true, dump things that appear after. 782 Flags are the TDF_* masks as documented in dumpfile.h. 783 NB: With TDF_DETAILS, it is assumed that cfun is available, so 784 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */ 785 786 void 787 dump_bb_info (FILE *outf, basic_block bb, int indent, dump_flags_t flags, 788 bool do_header, bool do_footer) 789 { 790 edge_iterator ei; 791 edge e; 792 static const char * const bb_bitnames[] = 793 { 794 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME , 795 #include "cfg-flags.def" 796 NULL 797 #undef DEF_BASIC_BLOCK_FLAG 798 }; 799 const unsigned n_bitnames = ARRAY_SIZE (bb_bitnames); 800 bool first; 801 char *s_indent = (char *) alloca ((size_t) indent + 1); 802 memset ((void *) s_indent, ' ', (size_t) indent); 803 s_indent[indent] = '\0'; 804 805 gcc_assert (bb->flags <= BB_ALL_FLAGS); 806 807 if (do_header) 808 { 809 unsigned i; 810 811 fputs (";; ", outf); 812 fprintf (outf, "%sbasic block %d, loop depth %d", 813 s_indent, bb->index, bb_loop_depth (bb)); 814 if (flags & TDF_DETAILS) 815 { 816 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl); 817 if (bb->count.initialized_p ()) 818 { 819 fputs (", count ", outf); 820 bb->count.dump (outf, cfun); 821 } 822 if (maybe_hot_bb_p (fun, bb)) 823 fputs (", maybe hot", outf); 824 if (probably_never_executed_bb_p (fun, bb)) 825 fputs (", probably never executed", outf); 826 } 827 fputc ('\n', outf); 828 829 if (flags & TDF_DETAILS) 830 { 831 check_bb_profile (bb, outf, indent); 832 fputs (";; ", outf); 833 fprintf (outf, "%s prev block ", s_indent); 834 if (bb->prev_bb) 835 fprintf (outf, "%d", bb->prev_bb->index); 836 else 837 fprintf (outf, "(nil)"); 838 fprintf (outf, ", next block "); 839 if (bb->next_bb) 840 fprintf (outf, "%d", bb->next_bb->index); 841 else 842 fprintf (outf, "(nil)"); 843 844 fputs (", flags:", outf); 845 first = true; 846 for (i = 0; i < n_bitnames; i++) 847 if (bb->flags & (1 << i)) 848 { 849 if (first) 850 fputs (" (", outf); 851 else 852 fputs (", ", outf); 853 first = false; 854 fputs (bb_bitnames[i], outf); 855 } 856 if (!first) 857 fputc (')', outf); 858 fputc ('\n', outf); 859 } 860 861 fputs (";; ", outf); 862 fprintf (outf, "%s pred: ", s_indent); 863 first = true; 864 FOR_EACH_EDGE (e, ei, bb->preds) 865 { 866 if (! first) 867 { 868 fputs (";; ", outf); 869 fprintf (outf, "%s ", s_indent); 870 } 871 first = false; 872 dump_edge_info (outf, e, flags, 0); 873 fputc ('\n', outf); 874 } 875 if (first) 876 fputc ('\n', outf); 877 } 878 879 if (do_footer) 880 { 881 fputs (";; ", outf); 882 fprintf (outf, "%s succ: ", s_indent); 883 first = true; 884 FOR_EACH_EDGE (e, ei, bb->succs) 885 { 886 if (! first) 887 { 888 fputs (";; ", outf); 889 fprintf (outf, "%s ", s_indent); 890 } 891 first = false; 892 dump_edge_info (outf, e, flags, 1); 893 fputc ('\n', outf); 894 } 895 if (first) 896 fputc ('\n', outf); 897 } 898 } 899 900 /* Dumps a brief description of cfg to FILE. */ 901 902 void 903 brief_dump_cfg (FILE *file, dump_flags_t flags) 904 { 905 basic_block bb; 906 907 FOR_EACH_BB_FN (bb, cfun) 908 { 909 dump_bb_info (file, bb, 0, flags & TDF_DETAILS, true, true); 910 } 911 } 912 913 /* Set probability of E to NEW_PROB and rescale other edges 914 from E->src so their sum remains the same. */ 915 916 void 917 set_edge_probability_and_rescale_others (edge e, profile_probability new_prob) 918 { 919 edge e2; 920 edge_iterator ei; 921 if (e->probability == new_prob) 922 return; 923 /* If we made E unconditional, drop other frequencies to 0. */ 924 if (new_prob == profile_probability::always ()) 925 { 926 FOR_EACH_EDGE (e2, ei, e->src->succs) 927 if (e2 != e) 928 e2->probability = profile_probability::never (); 929 } 930 else 931 { 932 int n = 0; 933 edge other_e = NULL; 934 935 /* See how many other edges are leaving exit_edge->src. */ 936 FOR_EACH_EDGE (e2, ei, e->src->succs) 937 if (e2 != e && !(e2->flags & EDGE_FAKE)) 938 { 939 other_e = e2; 940 n++; 941 } 942 /* If there is only one other edge with non-zero probability we do not 943 need to scale which drops quality of profile from precise 944 to adjusted. */ 945 if (n == 1) 946 other_e->probability = new_prob.invert (); 947 /* Nothing to do if there are no other edges. */ 948 else if (!n) 949 ; 950 /* Do scaling if possible. */ 951 else if (e->probability.invert ().nonzero_p ()) 952 { 953 profile_probability num = new_prob.invert (), 954 den = e->probability.invert (); 955 FOR_EACH_EDGE (e2, ei, e->src->succs) 956 if (e2 != e && !(e2->flags & EDGE_FAKE)) 957 e2->probability = e2->probability.apply_scale (num, den); 958 } 959 else 960 { 961 if (dump_file && (dump_flags & TDF_DETAILS)) 962 fprintf (dump_file, 963 ";; probability of edge %i->%i set reduced from 1." 964 " The remaining edges are left inconsistent.\n", 965 e->src->index, e->dest->index); 966 FOR_EACH_EDGE (e2, ei, e->src->succs) 967 if (e2 != e && !(e2->flags & EDGE_FAKE)) 968 e2->probability = new_prob.invert ().guessed () / n; 969 } 970 } 971 e->probability = new_prob; 972 } 973 974 /* An edge originally destinating BB of COUNT has been proved to 975 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be 976 redirected to destination of TAKEN_EDGE. 977 978 This function may leave the profile inconsistent in the case TAKEN_EDGE 979 frequency or count is believed to be lower than COUNT 980 respectively. */ 981 void 982 update_bb_profile_for_threading (basic_block bb, 983 profile_count count, edge taken_edge) 984 { 985 gcc_assert (bb == taken_edge->src); 986 987 /* If there is no profile or the threaded path is never executed 988 we don't need to upate. */ 989 if (!bb->count.initialized_p () 990 || count == profile_count::zero ()) 991 return; 992 993 if (bb->count < count) 994 { 995 if (dump_file) 996 fprintf (dump_file, "bb %i count became negative after threading", 997 bb->index); 998 /* If probabilities looks very off, scale down and reduce to guesses 999 to avoid dropping the other path close to zero. */ 1000 if (bb->count < count.apply_scale (7, 8)) 1001 count = bb->count.apply_scale (1, 2).guessed (); 1002 } 1003 1004 /* If bb->count will become zero, the probabilities on the original path 1005 are not really known, but it is probably better to keep original ones 1006 then try to invent something new. */ 1007 if (!(bb->count <= count)) 1008 { 1009 profile_probability prob; 1010 /* Compute the probability of TAKEN_EDGE being reached via threaded edge. 1011 Watch for overflows. */ 1012 if (bb->count.nonzero_p ()) 1013 prob = count.probability_in (bb->count); 1014 else 1015 prob = taken_edge->probability.apply_scale (1, 2).guessed (); 1016 if (prob > taken_edge->probability) 1017 { 1018 if (dump_file) 1019 { 1020 fprintf (dump_file, "Jump threading proved that the probability " 1021 "of edge %i->%i was originally estimated too small. " 1022 "(it is ", 1023 taken_edge->src->index, taken_edge->dest->index); 1024 taken_edge->probability.dump (dump_file); 1025 fprintf (dump_file, " should be "); 1026 prob.dump (dump_file); 1027 fprintf (dump_file, ")\n"); 1028 } 1029 prob = taken_edge->probability.apply_scale (6, 8).guessed (); 1030 } 1031 set_edge_probability_and_rescale_others (taken_edge, 1032 (taken_edge->probability - prob) 1033 / prob.invert ()); 1034 } 1035 bb->count -= count; 1036 } 1037 1038 /* Multiply all frequencies of basic blocks in array BBS of length NBBS 1039 by NUM/DEN, in profile_count arithmetic. More accurate than previous 1040 function but considerably slower. */ 1041 void 1042 scale_bbs_frequencies_profile_count (basic_block *bbs, int nbbs, 1043 profile_count num, profile_count den) 1044 { 1045 int i; 1046 if (num == profile_count::zero () || den.nonzero_p ()) 1047 for (i = 0; i < nbbs; i++) 1048 bbs[i]->count = bbs[i]->count.apply_scale (num, den); 1049 } 1050 1051 /* Multiply all frequencies of basic blocks in array BBS of length NBBS 1052 by NUM/DEN, in profile_count arithmetic. More accurate than previous 1053 function but considerably slower. */ 1054 void 1055 scale_bbs_frequencies (basic_block *bbs, int nbbs, 1056 profile_probability p) 1057 { 1058 int i; 1059 1060 for (i = 0; i < nbbs; i++) 1061 bbs[i]->count = bbs[i]->count.apply_probability (p); 1062 } 1063 1064 /* Data structures used to maintain mapping between basic blocks and 1065 copies. */ 1066 typedef hash_map<int_hash<int, -1, -2>, int> copy_map_t; 1067 static copy_map_t *bb_original; 1068 static copy_map_t *bb_copy; 1069 1070 /* And between loops and copies. */ 1071 static copy_map_t *loop_copy; 1072 1073 /* Initialize the data structures to maintain mapping between blocks 1074 and its copies. */ 1075 void 1076 initialize_original_copy_tables (void) 1077 { 1078 bb_original = new copy_map_t (10); 1079 bb_copy = new copy_map_t (10); 1080 loop_copy = new copy_map_t (10); 1081 } 1082 1083 /* Reset the data structures to maintain mapping between blocks and 1084 its copies. */ 1085 1086 void 1087 reset_original_copy_tables (void) 1088 { 1089 bb_original->empty (); 1090 bb_copy->empty (); 1091 loop_copy->empty (); 1092 } 1093 1094 /* Free the data structures to maintain mapping between blocks and 1095 its copies. */ 1096 void 1097 free_original_copy_tables (void) 1098 { 1099 delete bb_copy; 1100 bb_copy = NULL; 1101 delete bb_original; 1102 bb_original = NULL; 1103 delete loop_copy; 1104 loop_copy = NULL; 1105 } 1106 1107 /* Return true iff we have had a call to initialize_original_copy_tables 1108 without a corresponding call to free_original_copy_tables. */ 1109 1110 bool 1111 original_copy_tables_initialized_p (void) 1112 { 1113 return bb_copy != NULL; 1114 } 1115 1116 /* Removes the value associated with OBJ from table TAB. */ 1117 1118 static void 1119 copy_original_table_clear (copy_map_t *tab, unsigned obj) 1120 { 1121 if (!original_copy_tables_initialized_p ()) 1122 return; 1123 1124 tab->remove (obj); 1125 } 1126 1127 /* Sets the value associated with OBJ in table TAB to VAL. 1128 Do nothing when data structures are not initialized. */ 1129 1130 static void 1131 copy_original_table_set (copy_map_t *tab, 1132 unsigned obj, unsigned val) 1133 { 1134 if (!original_copy_tables_initialized_p ()) 1135 return; 1136 1137 tab->put (obj, val); 1138 } 1139 1140 /* Set original for basic block. Do nothing when data structures are not 1141 initialized so passes not needing this don't need to care. */ 1142 void 1143 set_bb_original (basic_block bb, basic_block original) 1144 { 1145 copy_original_table_set (bb_original, bb->index, original->index); 1146 } 1147 1148 /* Get the original basic block. */ 1149 basic_block 1150 get_bb_original (basic_block bb) 1151 { 1152 gcc_assert (original_copy_tables_initialized_p ()); 1153 1154 int *entry = bb_original->get (bb->index); 1155 if (entry) 1156 return BASIC_BLOCK_FOR_FN (cfun, *entry); 1157 else 1158 return NULL; 1159 } 1160 1161 /* Set copy for basic block. Do nothing when data structures are not 1162 initialized so passes not needing this don't need to care. */ 1163 void 1164 set_bb_copy (basic_block bb, basic_block copy) 1165 { 1166 copy_original_table_set (bb_copy, bb->index, copy->index); 1167 } 1168 1169 /* Get the copy of basic block. */ 1170 basic_block 1171 get_bb_copy (basic_block bb) 1172 { 1173 gcc_assert (original_copy_tables_initialized_p ()); 1174 1175 int *entry = bb_copy->get (bb->index); 1176 if (entry) 1177 return BASIC_BLOCK_FOR_FN (cfun, *entry); 1178 else 1179 return NULL; 1180 } 1181 1182 /* Set copy for LOOP to COPY. Do nothing when data structures are not 1183 initialized so passes not needing this don't need to care. */ 1184 1185 void 1186 set_loop_copy (class loop *loop, class loop *copy) 1187 { 1188 if (!copy) 1189 copy_original_table_clear (loop_copy, loop->num); 1190 else 1191 copy_original_table_set (loop_copy, loop->num, copy->num); 1192 } 1193 1194 /* Get the copy of LOOP. */ 1195 1196 class loop * 1197 get_loop_copy (class loop *loop) 1198 { 1199 gcc_assert (original_copy_tables_initialized_p ()); 1200 1201 int *entry = loop_copy->get (loop->num); 1202 if (entry) 1203 return get_loop (cfun, *entry); 1204 else 1205 return NULL; 1206 } 1207 1208 /* Scales the frequencies of all basic blocks that are strictly 1209 dominated by BB by NUM/DEN. */ 1210 1211 void 1212 scale_strictly_dominated_blocks (basic_block bb, 1213 profile_count num, profile_count den) 1214 { 1215 basic_block son; 1216 1217 if (!den.nonzero_p () && !(num == profile_count::zero ())) 1218 return; 1219 auto_vec <basic_block, 8> worklist; 1220 worklist.safe_push (bb); 1221 1222 while (!worklist.is_empty ()) 1223 for (son = first_dom_son (CDI_DOMINATORS, worklist.pop ()); 1224 son; 1225 son = next_dom_son (CDI_DOMINATORS, son)) 1226 { 1227 son->count = son->count.apply_scale (num, den); 1228 worklist.safe_push (son); 1229 } 1230 } 1231