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
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
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
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. */
36 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "insn-config.h"
54 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, 0.5,
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
,
67 static void combine_predictions_for_insn
PARAMS ((rtx
, basic_block
));
68 static void dump_prediction
PARAMS ((enum br_predictor
, 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 *,
77 static void process_note_prediction
PARAMS ((basic_block
, int *,
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. */
90 const char *const name
; /* Name used in the debugging dumps. */
91 const int hitrate
; /* Expected hitrate used by
92 predict_insn_def call. */
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. */
113 /* Return true in case BB can be CPU intensive and should be optimized
114 for maximal perofmrance. */
120 if (profile_info
.count_profiles_merged
121 && flag_branch_probabilities
123 < profile_info
.max_counter_in_program
124 / PARAM_VALUE (HOT_BB_COUNT_FRACTION
)))
126 if (bb
->frequency
< BB_FREQ_MAX
/ PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
))
131 /* Return true in case BB is cold and should be optimized for size. */
134 probably_cold_bb_p (bb
)
137 if (profile_info
.count_profiles_merged
138 && flag_branch_probabilities
140 < profile_info
.max_counter_in_program
141 / PARAM_VALUE (HOT_BB_COUNT_FRACTION
)))
143 if (bb
->frequency
< BB_FREQ_MAX
/ PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
))
148 /* Return true in case BB is probably never executed. */
150 probably_never_executed_bb_p (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;
160 /* Return true if the one of outgoing edges is already predicted by
164 predicted_by_p (bb
, predictor
)
166 enum br_predictor predictor
;
169 if (!INSN_P (bb
->end
))
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
)
179 predict_insn (insn
, predictor
, probability
)
182 enum br_predictor predictor
;
184 if (!any_condjump_p (insn
))
188 = gen_rtx_EXPR_LIST (REG_BR_PRED
,
189 gen_rtx_CONCAT (VOIDmode
,
190 GEN_INT ((int) predictor
),
191 GEN_INT ((int) probability
)),
195 /* Predict insn by given predictor. */
198 predict_insn_def (insn
, predictor
, taken
)
200 enum br_predictor predictor
;
201 enum prediction taken
;
203 int probability
= predictor_info
[(int) predictor
].hitrate
;
206 probability
= REG_BR_PROB_BASE
- probability
;
208 predict_insn (insn
, predictor
, probability
);
211 /* Predict edge E with given probability if possible. */
214 predict_edge (e
, predictor
, probability
)
217 enum br_predictor predictor
;
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
))
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. */
238 can_predict_insn_p (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. */
249 predict_edge_def (e
, predictor
, taken
)
251 enum br_predictor predictor
;
252 enum prediction taken
;
254 int probability
= predictor_info
[(int) predictor
].hitrate
;
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. */
266 invert_br_probabilities (insn
)
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. */
282 dump_prediction (predictor
, probability
, bb
, used
)
283 enum br_predictor predictor
;
293 while (e
&& (e
->flags
& EDGE_FALLTHRU
))
296 fprintf (rtl_dump_file
, " %s heuristics%s: %.1f%%",
297 predictor_info
[predictor
].name
,
298 used
? "" : " (ignored)", probability
* 100.0 / REG_BR_PROB_BASE
);
302 fprintf (rtl_dump_file
, " exec ");
303 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
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. */
319 combine_predictions_for_insn (insn
, bb
)
323 rtx prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
324 rtx
*pnote
= ®_NOTES (insn
);
326 int best_probability
= PROB_EVEN
;
327 int best_predictor
= END_PREDICTORS
;
328 int combined_probability
= REG_BR_PROB_BASE
/ 2;
330 bool first_match
= false;
334 fprintf (rtl_dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
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));
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. */
356 /* If one probability is 0% and one 100%, avoid division by zero. */
357 combined_probability
= REG_BR_PROB_BASE
/ 2;
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
)
371 dump_prediction (PRED_NO_PREDICTION
, combined_probability
, bb
, true);
374 dump_prediction (PRED_DS_THEORY
, combined_probability
, bb
, !first_match
);
375 dump_prediction (PRED_FIRST_MATCH
, best_probability
, bb
, first_match
);
379 combined_probability
= best_probability
;
380 dump_prediction (PRED_COMBINED
, combined_probability
, bb
, true);
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);
394 pnote
= &XEXP (*pnote
, 1);
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
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
421 estimate_probability (loops_info
)
422 struct loops
*loops_info
;
424 dominance_info dominators
, post_dominators
;
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
434 for (i
= 1; i
< loops_info
->num
; i
++)
436 basic_block bb
, *bbs
;
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;
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
456 if (!can_predict_insn_p (bb
->end
)
457 || predicted_by_p (bb
, PRED_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
)
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. */
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
))
479 - predictor_info
[(int) PRED_LOOP_EXIT
].hitrate
)
484 /* Attempt to predict conditional jumps using a number of heuristics. */
487 rtx last_insn
= bb
->end
;
491 if (! can_predict_insn_p (last_insn
))
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
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
))
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
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
);
533 cond
= get_condition (last_insn
, &earliest
);
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
);
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
))
558 /* Unconditional branch. */
559 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
560 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
565 /* Floating point comparisons appears to behave in a very
566 inpredictable way because of special role of = tests in
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
)
576 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
581 /* Floating point comparisons appears to behave in a very
582 inpredictable way because of special role of = tests in
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
)
592 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
596 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
600 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
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
);
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
);
622 /* Attach the combined probability to each conditional jump. */
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
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
))
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);
660 /* Never propagate across labels. */
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
))
673 /* Look for insns that clobber the EV register. */
674 if (ev
&& reg_set_p (ev_reg
, insn
))
679 /* Collect the branch condition, hopefully relative to EV_REG. */
680 /* ??? At present we'll miss things like
681 (expected_value (eq r70 0))
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
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
)
694 /* Substitute and simplify. Given that the expression we're
695 building involves two constants, we should wind up with either
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
)
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. */
712 last_basic_block_p (bb
)
715 if (bb
== EXIT_BLOCK_PTR
)
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). */
731 process_note_prediction (bb
, heads
, dominators
, post_dominators
, pred
, flags
)
734 dominance_info dominators
;
735 dominance_info post_dominators
;
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). */
751 basic_block next_ai
= get_immediate_dominator (dominators
, bb
);
754 while (heads
[next_ai
->index
] < 0)
756 if (!dominated_by_p (post_dominators
, next_ai
, bb
))
758 heads
[next_ai
->index
] = ai
->index
;
760 next_ai
= get_immediate_dominator (dominators
, next_ai
);
762 if (!dominated_by_p (post_dominators
, next_ai
, bb
))
763 head
= next_ai
->index
;
765 head
= heads
[next_ai
->index
];
766 while (next_ai
!= bb
)
769 if (heads
[ai
->index
] == ENTRY_BLOCK
)
770 ai
= ENTRY_BLOCK_PTR
;
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
))
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. */
793 process_note_predictions (bb
, heads
, dominators
, post_dominators
)
796 dominance_info dominators
;
797 dominance_info post_dominators
;
802 /* Additionaly, we check here for blocks with no successors. */
803 int contained_noreturn_call
= 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
)
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;
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
,
832 alg
, (int) NOTE_PREDICTION_FLAGS (insn
));
836 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
837 if (!(e
->flags
& EDGE_FAKE
))
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
,
847 post_dominators
, PRED_NORETURN
, NOT_TAKEN
);
851 /* Gathers NOTE_INSN_PREDICTIONs and turns them into
852 branch probabilities. */
855 note_prediction_to_br_prob ()
858 dominance_info post_dominators
, dominators
;
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. */
875 process_note_predictions (bb
, heads
, dominators
, post_dominators
);
877 free_dominance_info (post_dominators
);
878 free_dominance_info (dominators
);
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. */
895 /* True if block needs to be visited in prop_freqency. */
898 /* Number of predecessors we need to visit first. */
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. */
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. */
920 propagate_freq (loop
)
923 basic_block head
= loop
->header
;
929 /* For each basic block we need to visit count number of his predecessors
930 we need to visit first. */
933 if (BLOCK_INFO (bb
)->tovisit
)
937 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
938 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
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
));
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. */
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
))
970 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
971 if (EDGE_INFO (e
)->back_edge
)
973 REAL_ARITHMETIC (cyclic_probability
, PLUS_EXPR
,
975 EDGE_INFO (e
)->back_edge_prob
);
977 else if (!(e
->flags
& EDGE_DFS_BACK
))
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
)
1036 BLOCK_INFO (last
)->next
= e
->dest
;
1044 /* Estimate probabilities of loopback edges in loops at same nest level. */
1047 estimate_loops_at_level (first_loop
)
1048 struct loop
*first_loop
;
1052 for (loop
= first_loop
; loop
; loop
= loop
->next
)
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;
1071 propagate_freq (loop
);
1075 /* Convert counts measured by profile driven feedback to frequencies. */
1080 HOST_WIDEST_INT count_max
= 1;
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. */
1096 expensive_function_p (threshold
)
1099 unsigned int sum
= 0;
1103 /* We can not compute accurately for large thresholds due to scaled
1105 if (threshold
> BB_FREQ_MAX
)
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)
1114 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
1115 limit
= ENTRY_BLOCK_PTR
->frequency
* threshold
;
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
;
1133 /* Estimate basic blocks frequency by given branch probabilities. */
1136 estimate_bb_frequencies (loops
)
1137 struct loops
*loops
;
1140 REAL_VALUE_TYPE freq_max
;
1141 enum machine_mode double_mode
= TYPE_MODE (double_type_node
);
1143 if (flag_branch_probabilities
)
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
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. */
1173 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1176 if (e
->probability
!= 0)
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
)
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
,
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
));
1211 if (REAL_VALUES_LESS
1212 (freq_max
, BLOCK_INFO (bb
)->frequency
))
1213 memcpy (&freq_max
, &BLOCK_INFO (bb
)->frequency
,
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
,
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. */
1237 compute_function_frequency ()
1241 if (!profile_info
.count_profiles_merged
1242 || !flag_branch_probabilities
)
1244 cfun
->function_frequency
= FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
;
1247 if (maybe_hot_bb_p (bb
))
1249 cfun
->function_frequency
= FUNCTION_FREQUENCY_HOT
;
1252 if (!probably_never_executed_bb_p (bb
))
1253 cfun
->function_frequency
= FUNCTION_FREQUENCY_NORMAL
;
1257 /* Choose appropriate section for the function. */
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
))
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
);