s390.c (optimization_options): Set flag_asynchronous_unwind_tables to 1 by default.
[official-gcc.git] / gcc / predict.c
blob7e581468dec7097e7a69cdfebab1e138263affed
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 /* References:
23 [1] "Branch Prediction for Free"
24 Ball and Larus; PLDI '93.
25 [2] "Static Branch Frequency and Program Profile Analysis"
26 Wu and Larus; MICRO-27.
27 [3] "Corpus-based Static Branch Prediction"
28 Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "insn-config.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "function.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "expr.h"
47 #include "predict.h"
48 #include "profile.h"
49 #include "real.h"
50 #include "params.h"
51 #include "target.h"
52 #include "loop.h"
54 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, 0.5,
55 REAL_BB_FREQ_MAX. */
56 static REAL_VALUE_TYPE real_zero, real_one, real_almost_one, real_br_prob_base,
57 real_one_half, real_bb_freq_max;
59 /* Random guesstimation given names. */
60 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
61 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
62 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
63 #define PROB_ALWAYS (REG_BR_PROB_BASE)
65 static bool predicted_by_p PARAMS ((basic_block,
66 enum br_predictor));
67 static void combine_predictions_for_insn PARAMS ((rtx, basic_block));
68 static void dump_prediction PARAMS ((enum br_predictor, int,
69 basic_block, int));
70 static void estimate_loops_at_level PARAMS ((struct loop *loop));
71 static void propagate_freq PARAMS ((struct loop *));
72 static void estimate_bb_frequencies PARAMS ((struct loops *));
73 static void counts_to_freqs PARAMS ((void));
74 static void process_note_predictions PARAMS ((basic_block, int *,
75 dominance_info,
76 dominance_info));
77 static void process_note_prediction PARAMS ((basic_block, int *,
78 dominance_info,
79 dominance_info, int, int));
80 static bool last_basic_block_p PARAMS ((basic_block));
81 static void compute_function_frequency PARAMS ((void));
82 static void choose_function_section PARAMS ((void));
83 static bool can_predict_insn_p PARAMS ((rtx));
85 /* Information we hold about each branch predictor.
86 Filled using information from predict.def. */
88 struct predictor_info
90 const char *const name; /* Name used in the debugging dumps. */
91 const int hitrate; /* Expected hitrate used by
92 predict_insn_def call. */
93 const int flags;
96 /* Use given predictor without Dempster-Shaffer theory if it matches
97 using first_match heuristics. */
98 #define PRED_FLAG_FIRST_MATCH 1
100 /* Recompute hitrate in percent to our representation. */
102 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
104 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
105 static const struct predictor_info predictor_info[]= {
106 #include "predict.def"
108 /* Upper bound on predictors. */
109 {NULL, 0, 0}
111 #undef DEF_PREDICTOR
113 /* Return true in case BB can be CPU intensive and should be optimized
114 for maximal perofmrance. */
116 bool
117 maybe_hot_bb_p (bb)
118 basic_block bb;
120 if (profile_info.count_profiles_merged
121 && flag_branch_probabilities
122 && (bb->count
123 < profile_info.max_counter_in_program
124 / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
125 return false;
126 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
127 return false;
128 return true;
131 /* Return true in case BB is cold and should be optimized for size. */
133 bool
134 probably_cold_bb_p (bb)
135 basic_block bb;
137 if (profile_info.count_profiles_merged
138 && flag_branch_probabilities
139 && (bb->count
140 < profile_info.max_counter_in_program
141 / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
142 return true;
143 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
144 return true;
145 return false;
148 /* Return true in case BB is probably never executed. */
149 bool
150 probably_never_executed_bb_p (bb)
151 basic_block bb;
153 if (profile_info.count_profiles_merged
154 && flag_branch_probabilities)
155 return ((bb->count + profile_info.count_profiles_merged / 2)
156 / profile_info.count_profiles_merged) == 0;
157 return false;
160 /* Return true if the one of outgoing edges is already predicted by
161 PREDICTOR. */
163 static bool
164 predicted_by_p (bb, predictor)
165 basic_block bb;
166 enum br_predictor predictor;
168 rtx note;
169 if (!INSN_P (bb->end))
170 return false;
171 for (note = REG_NOTES (bb->end); note; note = XEXP (note, 1))
172 if (REG_NOTE_KIND (note) == REG_BR_PRED
173 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
174 return true;
175 return false;
178 void
179 predict_insn (insn, predictor, probability)
180 rtx insn;
181 int probability;
182 enum br_predictor predictor;
184 if (!any_condjump_p (insn))
185 abort ();
187 REG_NOTES (insn)
188 = gen_rtx_EXPR_LIST (REG_BR_PRED,
189 gen_rtx_CONCAT (VOIDmode,
190 GEN_INT ((int) predictor),
191 GEN_INT ((int) probability)),
192 REG_NOTES (insn));
195 /* Predict insn by given predictor. */
197 void
198 predict_insn_def (insn, predictor, taken)
199 rtx insn;
200 enum br_predictor predictor;
201 enum prediction taken;
203 int probability = predictor_info[(int) predictor].hitrate;
205 if (taken != TAKEN)
206 probability = REG_BR_PROB_BASE - probability;
208 predict_insn (insn, predictor, probability);
211 /* Predict edge E with given probability if possible. */
213 void
214 predict_edge (e, predictor, probability)
215 edge e;
216 int probability;
217 enum br_predictor predictor;
219 rtx last_insn;
220 last_insn = e->src->end;
222 /* We can store the branch prediction information only about
223 conditional jumps. */
224 if (!any_condjump_p (last_insn))
225 return;
227 /* We always store probability of branching. */
228 if (e->flags & EDGE_FALLTHRU)
229 probability = REG_BR_PROB_BASE - probability;
231 predict_insn (last_insn, predictor, probability);
234 /* Return true when we can store prediction on insn INSN.
235 At the moment we represent predictions only on conditional
236 jumps, not at computed jump or other complicated cases. */
237 static bool
238 can_predict_insn_p (insn)
239 rtx insn;
241 return (GET_CODE (insn) == JUMP_INSN
242 && any_condjump_p (insn)
243 && BLOCK_FOR_INSN (insn)->succ->succ_next);
246 /* Predict edge E by given predictor if possible. */
248 void
249 predict_edge_def (e, predictor, taken)
250 edge e;
251 enum br_predictor predictor;
252 enum prediction taken;
254 int probability = predictor_info[(int) predictor].hitrate;
256 if (taken != TAKEN)
257 probability = REG_BR_PROB_BASE - probability;
259 predict_edge (e, predictor, probability);
262 /* Invert all branch predictions or probability notes in the INSN. This needs
263 to be done each time we invert the condition used by the jump. */
265 void
266 invert_br_probabilities (insn)
267 rtx insn;
269 rtx note;
271 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
272 if (REG_NOTE_KIND (note) == REG_BR_PROB)
273 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
274 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
275 XEXP (XEXP (note, 0), 1)
276 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
279 /* Dump information about the branch prediction to the output file. */
281 static void
282 dump_prediction (predictor, probability, bb, used)
283 enum br_predictor predictor;
284 int probability;
285 basic_block bb;
286 int used;
288 edge e = bb->succ;
290 if (!rtl_dump_file)
291 return;
293 while (e && (e->flags & EDGE_FALLTHRU))
294 e = e->succ_next;
296 fprintf (rtl_dump_file, " %s heuristics%s: %.1f%%",
297 predictor_info[predictor].name,
298 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
300 if (bb->count)
302 fprintf (rtl_dump_file, " exec ");
303 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
304 if (e)
306 fprintf (rtl_dump_file, " hit ");
307 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, e->count);
308 fprintf (rtl_dump_file, " (%.1f%%)", e->count * 100.0 / bb->count);
312 fprintf (rtl_dump_file, "\n");
315 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
316 note if not already present. Remove now useless REG_BR_PRED notes. */
318 static void
319 combine_predictions_for_insn (insn, bb)
320 rtx insn;
321 basic_block bb;
323 rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
324 rtx *pnote = &REG_NOTES (insn);
325 rtx note;
326 int best_probability = PROB_EVEN;
327 int best_predictor = END_PREDICTORS;
328 int combined_probability = REG_BR_PROB_BASE / 2;
329 int d;
330 bool first_match = false;
331 bool found = false;
333 if (rtl_dump_file)
334 fprintf (rtl_dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
335 bb->index);
337 /* We implement "first match" heuristics and use probability guessed
338 by predictor with smallest index. In the future we will use better
339 probability combination techniques. */
340 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
341 if (REG_NOTE_KIND (note) == REG_BR_PRED)
343 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
344 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
346 found = true;
347 if (best_predictor > predictor)
348 best_probability = probability, best_predictor = predictor;
350 d = (combined_probability * probability
351 + (REG_BR_PROB_BASE - combined_probability)
352 * (REG_BR_PROB_BASE - probability));
354 /* Use FP math to avoid overflows of 32bit integers. */
355 if (d == 0)
356 /* If one probability is 0% and one 100%, avoid division by zero. */
357 combined_probability = REG_BR_PROB_BASE / 2;
358 else
359 combined_probability = (((double) combined_probability) * probability
360 * REG_BR_PROB_BASE / d + 0.5);
363 /* Decide which heuristic to use. In case we didn't match anything,
364 use no_prediction heuristic, in case we did match, use either
365 first match or Dempster-Shaffer theory depending on the flags. */
367 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
368 first_match = true;
370 if (!found)
371 dump_prediction (PRED_NO_PREDICTION, combined_probability, bb, true);
372 else
374 dump_prediction (PRED_DS_THEORY, combined_probability, bb, !first_match);
375 dump_prediction (PRED_FIRST_MATCH, best_probability, bb, first_match);
378 if (first_match)
379 combined_probability = best_probability;
380 dump_prediction (PRED_COMBINED, combined_probability, bb, true);
382 while (*pnote)
384 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
386 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
387 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
389 dump_prediction (predictor, probability, bb,
390 !first_match || best_predictor == predictor);
391 *pnote = XEXP (*pnote, 1);
393 else
394 pnote = &XEXP (*pnote, 1);
397 if (!prob_note)
399 REG_NOTES (insn)
400 = gen_rtx_EXPR_LIST (REG_BR_PROB,
401 GEN_INT (combined_probability), REG_NOTES (insn));
403 /* Save the prediction into CFG in case we are seeing non-degenerated
404 conditional jump. */
405 if (bb->succ->succ_next)
407 BRANCH_EDGE (bb)->probability = combined_probability;
408 FALLTHRU_EDGE (bb)->probability
409 = REG_BR_PROB_BASE - combined_probability;
414 /* Statically estimate the probability that a branch will be taken.
415 ??? In the next revision there will be a number of other predictors added
416 from the above references. Further, each heuristic will be factored out
417 into its own function for clarity (and to facilitate the combination of
418 predictions). */
420 void
421 estimate_probability (loops_info)
422 struct loops *loops_info;
424 dominance_info dominators, post_dominators;
425 basic_block bb;
426 int i;
428 connect_infinite_loops_to_exit ();
429 dominators = calculate_dominance_info (CDI_DOMINATORS);
430 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
432 /* Try to predict out blocks in a loop that are not part of a
433 natural loop. */
434 for (i = 1; i < loops_info->num; i++)
436 basic_block bb, *bbs;
437 int j;
438 int exits;
439 struct loop *loop = loops_info->parray[i];
441 flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
442 exits = loop->num_exits;
444 bbs = get_loop_body (loop);
445 for (j = 0; j < loop->num_nodes; j++)
447 int header_found = 0;
448 edge e;
450 bb = bbs[j];
452 /* Bypass loop heuristics on continue statement. These
453 statements construct loops via "non-loop" constructs
454 in the source language and are better to be handled
455 separately. */
456 if (!can_predict_insn_p (bb->end)
457 || predicted_by_p (bb, PRED_CONTINUE))
458 continue;
460 /* Loop branch heuristics - predict an edge back to a
461 loop's head as taken. */
462 for (e = bb->succ; e; e = e->succ_next)
463 if (e->dest == loop->header
464 && e->src == loop->latch)
466 header_found = 1;
467 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
470 /* Loop exit heuristics - predict an edge exiting the loop if the
471 conditinal has no loop header successors as not taken. */
472 if (!header_found)
473 for (e = bb->succ; e; e = e->succ_next)
474 if (e->dest->index < 0
475 || !flow_bb_inside_loop_p (loop, e->dest))
476 predict_edge
477 (e, PRED_LOOP_EXIT,
478 (REG_BR_PROB_BASE
479 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
480 / exits);
484 /* Attempt to predict conditional jumps using a number of heuristics. */
485 FOR_EACH_BB (bb)
487 rtx last_insn = bb->end;
488 rtx cond, earliest;
489 edge e;
491 if (! can_predict_insn_p (last_insn))
492 continue;
494 for (e = bb->succ; e; e = e->succ_next)
496 /* Predict early returns to be probable, as we've already taken
497 care for error returns and other are often used for fast paths
498 trought function. */
499 if ((e->dest == EXIT_BLOCK_PTR
500 || (e->dest->succ && !e->dest->succ->succ_next
501 && e->dest->succ->dest == EXIT_BLOCK_PTR))
502 && !predicted_by_p (bb, PRED_NULL_RETURN)
503 && !predicted_by_p (bb, PRED_CONST_RETURN)
504 && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
505 && !last_basic_block_p (e->dest))
506 predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
508 /* Look for block we are guarding (ie we dominate it,
509 but it doesn't postdominate us). */
510 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
511 && dominated_by_p (dominators, e->dest, e->src)
512 && !dominated_by_p (post_dominators, e->src, e->dest))
514 rtx insn;
516 /* The call heuristic claims that a guarded function call
517 is improbable. This is because such calls are often used
518 to signal exceptional situations such as printing error
519 messages. */
520 for (insn = e->dest->head; insn != NEXT_INSN (e->dest->end);
521 insn = NEXT_INSN (insn))
522 if (GET_CODE (insn) == CALL_INSN
523 /* Constant and pure calls are hardly used to signalize
524 something exceptional. */
525 && ! CONST_OR_PURE_CALL_P (insn))
527 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
528 break;
533 cond = get_condition (last_insn, &earliest);
534 if (! cond)
535 continue;
537 /* Try "pointer heuristic."
538 A comparison ptr == 0 is predicted as false.
539 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
540 if (GET_RTX_CLASS (GET_CODE (cond)) == '<'
541 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
542 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
544 if (GET_CODE (cond) == EQ)
545 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
546 else if (GET_CODE (cond) == NE)
547 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
549 else
551 /* Try "opcode heuristic."
552 EQ tests are usually false and NE tests are usually true. Also,
553 most quantities are positive, so we can make the appropriate guesses
554 about signed comparisons against zero. */
555 switch (GET_CODE (cond))
557 case CONST_INT:
558 /* Unconditional branch. */
559 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
560 cond == const0_rtx ? NOT_TAKEN : TAKEN);
561 break;
563 case EQ:
564 case UNEQ:
565 /* Floating point comparisons appears to behave in a very
566 inpredictable way because of special role of = tests in
567 FP code. */
568 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
570 /* Comparisons with 0 are often used for booleans and there is
571 nothing usefull to predict about them. */
572 else if (XEXP (cond, 1) == const0_rtx
573 || XEXP (cond, 0) == const0_rtx)
575 else
576 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
577 break;
579 case NE:
580 case LTGT:
581 /* Floating point comparisons appears to behave in a very
582 inpredictable way because of special role of = tests in
583 FP code. */
584 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
586 /* Comparisons with 0 are often used for booleans and there is
587 nothing usefull to predict about them. */
588 else if (XEXP (cond, 1) == const0_rtx
589 || XEXP (cond, 0) == const0_rtx)
591 else
592 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
593 break;
595 case ORDERED:
596 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
597 break;
599 case UNORDERED:
600 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
601 break;
603 case LE:
604 case LT:
605 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
606 || XEXP (cond, 1) == constm1_rtx)
607 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
608 break;
610 case GE:
611 case GT:
612 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
613 || XEXP (cond, 1) == constm1_rtx)
614 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
615 break;
617 default:
618 break;
622 /* Attach the combined probability to each conditional jump. */
623 FOR_EACH_BB (bb)
624 if (GET_CODE (bb->end) == JUMP_INSN
625 && any_condjump_p (bb->end)
626 && bb->succ->succ_next != NULL)
627 combine_predictions_for_insn (bb->end, bb);
629 free_dominance_info (post_dominators);
630 free_dominance_info (dominators);
632 remove_fake_edges ();
633 estimate_bb_frequencies (loops_info);
636 /* __builtin_expect dropped tokens into the insn stream describing expected
637 values of registers. Generate branch probabilities based off these
638 values. */
640 void
641 expected_value_to_br_prob ()
643 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
645 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
647 switch (GET_CODE (insn))
649 case NOTE:
650 /* Look for expected value notes. */
651 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
653 ev = NOTE_EXPECTED_VALUE (insn);
654 ev_reg = XEXP (ev, 0);
655 delete_insn (insn);
657 continue;
659 case CODE_LABEL:
660 /* Never propagate across labels. */
661 ev = NULL_RTX;
662 continue;
664 case JUMP_INSN:
665 /* Look for simple conditional branches. If we haven't got an
666 expected value yet, no point going further. */
667 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX
668 || ! any_condjump_p (insn))
669 continue;
670 break;
672 default:
673 /* Look for insns that clobber the EV register. */
674 if (ev && reg_set_p (ev_reg, insn))
675 ev = NULL_RTX;
676 continue;
679 /* Collect the branch condition, hopefully relative to EV_REG. */
680 /* ??? At present we'll miss things like
681 (expected_value (eq r70 0))
682 (set r71 -1)
683 (set r80 (lt r70 r71))
684 (set pc (if_then_else (ne r80 0) ...))
685 as canonicalize_condition will render this to us as
686 (lt r70, r71)
687 Could use cselib to try and reduce this further. */
688 cond = XEXP (SET_SRC (pc_set (insn)), 0);
689 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg);
690 if (! cond || XEXP (cond, 0) != ev_reg
691 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
692 continue;
694 /* Substitute and simplify. Given that the expression we're
695 building involves two constants, we should wind up with either
696 true or false. */
697 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
698 XEXP (ev, 1), XEXP (cond, 1));
699 cond = simplify_rtx (cond);
701 /* Turn the condition into a scaled branch probability. */
702 if (cond != const_true_rtx && cond != const0_rtx)
703 abort ();
704 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
705 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
709 /* Check whether this is the last basic block of function. Commonly tehre
710 is one extra common cleanup block. */
711 static bool
712 last_basic_block_p (bb)
713 basic_block bb;
715 if (bb == EXIT_BLOCK_PTR)
716 return false;
718 return (bb->next_bb == EXIT_BLOCK_PTR
719 || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
720 && bb->succ && !bb->succ->succ_next
721 && bb->succ->dest->next_bb == EXIT_BLOCK_PTR));
724 /* Sets branch probabilities according to PREDiction and FLAGS. HEADS[bb->index]
725 should be index of basic block in that we need to alter branch predictions
726 (i.e. the first of our dominators such that we do not post-dominate it)
727 (but we fill this information on demand, so -1 may be there in case this
728 was not needed yet). */
730 static void
731 process_note_prediction (bb, heads, dominators, post_dominators, pred, flags)
732 basic_block bb;
733 int *heads;
734 dominance_info dominators;
735 dominance_info post_dominators;
736 int pred;
737 int flags;
739 edge e;
740 int y;
741 bool taken;
743 taken = flags & IS_TAKEN;
745 if (heads[bb->index] < 0)
747 /* This is first time we need this field in heads array; so
748 find first dominator that we do not post-dominate (we are
749 using already known members of heads array). */
750 basic_block ai = bb;
751 basic_block next_ai = get_immediate_dominator (dominators, bb);
752 int head;
754 while (heads[next_ai->index] < 0)
756 if (!dominated_by_p (post_dominators, next_ai, bb))
757 break;
758 heads[next_ai->index] = ai->index;
759 ai = next_ai;
760 next_ai = get_immediate_dominator (dominators, next_ai);
762 if (!dominated_by_p (post_dominators, next_ai, bb))
763 head = next_ai->index;
764 else
765 head = heads[next_ai->index];
766 while (next_ai != bb)
768 next_ai = ai;
769 if (heads[ai->index] == ENTRY_BLOCK)
770 ai = ENTRY_BLOCK_PTR;
771 else
772 ai = BASIC_BLOCK (heads[ai->index]);
773 heads[next_ai->index] = head;
776 y = heads[bb->index];
778 /* Now find the edge that leads to our branch and aply the prediction. */
780 if (y == last_basic_block || !can_predict_insn_p (BASIC_BLOCK (y)->end))
781 return;
782 for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next)
783 if (e->dest->index >= 0
784 && dominated_by_p (post_dominators, e->dest, bb))
785 predict_edge_def (e, pred, taken);
788 /* Gathers NOTE_INSN_PREDICTIONs in given basic block and turns them
789 into branch probabilities. For description of heads array, see
790 process_note_prediction. */
792 static void
793 process_note_predictions (bb, heads, dominators, post_dominators)
794 basic_block bb;
795 int *heads;
796 dominance_info dominators;
797 dominance_info post_dominators;
799 rtx insn;
800 edge e;
802 /* Additionaly, we check here for blocks with no successors. */
803 int contained_noreturn_call = 0;
804 int was_bb_head = 0;
805 int noreturn_block = 1;
807 for (insn = bb->end; insn;
808 was_bb_head |= (insn == bb->head), insn = PREV_INSN (insn))
810 if (GET_CODE (insn) != NOTE)
812 if (was_bb_head)
813 break;
814 else
816 /* Noreturn calls cause program to exit, therefore they are
817 always predicted as not taken. */
818 if (GET_CODE (insn) == CALL_INSN
819 && find_reg_note (insn, REG_NORETURN, NULL))
820 contained_noreturn_call = 1;
821 continue;
824 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION)
826 int alg = (int) NOTE_PREDICTION_ALG (insn);
827 /* Process single prediction note. */
828 process_note_prediction (bb,
829 heads,
830 dominators,
831 post_dominators,
832 alg, (int) NOTE_PREDICTION_FLAGS (insn));
833 delete_insn (insn);
836 for (e = bb->succ; e; e = e->succ_next)
837 if (!(e->flags & EDGE_FAKE))
838 noreturn_block = 0;
839 if (contained_noreturn_call)
841 /* This block ended from other reasons than because of return.
842 If it is because of noreturn call, this should certainly not
843 be taken. Otherwise it is probably some error recovery. */
844 process_note_prediction (bb,
845 heads,
846 dominators,
847 post_dominators, PRED_NORETURN, NOT_TAKEN);
851 /* Gathers NOTE_INSN_PREDICTIONs and turns them into
852 branch probabilities. */
854 void
855 note_prediction_to_br_prob ()
857 basic_block bb;
858 dominance_info post_dominators, dominators;
859 int *heads;
861 /* To enable handling of noreturn blocks. */
862 add_noreturn_fake_exit_edges ();
863 connect_infinite_loops_to_exit ();
865 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
866 dominators = calculate_dominance_info (CDI_DOMINATORS);
868 heads = xmalloc (sizeof (int) * last_basic_block);
869 memset (heads, -1, sizeof (int) * last_basic_block);
870 heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block;
872 /* Process all prediction notes. */
874 FOR_EACH_BB (bb)
875 process_note_predictions (bb, heads, dominators, post_dominators);
877 free_dominance_info (post_dominators);
878 free_dominance_info (dominators);
879 free (heads);
881 remove_fake_edges ();
884 /* This is used to carry information about basic blocks. It is
885 attached to the AUX field of the standard CFG block. */
887 typedef struct block_info_def
889 /* Estimated frequency of execution of basic_block. */
890 REAL_VALUE_TYPE frequency;
892 /* To keep queue of basic blocks to process. */
893 basic_block next;
895 /* True if block needs to be visited in prop_freqency. */
896 int tovisit:1;
898 /* Number of predecessors we need to visit first. */
899 int npredecessors;
900 } *block_info;
902 /* Similar information for edges. */
903 typedef struct edge_info_def
905 /* In case edge is an loopback edge, the probability edge will be reached
906 in case header is. Estimated number of iterations of the loop can be
907 then computed as 1 / (1 - back_edge_prob). */
908 REAL_VALUE_TYPE back_edge_prob;
909 /* True if the edge is an loopback edge in the natural loop. */
910 int back_edge:1;
911 } *edge_info;
913 #define BLOCK_INFO(B) ((block_info) (B)->aux)
914 #define EDGE_INFO(E) ((edge_info) (E)->aux)
916 /* Helper function for estimate_bb_frequencies.
917 Propagate the frequencies for LOOP. */
919 static void
920 propagate_freq (loop)
921 struct loop *loop;
923 basic_block head = loop->header;
924 basic_block bb;
925 basic_block last;
926 edge e;
927 basic_block nextbb;
929 /* For each basic block we need to visit count number of his predecessors
930 we need to visit first. */
931 FOR_EACH_BB (bb)
933 if (BLOCK_INFO (bb)->tovisit)
935 int count = 0;
937 for (e = bb->pred; e; e = e->pred_next)
938 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
939 count++;
940 else if (BLOCK_INFO (e->src)->tovisit
941 && rtl_dump_file && !EDGE_INFO (e)->back_edge)
942 fprintf (rtl_dump_file,
943 "Irreducible region hit, ignoring edge to %i->%i\n",
944 e->src->index, bb->index);
945 BLOCK_INFO (bb)->npredecessors = count;
949 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
950 last = head;
951 for (bb = head; bb; bb = nextbb)
953 REAL_VALUE_TYPE cyclic_probability, frequency;
955 memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
956 memcpy (&frequency, &real_zero, sizeof (real_zero));
958 nextbb = BLOCK_INFO (bb)->next;
959 BLOCK_INFO (bb)->next = NULL;
961 /* Compute frequency of basic block. */
962 if (bb != head)
964 #ifdef ENABLE_CHECKING
965 for (e = bb->pred; e; e = e->pred_next)
966 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
967 abort ();
968 #endif
970 for (e = bb->pred; e; e = e->pred_next)
971 if (EDGE_INFO (e)->back_edge)
973 REAL_ARITHMETIC (cyclic_probability, PLUS_EXPR,
974 cyclic_probability,
975 EDGE_INFO (e)->back_edge_prob);
977 else if (!(e->flags & EDGE_DFS_BACK))
979 REAL_VALUE_TYPE tmp;
981 /* frequency += (e->probability
982 * BLOCK_INFO (e->src)->frequency /
983 REG_BR_PROB_BASE); */
985 REAL_VALUE_FROM_INT (tmp, e->probability, 0,
986 TYPE_MODE (double_type_node));
987 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp,
988 BLOCK_INFO (e->src)->frequency);
989 REAL_ARITHMETIC (tmp, RDIV_EXPR, tmp, real_br_prob_base);
990 REAL_ARITHMETIC (frequency, PLUS_EXPR, frequency, tmp);
993 if (REAL_VALUES_LESS (real_almost_one, cyclic_probability))
994 memcpy (&cyclic_probability, &real_almost_one, sizeof (real_zero));
996 /* BLOCK_INFO (bb)->frequency = frequency / (1 - cyclic_probability)
999 REAL_ARITHMETIC (cyclic_probability, MINUS_EXPR, real_one,
1000 cyclic_probability);
1001 REAL_ARITHMETIC (BLOCK_INFO (bb)->frequency,
1002 RDIV_EXPR, frequency, cyclic_probability);
1005 BLOCK_INFO (bb)->tovisit = 0;
1007 /* Compute back edge frequencies. */
1008 for (e = bb->succ; e; e = e->succ_next)
1009 if (e->dest == head)
1011 REAL_VALUE_TYPE tmp;
1013 /* EDGE_INFO (e)->back_edge_prob
1014 = ((e->probability * BLOCK_INFO (bb)->frequency)
1015 / REG_BR_PROB_BASE); */
1016 REAL_VALUE_FROM_INT (tmp, e->probability, 0,
1017 TYPE_MODE (double_type_node));
1018 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp,
1019 BLOCK_INFO (bb)->frequency);
1020 REAL_ARITHMETIC (EDGE_INFO (e)->back_edge_prob,
1021 RDIV_EXPR, tmp, real_br_prob_base);
1025 /* Propagate to successor blocks. */
1026 for (e = bb->succ; e; e = e->succ_next)
1027 if (!(e->flags & EDGE_DFS_BACK)
1028 && BLOCK_INFO (e->dest)->npredecessors)
1030 BLOCK_INFO (e->dest)->npredecessors--;
1031 if (!BLOCK_INFO (e->dest)->npredecessors)
1033 if (!nextbb)
1034 nextbb = e->dest;
1035 else
1036 BLOCK_INFO (last)->next = e->dest;
1038 last = e->dest;
1044 /* Estimate probabilities of loopback edges in loops at same nest level. */
1046 static void
1047 estimate_loops_at_level (first_loop)
1048 struct loop *first_loop;
1050 struct loop *loop;
1052 for (loop = first_loop; loop; loop = loop->next)
1054 edge e;
1055 basic_block *bbs;
1056 int i;
1058 estimate_loops_at_level (loop->inner);
1060 if (loop->latch->succ) /* Do not do this for dummy function loop. */
1062 /* Find current loop back edge and mark it. */
1063 e = loop_latch_edge (loop);
1064 EDGE_INFO (e)->back_edge = 1;
1067 bbs = get_loop_body (loop);
1068 for (i = 0; i < loop->num_nodes; i++)
1069 BLOCK_INFO (bbs[i])->tovisit = 1;
1070 free (bbs);
1071 propagate_freq (loop);
1075 /* Convert counts measured by profile driven feedback to frequencies. */
1077 static void
1078 counts_to_freqs ()
1080 HOST_WIDEST_INT count_max = 1;
1081 basic_block bb;
1083 FOR_EACH_BB (bb)
1084 count_max = MAX (bb->count, count_max);
1086 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1087 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
1090 /* Return true if function is likely to be expensive, so there is no point to
1091 optimize performance of prologue, epilogue or do inlining at the expense
1092 of code size growth. THRESHOLD is the limit of number of isntructions
1093 function can execute at average to be still considered not expensive. */
1095 bool
1096 expensive_function_p (threshold)
1097 int threshold;
1099 unsigned int sum = 0;
1100 basic_block bb;
1101 unsigned int limit;
1103 /* We can not compute accurately for large thresholds due to scaled
1104 frequencies. */
1105 if (threshold > BB_FREQ_MAX)
1106 abort ();
1108 /* Frequencies are out of range. This either means that function contains
1109 internal loop executing more than BB_FREQ_MAX times or profile feedback
1110 is available and function has not been executed at all. */
1111 if (ENTRY_BLOCK_PTR->frequency == 0)
1112 return true;
1114 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
1115 limit = ENTRY_BLOCK_PTR->frequency * threshold;
1116 FOR_EACH_BB (bb)
1118 rtx insn;
1120 for (insn = bb->head; insn != NEXT_INSN (bb->end);
1121 insn = NEXT_INSN (insn))
1122 if (active_insn_p (insn))
1124 sum += bb->frequency;
1125 if (sum > limit)
1126 return true;
1130 return false;
1133 /* Estimate basic blocks frequency by given branch probabilities. */
1135 static void
1136 estimate_bb_frequencies (loops)
1137 struct loops *loops;
1139 basic_block bb;
1140 REAL_VALUE_TYPE freq_max;
1141 enum machine_mode double_mode = TYPE_MODE (double_type_node);
1143 if (flag_branch_probabilities)
1144 counts_to_freqs ();
1145 else
1147 REAL_VALUE_FROM_INT (real_zero, 0, 0, double_mode);
1148 REAL_VALUE_FROM_INT (real_one, 1, 0, double_mode);
1149 REAL_VALUE_FROM_INT (real_br_prob_base, REG_BR_PROB_BASE, 0, double_mode);
1150 REAL_VALUE_FROM_INT (real_bb_freq_max, BB_FREQ_MAX, 0, double_mode);
1151 REAL_VALUE_FROM_INT (real_one_half, 2, 0, double_mode);
1153 REAL_ARITHMETIC (real_one_half, RDIV_EXPR, real_one, real_one_half);
1155 REAL_ARITHMETIC (real_almost_one, RDIV_EXPR, real_one, real_br_prob_base);
1156 REAL_ARITHMETIC (real_almost_one, MINUS_EXPR, real_one, real_almost_one);
1158 mark_dfs_back_edges ();
1159 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
1160 notes. */
1161 FOR_EACH_BB (bb)
1163 rtx last_insn = bb->end;
1165 if (!can_predict_insn_p (last_insn))
1167 /* We can predict only conditional jumps at the moment.
1168 Expect each edge to be equally probable.
1169 ?? In the future we want to make abnormal edges improbable. */
1170 int nedges = 0;
1171 edge e;
1173 for (e = bb->succ; e; e = e->succ_next)
1175 nedges++;
1176 if (e->probability != 0)
1177 break;
1179 if (!e)
1180 for (e = bb->succ; e; e = e->succ_next)
1181 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
1185 ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
1187 /* Set up block info for each basic block. */
1188 alloc_aux_for_blocks (sizeof (struct block_info_def));
1189 alloc_aux_for_edges (sizeof (struct edge_info_def));
1190 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1192 edge e;
1194 BLOCK_INFO (bb)->tovisit = 0;
1195 for (e = bb->succ; e; e = e->succ_next)
1197 REAL_VALUE_FROM_INT (EDGE_INFO (e)->back_edge_prob,
1198 e->probability, 0, double_mode);
1199 REAL_ARITHMETIC (EDGE_INFO (e)->back_edge_prob,
1200 RDIV_EXPR, EDGE_INFO (e)->back_edge_prob,
1201 real_br_prob_base);
1205 /* First compute probabilities locally for each loop from innermost
1206 to outermost to examine probabilities for back edges. */
1207 estimate_loops_at_level (loops->tree_root);
1209 memcpy (&freq_max, &real_zero, sizeof (real_zero));
1210 FOR_EACH_BB (bb)
1211 if (REAL_VALUES_LESS
1212 (freq_max, BLOCK_INFO (bb)->frequency))
1213 memcpy (&freq_max, &BLOCK_INFO (bb)->frequency,
1214 sizeof (freq_max));
1216 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1218 REAL_VALUE_TYPE tmp;
1220 REAL_ARITHMETIC (tmp, MULT_EXPR, BLOCK_INFO (bb)->frequency,
1221 real_bb_freq_max);
1222 REAL_ARITHMETIC (tmp, RDIV_EXPR, tmp, freq_max);
1223 REAL_ARITHMETIC (tmp, PLUS_EXPR, tmp, real_one_half);
1224 bb->frequency = REAL_VALUE_UNSIGNED_FIX (tmp);
1227 free_aux_for_blocks ();
1228 free_aux_for_edges ();
1230 compute_function_frequency ();
1231 if (flag_reorder_functions)
1232 choose_function_section ();
1235 /* Decide whether function is hot, cold or unlikely executed. */
1236 static void
1237 compute_function_frequency ()
1239 basic_block bb;
1241 if (!profile_info.count_profiles_merged
1242 || !flag_branch_probabilities)
1243 return;
1244 cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
1245 FOR_EACH_BB (bb)
1247 if (maybe_hot_bb_p (bb))
1249 cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
1250 return;
1252 if (!probably_never_executed_bb_p (bb))
1253 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
1257 /* Choose appropriate section for the function. */
1258 static void
1259 choose_function_section ()
1261 if (DECL_SECTION_NAME (current_function_decl)
1262 || !targetm.have_named_sections
1263 /* Theoretically we can split the gnu.linkonce text section too,
1264 but this requires more work as the frequency needs to match
1265 for all generated objects so we need to merge the frequency
1266 of all instances. For now just never set frequency for these. */
1267 || !DECL_ONE_ONLY (current_function_decl))
1268 return;
1269 if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
1270 DECL_SECTION_NAME (current_function_decl) =
1271 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
1272 if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1273 DECL_SECTION_NAME (current_function_decl) =
1274 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
1275 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);