From: Jan Hubicka Date: Sat, 1 Jul 2023 07:09:39 +0000 (+0200) Subject: Fix update_bb_profile_for_threading X-Git-Tag: basepoints/gcc-15~7911 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02460c0b8c9000359a09440f9532664a7835f158;p=thirdparty%2Fgcc.git Fix update_bb_profile_for_threading Fix profile some of profile mismatched caused by profile updating. It seems that I misupdated update_bb_profile_for_threading in 2017 which results in invalid updates from rtl threading and threadbackwards. update_bb_profile_for_threading knows that some paths to BB are being redirected elsehwere and those paths will exit from BB with E. So it needs to determine probability of the duplicated path and redistribute probablities. For some reaosn however the conditonal probability of redirected path is computed after its counts is subtracted which is wrong and often results in probability greater than 100%. I also fixed error mesage. Compilling tramp3d I now get following passes producing mismpatches: Pass dump id and name |static mismatcdynamic mismatch |in count |in count 113t fre | 2 +2| 0 114t mergephi | 2 | 0 115t threadfull | 2 | 0 116t vrp | 2 | 0 127t ch | 307 +305| 347194302 +347194302 130t thread | 313 +6| 347221478 +27176 131t dom | 321 +8| 346841121 -380357 134t reassoc | 323 +2| 346841121 136t forwprop | 327 +4| 347026371 +185250 144t pre | 326 -1| 347040926 +14555 172t ifcvt | 338 +2| 347218249 +156280 173t vect | 409 +71| 356357418 +9139169 176t cunroll | 377 -32| 126071925 -230285493 183t loopdone | 376 -1| 126015489 -56436 194t tracer | 379 +3| 127258199 +1242710 197t dom | 375 -4| 128352165 +1093966 199t threadfull | 379 +4| 128526112 +173947 200t vrp | 381 +2| 128724673 +198561 204t dce | 374 -7| 128632495 -92178 206t sink | 370 -4| 128618043 -14452 211t cddce | 372 +2| 128632495 +14452 248t ehcleanup | 370 -2| 128618755 -13740 255t optimized | 362 -8| 128576810 -41945 256r expand | 356 -6| 128899768 +322958 258r into_cfglayout | 353 -3| 129051765 +151997 259r jump | 354 +1| 129051765 262r cse1 | 353 -1| 129051765 275r loop2_unroll | 355 +2| 132182110 +3130345 277r loop2_done | 354 -1| 132182109 -1 312r pro_and_epilogue | 371 +17| 132222324 +40215 323r bbro | 375 +4| 132095926 -126398 Without the patch at jump2 time we get over 432 mismatches, so 15% improvement. Some of the mismathces are unavoidable. I think ch mismatches are mostly due to loop header copying where the header condition constant propagates. Most common case should be threadable in early optimizations and we also could do better on profile updating here. Bootstrapped/regtested x6_64-linux, comitted. gcc/ChangeLog: PR tree-optimization/103680 * cfg.cc (update_bb_profile_for_threading): Fix profile update; make message clearer. gcc/testsuite/ChangeLog: PR tree-optimization/103680 * gcc.dg/tree-ssa/pr103680.c: New test. * gcc.dg/tree-prof/cmpsf-1.c: Un-xfail. --- diff --git a/gcc/cfg.cc b/gcc/cfg.cc index 897ef534ff58..57b401109601 100644 --- a/gcc/cfg.cc +++ b/gcc/cfg.cc @@ -922,7 +922,6 @@ update_bb_profile_for_threading (basic_block bb, fprintf (dump_file, "bb %i count became negative after threading", bb->index); } - bb->count -= count; /* Compute the probability of TAKEN_EDGE being reached via threaded edge. Watch for overflows. */ @@ -934,8 +933,8 @@ update_bb_profile_for_threading (basic_block bb, { if (dump_file) { - fprintf (dump_file, "Jump threading proved probability of edge " - "%i->%i too small (it is ", + fprintf (dump_file, "Jump threading proved that the probability of edge " + "%i->%i was originally estimated too small (it is ", taken_edge->src->index, taken_edge->dest->index); taken_edge->probability.dump (dump_file); fprintf (dump_file, " should be "); @@ -945,6 +944,8 @@ update_bb_profile_for_threading (basic_block bb, prob = taken_edge->probability.apply_scale (6, 8); } + bb->count -= count; + /* Now rescale the probabilities. */ taken_edge->probability -= prob; prob = prob.invert (); diff --git a/gcc/testsuite/gcc.dg/tree-prof/cmpsf-1.c b/gcc/testsuite/gcc.dg/tree-prof/cmpsf-1.c index 696f459e6052..537d15d4bfa4 100644 --- a/gcc/testsuite/gcc.dg/tree-prof/cmpsf-1.c +++ b/gcc/testsuite/gcc.dg/tree-prof/cmpsf-1.c @@ -181,4 +181,4 @@ main (void) exit (0); } -/* { dg-final-use-not-autofdo { scan-tree-dump-not "Invalid sum" "dom2" { xfail *-*-* } } } */ +/* { dg-final-use-not-autofdo { scan-tree-dump-not "Invalid sum" "dom2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr103680.c b/gcc/testsuite/gcc.dg/tree-ssa/pr103680.c new file mode 100644 index 000000000000..30599fc1ef2e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103680.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized-details-blocks -fno-early-inlining" } */ +void foo (); +static void +test (int i) +{ + if (__builtin_expect_with_probability (i > 5, 1, 0.6)) + foo (); +} +void +test2(int i) +{ + test (i); + if (__builtin_expect_with_probability (i > 4, 1, 0.7)) + foo (); +} +/* { dg-final { scan-tree-dump-not "Invalid sum" "optimized" } } */