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 #ifdef SGI_MONGOOSE
00038
00039 #include "rtl.h"
00040 #endif
00041 #include "tree.h"
00042 #ifndef SGI_MONGOOSE
00043 #include "rtl.h"
00044 #endif
00045 #include "tm_p.h"
00046 #include "hard-reg-set.h"
00047 #include "basic-block.h"
00048 #include "insn-config.h"
00049 #include "regs.h"
00050 #include "flags.h"
00051 #include "output.h"
00052 #include "function.h"
00053 #include "except.h"
00054 #include "toplev.h"
00055 #include "recog.h"
00056 #include "expr.h"
00057 #include "predict.h"
00058 #include "profile.h"
00059 #include "real.h"
00060 #include "params.h"
00061 #include "target.h"
00062 #include "loop.h"
00063
00064
00065
00066 static REAL_VALUE_TYPE real_zero, real_one, real_almost_one, real_br_prob_base,
00067 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
00068
00069
00070 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
00071 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
00072 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
00073 #define PROB_ALWAYS (REG_BR_PROB_BASE)
00074
00075 static bool predicted_by_p PARAMS ((basic_block,
00076 enum br_predictor));
00077 static void combine_predictions_for_insn PARAMS ((rtx, basic_block));
00078 static void dump_prediction PARAMS ((enum br_predictor, int,
00079 basic_block, int));
00080 static void estimate_loops_at_level PARAMS ((struct loop *loop));
00081 static void propagate_freq PARAMS ((struct loop *));
00082 static void estimate_bb_frequencies PARAMS ((struct loops *));
00083 static void counts_to_freqs PARAMS ((void));
00084 static void process_note_predictions PARAMS ((basic_block, int *,
00085 dominance_info,
00086 dominance_info));
00087 static void process_note_prediction PARAMS ((basic_block, int *,
00088 dominance_info,
00089 dominance_info, int, int));
00090 static bool last_basic_block_p PARAMS ((basic_block));
00091 static void compute_function_frequency PARAMS ((void));
00092 static void choose_function_section PARAMS ((void));
00093 static bool can_predict_insn_p PARAMS ((rtx));
00094
00095
00096
00097
00098 struct predictor_info
00099 {
00100 const char *const name;
00101 const int hitrate;
00102
00103 const int flags;
00104 };
00105
00106
00107
00108 #define PRED_FLAG_FIRST_MATCH 1
00109
00110
00111
00112 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
00113
00114 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
00115 static const struct predictor_info predictor_info[]= {
00116 #include "predict.def"
00117
00118
00119 {NULL, 0, 0}
00120 };
00121 #undef DEF_PREDICTOR
00122
00123
00124
00125
00126 bool
00127 maybe_hot_bb_p (bb)
00128 basic_block bb;
00129 {
00130 if (profile_info.count_profiles_merged
00131 && flag_branch_probabilities
00132 && (bb->count
00133 < profile_info.max_counter_in_program
00134 / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
00135 return false;
00136 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
00137 return false;
00138 return true;
00139 }
00140
00141
00142
00143 bool
00144 probably_cold_bb_p (bb)
00145 basic_block bb;
00146 {
00147 if (profile_info.count_profiles_merged
00148 && flag_branch_probabilities
00149 && (bb->count
00150 < profile_info.max_counter_in_program
00151 / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
00152 return true;
00153 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
00154 return true;
00155 return false;
00156 }
00157
00158
00159 bool
00160 probably_never_executed_bb_p (bb)
00161 basic_block bb;
00162 {
00163 if (profile_info.count_profiles_merged
00164 && flag_branch_probabilities)
00165 return ((bb->count + profile_info.count_profiles_merged / 2)
00166 / profile_info.count_profiles_merged) == 0;
00167 return false;
00168 }
00169
00170
00171
00172
00173 static bool
00174 predicted_by_p (bb, predictor)
00175 basic_block bb;
00176 enum br_predictor predictor;
00177 {
00178 rtx note;
00179 if (!INSN_P (bb->end))
00180 return false;
00181 for (note = REG_NOTES (bb->end); note; note = XEXP (note, 1))
00182 if (REG_NOTE_KIND (note) == REG_BR_PRED
00183 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
00184 return true;
00185 return false;
00186 }
00187
00188 void
00189 predict_insn (insn, predictor, probability)
00190 rtx insn;
00191 int probability;
00192 enum br_predictor predictor;
00193 {
00194 if (!any_condjump_p (insn))
00195 abort ();
00196 if (!flag_guess_branch_prob)
00197 return;
00198
00199 REG_NOTES (insn)
00200 = gen_rtx_EXPR_LIST (REG_BR_PRED,
00201 gen_rtx_CONCAT (VOIDmode,
00202 GEN_INT ((int) predictor),
00203 GEN_INT ((int) probability)),
00204 REG_NOTES (insn));
00205 }
00206
00207
00208
00209 void
00210 predict_insn_def (insn, predictor, taken)
00211 rtx insn;
00212 enum br_predictor predictor;
00213 enum prediction taken;
00214 {
00215 int probability = predictor_info[(int) predictor].hitrate;
00216
00217 if (taken != TAKEN)
00218 probability = REG_BR_PROB_BASE - probability;
00219
00220 predict_insn (insn, predictor, probability);
00221 }
00222
00223
00224
00225 void
00226 predict_edge (e, predictor, probability)
00227 edge e;
00228 int probability;
00229 enum br_predictor predictor;
00230 {
00231 rtx last_insn;
00232 last_insn = e->src->end;
00233
00234
00235
00236 if (!any_condjump_p (last_insn))
00237 return;
00238
00239
00240 if (e->flags & EDGE_FALLTHRU)
00241 probability = REG_BR_PROB_BASE - probability;
00242
00243 predict_insn (last_insn, predictor, probability);
00244 }
00245
00246
00247
00248
00249 static bool
00250 can_predict_insn_p (insn)
00251 rtx insn;
00252 {
00253 return (GET_CODE (insn) == JUMP_INSN
00254 && any_condjump_p (insn)
00255 && BLOCK_FOR_INSN (insn)->succ->succ_next);
00256 }
00257
00258
00259
00260 void
00261 predict_edge_def (e, predictor, taken)
00262 edge e;
00263 enum br_predictor predictor;
00264 enum prediction taken;
00265 {
00266 int probability = predictor_info[(int) predictor].hitrate;
00267
00268 if (taken != TAKEN)
00269 probability = REG_BR_PROB_BASE - probability;
00270
00271 predict_edge (e, predictor, probability);
00272 }
00273
00274
00275
00276
00277 void
00278 invert_br_probabilities (insn)
00279 rtx insn;
00280 {
00281 rtx note;
00282
00283 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
00284 if (REG_NOTE_KIND (note) == REG_BR_PROB)
00285 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
00286 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
00287 XEXP (XEXP (note, 0), 1)
00288 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
00289 }
00290
00291
00292
00293 static void
00294 dump_prediction (predictor, probability, bb, used)
00295 enum br_predictor predictor;
00296 int probability;
00297 basic_block bb;
00298 int used;
00299 {
00300 edge e = bb->succ;
00301
00302 if (!rtl_dump_file)
00303 return;
00304
00305 while (e && (e->flags & EDGE_FALLTHRU))
00306 e = e->succ_next;
00307
00308 fprintf (rtl_dump_file, " %s heuristics%s: %.1f%%",
00309 predictor_info[predictor].name,
00310 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
00311
00312 if (bb->count)
00313 {
00314 fprintf (rtl_dump_file, " exec ");
00315 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
00316 if (e)
00317 {
00318 fprintf (rtl_dump_file, " hit ");
00319 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, e->count);
00320 fprintf (rtl_dump_file, " (%.1f%%)", e->count * 100.0 / bb->count);
00321 }
00322 }
00323
00324 fprintf (rtl_dump_file, "\n");
00325 }
00326
00327
00328
00329
00330 static void
00331 combine_predictions_for_insn (insn, bb)
00332 rtx insn;
00333 basic_block bb;
00334 {
00335 rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
00336 rtx *pnote = ®_NOTES (insn);
00337 rtx note;
00338 int best_probability = PROB_EVEN;
00339 int best_predictor = END_PREDICTORS;
00340 int combined_probability = REG_BR_PROB_BASE / 2;
00341 int d;
00342 bool first_match = false;
00343 bool found = false;
00344
00345 if (rtl_dump_file)
00346 fprintf (rtl_dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
00347 bb->index);
00348
00349
00350
00351
00352 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
00353 if (REG_NOTE_KIND (note) == REG_BR_PRED)
00354 {
00355 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
00356 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
00357
00358 found = true;
00359 if (best_predictor > predictor)
00360 best_probability = probability, best_predictor = predictor;
00361
00362 d = (combined_probability * probability
00363 + (REG_BR_PROB_BASE - combined_probability)
00364 * (REG_BR_PROB_BASE - probability));
00365
00366
00367 if (d == 0)
00368
00369 combined_probability = REG_BR_PROB_BASE / 2;
00370 else
00371 combined_probability = (((double) combined_probability) * probability
00372 * REG_BR_PROB_BASE / d + 0.5);
00373 }
00374
00375
00376
00377
00378
00379 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
00380 first_match = true;
00381
00382 if (!found)
00383 dump_prediction (PRED_NO_PREDICTION, combined_probability, bb, true);
00384 else
00385 {
00386 dump_prediction (PRED_DS_THEORY, combined_probability, bb, !first_match);
00387 dump_prediction (PRED_FIRST_MATCH, best_probability, bb, first_match);
00388 }
00389
00390 if (first_match)
00391 combined_probability = best_probability;
00392 dump_prediction (PRED_COMBINED, combined_probability, bb, true);
00393
00394 while (*pnote)
00395 {
00396 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
00397 {
00398 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
00399 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
00400
00401 dump_prediction (predictor, probability, bb,
00402 !first_match || best_predictor == predictor);
00403 *pnote = XEXP (*pnote, 1);
00404 }
00405 else
00406 pnote = &XEXP (*pnote, 1);
00407 }
00408
00409 if (!prob_note)
00410 {
00411 REG_NOTES (insn)
00412 = gen_rtx_EXPR_LIST (REG_BR_PROB,
00413 GEN_INT (combined_probability), REG_NOTES (insn));
00414
00415
00416
00417 if (bb->succ->succ_next)
00418 {
00419 BRANCH_EDGE (bb)->probability = combined_probability;
00420 FALLTHRU_EDGE (bb)->probability
00421 = REG_BR_PROB_BASE - combined_probability;
00422 }
00423 }
00424 }
00425
00426
00427
00428
00429
00430
00431
00432 void
00433 estimate_probability (loops_info)
00434 struct loops *loops_info;
00435 {
00436 dominance_info dominators, post_dominators;
00437 basic_block bb;
00438 int i;
00439
00440 connect_infinite_loops_to_exit ();
00441 dominators = calculate_dominance_info (CDI_DOMINATORS);
00442 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
00443
00444
00445
00446 for (i = 1; i < loops_info->num; i++)
00447 {
00448 basic_block bb, *bbs;
00449 int j;
00450 int exits;
00451 struct loop *loop = loops_info->parray[i];
00452
00453 flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
00454 exits = loop->num_exits;
00455
00456 bbs = get_loop_body (loop);
00457 for (j = 0; j < loop->num_nodes; j++)
00458 {
00459 int header_found = 0;
00460 edge e;
00461
00462 bb = bbs[j];
00463
00464
00465
00466
00467
00468 if (!can_predict_insn_p (bb->end)
00469 || predicted_by_p (bb, PRED_CONTINUE))
00470 continue;
00471
00472
00473
00474 for (e = bb->succ; e; e = e->succ_next)
00475 if (e->dest == loop->header
00476 && e->src == loop->latch)
00477 {
00478 header_found = 1;
00479 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
00480 }
00481
00482
00483
00484 if (!header_found)
00485 for (e = bb->succ; e; e = e->succ_next)
00486 if (e->dest->index < 0
00487 || !flow_bb_inside_loop_p (loop, e->dest))
00488 predict_edge
00489 (e, PRED_LOOP_EXIT,
00490 (REG_BR_PROB_BASE
00491 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
00492 / exits);
00493 }
00494 }
00495
00496
00497 FOR_EACH_BB (bb)
00498 {
00499 rtx last_insn = bb->end;
00500 rtx cond, earliest;
00501 edge e;
00502
00503 if (! can_predict_insn_p (last_insn))
00504 continue;
00505
00506 for (e = bb->succ; e; e = e->succ_next)
00507 {
00508
00509
00510
00511 if ((e->dest == EXIT_BLOCK_PTR
00512 || (e->dest->succ && !e->dest->succ->succ_next
00513 && e->dest->succ->dest == EXIT_BLOCK_PTR))
00514 && !predicted_by_p (bb, PRED_NULL_RETURN)
00515 && !predicted_by_p (bb, PRED_CONST_RETURN)
00516 && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
00517 && !last_basic_block_p (e->dest))
00518 predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
00519
00520
00521
00522 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
00523 && dominated_by_p (dominators, e->dest, e->src)
00524 && !dominated_by_p (post_dominators, e->src, e->dest))
00525 {
00526 rtx insn;
00527
00528
00529
00530
00531
00532 for (insn = e->dest->head; insn != NEXT_INSN (e->dest->end);
00533 insn = NEXT_INSN (insn))
00534 if (GET_CODE (insn) == CALL_INSN
00535
00536
00537 && ! CONST_OR_PURE_CALL_P (insn))
00538 {
00539 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
00540 break;
00541 }
00542 }
00543 }
00544
00545 cond = get_condition (last_insn, &earliest);
00546 if (! cond)
00547 continue;
00548
00549
00550
00551
00552 if (GET_RTX_CLASS (GET_CODE (cond)) == '<'
00553 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
00554 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
00555 {
00556 if (GET_CODE (cond) == EQ)
00557 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
00558 else if (GET_CODE (cond) == NE)
00559 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
00560 }
00561 else
00562
00563
00564
00565
00566
00567 switch (GET_CODE (cond))
00568 {
00569 case CONST_INT:
00570
00571 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
00572 cond == const0_rtx ? NOT_TAKEN : TAKEN);
00573 break;
00574
00575 case EQ:
00576 case UNEQ:
00577
00578
00579
00580 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
00581 ;
00582
00583
00584 else if (XEXP (cond, 1) == const0_rtx
00585 || XEXP (cond, 0) == const0_rtx)
00586 ;
00587 else
00588 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
00589 break;
00590
00591 case NE:
00592 case LTGT:
00593
00594
00595
00596 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
00597 ;
00598
00599
00600 else if (XEXP (cond, 1) == const0_rtx
00601 || XEXP (cond, 0) == const0_rtx)
00602 ;
00603 else
00604 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
00605 break;
00606
00607 case ORDERED:
00608 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
00609 break;
00610
00611 case UNORDERED:
00612 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
00613 break;
00614
00615 case LE:
00616 case LT:
00617 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
00618 || XEXP (cond, 1) == constm1_rtx)
00619 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
00620 break;
00621
00622 case GE:
00623 case GT:
00624 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
00625 || XEXP (cond, 1) == constm1_rtx)
00626 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
00627 break;
00628
00629 default:
00630 break;
00631 }
00632 }
00633
00634
00635 FOR_EACH_BB (bb)
00636 if (GET_CODE (bb->end) == JUMP_INSN
00637 && any_condjump_p (bb->end)
00638 && bb->succ->succ_next != NULL)
00639 combine_predictions_for_insn (bb->end, bb);
00640
00641 free_dominance_info (post_dominators);
00642 free_dominance_info (dominators);
00643
00644 remove_fake_edges ();
00645 estimate_bb_frequencies (loops_info);
00646 }
00647
00648
00649
00650
00651
00652 void
00653 expected_value_to_br_prob ()
00654 {
00655 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
00656
00657 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
00658 {
00659 switch (GET_CODE (insn))
00660 {
00661 case NOTE:
00662
00663 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
00664 {
00665 ev = NOTE_EXPECTED_VALUE (insn);
00666 ev_reg = XEXP (ev, 0);
00667 delete_insn (insn);
00668 }
00669 continue;
00670
00671 case CODE_LABEL:
00672
00673 ev = NULL_RTX;
00674 continue;
00675
00676 case JUMP_INSN:
00677
00678
00679 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX
00680 || ! any_condjump_p (insn))
00681 continue;
00682 break;
00683
00684 default:
00685
00686 if (ev && reg_set_p (ev_reg, insn))
00687 ev = NULL_RTX;
00688 continue;
00689 }
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 cond = XEXP (SET_SRC (pc_set (insn)), 0);
00701 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg);
00702 if (! cond || XEXP (cond, 0) != ev_reg
00703 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
00704 continue;
00705
00706
00707
00708
00709 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
00710 XEXP (ev, 1), XEXP (cond, 1));
00711 cond = simplify_rtx (cond);
00712
00713
00714 if (cond != const_true_rtx && cond != const0_rtx)
00715 abort ();
00716 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
00717 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
00718 }
00719 }
00720
00721
00722
00723 static bool
00724 last_basic_block_p (bb)
00725 basic_block bb;
00726 {
00727 if (bb == EXIT_BLOCK_PTR)
00728 return false;
00729
00730 return (bb->next_bb == EXIT_BLOCK_PTR
00731 || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
00732 && bb->succ && !bb->succ->succ_next
00733 && bb->succ->dest->next_bb == EXIT_BLOCK_PTR));
00734 }
00735
00736
00737
00738
00739
00740
00741
00742 static void
00743 process_note_prediction (bb, heads, dominators, post_dominators, pred, flags)
00744 basic_block bb;
00745 int *heads;
00746 dominance_info dominators;
00747 dominance_info post_dominators;
00748 int pred;
00749 int flags;
00750 {
00751 edge e;
00752 int y;
00753 bool taken;
00754
00755 taken = flags & IS_TAKEN;
00756
00757 if (heads[bb->index] < 0)
00758 {
00759
00760
00761
00762 basic_block ai = bb;
00763 basic_block next_ai = get_immediate_dominator (dominators, bb);
00764 int head;
00765
00766 while (heads[next_ai->index] < 0)
00767 {
00768 if (!dominated_by_p (post_dominators, next_ai, bb))
00769 break;
00770 heads[next_ai->index] = ai->index;
00771 ai = next_ai;
00772 next_ai = get_immediate_dominator (dominators, next_ai);
00773 }
00774 if (!dominated_by_p (post_dominators, next_ai, bb))
00775 head = next_ai->index;
00776 else
00777 head = heads[next_ai->index];
00778 while (next_ai != bb)
00779 {
00780 next_ai = ai;
00781 if (heads[ai->index] == ENTRY_BLOCK)
00782 ai = ENTRY_BLOCK_PTR;
00783 else
00784 ai = BASIC_BLOCK (heads[ai->index]);
00785 heads[next_ai->index] = head;
00786 }
00787 }
00788 y = heads[bb->index];
00789
00790
00791
00792 if (y == last_basic_block || !can_predict_insn_p (BASIC_BLOCK (y)->end))
00793 return;
00794 for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next)
00795 if (e->dest->index >= 0
00796 && dominated_by_p (post_dominators, e->dest, bb))
00797 predict_edge_def (e, pred, taken);
00798 }
00799
00800
00801
00802
00803
00804 static void
00805 process_note_predictions (bb, heads, dominators, post_dominators)
00806 basic_block bb;
00807 int *heads;
00808 dominance_info dominators;
00809 dominance_info post_dominators;
00810 {
00811 rtx insn;
00812 edge e;
00813
00814
00815 int contained_noreturn_call = 0;
00816 int was_bb_head = 0;
00817 int noreturn_block = 1;
00818
00819 for (insn = bb->end; insn;
00820 was_bb_head |= (insn == bb->head), insn = PREV_INSN (insn))
00821 {
00822 if (GET_CODE (insn) != NOTE)
00823 {
00824 if (was_bb_head)
00825 break;
00826 else
00827 {
00828
00829
00830 if (GET_CODE (insn) == CALL_INSN
00831 && find_reg_note (insn, REG_NORETURN, NULL))
00832 contained_noreturn_call = 1;
00833 continue;
00834 }
00835 }
00836 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION)
00837 {
00838 int alg = (int) NOTE_PREDICTION_ALG (insn);
00839
00840 process_note_prediction (bb,
00841 heads,
00842 dominators,
00843 post_dominators,
00844 alg, (int) NOTE_PREDICTION_FLAGS (insn));
00845 delete_insn (insn);
00846 }
00847 }
00848 for (e = bb->succ; e; e = e->succ_next)
00849 if (!(e->flags & EDGE_FAKE))
00850 noreturn_block = 0;
00851 if (contained_noreturn_call)
00852 {
00853
00854
00855
00856 process_note_prediction (bb,
00857 heads,
00858 dominators,
00859 post_dominators, PRED_NORETURN, NOT_TAKEN);
00860 }
00861 }
00862
00863
00864
00865
00866 void
00867 note_prediction_to_br_prob ()
00868 {
00869 basic_block bb;
00870 dominance_info post_dominators, dominators;
00871 int *heads;
00872
00873
00874 add_noreturn_fake_exit_edges ();
00875 connect_infinite_loops_to_exit ();
00876
00877 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
00878 dominators = calculate_dominance_info (CDI_DOMINATORS);
00879
00880 heads = xmalloc (sizeof (int) * last_basic_block);
00881 memset (heads, -1, sizeof (int) * last_basic_block);
00882 heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block;
00883
00884
00885
00886 FOR_EACH_BB (bb)
00887 process_note_predictions (bb, heads, dominators, post_dominators);
00888
00889 free_dominance_info (post_dominators);
00890 free_dominance_info (dominators);
00891 free (heads);
00892
00893 remove_fake_edges ();
00894 }
00895
00896
00897
00898
00899 typedef struct block_info_def
00900 {
00901
00902 REAL_VALUE_TYPE frequency;
00903
00904
00905 basic_block next;
00906
00907
00908 int tovisit:1;
00909
00910
00911 int npredecessors;
00912 } *block_info;
00913
00914
00915 typedef struct edge_info_def
00916 {
00917
00918
00919
00920 REAL_VALUE_TYPE back_edge_prob;
00921
00922 int back_edge:1;
00923 } *edge_info;
00924
00925 #define BLOCK_INFO(B) ((block_info) (B)->aux)
00926 #define EDGE_INFO(E) ((edge_info) (E)->aux)
00927
00928
00929
00930
00931 static void
00932 propagate_freq (loop)
00933 struct loop *loop;
00934 {
00935 basic_block head = loop->header;
00936 basic_block bb;
00937 basic_block last;
00938 edge e;
00939 basic_block nextbb;
00940
00941
00942
00943 FOR_EACH_BB (bb)
00944 {
00945 if (BLOCK_INFO (bb)->tovisit)
00946 {
00947 int count = 0;
00948
00949 for (e = bb->pred; e; e = e->pred_next)
00950 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
00951 count++;
00952 else if (BLOCK_INFO (e->src)->tovisit
00953 && rtl_dump_file && !EDGE_INFO (e)->back_edge)
00954 fprintf (rtl_dump_file,
00955 "Irreducible region hit, ignoring edge to %i->%i\n",
00956 e->src->index, bb->index);
00957 BLOCK_INFO (bb)->npredecessors = count;
00958 }
00959 }
00960
00961 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
00962 last = head;
00963 for (bb = head; bb; bb = nextbb)
00964 {
00965 REAL_VALUE_TYPE cyclic_probability, frequency;
00966
00967 memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
00968 memcpy (&frequency, &real_zero, sizeof (real_zero));
00969
00970 nextbb = BLOCK_INFO (bb)->next;
00971 BLOCK_INFO (bb)->next = NULL;
00972
00973
00974 if (bb != head)
00975 {
00976 #ifdef ENABLE_CHECKING
00977 for (e = bb->pred; e; e = e->pred_next)
00978 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
00979 abort ();
00980 #endif
00981
00982 for (e = bb->pred; e; e = e->pred_next)
00983 if (EDGE_INFO (e)->back_edge)
00984 {
00985 REAL_ARITHMETIC (cyclic_probability, PLUS_EXPR,
00986 cyclic_probability,
00987 EDGE_INFO (e)->back_edge_prob);
00988 }
00989 else if (!(e->flags & EDGE_DFS_BACK))
00990 {
00991 REAL_VALUE_TYPE tmp;
00992
00993
00994
00995
00996
00997 REAL_VALUE_FROM_INT (tmp, e->probability, 0,
00998 TYPE_MODE (double_type_node));
00999 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp,
01000 BLOCK_INFO (e->src)->frequency);
01001 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp, real_inv_br_prob_base);
01002 REAL_ARITHMETIC (frequency, PLUS_EXPR, frequency, tmp);
01003 }
01004
01005 if (REAL_VALUES_IDENTICAL (cyclic_probability, real_zero))
01006 memcpy (&BLOCK_INFO (bb)->frequency, &frequency, sizeof (frequency));
01007 else
01008 {
01009 if (REAL_VALUES_LESS (real_almost_one, cyclic_probability))
01010 memcpy (&cyclic_probability, &real_almost_one, sizeof (real_zero));
01011
01012
01013
01014
01015 REAL_ARITHMETIC (cyclic_probability, MINUS_EXPR, real_one,
01016 cyclic_probability);
01017 REAL_ARITHMETIC (BLOCK_INFO (bb)->frequency,
01018 RDIV_EXPR, frequency, cyclic_probability);
01019 }
01020 }
01021
01022 BLOCK_INFO (bb)->tovisit = 0;
01023
01024
01025 for (e = bb->succ; e; e = e->succ_next)
01026 if (e->dest == head)
01027 {
01028 REAL_VALUE_TYPE tmp;
01029
01030
01031
01032
01033 REAL_VALUE_FROM_INT (tmp, e->probability, 0,
01034 TYPE_MODE (double_type_node));
01035 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp,
01036 BLOCK_INFO (bb)->frequency);
01037 REAL_ARITHMETIC (EDGE_INFO (e)->back_edge_prob,
01038 MULT_EXPR, tmp, real_inv_br_prob_base);
01039
01040 }
01041
01042
01043 for (e = bb->succ; e; e = e->succ_next)
01044 if (!(e->flags & EDGE_DFS_BACK)
01045 && BLOCK_INFO (e->dest)->npredecessors)
01046 {
01047 BLOCK_INFO (e->dest)->npredecessors--;
01048 if (!BLOCK_INFO (e->dest)->npredecessors)
01049 {
01050 if (!nextbb)
01051 nextbb = e->dest;
01052 else
01053 BLOCK_INFO (last)->next = e->dest;
01054
01055 last = e->dest;
01056 }
01057 }
01058 }
01059 }
01060
01061
01062
01063 static void
01064 estimate_loops_at_level (first_loop)
01065 struct loop *first_loop;
01066 {
01067 struct loop *loop;
01068
01069 for (loop = first_loop; loop; loop = loop->next)
01070 {
01071 edge e;
01072 basic_block *bbs;
01073 int i;
01074
01075 estimate_loops_at_level (loop->inner);
01076
01077 if (loop->latch->succ)
01078 {
01079
01080 e = loop_latch_edge (loop);
01081 EDGE_INFO (e)->back_edge = 1;
01082 }
01083
01084 bbs = get_loop_body (loop);
01085 for (i = 0; i < loop->num_nodes; i++)
01086 BLOCK_INFO (bbs[i])->tovisit = 1;
01087 free (bbs);
01088 propagate_freq (loop);
01089 }
01090 }
01091
01092
01093
01094 static void
01095 counts_to_freqs ()
01096 {
01097 HOST_WIDEST_INT count_max = 1;
01098 basic_block bb;
01099
01100 FOR_EACH_BB (bb)
01101 count_max = MAX (bb->count, count_max);
01102
01103 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
01104 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
01105 }
01106
01107
01108
01109
01110
01111
01112 bool
01113 expensive_function_p (threshold)
01114 int threshold;
01115 {
01116 unsigned int sum = 0;
01117 basic_block bb;
01118 unsigned int limit;
01119
01120
01121
01122 if (threshold > BB_FREQ_MAX)
01123 abort ();
01124
01125
01126
01127
01128 if (ENTRY_BLOCK_PTR->frequency == 0)
01129 return true;
01130
01131
01132 limit = ENTRY_BLOCK_PTR->frequency * threshold;
01133 FOR_EACH_BB (bb)
01134 {
01135 rtx insn;
01136
01137 for (insn = bb->head; insn != NEXT_INSN (bb->end);
01138 insn = NEXT_INSN (insn))
01139 if (active_insn_p (insn))
01140 {
01141 sum += bb->frequency;
01142 if (sum > limit)
01143 return true;
01144 }
01145 }
01146
01147 return false;
01148 }
01149
01150
01151
01152 static void
01153 estimate_bb_frequencies (loops)
01154 struct loops *loops;
01155 {
01156 basic_block bb;
01157 REAL_VALUE_TYPE freq_max;
01158 enum machine_mode double_mode = TYPE_MODE (double_type_node);
01159
01160 if (flag_branch_probabilities)
01161 counts_to_freqs ();
01162 else
01163 {
01164 REAL_VALUE_FROM_INT (real_zero, 0, 0, double_mode);
01165 REAL_VALUE_FROM_INT (real_one, 1, 0, double_mode);
01166 REAL_VALUE_FROM_INT (real_br_prob_base, REG_BR_PROB_BASE, 0, double_mode);
01167 REAL_VALUE_FROM_INT (real_bb_freq_max, BB_FREQ_MAX, 0, double_mode);
01168 REAL_VALUE_FROM_INT (real_one_half, 2, 0, double_mode);
01169 REAL_ARITHMETIC (real_one_half, RDIV_EXPR, real_one, real_one_half);
01170 REAL_ARITHMETIC (real_inv_br_prob_base, RDIV_EXPR, real_one, real_br_prob_base);
01171 REAL_ARITHMETIC (real_almost_one, MINUS_EXPR, real_one, real_inv_br_prob_base);
01172
01173 mark_dfs_back_edges ();
01174
01175
01176 FOR_EACH_BB (bb)
01177 {
01178 rtx last_insn = bb->end;
01179
01180 if (!can_predict_insn_p (last_insn))
01181 {
01182
01183
01184
01185 int nedges = 0;
01186 edge e;
01187
01188 for (e = bb->succ; e; e = e->succ_next)
01189 {
01190 nedges++;
01191 if (e->probability != 0)
01192 break;
01193 }
01194 if (!e)
01195 for (e = bb->succ; e; e = e->succ_next)
01196 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
01197 }
01198 }
01199
01200 ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
01201
01202
01203 alloc_aux_for_blocks (sizeof (struct block_info_def));
01204 alloc_aux_for_edges (sizeof (struct edge_info_def));
01205 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
01206 {
01207 edge e;
01208
01209 BLOCK_INFO (bb)->tovisit = 0;
01210 for (e = bb->succ; e; e = e->succ_next)
01211 {
01212 REAL_VALUE_FROM_INT (EDGE_INFO (e)->back_edge_prob,
01213 e->probability, 0, double_mode);
01214 REAL_ARITHMETIC (EDGE_INFO (e)->back_edge_prob,
01215 MULT_EXPR, EDGE_INFO (e)->back_edge_prob,
01216 real_inv_br_prob_base);
01217 }
01218 }
01219
01220
01221
01222 estimate_loops_at_level (loops->tree_root);
01223
01224 memcpy (&freq_max, &real_zero, sizeof (real_zero));
01225 FOR_EACH_BB (bb)
01226 if (REAL_VALUES_LESS
01227 (freq_max, BLOCK_INFO (bb)->frequency))
01228 memcpy (&freq_max, &BLOCK_INFO (bb)->frequency,
01229 sizeof (freq_max));
01230
01231 REAL_ARITHMETIC (freq_max, RDIV_EXPR, real_bb_freq_max, freq_max);
01232
01233 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
01234 {
01235 REAL_VALUE_TYPE tmp;
01236
01237 REAL_ARITHMETIC (tmp, MULT_EXPR, BLOCK_INFO (bb)->frequency,
01238 freq_max);
01239 REAL_ARITHMETIC (tmp, PLUS_EXPR, tmp, real_one_half);
01240 bb->frequency = REAL_VALUE_UNSIGNED_FIX (tmp);
01241 }
01242
01243 free_aux_for_blocks ();
01244 free_aux_for_edges ();
01245 }
01246 compute_function_frequency ();
01247 if (flag_reorder_functions)
01248 choose_function_section ();
01249 }
01250
01251
01252 static void
01253 compute_function_frequency ()
01254 {
01255 basic_block bb;
01256
01257 if (!profile_info.count_profiles_merged
01258 || !flag_branch_probabilities)
01259 return;
01260 cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
01261 FOR_EACH_BB (bb)
01262 {
01263 if (maybe_hot_bb_p (bb))
01264 {
01265 cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
01266 return;
01267 }
01268 if (!probably_never_executed_bb_p (bb))
01269 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
01270 }
01271 }
01272
01273
01274 static void
01275 choose_function_section ()
01276 {
01277 if (DECL_SECTION_NAME (current_function_decl)
01278 || !targetm.have_named_sections
01279
01280
01281
01282
01283 || DECL_ONE_ONLY (current_function_decl))
01284 return;
01285 if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
01286 DECL_SECTION_NAME (current_function_decl) =
01287 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
01288 if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
01289 DECL_SECTION_NAME (current_function_decl) =
01290 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
01291 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
01292 }