00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "config.h"
00036 #include "system.h"
00037 #include "coretypes.h"
00038 #include "tm.h"
00039 #include "tree.h"
00040 #include "rtl.h"
00041 #include "tm_p.h"
00042 #include "hard-reg-set.h"
00043 #include "basic-block.h"
00044 #include "insn-config.h"
00045 #include "regs.h"
00046 #include "flags.h"
00047 #include "output.h"
00048 #include "function.h"
00049 #include "except.h"
00050 #include "toplev.h"
00051 #include "recog.h"
00052 #include "expr.h"
00053 #include "predict.h"
00054 #include "coverage.h"
00055 #include "sreal.h"
00056 #include "params.h"
00057 #include "target.h"
00058 #include "cfgloop.h"
00059 #include "tree-flow.h"
00060 #include "ggc.h"
00061 #include "tree-dump.h"
00062 #include "tree-pass.h"
00063 #include "timevar.h"
00064 #include "tree-scalar-evolution.h"
00065 #include "cfgloop.h"
00066
00067
00068
00069 static sreal real_zero, real_one, real_almost_one, real_br_prob_base,
00070 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
00071
00072
00073 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 100 - 1)
00074 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
00075 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
00076 #define PROB_ALWAYS (REG_BR_PROB_BASE)
00077
00078 static void combine_predictions_for_insn (rtx, basic_block);
00079 static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int);
00080 static void estimate_loops_at_level (struct loop *, bitmap);
00081 static void propagate_freq (struct loop *, bitmap);
00082 static void estimate_bb_frequencies (struct loops *);
00083 static void predict_paths_leading_to (basic_block, int *, enum br_predictor, enum prediction);
00084 static bool last_basic_block_p (basic_block);
00085 static void compute_function_frequency (void);
00086 static void choose_function_section (void);
00087 static bool can_predict_insn_p (rtx);
00088
00089
00090
00091
00092 struct predictor_info
00093 {
00094 const char *const name;
00095 const int hitrate;
00096
00097 const int flags;
00098 };
00099
00100
00101
00102 #define PRED_FLAG_FIRST_MATCH 1
00103
00104
00105
00106 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
00107
00108 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
00109 static const struct predictor_info predictor_info[]= {
00110 #include "predict.def"
00111
00112
00113 {NULL, 0, 0}
00114 };
00115 #undef DEF_PREDICTOR
00116
00117
00118
00119
00120 bool
00121 maybe_hot_bb_p (basic_block bb)
00122 {
00123 if (profile_info && flag_branch_probabilities
00124 && (bb->count
00125 < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
00126 return false;
00127 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
00128 return false;
00129 return true;
00130 }
00131
00132
00133
00134 bool
00135 probably_cold_bb_p (basic_block bb)
00136 {
00137 if (profile_info && flag_branch_probabilities
00138 && (bb->count
00139 < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
00140 return true;
00141 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
00142 return true;
00143 return false;
00144 }
00145
00146
00147 bool
00148 probably_never_executed_bb_p (basic_block bb)
00149 {
00150 if (profile_info && flag_branch_probabilities)
00151 return ((bb->count + profile_info->runs / 2) / profile_info->runs) == 0;
00152 return false;
00153 }
00154
00155
00156
00157
00158 bool
00159 rtl_predicted_by_p (basic_block bb, enum br_predictor predictor)
00160 {
00161 rtx note;
00162 if (!INSN_P (BB_END (bb)))
00163 return false;
00164 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
00165 if (REG_NOTE_KIND (note) == REG_BR_PRED
00166 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
00167 return true;
00168 return false;
00169 }
00170
00171
00172
00173
00174 bool
00175 tree_predicted_by_p (basic_block bb, enum br_predictor predictor)
00176 {
00177 struct edge_prediction *i;
00178 for (i = bb->predictions; i; i = i->ep_next)
00179 if (i->ep_predictor == predictor)
00180 return true;
00181 return false;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 static bool
00201 probability_reliable_p (int prob)
00202 {
00203 return (profile_status == PROFILE_READ
00204 || (profile_status == PROFILE_GUESSED
00205 && (prob <= HITRATE (1) || prob >= HITRATE (99))));
00206 }
00207
00208
00209 bool
00210 edge_probability_reliable_p (edge e)
00211 {
00212 return probability_reliable_p (e->probability);
00213 }
00214
00215
00216 bool
00217 br_prob_note_reliable_p (rtx note)
00218 {
00219 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
00220 return probability_reliable_p (INTVAL (XEXP (note, 0)));
00221 }
00222
00223 static void
00224 predict_insn (rtx insn, enum br_predictor predictor, int probability)
00225 {
00226 gcc_assert (any_condjump_p (insn));
00227 if (!flag_guess_branch_prob)
00228 return;
00229
00230 REG_NOTES (insn)
00231 = gen_rtx_EXPR_LIST (REG_BR_PRED,
00232 gen_rtx_CONCAT (VOIDmode,
00233 GEN_INT ((int) predictor),
00234 GEN_INT ((int) probability)),
00235 REG_NOTES (insn));
00236 }
00237
00238
00239
00240 void
00241 predict_insn_def (rtx insn, enum br_predictor predictor,
00242 enum prediction taken)
00243 {
00244 int probability = predictor_info[(int) predictor].hitrate;
00245
00246 if (taken != TAKEN)
00247 probability = REG_BR_PROB_BASE - probability;
00248
00249 predict_insn (insn, predictor, probability);
00250 }
00251
00252
00253
00254 void
00255 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
00256 {
00257 rtx last_insn;
00258 last_insn = BB_END (e->src);
00259
00260
00261
00262 if (!any_condjump_p (last_insn))
00263 return;
00264
00265
00266 if (e->flags & EDGE_FALLTHRU)
00267 probability = REG_BR_PROB_BASE - probability;
00268
00269 predict_insn (last_insn, predictor, probability);
00270 }
00271
00272
00273 void
00274 tree_predict_edge (edge e, enum br_predictor predictor, int probability)
00275 {
00276 gcc_assert (profile_status != PROFILE_GUESSED);
00277 if ((e->src != ENTRY_BLOCK_PTR && EDGE_COUNT (e->src->succs) > 1)
00278 && flag_guess_branch_prob && optimize)
00279 {
00280 struct edge_prediction *i = ggc_alloc (sizeof (struct edge_prediction));
00281
00282 i->ep_next = e->src->predictions;
00283 e->src->predictions = i;
00284 i->ep_probability = probability;
00285 i->ep_predictor = predictor;
00286 i->ep_edge = e;
00287 }
00288 }
00289
00290
00291
00292 void
00293 remove_predictions_associated_with_edge (edge e)
00294 {
00295 if (e->src->predictions)
00296 {
00297 struct edge_prediction **prediction = &e->src->predictions;
00298 while (*prediction)
00299 {
00300 if ((*prediction)->ep_edge == e)
00301 *prediction = (*prediction)->ep_next;
00302 else
00303 prediction = &((*prediction)->ep_next);
00304 }
00305 }
00306 }
00307
00308
00309
00310
00311 static bool
00312 can_predict_insn_p (rtx insn)
00313 {
00314 return (JUMP_P (insn)
00315 && any_condjump_p (insn)
00316 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
00317 }
00318
00319
00320
00321 void
00322 predict_edge_def (edge e, enum br_predictor predictor,
00323 enum prediction taken)
00324 {
00325 int probability = predictor_info[(int) predictor].hitrate;
00326
00327 if (taken != TAKEN)
00328 probability = REG_BR_PROB_BASE - probability;
00329
00330 predict_edge (e, predictor, probability);
00331 }
00332
00333
00334
00335
00336 void
00337 invert_br_probabilities (rtx insn)
00338 {
00339 rtx note;
00340
00341 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
00342 if (REG_NOTE_KIND (note) == REG_BR_PROB)
00343 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
00344 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
00345 XEXP (XEXP (note, 0), 1)
00346 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
00347 }
00348
00349
00350
00351 static void
00352 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
00353 basic_block bb, int used)
00354 {
00355 edge e;
00356 edge_iterator ei;
00357
00358 if (!file)
00359 return;
00360
00361 FOR_EACH_EDGE (e, ei, bb->succs)
00362 if (! (e->flags & EDGE_FALLTHRU))
00363 break;
00364
00365 fprintf (file, " %s heuristics%s: %.1f%%",
00366 predictor_info[predictor].name,
00367 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
00368
00369 if (bb->count)
00370 {
00371 fprintf (file, " exec ");
00372 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
00373 if (e)
00374 {
00375 fprintf (file, " hit ");
00376 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
00377 fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count);
00378 }
00379 }
00380
00381 fprintf (file, "\n");
00382 }
00383
00384
00385
00386 static void
00387 set_even_probabilities (basic_block bb)
00388 {
00389 int nedges = 0;
00390 edge e;
00391 edge_iterator ei;
00392
00393 FOR_EACH_EDGE (e, ei, bb->succs)
00394 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
00395 nedges ++;
00396 FOR_EACH_EDGE (e, ei, bb->succs)
00397 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
00398 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
00399 else
00400 e->probability = 0;
00401 }
00402
00403
00404
00405
00406 static void
00407 combine_predictions_for_insn (rtx insn, basic_block bb)
00408 {
00409 rtx prob_note;
00410 rtx *pnote;
00411 rtx note;
00412 int best_probability = PROB_EVEN;
00413 int best_predictor = END_PREDICTORS;
00414 int combined_probability = REG_BR_PROB_BASE / 2;
00415 int d;
00416 bool first_match = false;
00417 bool found = false;
00418
00419 if (!can_predict_insn_p (insn))
00420 {
00421 set_even_probabilities (bb);
00422 return;
00423 }
00424
00425 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
00426 pnote = ®_NOTES (insn);
00427 if (dump_file)
00428 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
00429 bb->index);
00430
00431
00432
00433 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
00434 if (REG_NOTE_KIND (note) == REG_BR_PRED)
00435 {
00436 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
00437 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
00438
00439 found = true;
00440 if (best_predictor > predictor)
00441 best_probability = probability, best_predictor = predictor;
00442
00443 d = (combined_probability * probability
00444 + (REG_BR_PROB_BASE - combined_probability)
00445 * (REG_BR_PROB_BASE - probability));
00446
00447
00448 if (d == 0)
00449
00450 combined_probability = REG_BR_PROB_BASE / 2;
00451 else
00452 combined_probability = (((double) combined_probability) * probability
00453 * REG_BR_PROB_BASE / d + 0.5);
00454 }
00455
00456
00457
00458
00459
00460 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
00461 first_match = true;
00462
00463 if (!found)
00464 dump_prediction (dump_file, PRED_NO_PREDICTION,
00465 combined_probability, bb, true);
00466 else
00467 {
00468 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
00469 bb, !first_match);
00470 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
00471 bb, first_match);
00472 }
00473
00474 if (first_match)
00475 combined_probability = best_probability;
00476 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
00477
00478 while (*pnote)
00479 {
00480 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
00481 {
00482 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
00483 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
00484
00485 dump_prediction (dump_file, predictor, probability, bb,
00486 !first_match || best_predictor == predictor);
00487 *pnote = XEXP (*pnote, 1);
00488 }
00489 else
00490 pnote = &XEXP (*pnote, 1);
00491 }
00492
00493 if (!prob_note)
00494 {
00495 REG_NOTES (insn)
00496 = gen_rtx_EXPR_LIST (REG_BR_PROB,
00497 GEN_INT (combined_probability), REG_NOTES (insn));
00498
00499
00500
00501 if (!single_succ_p (bb))
00502 {
00503 BRANCH_EDGE (bb)->probability = combined_probability;
00504 FALLTHRU_EDGE (bb)->probability
00505 = REG_BR_PROB_BASE - combined_probability;
00506 }
00507 }
00508 else if (!single_succ_p (bb))
00509 {
00510 int prob = INTVAL (XEXP (prob_note, 0));
00511
00512 BRANCH_EDGE (bb)->probability = prob;
00513 FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob;
00514 }
00515 else
00516 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
00517 }
00518
00519
00520
00521
00522 static void
00523 combine_predictions_for_bb (basic_block bb)
00524 {
00525 int best_probability = PROB_EVEN;
00526 int best_predictor = END_PREDICTORS;
00527 int combined_probability = REG_BR_PROB_BASE / 2;
00528 int d;
00529 bool first_match = false;
00530 bool found = false;
00531 struct edge_prediction *pred;
00532 int nedges = 0;
00533 edge e, first = NULL, second = NULL;
00534 edge_iterator ei;
00535
00536 FOR_EACH_EDGE (e, ei, bb->succs)
00537 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
00538 {
00539 nedges ++;
00540 if (first && !second)
00541 second = e;
00542 if (!first)
00543 first = e;
00544 }
00545
00546
00547
00548
00549
00550
00551
00552 if (nedges != 2)
00553 {
00554 if (!bb->count)
00555 set_even_probabilities (bb);
00556 bb->predictions = NULL;
00557 if (dump_file)
00558 fprintf (dump_file, "%i edges in bb %i predicted to even probabilities\n",
00559 nedges, bb->index);
00560 return;
00561 }
00562
00563 if (dump_file)
00564 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
00565
00566
00567
00568 for (pred = bb->predictions; pred; pred = pred->ep_next)
00569 {
00570 int predictor = pred->ep_predictor;
00571 int probability = pred->ep_probability;
00572
00573 if (pred->ep_edge != first)
00574 probability = REG_BR_PROB_BASE - probability;
00575
00576 found = true;
00577 if (best_predictor > predictor)
00578 best_probability = probability, best_predictor = predictor;
00579
00580 d = (combined_probability * probability
00581 + (REG_BR_PROB_BASE - combined_probability)
00582 * (REG_BR_PROB_BASE - probability));
00583
00584
00585 if (d == 0)
00586
00587 combined_probability = REG_BR_PROB_BASE / 2;
00588 else
00589 combined_probability = (((double) combined_probability) * probability
00590 * REG_BR_PROB_BASE / d + 0.5);
00591 }
00592
00593
00594
00595
00596
00597 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
00598 first_match = true;
00599
00600 if (!found)
00601 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb, true);
00602 else
00603 {
00604 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
00605 !first_match);
00606 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
00607 first_match);
00608 }
00609
00610 if (first_match)
00611 combined_probability = best_probability;
00612 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
00613
00614 for (pred = bb->predictions; pred; pred = pred->ep_next)
00615 {
00616 int predictor = pred->ep_predictor;
00617 int probability = pred->ep_probability;
00618
00619 if (pred->ep_edge != EDGE_SUCC (bb, 0))
00620 probability = REG_BR_PROB_BASE - probability;
00621 dump_prediction (dump_file, predictor, probability, bb,
00622 !first_match || best_predictor == predictor);
00623 }
00624 bb->predictions = NULL;
00625
00626 if (!bb->count)
00627 {
00628 first->probability = combined_probability;
00629 second->probability = REG_BR_PROB_BASE - combined_probability;
00630 }
00631 }
00632
00633
00634
00635
00636 static void
00637 predict_loops (struct loops *loops_info, bool rtlsimpleloops)
00638 {
00639 unsigned i;
00640
00641 if (!rtlsimpleloops)
00642 scev_initialize (loops_info);
00643
00644
00645
00646 for (i = 1; i < loops_info->num; i++)
00647 {
00648 basic_block bb, *bbs;
00649 unsigned j;
00650 unsigned n_exits;
00651 struct loop *loop = loops_info->parray[i];
00652 struct niter_desc desc;
00653 unsigned HOST_WIDE_INT niter;
00654 edge *exits;
00655
00656 exits = get_loop_exit_edges (loop, &n_exits);
00657
00658 if (rtlsimpleloops)
00659 {
00660 iv_analysis_loop_init (loop);
00661 find_simple_exit (loop, &desc);
00662
00663 if (desc.simple_p && desc.const_iter)
00664 {
00665 int prob;
00666 niter = desc.niter + 1;
00667 if (niter == 0)
00668 niter = desc.niter;
00669 if (niter
00670 > (unsigned int) PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS))
00671 niter = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
00672
00673 prob = (REG_BR_PROB_BASE
00674 - (REG_BR_PROB_BASE + niter /2) / niter);
00675
00676
00677 if (prob == REG_BR_PROB_BASE)
00678 prob = REG_BR_PROB_BASE - 1;
00679 predict_edge (desc.in_edge, PRED_LOOP_ITERATIONS,
00680 prob);
00681 }
00682 }
00683 else
00684 {
00685 struct tree_niter_desc niter_desc;
00686
00687 for (j = 0; j < n_exits; j++)
00688 {
00689 tree niter = NULL;
00690
00691 if (number_of_iterations_exit (loop, exits[j], &niter_desc, false))
00692 niter = niter_desc.niter;
00693 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
00694 niter = loop_niter_by_eval (loop, exits[j]);
00695
00696 if (TREE_CODE (niter) == INTEGER_CST)
00697 {
00698 int probability;
00699 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
00700 if (host_integerp (niter, 1)
00701 && tree_int_cst_lt (niter,
00702 build_int_cstu (NULL_TREE, max - 1)))
00703 {
00704 HOST_WIDE_INT nitercst = tree_low_cst (niter, 1) + 1;
00705 probability = ((REG_BR_PROB_BASE + nitercst / 2)
00706 / nitercst);
00707 }
00708 else
00709 probability = ((REG_BR_PROB_BASE + max / 2) / max);
00710
00711 predict_edge (exits[j], PRED_LOOP_ITERATIONS, probability);
00712 }
00713 }
00714
00715 }
00716 free (exits);
00717
00718 bbs = get_loop_body (loop);
00719
00720 for (j = 0; j < loop->num_nodes; j++)
00721 {
00722 int header_found = 0;
00723 edge e;
00724 edge_iterator ei;
00725
00726 bb = bbs[j];
00727
00728
00729
00730
00731
00732 if ((rtlsimpleloops && !can_predict_insn_p (BB_END (bb)))
00733 || predicted_by_p (bb, PRED_CONTINUE))
00734 continue;
00735
00736
00737
00738 if (bb == loop->latch)
00739 {
00740 e = find_edge (loop->latch, loop->header);
00741 if (e)
00742 {
00743 header_found = 1;
00744 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
00745 }
00746 }
00747
00748
00749
00750 if (!header_found)
00751 {
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766 int probability = ((REG_BR_PROB_BASE
00767 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
00768 / n_exits);
00769 if (probability < HITRATE (2))
00770 probability = HITRATE (2);
00771 FOR_EACH_EDGE (e, ei, bb->succs)
00772 if (e->dest->index < NUM_FIXED_BLOCKS
00773 || !flow_bb_inside_loop_p (loop, e->dest))
00774 predict_edge (e, PRED_LOOP_EXIT, probability);
00775 }
00776 }
00777
00778
00779 free (bbs);
00780 }
00781
00782 if (!rtlsimpleloops)
00783 {
00784 scev_finalize ();
00785 current_loops = NULL;
00786 }
00787 }
00788
00789
00790
00791 static void
00792 bb_estimate_probability_locally (basic_block bb)
00793 {
00794 rtx last_insn = BB_END (bb);
00795 rtx cond;
00796
00797 if (! can_predict_insn_p (last_insn))
00798 return;
00799 cond = get_condition (last_insn, NULL, false, false);
00800 if (! cond)
00801 return;
00802
00803
00804
00805
00806 if (COMPARISON_P (cond)
00807 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
00808 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
00809 {
00810 if (GET_CODE (cond) == EQ)
00811 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
00812 else if (GET_CODE (cond) == NE)
00813 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
00814 }
00815 else
00816
00817
00818
00819
00820
00821 switch (GET_CODE (cond))
00822 {
00823 case CONST_INT:
00824
00825 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
00826 cond == const0_rtx ? NOT_TAKEN : TAKEN);
00827 break;
00828
00829 case EQ:
00830 case UNEQ:
00831
00832
00833
00834 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
00835 ;
00836
00837
00838 else if (XEXP (cond, 1) == const0_rtx
00839 || XEXP (cond, 0) == const0_rtx)
00840 ;
00841 else
00842 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
00843 break;
00844
00845 case NE:
00846 case LTGT:
00847
00848
00849
00850 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
00851 ;
00852
00853
00854 else if (XEXP (cond, 1) == const0_rtx
00855 || XEXP (cond, 0) == const0_rtx)
00856 ;
00857 else
00858 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
00859 break;
00860
00861 case ORDERED:
00862 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
00863 break;
00864
00865 case UNORDERED:
00866 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
00867 break;
00868
00869 case LE:
00870 case LT:
00871 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
00872 || XEXP (cond, 1) == constm1_rtx)
00873 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
00874 break;
00875
00876 case GE:
00877 case GT:
00878 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
00879 || XEXP (cond, 1) == constm1_rtx)
00880 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
00881 break;
00882
00883 default:
00884 break;
00885 }
00886 }
00887
00888
00889 void
00890 guess_outgoing_edge_probabilities (basic_block bb)
00891 {
00892 bb_estimate_probability_locally (bb);
00893 combine_predictions_for_insn (BB_END (bb), bb);
00894 }
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904 static tree
00905 expr_expected_value (tree expr, bitmap visited)
00906 {
00907 if (TREE_CONSTANT (expr))
00908 return expr;
00909 else if (TREE_CODE (expr) == SSA_NAME)
00910 {
00911 tree def = SSA_NAME_DEF_STMT (expr);
00912
00913
00914 if (bitmap_bit_p (visited, SSA_NAME_VERSION (expr)))
00915 return NULL;
00916 bitmap_set_bit (visited, SSA_NAME_VERSION (expr));
00917
00918 if (TREE_CODE (def) == PHI_NODE)
00919 {
00920
00921
00922 int i;
00923 tree val = NULL, new_val;
00924
00925 for (i = 0; i < PHI_NUM_ARGS (def); i++)
00926 {
00927 tree arg = PHI_ARG_DEF (def, i);
00928
00929
00930
00931
00932
00933
00934
00935 if (arg == PHI_RESULT (def))
00936 continue;
00937
00938 new_val = expr_expected_value (arg, visited);
00939 if (!new_val)
00940 return NULL;
00941 if (!val)
00942 val = new_val;
00943 else if (!operand_equal_p (val, new_val, false))
00944 return NULL;
00945 }
00946 return val;
00947 }
00948 if (TREE_CODE (def) != MODIFY_EXPR || TREE_OPERAND (def, 0) != expr)
00949 return NULL;
00950 return expr_expected_value (TREE_OPERAND (def, 1), visited);
00951 }
00952 else if (TREE_CODE (expr) == CALL_EXPR)
00953 {
00954 tree decl = get_callee_fndecl (expr);
00955 if (!decl)
00956 return NULL;
00957 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
00958 && DECL_FUNCTION_CODE (decl) == BUILT_IN_EXPECT)
00959 {
00960 tree arglist = TREE_OPERAND (expr, 1);
00961 tree val;
00962
00963 if (arglist == NULL_TREE
00964 || TREE_CHAIN (arglist) == NULL_TREE)
00965 return NULL;
00966 val = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (expr, 1)));
00967 if (TREE_CONSTANT (val))
00968 return val;
00969 return TREE_VALUE (TREE_CHAIN (TREE_OPERAND (expr, 1)));
00970 }
00971 }
00972 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
00973 {
00974 tree op0, op1, res;
00975 op0 = expr_expected_value (TREE_OPERAND (expr, 0), visited);
00976 if (!op0)
00977 return NULL;
00978 op1 = expr_expected_value (TREE_OPERAND (expr, 1), visited);
00979 if (!op1)
00980 return NULL;
00981 res = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr), op0, op1);
00982 if (TREE_CONSTANT (res))
00983 return res;
00984 return NULL;
00985 }
00986 if (UNARY_CLASS_P (expr))
00987 {
00988 tree op0, res;
00989 op0 = expr_expected_value (TREE_OPERAND (expr, 0), visited);
00990 if (!op0)
00991 return NULL;
00992 res = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr), op0);
00993 if (TREE_CONSTANT (res))
00994 return res;
00995 return NULL;
00996 }
00997 return NULL;
00998 }
00999
01000
01001 static void
01002 strip_builtin_expect (void)
01003 {
01004 basic_block bb;
01005 FOR_EACH_BB (bb)
01006 {
01007 block_stmt_iterator bi;
01008 for (bi = bsi_start (bb); !bsi_end_p (bi); bsi_next (&bi))
01009 {
01010 tree stmt = bsi_stmt (bi);
01011 tree fndecl;
01012 tree arglist;
01013
01014 if (TREE_CODE (stmt) == MODIFY_EXPR
01015 && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR
01016 && (fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 1)))
01017 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
01018 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
01019 && (arglist = TREE_OPERAND (TREE_OPERAND (stmt, 1), 1))
01020 && TREE_CHAIN (arglist))
01021 {
01022 TREE_OPERAND (stmt, 1) = TREE_VALUE (arglist);
01023 update_stmt (stmt);
01024 }
01025 }
01026 }
01027 }
01028
01029
01030 static void
01031 tree_predict_by_opcode (basic_block bb)
01032 {
01033 tree stmt = last_stmt (bb);
01034 edge then_edge;
01035 tree cond;
01036 tree op0;
01037 tree type;
01038 tree val;
01039 bitmap visited;
01040 edge_iterator ei;
01041
01042 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
01043 return;
01044 FOR_EACH_EDGE (then_edge, ei, bb->succs)
01045 if (then_edge->flags & EDGE_TRUE_VALUE)
01046 break;
01047 cond = TREE_OPERAND (stmt, 0);
01048 if (!COMPARISON_CLASS_P (cond))
01049 return;
01050 op0 = TREE_OPERAND (cond, 0);
01051 type = TREE_TYPE (op0);
01052 visited = BITMAP_ALLOC (NULL);
01053 val = expr_expected_value (cond, visited);
01054 BITMAP_FREE (visited);
01055 if (val)
01056 {
01057 if (integer_zerop (val))
01058 predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, NOT_TAKEN);
01059 else
01060 predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, TAKEN);
01061 return;
01062 }
01063
01064
01065
01066 if (POINTER_TYPE_P (type))
01067 {
01068 if (TREE_CODE (cond) == EQ_EXPR)
01069 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
01070 else if (TREE_CODE (cond) == NE_EXPR)
01071 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
01072 }
01073 else
01074
01075
01076
01077
01078
01079 switch (TREE_CODE (cond))
01080 {
01081 case EQ_EXPR:
01082 case UNEQ_EXPR:
01083
01084
01085
01086 if (FLOAT_TYPE_P (type))
01087 ;
01088
01089
01090 else if (integer_zerop (op0)
01091 || integer_zerop (TREE_OPERAND (cond, 1)))
01092 ;
01093 else
01094 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
01095 break;
01096
01097 case NE_EXPR:
01098 case LTGT_EXPR:
01099
01100
01101
01102 if (FLOAT_TYPE_P (type))
01103 ;
01104
01105
01106 else if (integer_zerop (op0)
01107 || integer_zerop (TREE_OPERAND (cond, 1)))
01108 ;
01109 else
01110 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
01111 break;
01112
01113 case ORDERED_EXPR:
01114 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
01115 break;
01116
01117 case UNORDERED_EXPR:
01118 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
01119 break;
01120
01121 case LE_EXPR:
01122 case LT_EXPR:
01123 if (integer_zerop (TREE_OPERAND (cond, 1))
01124 || integer_onep (TREE_OPERAND (cond, 1))
01125 || integer_all_onesp (TREE_OPERAND (cond, 1))
01126 || real_zerop (TREE_OPERAND (cond, 1))
01127 || real_onep (TREE_OPERAND (cond, 1))
01128 || real_minus_onep (TREE_OPERAND (cond, 1)))
01129 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
01130 break;
01131
01132 case GE_EXPR:
01133 case GT_EXPR:
01134 if (integer_zerop (TREE_OPERAND (cond, 1))
01135 || integer_onep (TREE_OPERAND (cond, 1))
01136 || integer_all_onesp (TREE_OPERAND (cond, 1))
01137 || real_zerop (TREE_OPERAND (cond, 1))
01138 || real_onep (TREE_OPERAND (cond, 1))
01139 || real_minus_onep (TREE_OPERAND (cond, 1)))
01140 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
01141 break;
01142
01143 default:
01144 break;
01145 }
01146 }
01147
01148
01149 static enum br_predictor
01150 return_prediction (tree val, enum prediction *prediction)
01151 {
01152
01153 if (!val)
01154 return PRED_NO_PREDICTION;
01155
01156 if (POINTER_TYPE_P (TREE_TYPE (val)))
01157 {
01158
01159 if (integer_zerop (val))
01160 {
01161 *prediction = NOT_TAKEN;
01162 return PRED_NULL_RETURN;
01163 }
01164 }
01165 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
01166 {
01167
01168
01169 if (TREE_CODE (val) == INTEGER_CST
01170 && tree_int_cst_sgn (val) < 0)
01171 {
01172 *prediction = NOT_TAKEN;
01173 return PRED_NEGATIVE_RETURN;
01174 }
01175
01176
01177
01178 if (TREE_CONSTANT (val)
01179 && (!integer_zerop (val) && !integer_onep (val)))
01180 {
01181 *prediction = TAKEN;
01182 return PRED_NEGATIVE_RETURN;
01183 }
01184 }
01185 return PRED_NO_PREDICTION;
01186 }
01187
01188
01189
01190 static void
01191 apply_return_prediction (int *heads)
01192 {
01193 tree return_stmt = NULL;
01194 tree return_val;
01195 edge e;
01196 tree phi;
01197 int phi_num_args, i;
01198 enum br_predictor pred;
01199 enum prediction direction;
01200 edge_iterator ei;
01201
01202 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
01203 {
01204 return_stmt = last_stmt (e->src);
01205 if (TREE_CODE (return_stmt) == RETURN_EXPR)
01206 break;
01207 }
01208 if (!e)
01209 return;
01210 return_val = TREE_OPERAND (return_stmt, 0);
01211 if (!return_val)
01212 return;
01213 if (TREE_CODE (return_val) == MODIFY_EXPR)
01214 return_val = TREE_OPERAND (return_val, 1);
01215 if (TREE_CODE (return_val) != SSA_NAME
01216 || !SSA_NAME_DEF_STMT (return_val)
01217 || TREE_CODE (SSA_NAME_DEF_STMT (return_val)) != PHI_NODE)
01218 return;
01219 for (phi = SSA_NAME_DEF_STMT (return_val); phi; phi = PHI_CHAIN (phi))
01220 if (PHI_RESULT (phi) == return_val)
01221 break;
01222 if (!phi)
01223 return;
01224 phi_num_args = PHI_NUM_ARGS (phi);
01225 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
01226
01227
01228
01229
01230 for (i = 1; i < phi_num_args; i++)
01231 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
01232 break;
01233 if (i != phi_num_args)
01234 for (i = 0; i < phi_num_args; i++)
01235 {
01236 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
01237 if (pred != PRED_NO_PREDICTION)
01238 predict_paths_leading_to (PHI_ARG_EDGE (phi, i)->src, heads, pred,
01239 direction);
01240 }
01241 }
01242
01243
01244
01245
01246
01247 static void
01248 tree_bb_level_predictions (void)
01249 {
01250 basic_block bb;
01251 int *heads;
01252
01253 heads = XNEWVEC (int, last_basic_block);
01254 memset (heads, ENTRY_BLOCK, sizeof (int) * last_basic_block);
01255 heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block;
01256
01257 apply_return_prediction (heads);
01258
01259 FOR_EACH_BB (bb)
01260 {
01261 block_stmt_iterator bsi = bsi_last (bb);
01262
01263 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
01264 {
01265 tree stmt = bsi_stmt (bsi);
01266 switch (TREE_CODE (stmt))
01267 {
01268 case MODIFY_EXPR:
01269 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
01270 {
01271 stmt = TREE_OPERAND (stmt, 1);
01272 goto call_expr;
01273 }
01274 break;
01275 case CALL_EXPR:
01276 call_expr:;
01277 if (call_expr_flags (stmt) & ECF_NORETURN)
01278 predict_paths_leading_to (bb, heads, PRED_NORETURN,
01279 NOT_TAKEN);
01280 break;
01281 default:
01282 break;
01283 }
01284 }
01285 }
01286
01287 free (heads);
01288 }
01289
01290
01291 static unsigned int
01292 tree_estimate_probability (void)
01293 {
01294 basic_block bb;
01295 struct loops loops_info;
01296
01297 flow_loops_find (&loops_info);
01298 if (dump_file && (dump_flags & TDF_DETAILS))
01299 flow_loops_dump (&loops_info, dump_file, NULL, 0);
01300
01301 add_noreturn_fake_exit_edges ();
01302 connect_infinite_loops_to_exit ();
01303 calculate_dominance_info (CDI_DOMINATORS);
01304 calculate_dominance_info (CDI_POST_DOMINATORS);
01305
01306 tree_bb_level_predictions ();
01307
01308 mark_irreducible_loops (&loops_info);
01309 predict_loops (&loops_info, false);
01310
01311 FOR_EACH_BB (bb)
01312 {
01313 edge e;
01314 edge_iterator ei;
01315
01316 FOR_EACH_EDGE (e, ei, bb->succs)
01317 {
01318
01319
01320
01321 if (e->dest == EXIT_BLOCK_PTR
01322 && TREE_CODE (last_stmt (bb)) == RETURN_EXPR
01323 && !single_pred_p (bb))
01324 {
01325 edge e1;
01326 edge_iterator ei1;
01327
01328 FOR_EACH_EDGE (e1, ei1, bb->preds)
01329 if (!predicted_by_p (e1->src, PRED_NULL_RETURN)
01330 && !predicted_by_p (e1->src, PRED_CONST_RETURN)
01331 && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN)
01332 && !last_basic_block_p (e1->src))
01333 predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
01334 }
01335
01336
01337
01338 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
01339 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
01340 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
01341 {
01342 block_stmt_iterator bi;
01343
01344
01345
01346
01347
01348 for (bi = bsi_start (e->dest); !bsi_end_p (bi);
01349 bsi_next (&bi))
01350 {
01351 tree stmt = bsi_stmt (bi);
01352 if ((TREE_CODE (stmt) == CALL_EXPR
01353 || (TREE_CODE (stmt) == MODIFY_EXPR
01354 && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR))
01355
01356
01357 && TREE_SIDE_EFFECTS (stmt))
01358 {
01359 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
01360 break;
01361 }
01362 }
01363 }
01364 }
01365 tree_predict_by_opcode (bb);
01366 }
01367 FOR_EACH_BB (bb)
01368 combine_predictions_for_bb (bb);
01369
01370 strip_builtin_expect ();
01371 estimate_bb_frequencies (&loops_info);
01372 free_dominance_info (CDI_POST_DOMINATORS);
01373 remove_fake_exit_edges ();
01374 flow_loops_free (&loops_info);
01375 if (dump_file && (dump_flags & TDF_DETAILS))
01376 dump_tree_cfg (dump_file, dump_flags);
01377 if (profile_status == PROFILE_ABSENT)
01378 profile_status = PROFILE_GUESSED;
01379 return 0;
01380 }
01381
01382
01383
01384
01385
01386 void
01387 expected_value_to_br_prob (void)
01388 {
01389 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
01390
01391 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
01392 {
01393 switch (GET_CODE (insn))
01394 {
01395 case NOTE:
01396
01397 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
01398 {
01399 ev = NOTE_EXPECTED_VALUE (insn);
01400 ev_reg = XEXP (ev, 0);
01401 delete_insn (insn);
01402 }
01403 continue;
01404
01405 case CODE_LABEL:
01406
01407 ev = NULL_RTX;
01408 continue;
01409
01410 case JUMP_INSN:
01411
01412
01413 if (!JUMP_P (insn) || ev == NULL_RTX
01414 || ! any_condjump_p (insn))
01415 continue;
01416 break;
01417
01418 default:
01419
01420 if (ev && reg_set_p (ev_reg, insn))
01421 ev = NULL_RTX;
01422 continue;
01423 }
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434 cond = XEXP (SET_SRC (pc_set (insn)), 0);
01435 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg,
01436 false, false);
01437 if (! cond || XEXP (cond, 0) != ev_reg
01438 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
01439 continue;
01440
01441
01442
01443
01444 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
01445 XEXP (ev, 1), XEXP (cond, 1));
01446 cond = simplify_rtx (cond);
01447
01448
01449 gcc_assert (cond == const_true_rtx || cond == const0_rtx);
01450 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
01451 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
01452 }
01453 }
01454
01455
01456
01457 static bool
01458 last_basic_block_p (basic_block bb)
01459 {
01460 if (bb == EXIT_BLOCK_PTR)
01461 return false;
01462
01463 return (bb->next_bb == EXIT_BLOCK_PTR
01464 || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
01465 && single_succ_p (bb)
01466 && single_succ (bb)->next_bb == EXIT_BLOCK_PTR));
01467 }
01468
01469
01470
01471
01472
01473
01474
01475 static void
01476 predict_paths_leading_to (basic_block bb, int *heads, enum br_predictor pred,
01477 enum prediction taken)
01478 {
01479 edge e;
01480 edge_iterator ei;
01481 int y;
01482
01483 if (heads[bb->index] == ENTRY_BLOCK)
01484 {
01485
01486
01487
01488 basic_block ai = bb;
01489 basic_block next_ai = get_immediate_dominator (CDI_DOMINATORS, bb);
01490 int head;
01491
01492 while (heads[next_ai->index] == ENTRY_BLOCK)
01493 {
01494 if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb))
01495 break;
01496 heads[next_ai->index] = ai->index;
01497 ai = next_ai;
01498 next_ai = get_immediate_dominator (CDI_DOMINATORS, next_ai);
01499 }
01500 if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb))
01501 head = next_ai->index;
01502 else
01503 head = heads[next_ai->index];
01504 while (next_ai != bb)
01505 {
01506 next_ai = ai;
01507 ai = BASIC_BLOCK (heads[ai->index]);
01508 heads[next_ai->index] = head;
01509 }
01510 }
01511 y = heads[bb->index];
01512
01513
01514
01515 if (y == last_basic_block)
01516 return;
01517 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (y)->succs)
01518 if (e->dest->index >= NUM_FIXED_BLOCKS
01519 && dominated_by_p (CDI_POST_DOMINATORS, e->dest, bb))
01520 predict_edge_def (e, pred, taken);
01521 }
01522
01523
01524
01525
01526 typedef struct block_info_def
01527 {
01528
01529 sreal frequency;
01530
01531
01532 basic_block next;
01533
01534
01535 int npredecessors;
01536 } *block_info;
01537
01538
01539 typedef struct edge_info_def
01540 {
01541
01542
01543
01544 sreal back_edge_prob;
01545
01546 unsigned int back_edge:1;
01547 } *edge_info;
01548
01549 #define BLOCK_INFO(B) ((block_info) (B)->aux)
01550 #define EDGE_INFO(E) ((edge_info) (E)->aux)
01551
01552
01553
01554
01555 static void
01556 propagate_freq (struct loop *loop, bitmap tovisit)
01557 {
01558 basic_block head = loop->header;
01559 basic_block bb;
01560 basic_block last;
01561 unsigned i;
01562 edge e;
01563 basic_block nextbb;
01564 bitmap_iterator bi;
01565
01566
01567
01568 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
01569 {
01570 edge_iterator ei;
01571 int count = 0;
01572
01573
01574
01575
01576 bb = BASIC_BLOCK (i);
01577
01578 FOR_EACH_EDGE (e, ei, bb->preds)
01579 {
01580 bool visit = bitmap_bit_p (tovisit, e->src->index);
01581
01582 if (visit && !(e->flags & EDGE_DFS_BACK))
01583 count++;
01584 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
01585 fprintf (dump_file,
01586 "Irreducible region hit, ignoring edge to %i->%i\n",
01587 e->src->index, bb->index);
01588 }
01589 BLOCK_INFO (bb)->npredecessors = count;
01590 }
01591
01592 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
01593 last = head;
01594 for (bb = head; bb; bb = nextbb)
01595 {
01596 edge_iterator ei;
01597 sreal cyclic_probability, frequency;
01598
01599 memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
01600 memcpy (&frequency, &real_zero, sizeof (real_zero));
01601
01602 nextbb = BLOCK_INFO (bb)->next;
01603 BLOCK_INFO (bb)->next = NULL;
01604
01605
01606 if (bb != head)
01607 {
01608 #ifdef ENABLE_CHECKING
01609 FOR_EACH_EDGE (e, ei, bb->preds)
01610 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
01611 || (e->flags & EDGE_DFS_BACK));
01612 #endif
01613
01614 FOR_EACH_EDGE (e, ei, bb->preds)
01615 if (EDGE_INFO (e)->back_edge)
01616 {
01617 sreal_add (&cyclic_probability, &cyclic_probability,
01618 &EDGE_INFO (e)->back_edge_prob);
01619 }
01620 else if (!(e->flags & EDGE_DFS_BACK))
01621 {
01622 sreal tmp;
01623
01624
01625
01626
01627
01628 sreal_init (&tmp, e->probability, 0);
01629 sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency);
01630 sreal_mul (&tmp, &tmp, &real_inv_br_prob_base);
01631 sreal_add (&frequency, &frequency, &tmp);
01632 }
01633
01634 if (sreal_compare (&cyclic_probability, &real_zero) == 0)
01635 {
01636 memcpy (&BLOCK_INFO (bb)->frequency, &frequency,
01637 sizeof (frequency));
01638 }
01639 else
01640 {
01641 if (sreal_compare (&cyclic_probability, &real_almost_one) > 0)
01642 {
01643 memcpy (&cyclic_probability, &real_almost_one,
01644 sizeof (real_almost_one));
01645 }
01646
01647
01648
01649
01650 sreal_sub (&cyclic_probability, &real_one, &cyclic_probability);
01651 sreal_div (&BLOCK_INFO (bb)->frequency,
01652 &frequency, &cyclic_probability);
01653 }
01654 }
01655
01656 bitmap_clear_bit (tovisit, bb->index);
01657
01658 e = find_edge (bb, head);
01659 if (e)
01660 {
01661 sreal tmp;
01662
01663
01664
01665
01666
01667 sreal_init (&tmp, e->probability, 0);
01668 sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency);
01669 sreal_mul (&EDGE_INFO (e)->back_edge_prob,
01670 &tmp, &real_inv_br_prob_base);
01671 }
01672
01673
01674 FOR_EACH_EDGE (e, ei, bb->succs)
01675 if (!(e->flags & EDGE_DFS_BACK)
01676 && BLOCK_INFO (e->dest)->npredecessors)
01677 {
01678 BLOCK_INFO (e->dest)->npredecessors--;
01679 if (!BLOCK_INFO (e->dest)->npredecessors)
01680 {
01681 if (!nextbb)
01682 nextbb = e->dest;
01683 else
01684 BLOCK_INFO (last)->next = e->dest;
01685
01686 last = e->dest;
01687 }
01688 }
01689 }
01690 }
01691
01692
01693
01694 static void
01695 estimate_loops_at_level (struct loop *first_loop, bitmap tovisit)
01696 {
01697 struct loop *loop;
01698
01699 for (loop = first_loop; loop; loop = loop->next)
01700 {
01701 edge e;
01702 basic_block *bbs;
01703 unsigned i;
01704
01705 estimate_loops_at_level (loop->inner, tovisit);
01706
01707
01708 if (EDGE_COUNT (loop->latch->succs) > 0)
01709 {
01710
01711 e = loop_latch_edge (loop);
01712 EDGE_INFO (e)->back_edge = 1;
01713 }
01714
01715 bbs = get_loop_body (loop);
01716 for (i = 0; i < loop->num_nodes; i++)
01717 bitmap_set_bit (tovisit, bbs[i]->index);
01718 free (bbs);
01719 propagate_freq (loop, tovisit);
01720 }
01721 }
01722
01723
01724
01725
01726 int
01727 counts_to_freqs (void)
01728 {
01729 gcov_type count_max, true_count_max = 0;
01730 basic_block bb;
01731
01732 FOR_EACH_BB (bb)
01733 true_count_max = MAX (bb->count, true_count_max);
01734
01735 count_max = MAX (true_count_max, 1);
01736 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
01737 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
01738 return true_count_max;
01739 }
01740
01741
01742
01743
01744
01745
01746 bool
01747 expensive_function_p (int threshold)
01748 {
01749 unsigned int sum = 0;
01750 basic_block bb;
01751 unsigned int limit;
01752
01753
01754
01755 gcc_assert (threshold <= BB_FREQ_MAX);
01756
01757
01758
01759
01760 if (ENTRY_BLOCK_PTR->frequency == 0)
01761 return true;
01762
01763
01764 limit = ENTRY_BLOCK_PTR->frequency * threshold;
01765 FOR_EACH_BB (bb)
01766 {
01767 rtx insn;
01768
01769 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
01770 insn = NEXT_INSN (insn))
01771 if (active_insn_p (insn))
01772 {
01773 sum += bb->frequency;
01774 if (sum > limit)
01775 return true;
01776 }
01777 }
01778
01779 return false;
01780 }
01781
01782
01783
01784 static void
01785 estimate_bb_frequencies (struct loops *loops)
01786 {
01787 basic_block bb;
01788 sreal freq_max;
01789
01790 if (!flag_branch_probabilities || !counts_to_freqs ())
01791 {
01792 static int real_values_initialized = 0;
01793 bitmap tovisit;
01794
01795 if (!real_values_initialized)
01796 {
01797 real_values_initialized = 1;
01798 sreal_init (&real_zero, 0, 0);
01799 sreal_init (&real_one, 1, 0);
01800 sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0);
01801 sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0);
01802 sreal_init (&real_one_half, 1, -1);
01803 sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base);
01804 sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base);
01805 }
01806
01807 mark_dfs_back_edges ();
01808
01809 single_succ_edge (ENTRY_BLOCK_PTR)->probability = REG_BR_PROB_BASE;
01810
01811
01812 tovisit = BITMAP_ALLOC (NULL);
01813 alloc_aux_for_blocks (sizeof (struct block_info_def));
01814 alloc_aux_for_edges (sizeof (struct edge_info_def));
01815 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
01816 {
01817 edge e;
01818 edge_iterator ei;
01819
01820 FOR_EACH_EDGE (e, ei, bb->succs)
01821 {
01822 sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0);
01823 sreal_mul (&EDGE_INFO (e)->back_edge_prob,
01824 &EDGE_INFO (e)->back_edge_prob,
01825 &real_inv_br_prob_base);
01826 }
01827 }
01828
01829
01830
01831 estimate_loops_at_level (loops->tree_root, tovisit);
01832
01833 memcpy (&freq_max, &real_zero, sizeof (real_zero));
01834 FOR_EACH_BB (bb)
01835 if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0)
01836 memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max));
01837
01838 sreal_div (&freq_max, &real_bb_freq_max, &freq_max);
01839 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
01840 {
01841 sreal tmp;
01842
01843 sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max);
01844 sreal_add (&tmp, &tmp, &real_one_half);
01845 bb->frequency = sreal_to_int (&tmp);
01846 }
01847
01848 free_aux_for_blocks ();
01849 free_aux_for_edges ();
01850 BITMAP_FREE (tovisit);
01851 }
01852 compute_function_frequency ();
01853 if (flag_reorder_functions)
01854 choose_function_section ();
01855 }
01856
01857
01858 static void
01859 compute_function_frequency (void)
01860 {
01861 basic_block bb;
01862
01863 if (!profile_info || !flag_branch_probabilities)
01864 return;
01865 cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
01866 FOR_EACH_BB (bb)
01867 {
01868 if (maybe_hot_bb_p (bb))
01869 {
01870 cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
01871 return;
01872 }
01873 if (!probably_never_executed_bb_p (bb))
01874 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
01875 }
01876 }
01877
01878
01879 static void
01880 choose_function_section (void)
01881 {
01882 if (DECL_SECTION_NAME (current_function_decl)
01883 || !targetm.have_named_sections
01884
01885
01886
01887
01888 || DECL_ONE_ONLY (current_function_decl))
01889 return;
01890
01891
01892
01893
01894 if (flag_reorder_blocks_and_partition)
01895 return;
01896
01897 if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
01898 DECL_SECTION_NAME (current_function_decl) =
01899 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
01900 if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
01901 DECL_SECTION_NAME (current_function_decl) =
01902 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
01903 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
01904 }
01905
01906 static bool
01907 gate_estimate_probability (void)
01908 {
01909 #ifdef KEY
01910
01911 return !flag_spin_file && flag_guess_branch_prob;
01912 #else
01913 return flag_guess_branch_prob;
01914 #endif
01915 }
01916
01917 struct tree_opt_pass pass_profile =
01918 {
01919 "profile",
01920 gate_estimate_probability,
01921 tree_estimate_probability,
01922 NULL,
01923 NULL,
01924 0,
01925 TV_BRANCH_PROB,
01926 PROP_cfg,
01927 0,
01928 0,
01929 0,
01930 TODO_ggc_collect | TODO_verify_ssa,
01931 0
01932 };