comparison gcc/cfg.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Control flow graph manipulation code for GNU compiler. 1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc. 2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it under 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 7 the terms of the GNU General Public License as published by the Free
66 init_flow (struct function *the_fun) 66 init_flow (struct function *the_fun)
67 { 67 {
68 if (!the_fun->cfg) 68 if (!the_fun->cfg)
69 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> (); 69 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
70 n_edges_for_fn (the_fun) = 0; 70 n_edges_for_fn (the_fun) = 0;
71 the_fun->cfg->count_max = profile_count::uninitialized ();
71 ENTRY_BLOCK_PTR_FOR_FN (the_fun) 72 ENTRY_BLOCK_PTR_FOR_FN (the_fun)
72 = alloc_block (); 73 = alloc_block ();
73 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK; 74 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
74 EXIT_BLOCK_PTR_FOR_FN (the_fun) 75 EXIT_BLOCK_PTR_FOR_FN (the_fun)
75 = alloc_block (); 76 = alloc_block ();
76 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK; 77 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
77 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb 78 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
78 = EXIT_BLOCK_PTR_FOR_FN (the_fun); 79 = EXIT_BLOCK_PTR_FOR_FN (the_fun);
79 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb 80 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
80 = ENTRY_BLOCK_PTR_FOR_FN (the_fun); 81 = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
82 the_fun->cfg->edge_flags_allocated = EDGE_ALL_FLAGS;
83 the_fun->cfg->bb_flags_allocated = BB_ALL_FLAGS;
81 } 84 }
82 85
83 /* Helper function for remove_edge and clear_edges. Frees edge structure 86 /* Helper function for remove_edge and clear_edges. Frees edge structure
84 without actually removing it from the pred/succ arrays. */ 87 without actually removing it from the pred/succ arrays. */
85 88
383 /* Clear all basic block flags that do not have to be preserved. */ 386 /* Clear all basic block flags that do not have to be preserved. */
384 void 387 void
385 clear_bb_flags (void) 388 clear_bb_flags (void)
386 { 389 {
387 basic_block bb; 390 basic_block bb;
391 int flags_to_preserve = BB_FLAGS_TO_PRESERVE;
392 if (current_loops
393 && loops_state_satisfies_p (cfun, LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
394 flags_to_preserve |= BB_IRREDUCIBLE_LOOP;
388 395
389 FOR_ALL_BB_FN (bb, cfun) 396 FOR_ALL_BB_FN (bb, cfun)
390 bb->flags &= BB_FLAGS_TO_PRESERVE; 397 bb->flags &= flags_to_preserve;
391 } 398 }
392 399
393 /* Check the consistency of profile information. We can't do that 400 /* Check the consistency of profile information. We can't do that
394 in verify_flow_info, as the counts may get invalid for incompletely 401 in verify_flow_info, as the counts may get invalid for incompletely
395 solved graphs, later eliminating of conditionals or roundoff errors. 402 solved graphs, later eliminating of conditionals or roundoff errors.
445 } 452 }
446 } 453 }
447 } 454 }
448 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun)) 455 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
449 { 456 {
450 int sum = 0; 457 profile_count sum = profile_count::zero ();
451 FOR_EACH_EDGE (e, ei, bb->preds) 458 FOR_EACH_EDGE (e, ei, bb->preds)
452 sum += EDGE_FREQUENCY (e); 459 sum += e->count ();
453 if (abs (sum - bb->frequency) > 100) 460 if (sum.differs_from_p (bb->count))
454 fprintf (file, 461 {
455 ";; %sInvalid sum of incoming frequencies %i, should be %i\n", 462 fprintf (file, ";; %sInvalid sum of incoming counts ",
456 s_indent, sum, bb->frequency); 463 s_indent);
464 sum.dump (file);
465 fprintf (file, ", should be ");
466 bb->count.dump (file);
467 fprintf (file, "\n");
468 }
457 } 469 }
458 if (BB_PARTITION (bb) == BB_COLD_PARTITION) 470 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
459 { 471 {
460 /* Warn about inconsistencies in the partitioning that are 472 /* Warn about inconsistencies in the partitioning that are
461 currently caused by profile insanities created via optimization. */ 473 currently caused by profile insanities created via optimization. */
533 545
534 DEBUG_FUNCTION void 546 DEBUG_FUNCTION void
535 debug (edge_def &ref) 547 debug (edge_def &ref)
536 { 548 {
537 /* FIXME (crowl): Is this desireable? */ 549 /* FIXME (crowl): Is this desireable? */
538 dump_edge_info (stderr, &ref, 0, false); 550 dump_edge_info (stderr, &ref, TDF_NONE, false);
539 dump_edge_info (stderr, &ref, 0, true); 551 dump_edge_info (stderr, &ref, TDF_NONE, true);
540 } 552 }
541 553
542 DEBUG_FUNCTION void 554 DEBUG_FUNCTION void
543 debug (edge_def *ptr) 555 debug (edge_def *ptr)
544 { 556 {
545 if (ptr) 557 if (ptr)
546 debug (*ptr); 558 debug (*ptr);
547 else 559 else
548 fprintf (stderr, "<nil>\n"); 560 fprintf (stderr, "<nil>\n");
549 } 561 }
562
563 static void
564 debug_slim (edge e)
565 {
566 fprintf (stderr, "<edge 0x%p (%d -> %d)>", (void *) e,
567 e->src->index, e->dest->index);
568 }
569
570 DEFINE_DEBUG_VEC (edge)
571 DEFINE_DEBUG_HASH_SET (edge)
550 572
551 /* Simple routines to easily allocate AUX fields of basic blocks. */ 573 /* Simple routines to easily allocate AUX fields of basic blocks. */
552 574
553 static struct obstack block_aux_obstack; 575 static struct obstack block_aux_obstack;
554 static void *first_block_aux_obj = 0; 576 static void *first_block_aux_obj = 0;
749 if (bb->count.initialized_p ()) 771 if (bb->count.initialized_p ())
750 { 772 {
751 fputs (", count ", outf); 773 fputs (", count ", outf);
752 bb->count.dump (outf); 774 bb->count.dump (outf);
753 } 775 }
754 fprintf (outf, ", freq %i", bb->frequency);
755 if (maybe_hot_bb_p (fun, bb)) 776 if (maybe_hot_bb_p (fun, bb))
756 fputs (", maybe hot", outf); 777 fputs (", maybe hot", outf);
757 if (probably_never_executed_bb_p (fun, bb)) 778 if (probably_never_executed_bb_p (fun, bb))
758 fputs (", probably never executed", outf); 779 fputs (", probably never executed", outf);
759 } 780 }
841 { 862 {
842 dump_bb_info (file, bb, 0, flags & TDF_DETAILS, true, true); 863 dump_bb_info (file, bb, 0, flags & TDF_DETAILS, true, true);
843 } 864 }
844 } 865 }
845 866
846 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to 867 /* An edge originally destinating BB of COUNT has been proved to
847 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be 868 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
848 redirected to destination of TAKEN_EDGE. 869 redirected to destination of TAKEN_EDGE.
849 870
850 This function may leave the profile inconsistent in the case TAKEN_EDGE 871 This function may leave the profile inconsistent in the case TAKEN_EDGE
851 frequency or count is believed to be lower than FREQUENCY or COUNT 872 frequency or count is believed to be lower than COUNT
852 respectively. */ 873 respectively. */
853 void 874 void
854 update_bb_profile_for_threading (basic_block bb, int edge_frequency, 875 update_bb_profile_for_threading (basic_block bb,
855 profile_count count, edge taken_edge) 876 profile_count count, edge taken_edge)
856 { 877 {
857 edge c; 878 edge c;
858 profile_probability prob; 879 profile_probability prob;
859 edge_iterator ei; 880 edge_iterator ei;
864 fprintf (dump_file, "bb %i count became negative after threading", 885 fprintf (dump_file, "bb %i count became negative after threading",
865 bb->index); 886 bb->index);
866 } 887 }
867 bb->count -= count; 888 bb->count -= count;
868 889
869 bb->frequency -= edge_frequency;
870 if (bb->frequency < 0)
871 bb->frequency = 0;
872
873 /* Compute the probability of TAKEN_EDGE being reached via threaded edge. 890 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
874 Watch for overflows. */ 891 Watch for overflows. */
875 if (bb->frequency) 892 if (bb->count.nonzero_p ())
876 /* FIXME: We should get edge frequency as count. */ 893 prob = count.probability_in (bb->count);
877 prob = profile_probability::probability_in_gcov_type
878 (edge_frequency, bb->frequency);
879 else 894 else
880 prob = profile_probability::never (); 895 prob = profile_probability::never ();
881 if (prob > taken_edge->probability) 896 if (prob > taken_edge->probability)
882 { 897 {
883 if (dump_file) 898 if (dump_file)
897 taken_edge->probability -= prob; 912 taken_edge->probability -= prob;
898 prob = prob.invert (); 913 prob = prob.invert ();
899 if (prob == profile_probability::never ()) 914 if (prob == profile_probability::never ())
900 { 915 {
901 if (dump_file) 916 if (dump_file)
902 fprintf (dump_file, "Edge frequencies of bb %i has been reset, " 917 fprintf (dump_file, "Edge probabilities of bb %i has been reset, "
903 "frequency of block should end up being 0, it is %i\n", 918 "count of block should end up being 0, it is non-zero\n",
904 bb->index, bb->frequency); 919 bb->index);
905 EDGE_SUCC (bb, 0)->probability = profile_probability::guessed_always (); 920 EDGE_SUCC (bb, 0)->probability = profile_probability::guessed_always ();
906 ei = ei_start (bb->succs); 921 ei = ei_start (bb->succs);
907 ei_next (&ei); 922 ei_next (&ei);
908 for (; (c = ei_safe_edge (ei)); ei_next (&ei)) 923 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
909 c->probability = profile_probability::guessed_never (); 924 c->probability = profile_probability::guessed_never ();
916 931
917 gcc_assert (bb == taken_edge->src); 932 gcc_assert (bb == taken_edge->src);
918 } 933 }
919 934
920 /* Multiply all frequencies of basic blocks in array BBS of length NBBS 935 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
921 by NUM/DEN, in int arithmetic. May lose some accuracy. */ 936 by NUM/DEN, in profile_count arithmetic. More accurate than previous
922 void 937 function but considerably slower. */
923 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den) 938 void
939 scale_bbs_frequencies_profile_count (basic_block *bbs, int nbbs,
940 profile_count num, profile_count den)
924 { 941 {
925 int i; 942 int i;
926 if (num < 0) 943 if (num == profile_count::zero () || den.nonzero_p ())
927 num = 0; 944 for (i = 0; i < nbbs; i++)
928
929 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
930 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
931 and still safely fit in int during calculations. */
932 if (den > 1000)
933 {
934 if (num > 1000000)
935 return;
936
937 num = RDIV (1000 * num, den);
938 den = 1000;
939 }
940 if (num > 100 * den)
941 return;
942
943 for (i = 0; i < nbbs; i++)
944 {
945 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
946 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
947 if (bbs[i]->frequency > BB_FREQ_MAX)
948 bbs[i]->frequency = BB_FREQ_MAX;
949 bbs[i]->count = bbs[i]->count.apply_scale (num, den); 945 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
950 }
951 }
952
953 /* numbers smaller than this value are safe to multiply without getting
954 64bit overflow. */
955 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1))
956
957 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
958 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
959 function but considerably slower. */
960 void
961 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
962 gcov_type den)
963 {
964 int i;
965 gcov_type fraction = RDIV (num * 65536, den);
966
967 gcc_assert (fraction >= 0);
968
969 if (num < MAX_SAFE_MULTIPLIER)
970 for (i = 0; i < nbbs; i++)
971 {
972 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
973 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
974 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
975 else
976 bbs[i]->count = bbs[i]->count.apply_scale (fraction, 65536);
977 }
978 else
979 for (i = 0; i < nbbs; i++)
980 {
981 if (sizeof (gcov_type) > sizeof (int))
982 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
983 else
984 bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
985 bbs[i]->count = bbs[i]->count.apply_scale (fraction, 65536);
986 }
987 } 946 }
988 947
989 /* Multiply all frequencies of basic blocks in array BBS of length NBBS 948 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
990 by NUM/DEN, in profile_count arithmetic. More accurate than previous 949 by NUM/DEN, in profile_count arithmetic. More accurate than previous
991 function but considerably slower. */ 950 function but considerably slower. */
992 void 951 void
993 scale_bbs_frequencies_profile_count (basic_block *bbs, int nbbs,
994 profile_count num, profile_count den)
995 {
996 int i;
997
998 for (i = 0; i < nbbs; i++)
999 {
1000 bbs[i]->frequency = RDIV (bbs[i]->frequency * num.to_gcov_type (),
1001 den.to_gcov_type ());
1002 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
1003 }
1004 }
1005
1006 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1007 by NUM/DEN, in profile_count arithmetic. More accurate than previous
1008 function but considerably slower. */
1009 void
1010 scale_bbs_frequencies (basic_block *bbs, int nbbs, 952 scale_bbs_frequencies (basic_block *bbs, int nbbs,
1011 profile_probability p) 953 profile_probability p)
1012 { 954 {
1013 int i; 955 int i;
1014 956
1015 for (i = 0; i < nbbs; i++) 957 for (i = 0; i < nbbs; i++)
1016 { 958 bbs[i]->count = bbs[i]->count.apply_probability (p);
1017 bbs[i]->frequency = p.apply (bbs[i]->frequency);
1018 bbs[i]->count = bbs[i]->count.apply_probability (p);
1019 }
1020 } 959 }
1021 960
1022 /* Helper types for hash tables. */ 961 /* Helper types for hash tables. */
1023 962
1024 struct htab_bb_copy_original_entry 963 struct htab_bb_copy_original_entry