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