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"
50 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, 0.5,
52 static REAL_VALUE_TYPE real_zero
, real_one
, real_almost_one
, real_br_prob_base
,
53 real_one_half
, real_bb_freq_max
;
55 /* Random guesstimation given names. */
56 #define PROB_NEVER (0)
57 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
58 #define PROB_UNLIKELY (REG_BR_PROB_BASE * 4 / 10 - 1)
59 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
60 #define PROB_LIKELY (REG_BR_PROB_BASE - PROB_UNLIKELY)
61 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
62 #define PROB_ALWAYS (REG_BR_PROB_BASE)
64 static void combine_predictions_for_insn
PARAMS ((rtx
, basic_block
));
65 static void dump_prediction
PARAMS ((enum br_predictor
, int,
67 static void estimate_loops_at_level
PARAMS ((struct loop
*loop
));
68 static void propagate_freq
PARAMS ((basic_block
));
69 static void estimate_bb_frequencies
PARAMS ((struct loops
*));
70 static void counts_to_freqs
PARAMS ((void));
72 /* Information we hold about each branch predictor.
73 Filled using information from predict.def. */
77 const char *const name
; /* Name used in the debugging dumps. */
78 const int hitrate
; /* Expected hitrate used by
79 predict_insn_def call. */
83 /* Use given predictor without Dempster-Shaffer theory if it matches
84 using first_match heuristics. */
85 #define PRED_FLAG_FIRST_MATCH 1
87 /* Recompute hitrate in percent to our representation. */
89 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
91 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
92 static const struct predictor_info predictor_info
[]= {
93 #include "predict.def"
95 /* Upper bound on predictors. */
101 predict_insn (insn
, predictor
, probability
)
104 enum br_predictor predictor
;
106 if (!any_condjump_p (insn
))
110 = gen_rtx_EXPR_LIST (REG_BR_PRED
,
111 gen_rtx_CONCAT (VOIDmode
,
112 GEN_INT ((int) predictor
),
113 GEN_INT ((int) probability
)),
117 /* Predict insn by given predictor. */
120 predict_insn_def (insn
, predictor
, taken
)
122 enum br_predictor predictor
;
123 enum prediction taken
;
125 int probability
= predictor_info
[(int) predictor
].hitrate
;
128 probability
= REG_BR_PROB_BASE
- probability
;
130 predict_insn (insn
, predictor
, probability
);
133 /* Predict edge E with given probability if possible. */
136 predict_edge (e
, predictor
, probability
)
139 enum br_predictor predictor
;
142 last_insn
= e
->src
->end
;
144 /* We can store the branch prediction information only about
145 conditional jumps. */
146 if (!any_condjump_p (last_insn
))
149 /* We always store probability of branching. */
150 if (e
->flags
& EDGE_FALLTHRU
)
151 probability
= REG_BR_PROB_BASE
- probability
;
153 predict_insn (last_insn
, predictor
, probability
);
156 /* Predict edge E by given predictor if possible. */
159 predict_edge_def (e
, predictor
, taken
)
161 enum br_predictor predictor
;
162 enum prediction taken
;
164 int probability
= predictor_info
[(int) predictor
].hitrate
;
167 probability
= REG_BR_PROB_BASE
- probability
;
169 predict_edge (e
, predictor
, probability
);
172 /* Invert all branch predictions or probability notes in the INSN. This needs
173 to be done each time we invert the condition used by the jump. */
176 invert_br_probabilities (insn
)
181 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
182 if (REG_NOTE_KIND (note
) == REG_BR_PROB
)
183 XEXP (note
, 0) = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (note
, 0)));
184 else if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
185 XEXP (XEXP (note
, 0), 1)
186 = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (XEXP (note
, 0), 1)));
189 /* Dump information about the branch prediction to the output file. */
192 dump_prediction (predictor
, probability
, bb
, used
)
193 enum br_predictor predictor
;
203 while (e
&& (e
->flags
& EDGE_FALLTHRU
))
206 fprintf (rtl_dump_file
, " %s heuristics%s: %.1f%%",
207 predictor_info
[predictor
].name
,
208 used
? "" : " (ignored)", probability
* 100.0 / REG_BR_PROB_BASE
);
212 fprintf (rtl_dump_file
, " exec ");
213 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
216 fprintf (rtl_dump_file
, " hit ");
217 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
218 fprintf (rtl_dump_file
, " (%.1f%%)", e
->count
* 100.0 / bb
->count
);
222 fprintf (rtl_dump_file
, "\n");
225 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
226 note if not already present. Remove now useless REG_BR_PRED notes. */
229 combine_predictions_for_insn (insn
, bb
)
233 rtx prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
234 rtx
*pnote
= ®_NOTES (insn
);
236 int best_probability
= PROB_EVEN
;
237 int best_predictor
= END_PREDICTORS
;
238 int combined_probability
= REG_BR_PROB_BASE
/ 2;
240 bool first_match
= false;
244 fprintf (rtl_dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
247 /* We implement "first match" heuristics and use probability guessed
248 by predictor with smallest index. In the future we will use better
249 probability combination techniques. */
250 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
251 if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
253 int predictor
= INTVAL (XEXP (XEXP (note
, 0), 0));
254 int probability
= INTVAL (XEXP (XEXP (note
, 0), 1));
257 if (best_predictor
> predictor
)
258 best_probability
= probability
, best_predictor
= predictor
;
260 d
= (combined_probability
* probability
261 + (REG_BR_PROB_BASE
- combined_probability
)
262 * (REG_BR_PROB_BASE
- probability
));
264 /* Use FP math to avoid overflows of 32bit integers. */
266 /* If one probability is 0% and one 100%, avoid division by zero. */
267 combined_probability
= REG_BR_PROB_BASE
/ 2;
269 combined_probability
= (((double) combined_probability
) * probability
270 * REG_BR_PROB_BASE
/ d
+ 0.5);
273 /* Decide which heuristic to use. In case we didn't match anything,
274 use no_prediction heuristic, in case we did match, use either
275 first match or Dempster-Shaffer theory depending on the flags. */
277 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
281 dump_prediction (PRED_NO_PREDICTION
, combined_probability
, bb
, true);
284 dump_prediction (PRED_DS_THEORY
, combined_probability
, bb
, !first_match
);
285 dump_prediction (PRED_FIRST_MATCH
, best_probability
, bb
, first_match
);
289 combined_probability
= best_probability
;
290 dump_prediction (PRED_COMBINED
, combined_probability
, bb
, true);
294 if (REG_NOTE_KIND (*pnote
) == REG_BR_PRED
)
296 int predictor
= INTVAL (XEXP (XEXP (*pnote
, 0), 0));
297 int probability
= INTVAL (XEXP (XEXP (*pnote
, 0), 1));
299 dump_prediction (predictor
, probability
, bb
,
300 !first_match
|| best_predictor
== predictor
);
301 *pnote
= XEXP (*pnote
, 1);
304 pnote
= &XEXP (*pnote
, 1);
310 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
311 GEN_INT (combined_probability
), REG_NOTES (insn
));
313 /* Save the prediction into CFG in case we are seeing non-degenerated
315 if (bb
->succ
->succ_next
)
317 BRANCH_EDGE (bb
)->probability
= combined_probability
;
318 FALLTHRU_EDGE (bb
)->probability
319 = REG_BR_PROB_BASE
- combined_probability
;
324 /* Statically estimate the probability that a branch will be taken.
325 ??? In the next revision there will be a number of other predictors added
326 from the above references. Further, each heuristic will be factored out
327 into its own function for clarity (and to facilitate the combination of
331 estimate_probability (loops_info
)
332 struct loops
*loops_info
;
334 sbitmap
*dominators
, *post_dominators
;
336 int found_noreturn
= 0;
338 dominators
= sbitmap_vector_alloc (n_basic_blocks
, n_basic_blocks
);
339 post_dominators
= sbitmap_vector_alloc (n_basic_blocks
, n_basic_blocks
);
340 calculate_dominance_info (NULL
, dominators
, CDI_DOMINATORS
);
341 calculate_dominance_info (NULL
, post_dominators
, CDI_POST_DOMINATORS
);
343 /* Try to predict out blocks in a loop that are not part of a
345 for (i
= 0; i
< loops_info
->num
; i
++)
349 struct loop
*loop
= &loops_info
->array
[i
];
351 flow_loop_scan (loops_info
, loop
, LOOP_EXIT_EDGES
);
352 exits
= loop
->num_exits
;
354 for (j
= loop
->first
->index
; j
<= loop
->last
->index
; ++j
)
355 if (TEST_BIT (loop
->nodes
, j
))
357 int header_found
= 0;
360 /* Loop branch heuristics - predict an edge back to a
361 loop's head as taken. */
362 for (e
= BASIC_BLOCK(j
)->succ
; e
; e
= e
->succ_next
)
363 if (e
->dest
== loop
->header
364 && e
->src
== loop
->latch
)
367 predict_edge_def (e
, PRED_LOOP_BRANCH
, TAKEN
);
370 /* Loop exit heuristics - predict an edge exiting the loop if the
371 conditinal has no loop header successors as not taken. */
373 for (e
= BASIC_BLOCK(j
)->succ
; e
; e
= e
->succ_next
)
374 if (e
->dest
->index
< 0
375 || !TEST_BIT (loop
->nodes
, e
->dest
->index
))
379 - predictor_info
[(int) PRED_LOOP_EXIT
].hitrate
)
384 /* Attempt to predict conditional jumps using a number of heuristics. */
385 for (i
= 0; i
< n_basic_blocks
; i
++)
387 basic_block bb
= BASIC_BLOCK (i
);
388 rtx last_insn
= bb
->end
;
392 /* If block has no successor, predict all possible paths to it as
393 improbable, as the block contains a call to a noreturn function and
394 thus can be executed only once. */
395 if (bb
->succ
== NULL
&& !found_noreturn
)
399 /* ??? Postdominator claims each noreturn block to be postdominated
400 by each, so we need to run only once. This needs to be changed
401 once postdominace algorithm is updated to say something more
404 for (y
= 0; y
< n_basic_blocks
; y
++)
405 if (!TEST_BIT (post_dominators
[y
], i
))
406 for (e
= BASIC_BLOCK (y
)->succ
; e
; e
= e
->succ_next
)
407 if (e
->dest
->index
>= 0
408 && TEST_BIT (post_dominators
[e
->dest
->index
], i
))
409 predict_edge_def (e
, PRED_NORETURN
, NOT_TAKEN
);
412 if (GET_CODE (last_insn
) != JUMP_INSN
|| ! any_condjump_p (last_insn
))
415 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
417 /* Predict edges to blocks that return immediately to be
418 improbable. These are usually used to signal error states. */
419 if (e
->dest
== EXIT_BLOCK_PTR
420 || (e
->dest
->succ
&& !e
->dest
->succ
->succ_next
421 && e
->dest
->succ
->dest
== EXIT_BLOCK_PTR
))
422 predict_edge_def (e
, PRED_ERROR_RETURN
, NOT_TAKEN
);
424 /* Look for block we are guarding (ie we dominate it,
425 but it doesn't postdominate us). */
426 if (e
->dest
!= EXIT_BLOCK_PTR
&& e
->dest
!= bb
427 && TEST_BIT (dominators
[e
->dest
->index
], e
->src
->index
)
428 && !TEST_BIT (post_dominators
[e
->src
->index
], e
->dest
->index
))
432 /* The call heuristic claims that a guarded function call
433 is improbable. This is because such calls are often used
434 to signal exceptional situations such as printing error
436 for (insn
= e
->dest
->head
; insn
!= NEXT_INSN (e
->dest
->end
);
437 insn
= NEXT_INSN (insn
))
438 if (GET_CODE (insn
) == CALL_INSN
439 /* Constant and pure calls are hardly used to signalize
440 something exceptional. */
441 && ! CONST_OR_PURE_CALL_P (insn
))
443 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
449 cond
= get_condition (last_insn
, &earliest
);
453 /* Try "pointer heuristic."
454 A comparison ptr == 0 is predicted as false.
455 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
456 if (GET_RTX_CLASS (GET_CODE (cond
)) == '<'
457 && ((REG_P (XEXP (cond
, 0)) && REG_POINTER (XEXP (cond
, 0)))
458 || (REG_P (XEXP (cond
, 1)) && REG_POINTER (XEXP (cond
, 1)))))
460 if (GET_CODE (cond
) == EQ
)
461 predict_insn_def (last_insn
, PRED_POINTER
, NOT_TAKEN
);
462 else if (GET_CODE (cond
) == NE
)
463 predict_insn_def (last_insn
, PRED_POINTER
, TAKEN
);
467 /* Try "opcode heuristic."
468 EQ tests are usually false and NE tests are usually true. Also,
469 most quantities are positive, so we can make the appropriate guesses
470 about signed comparisons against zero. */
471 switch (GET_CODE (cond
))
474 /* Unconditional branch. */
475 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
476 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
481 /* Floating point comparisons appears to behave in a very
482 inpredictable way because of special role of = tests in
484 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
486 /* Comparisons with 0 are often used for booleans and there is
487 nothing usefull to predict about them. */
488 else if (XEXP (cond
, 1) == const0_rtx
489 || XEXP (cond
, 0) == const0_rtx
)
492 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
497 /* Floating point comparisons appears to behave in a very
498 inpredictable way because of special role of = tests in
500 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
502 /* Comparisons with 0 are often used for booleans and there is
503 nothing usefull to predict about them. */
504 else if (XEXP (cond
, 1) == const0_rtx
505 || XEXP (cond
, 0) == const0_rtx
)
508 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
512 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
516 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
521 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
522 || XEXP (cond
, 1) == constm1_rtx
)
523 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, NOT_TAKEN
);
528 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
529 || XEXP (cond
, 1) == constm1_rtx
)
530 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, TAKEN
);
538 /* Attach the combined probability to each conditional jump. */
539 for (i
= 0; i
< n_basic_blocks
; i
++)
540 if (GET_CODE (BLOCK_END (i
)) == JUMP_INSN
541 && any_condjump_p (BLOCK_END (i
)))
542 combine_predictions_for_insn (BLOCK_END (i
), BASIC_BLOCK (i
));
544 sbitmap_vector_free (post_dominators
);
545 sbitmap_vector_free (dominators
);
547 estimate_bb_frequencies (loops_info
);
550 /* __builtin_expect dropped tokens into the insn stream describing expected
551 values of registers. Generate branch probabilities based off these
555 expected_value_to_br_prob ()
557 rtx insn
, cond
, ev
= NULL_RTX
, ev_reg
= NULL_RTX
;
559 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
561 switch (GET_CODE (insn
))
564 /* Look for expected value notes. */
565 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EXPECTED_VALUE
)
567 ev
= NOTE_EXPECTED_VALUE (insn
);
568 ev_reg
= XEXP (ev
, 0);
574 /* Never propagate across labels. */
579 /* Look for simple conditional branches. If we haven't got an
580 expected value yet, no point going further. */
581 if (GET_CODE (insn
) != JUMP_INSN
|| ev
== NULL_RTX
582 || ! any_condjump_p (insn
))
587 /* Look for insns that clobber the EV register. */
588 if (ev
&& reg_set_p (ev_reg
, insn
))
593 /* Collect the branch condition, hopefully relative to EV_REG. */
594 /* ??? At present we'll miss things like
595 (expected_value (eq r70 0))
597 (set r80 (lt r70 r71))
598 (set pc (if_then_else (ne r80 0) ...))
599 as canonicalize_condition will render this to us as
601 Could use cselib to try and reduce this further. */
602 cond
= XEXP (SET_SRC (pc_set (insn
)), 0);
603 cond
= canonicalize_condition (insn
, cond
, 0, NULL
, ev_reg
);
604 if (! cond
|| XEXP (cond
, 0) != ev_reg
605 || GET_CODE (XEXP (cond
, 1)) != CONST_INT
)
608 /* Substitute and simplify. Given that the expression we're
609 building involves two constants, we should wind up with either
611 cond
= gen_rtx_fmt_ee (GET_CODE (cond
), VOIDmode
,
612 XEXP (ev
, 1), XEXP (cond
, 1));
613 cond
= simplify_rtx (cond
);
615 /* Turn the condition into a scaled branch probability. */
616 if (cond
!= const_true_rtx
&& cond
!= const0_rtx
)
618 predict_insn_def (insn
, PRED_BUILTIN_EXPECT
,
619 cond
== const_true_rtx
? TAKEN
: NOT_TAKEN
);
623 /* This is used to carry information about basic blocks. It is
624 attached to the AUX field of the standard CFG block. */
626 typedef struct block_info_def
628 /* Estimated frequency of execution of basic_block. */
629 REAL_VALUE_TYPE frequency
;
631 /* To keep queue of basic blocks to process. */
634 /* True if block needs to be visited in prop_freqency. */
637 /* Number of predecessors we need to visit first. */
641 /* Similar information for edges. */
642 typedef struct edge_info_def
644 /* In case edge is an loopback edge, the probability edge will be reached
645 in case header is. Estimated number of iterations of the loop can be
646 then computed as 1 / (1 - back_edge_prob). */
647 REAL_VALUE_TYPE back_edge_prob
;
648 /* True if the edge is an loopback edge in the natural loop. */
652 #define BLOCK_INFO(B) ((block_info) (B)->aux)
653 #define EDGE_INFO(E) ((edge_info) (E)->aux)
655 /* Helper function for estimate_bb_frequencies.
656 Propagate the frequencies for loops headed by HEAD. */
659 propagate_freq (head
)
662 basic_block bb
= head
;
663 basic_block last
= bb
;
668 /* For each basic block we need to visit count number of his predecessors
669 we need to visit first. */
670 for (n
= 0; n
< n_basic_blocks
; n
++)
672 basic_block bb
= BASIC_BLOCK (n
);
673 if (BLOCK_INFO (bb
)->tovisit
)
677 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
678 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
680 else if (BLOCK_INFO (e
->src
)->tovisit
681 && rtl_dump_file
&& !EDGE_INFO (e
)->back_edge
)
682 fprintf (rtl_dump_file
,
683 "Irreducible region hit, ignoring edge to %i->%i\n",
684 e
->src
->index
, bb
->index
);
685 BLOCK_INFO (bb
)->npredecessors
= count
;
689 memcpy (&BLOCK_INFO (head
)->frequency
, &real_one
, sizeof (real_one
));
690 for (; bb
; bb
= nextbb
)
692 REAL_VALUE_TYPE cyclic_probability
, frequency
;
694 memcpy (&cyclic_probability
, &real_zero
, sizeof (real_zero
));
695 memcpy (&frequency
, &real_zero
, sizeof (real_zero
));
697 nextbb
= BLOCK_INFO (bb
)->next
;
698 BLOCK_INFO (bb
)->next
= NULL
;
700 /* Compute frequency of basic block. */
703 #ifdef ENABLE_CHECKING
704 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
705 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
709 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
710 if (EDGE_INFO (e
)->back_edge
)
712 REAL_ARITHMETIC (cyclic_probability
, PLUS_EXPR
,
714 EDGE_INFO (e
)->back_edge_prob
);
716 else if (!(e
->flags
& EDGE_DFS_BACK
))
720 /* frequency += (e->probability
721 * BLOCK_INFO (e->src)->frequency /
722 REG_BR_PROB_BASE); */
724 REAL_VALUE_FROM_INT (tmp
, e
->probability
, 0, DFmode
);
725 REAL_ARITHMETIC (tmp
, MULT_EXPR
, tmp
,
726 BLOCK_INFO (e
->src
)->frequency
);
727 REAL_ARITHMETIC (tmp
, RDIV_EXPR
, tmp
, real_br_prob_base
);
728 REAL_ARITHMETIC (frequency
, PLUS_EXPR
, frequency
, tmp
);
731 if (REAL_VALUES_LESS (real_almost_one
, cyclic_probability
))
732 memcpy (&cyclic_probability
, &real_almost_one
, sizeof (real_zero
));
734 /* BLOCK_INFO (bb)->frequency = frequency / (1 - cyclic_probability)
737 REAL_ARITHMETIC (cyclic_probability
, MINUS_EXPR
, real_one
,
739 REAL_ARITHMETIC (BLOCK_INFO (bb
)->frequency
,
740 RDIV_EXPR
, frequency
, cyclic_probability
);
743 BLOCK_INFO (bb
)->tovisit
= 0;
745 /* Compute back edge frequencies. */
746 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
751 /* EDGE_INFO (e)->back_edge_prob
752 = ((e->probability * BLOCK_INFO (bb)->frequency)
753 / REG_BR_PROB_BASE); */
754 REAL_VALUE_FROM_INT (tmp
, e
->probability
, 0, DFmode
);
755 REAL_ARITHMETIC (tmp
, MULT_EXPR
, tmp
,
756 BLOCK_INFO (bb
)->frequency
);
757 REAL_ARITHMETIC (EDGE_INFO (e
)->back_edge_prob
,
758 RDIV_EXPR
, tmp
, real_br_prob_base
);
762 /* Propagate to successor blocks. */
763 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
764 if (!(e
->flags
& EDGE_DFS_BACK
)
765 && BLOCK_INFO (e
->dest
)->npredecessors
)
767 BLOCK_INFO (e
->dest
)->npredecessors
--;
768 if (!BLOCK_INFO (e
->dest
)->npredecessors
)
773 BLOCK_INFO (last
)->next
= e
->dest
;
781 /* Estimate probabilities of loopback edges in loops at same nest level. */
784 estimate_loops_at_level (first_loop
)
785 struct loop
*first_loop
;
787 struct loop
*l
, *loop
= first_loop
;
789 for (loop
= first_loop
; loop
; loop
= loop
->next
)
794 estimate_loops_at_level (loop
->inner
);
796 /* Find current loop back edge and mark it. */
797 for (e
= loop
->latch
->succ
; e
->dest
!= loop
->header
; e
= e
->succ_next
)
800 EDGE_INFO (e
)->back_edge
= 1;
802 /* In case the loop header is shared, ensure that it is the last
803 one sharing the same header, so we avoid redundant work. */
806 for (l
= loop
->next
; l
; l
= l
->next
)
807 if (l
->header
== loop
->header
)
814 /* Now merge all nodes of all loops with given header as not visited. */
815 for (l
= loop
->shared
? first_loop
: loop
; l
!= loop
->next
; l
= l
->next
)
816 if (loop
->header
== l
->header
)
817 EXECUTE_IF_SET_IN_SBITMAP (l
->nodes
, 0, n
,
818 BLOCK_INFO (BASIC_BLOCK (n
))->tovisit
= 1
821 propagate_freq (loop
->header
);
825 /* Convert counts measured by profile driven feedback to frequencies. */
830 HOST_WIDEST_INT count_max
= 1;
833 for (i
= 0; i
< n_basic_blocks
; i
++)
834 count_max
= MAX (BASIC_BLOCK (i
)->count
, count_max
);
836 for (i
= -2; i
< n_basic_blocks
; i
++)
841 bb
= ENTRY_BLOCK_PTR
;
845 bb
= BASIC_BLOCK (i
);
847 bb
->frequency
= (bb
->count
* BB_FREQ_MAX
+ count_max
/ 2) / count_max
;
851 /* Return true if function is likely to be expensive, so there is no point to
852 optimize performance of prologue, epilogue or do inlining at the expense
853 of code size growth. THRESHOLD is the limit of number of isntructions
854 function can execute at average to be still considered not expensive. */
857 expensive_function_p (threshold
)
860 unsigned int sum
= 0;
864 /* We can not compute accurately for large thresholds due to scaled
866 if (threshold
> BB_FREQ_MAX
)
869 /* Frequencies are out of range. This either means that function contains
870 internal loop executing more than BB_FREQ_MAX times or profile feedback
871 is available and function has not been executed at all. */
872 if (ENTRY_BLOCK_PTR
->frequency
== 0)
875 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
876 limit
= ENTRY_BLOCK_PTR
->frequency
* threshold
;
877 for (i
= 0; i
< n_basic_blocks
; i
++)
879 basic_block bb
= BASIC_BLOCK (i
);
882 for (insn
= bb
->head
; insn
!= NEXT_INSN (bb
->end
);
883 insn
= NEXT_INSN (insn
))
884 if (active_insn_p (insn
))
886 sum
+= bb
->frequency
;
895 /* Estimate basic blocks frequency by given branch probabilities. */
898 estimate_bb_frequencies (loops
)
902 REAL_VALUE_TYPE freq_max
;
904 REAL_VALUE_FROM_INT (real_zero
, 0, 0, DFmode
);
905 REAL_VALUE_FROM_INT (real_one
, 1, 0, DFmode
);
906 REAL_VALUE_FROM_INT (real_br_prob_base
, REG_BR_PROB_BASE
, 0, DFmode
);
907 REAL_VALUE_FROM_INT (real_bb_freq_max
, BB_FREQ_MAX
, 0, DFmode
);
908 REAL_VALUE_FROM_INT (real_one_half
, 2, 0, DFmode
);
910 REAL_ARITHMETIC (real_one_half
, RDIV_EXPR
, real_one
, real_one_half
);
912 REAL_ARITHMETIC (real_almost_one
, RDIV_EXPR
, real_one
, real_br_prob_base
);
913 REAL_ARITHMETIC (real_almost_one
, MINUS_EXPR
, real_one
, real_almost_one
);
915 mark_dfs_back_edges ();
916 if (flag_branch_probabilities
)
922 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
924 for (i
= 0; i
< n_basic_blocks
; i
++)
926 rtx last_insn
= BLOCK_END (i
);
928 if (GET_CODE (last_insn
) != JUMP_INSN
|| !any_condjump_p (last_insn
)
929 /* Avoid handling of conditional jumps jumping to fallthru edge. */
930 || BASIC_BLOCK (i
)->succ
->succ_next
== NULL
)
932 /* We can predict only conditional jumps at the moment.
933 Expect each edge to be equally probable.
934 ?? In the future we want to make abnormal edges improbable. */
938 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
941 if (e
->probability
!= 0)
945 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
946 e
->probability
= (REG_BR_PROB_BASE
+ nedges
/ 2) / nedges
;
950 ENTRY_BLOCK_PTR
->succ
->probability
= REG_BR_PROB_BASE
;
952 /* Set up block info for each basic block. */
953 alloc_aux_for_blocks (sizeof (struct block_info_def
));
954 alloc_aux_for_edges (sizeof (struct edge_info_def
));
955 for (i
= -2; i
< n_basic_blocks
; i
++)
961 bb
= ENTRY_BLOCK_PTR
;
965 bb
= BASIC_BLOCK (i
);
967 BLOCK_INFO (bb
)->tovisit
= 0;
968 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
971 REAL_VALUE_FROM_INT (EDGE_INFO (e
)->back_edge_prob
,
972 e
->probability
, 0, DFmode
);
973 REAL_ARITHMETIC (EDGE_INFO (e
)->back_edge_prob
,
974 RDIV_EXPR
, EDGE_INFO (e
)->back_edge_prob
,
979 /* First compute probabilities locally for each loop from innermost
980 to outermost to examine probabilities for back edges. */
981 estimate_loops_at_level (loops
->tree_root
);
983 /* Now fake loop around whole function to finalize probabilities. */
984 for (i
= 0; i
< n_basic_blocks
; i
++)
985 BLOCK_INFO (BASIC_BLOCK (i
))->tovisit
= 1;
987 BLOCK_INFO (ENTRY_BLOCK_PTR
)->tovisit
= 1;
988 BLOCK_INFO (EXIT_BLOCK_PTR
)->tovisit
= 1;
989 propagate_freq (ENTRY_BLOCK_PTR
);
991 memcpy (&freq_max
, &real_zero
, sizeof (real_zero
));
992 for (i
= 0; i
< n_basic_blocks
; i
++)
993 if (REAL_VALUES_LESS (freq_max
, BLOCK_INFO (BASIC_BLOCK (i
))->frequency
))
994 memcpy (&freq_max
, &BLOCK_INFO (BASIC_BLOCK (i
))->frequency
,
997 for (i
= -2; i
< n_basic_blocks
; i
++)
1000 REAL_VALUE_TYPE tmp
;
1003 bb
= ENTRY_BLOCK_PTR
;
1005 bb
= EXIT_BLOCK_PTR
;
1007 bb
= BASIC_BLOCK (i
);
1009 REAL_ARITHMETIC (tmp
, MULT_EXPR
, BLOCK_INFO (bb
)->frequency
,
1011 REAL_ARITHMETIC (tmp
, RDIV_EXPR
, tmp
, freq_max
);
1012 REAL_ARITHMETIC (tmp
, PLUS_EXPR
, tmp
, real_one_half
);
1013 bb
->frequency
= REAL_VALUE_UNSIGNED_FIX (tmp
);
1016 free_aux_for_blocks ();
1017 free_aux_for_edges ();