comparison gcc/shrink-wrap.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 /* Shrink-wrapping related optimizations. 1 /* Shrink-wrapping related optimizations.
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
155 const HARD_REG_SET defs, 155 const HARD_REG_SET defs,
156 bool *split_p, 156 bool *split_p,
157 struct dead_debug_local *debug) 157 struct dead_debug_local *debug)
158 { 158 {
159 rtx set, src, dest; 159 rtx set, src, dest;
160 bitmap live_out, live_in, bb_uses, bb_defs; 160 bitmap live_out, live_in, bb_uses = NULL, bb_defs = NULL;
161 unsigned int i, dregno, end_dregno; 161 unsigned int i, dregno, end_dregno;
162 unsigned int sregno = FIRST_PSEUDO_REGISTER; 162 unsigned int sregno = FIRST_PSEUDO_REGISTER;
163 unsigned int end_sregno = FIRST_PSEUDO_REGISTER; 163 unsigned int end_sregno = FIRST_PSEUDO_REGISTER;
164 basic_block next_block; 164 basic_block next_block;
165 edge live_edge; 165 edge live_edge;
307 307
308 /* At this point we are committed to moving INSN, but let's try to 308 /* At this point we are committed to moving INSN, but let's try to
309 move it as far as we can. */ 309 move it as far as we can. */
310 do 310 do
311 { 311 {
312 if (MAY_HAVE_DEBUG_INSNS) 312 if (MAY_HAVE_DEBUG_BIND_INSNS)
313 { 313 {
314 FOR_BB_INSNS_REVERSE (bb, dinsn) 314 FOR_BB_INSNS_REVERSE (bb, dinsn)
315 if (DEBUG_INSN_P (dinsn)) 315 if (DEBUG_BIND_INSN_P (dinsn))
316 { 316 {
317 df_ref use; 317 df_ref use;
318 FOR_EACH_INSN_USE (use, dinsn) 318 FOR_EACH_INSN_USE (use, dinsn)
319 if (refers_to_regno_p (dregno, end_dregno, 319 if (refers_to_regno_p (dregno, end_dregno,
320 DF_REF_REG (use), (rtx *) NULL)) 320 DF_REF_REG (use), (rtx *) NULL))
328 bb = next_block; 328 bb = next_block;
329 329
330 /* Check whether BB uses DEST or clobbers DEST. We need to add 330 /* Check whether BB uses DEST or clobbers DEST. We need to add
331 INSN to BB if so. Either way, DEST is no longer live on entry, 331 INSN to BB if so. Either way, DEST is no longer live on entry,
332 except for any part that overlaps SRC (next loop). */ 332 except for any part that overlaps SRC (next loop). */
333 bb_uses = &DF_LR_BB_INFO (bb)->use; 333 if (!*split_p)
334 bb_defs = &DF_LR_BB_INFO (bb)->def; 334 {
335 bb_uses = &DF_LR_BB_INFO (bb)->use;
336 bb_defs = &DF_LR_BB_INFO (bb)->def;
337 }
335 if (df_live) 338 if (df_live)
336 { 339 {
337 for (i = dregno; i < end_dregno; i++) 340 for (i = dregno; i < end_dregno; i++)
338 { 341 {
339 if (*split_p 342 if (*split_p
559 basic_block new_bb = create_basic_block (note, note, old_bb); 562 basic_block new_bb = create_basic_block (note, note, old_bb);
560 BB_COPY_PARTITION (new_bb, old_bb); 563 BB_COPY_PARTITION (new_bb, old_bb);
561 BB_END (old_bb) = end; 564 BB_END (old_bb) = end;
562 565
563 redirect_edge_succ (e, new_bb); 566 redirect_edge_succ (e, new_bb);
564 new_bb->frequency = EDGE_FREQUENCY (e); 567 new_bb->count = e->count ();
565 e->flags |= EDGE_FALLTHRU; 568 e->flags |= EDGE_FALLTHRU;
566 569
567 e = make_single_succ_edge (new_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); 570 e = make_single_succ_edge (new_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
568 } 571 }
569 572
878 /* Compute what fraction of the frequency and count of the blocks that run 881 /* Compute what fraction of the frequency and count of the blocks that run
879 both with and without prologue are for running with prologue. This gives 882 both with and without prologue are for running with prologue. This gives
880 the correct answer for reducible flow graphs; for irreducible flow graphs 883 the correct answer for reducible flow graphs; for irreducible flow graphs
881 our profile is messed up beyond repair anyway. */ 884 our profile is messed up beyond repair anyway. */
882 885
883 gcov_type num = 0; 886 profile_count num = profile_count::zero ();
884 gcov_type den = 0; 887 profile_count den = profile_count::zero ();
885 888
886 FOR_EACH_EDGE (e, ei, pro->preds) 889 FOR_EACH_EDGE (e, ei, pro->preds)
887 if (!dominated_by_p (CDI_DOMINATORS, e->src, pro)) 890 if (!dominated_by_p (CDI_DOMINATORS, e->src, pro))
888 { 891 {
889 num += EDGE_FREQUENCY (e); 892 if (e->count ().initialized_p ())
890 den += e->src->frequency; 893 num += e->count ();
894 if (e->src->count.initialized_p ())
895 den += e->src->count;
891 } 896 }
892
893 if (den == 0)
894 den = 1;
895 897
896 /* All is okay, so do it. */ 898 /* All is okay, so do it. */
897 899
898 crtl->shrink_wrapped = true; 900 crtl->shrink_wrapped = true;
899 if (dump_file) 901 if (dump_file)
917 if (EDGE_COUNT (dup->succs) == 0) 919 if (EDGE_COUNT (dup->succs) == 0)
918 emit_barrier_after_bb (dup); 920 emit_barrier_after_bb (dup);
919 921
920 if (dump_file) 922 if (dump_file)
921 fprintf (dump_file, "Duplicated %d to %d\n", bb->index, dup->index); 923 fprintf (dump_file, "Duplicated %d to %d\n", bb->index, dup->index);
922 924
923 bb->frequency = RDIV (num * bb->frequency, den); 925 if (num == profile_count::zero () || den.nonzero_p ())
924 dup->frequency -= bb->frequency; 926 bb->count = bb->count.apply_scale (num, den);
925 bb->count = bb->count.apply_scale (num, den);
926 dup->count -= bb->count; 927 dup->count -= bb->count;
927 } 928 }
928 929
929 /* Now change the edges to point to the copies, where appropriate. */ 930 /* Now change the edges to point to the copies, where appropriate. */
930 931
993 { 994 {
994 ei_next (&ei); 995 ei_next (&ei);
995 continue; 996 continue;
996 } 997 }
997 998
998 new_bb->count += e->src->count.apply_probability (e->probability); 999 new_bb->count += e->count ();
999 new_bb->frequency += EDGE_FREQUENCY (e);
1000 1000
1001 redirect_edge_and_branch_force (e, new_bb); 1001 redirect_edge_and_branch_force (e, new_bb);
1002 if (dump_file) 1002 if (dump_file)
1003 fprintf (dump_file, "Redirected edge from %d\n", e->src->index); 1003 fprintf (dump_file, "Redirected edge from %d\n", e->src->index);
1004 } 1004 }
1179 that does not come from backedges. Calculating this by simply 1179 that does not come from backedges. Calculating this by simply
1180 adding the cost of all edges that aren't backedges does not 1180 adding the cost of all edges that aren't backedges does not
1181 work: this does not always add up to the block frequency at 1181 work: this does not always add up to the block frequency at
1182 all, and even if it does, rounding error makes for bad 1182 all, and even if it does, rounding error makes for bad
1183 decisions. */ 1183 decisions. */
1184 SW (bb)->own_cost = bb->frequency; 1184 SW (bb)->own_cost = bb->count.to_frequency (cfun);
1185 1185
1186 edge e; 1186 edge e;
1187 edge_iterator ei; 1187 edge_iterator ei;
1188 FOR_EACH_EDGE (e, ei, bb->preds) 1188 FOR_EACH_EDGE (e, ei, bb->preds)
1189 if (dominated_by_p (CDI_DOMINATORS, e->src, bb)) 1189 if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
1251 } 1251 }
1252 1252
1253 /* Set HAS_COMPONENTS in every block to the maximum it can be set to without 1253 /* Set HAS_COMPONENTS in every block to the maximum it can be set to without
1254 setting it on any path from entry to exit where it was not already set 1254 setting it on any path from entry to exit where it was not already set
1255 somewhere (or, for blocks that have no path to the exit, consider only 1255 somewhere (or, for blocks that have no path to the exit, consider only
1256 paths from the entry to the block itself). */ 1256 paths from the entry to the block itself). Return whether any changes
1257 static void 1257 were made to some HAS_COMPONENTS. */
1258 static bool
1258 spread_components (sbitmap components) 1259 spread_components (sbitmap components)
1259 { 1260 {
1260 basic_block entry_block = ENTRY_BLOCK_PTR_FOR_FN (cfun); 1261 basic_block entry_block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1261 basic_block exit_block = EXIT_BLOCK_PTR_FOR_FN (cfun); 1262 basic_block exit_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
1262 1263
1371 todo.quick_push (e->src); 1372 todo.quick_push (e->src);
1372 1373
1373 bitmap_clear_bit (seen, bb->index); 1374 bitmap_clear_bit (seen, bb->index);
1374 } 1375 }
1375 1376
1377 todo.release ();
1378
1376 /* Finally, mark everything not not needed both forwards and backwards. */ 1379 /* Finally, mark everything not not needed both forwards and backwards. */
1377 1380
1381 bool did_changes = false;
1382
1378 FOR_EACH_BB_FN (bb, cfun) 1383 FOR_EACH_BB_FN (bb, cfun)
1379 { 1384 {
1385 bitmap_copy (old, SW (bb)->has_components);
1386
1380 bitmap_and (SW (bb)->head_components, SW (bb)->head_components, 1387 bitmap_and (SW (bb)->head_components, SW (bb)->head_components,
1381 SW (bb)->tail_components); 1388 SW (bb)->tail_components);
1382 bitmap_and_compl (SW (bb)->has_components, components, 1389 bitmap_and_compl (SW (bb)->has_components, components,
1383 SW (bb)->head_components); 1390 SW (bb)->head_components);
1391
1392 if (!did_changes && !bitmap_equal_p (old, SW (bb)->has_components))
1393 did_changes = true;
1384 } 1394 }
1385 1395
1386 FOR_ALL_BB_FN (bb, cfun) 1396 FOR_ALL_BB_FN (bb, cfun)
1387 { 1397 {
1388 if (dump_file) 1398 if (dump_file)
1390 fprintf (dump_file, "bb %d components:", bb->index); 1400 fprintf (dump_file, "bb %d components:", bb->index);
1391 dump_components ("has", SW (bb)->has_components); 1401 dump_components ("has", SW (bb)->has_components);
1392 fprintf (dump_file, "\n"); 1402 fprintf (dump_file, "\n");
1393 } 1403 }
1394 } 1404 }
1405
1406 return did_changes;
1395 } 1407 }
1396 1408
1397 /* If we cannot handle placing some component's prologues or epilogues where 1409 /* If we cannot handle placing some component's prologues or epilogues where
1398 we decided we should place them, unmark that component in COMPONENTS so 1410 we decided we should place them, unmark that component in COMPONENTS so
1399 that it is not wrapped separately. */ 1411 that it is not wrapped separately. */
1793 sbitmap_iterator sbi; 1805 sbitmap_iterator sbi;
1794 unsigned int j; 1806 unsigned int j;
1795 EXECUTE_IF_SET_IN_BITMAP (components, 0, j, sbi) 1807 EXECUTE_IF_SET_IN_BITMAP (components, 0, j, sbi)
1796 place_prologue_for_one_component (j, first_bb); 1808 place_prologue_for_one_component (j, first_bb);
1797 1809
1798 spread_components (components); 1810 /* Try to minimize the number of saves and restores. Do this as long as
1811 it changes anything. This does not iterate more than a few times. */
1812 int spread_times = 0;
1813 while (spread_components (components))
1814 {
1815 spread_times++;
1816
1817 if (dump_file)
1818 fprintf (dump_file, "Now spread %d times.\n", spread_times);
1819 }
1799 1820
1800 disqualify_problematic_components (components); 1821 disqualify_problematic_components (components);
1801 1822
1802 /* Don't separately shrink-wrap anything where the "main" prologue will 1823 /* Don't separately shrink-wrap anything where the "main" prologue will
1803 go; the target code can often optimize things if it is presented with 1824 go; the target code can often optimize things if it is presented with