1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License 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
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.
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "insn-config.h"
51 /* Random guesstimation given names. */
52 #define PROB_NEVER (0)
53 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
54 #define PROB_UNLIKELY (REG_BR_PROB_BASE * 4 / 10 - 1)
55 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
56 #define PROB_LIKELY (REG_BR_PROB_BASE - PROB_UNLIKELY)
57 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
58 #define PROB_ALWAYS (REG_BR_PROB_BASE)
60 static void combine_predictions_for_insn
PARAMS ((rtx
, basic_block
));
61 static void dump_prediction
PARAMS ((enum br_predictor
, int,
63 static void estimate_loops_at_level
PARAMS ((struct loop
*loop
));
64 static void propagate_freq
PARAMS ((basic_block
));
65 static void estimate_bb_frequencies
PARAMS ((struct loops
*));
66 static void counts_to_freqs
PARAMS ((void));
68 /* Information we hold about each branch predictor.
69 Filled using information from predict.def. */
72 const char *const name
; /* Name used in the debugging dumps. */
73 const int hitrate
; /* Expected hitrate used by
74 predict_insn_def call. */
78 /* Use given predictor without Dempster-Shaffer theory if it matches
79 using first_match heuristics. */
80 #define PRED_FLAG_FIRST_MATCH 1
82 /* Recompute hitrate in percent to our representation. */
84 #define HITRATE(VAL) ((int)((VAL) * REG_BR_PROB_BASE + 50) / 100)
86 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
87 static const struct predictor_info predictor_info
[] = {
88 #include "predict.def"
90 /* Upper bound on predictors. */
96 predict_insn (insn
, predictor
, probability
)
99 enum br_predictor predictor
;
101 if (!any_condjump_p (insn
))
104 = gen_rtx_EXPR_LIST (REG_BR_PRED
,
105 gen_rtx_CONCAT (VOIDmode
,
106 GEN_INT ((int) predictor
),
107 GEN_INT ((int) probability
)),
111 /* Predict insn by given predictor. */
113 predict_insn_def (insn
, predictor
, taken
)
115 enum br_predictor predictor
;
116 enum prediction taken
;
118 int probability
= predictor_info
[(int) predictor
].hitrate
;
120 probability
= REG_BR_PROB_BASE
- probability
;
121 predict_insn (insn
, predictor
, probability
);
124 /* Predict edge E with given probability if possible. */
126 predict_edge (e
, predictor
, probability
)
129 enum br_predictor predictor
;
132 last_insn
= e
->src
->end
;
134 /* We can store the branch prediction information only about
135 conditional jumps. */
136 if (!any_condjump_p (last_insn
))
139 /* We always store probability of branching. */
140 if (e
->flags
& EDGE_FALLTHRU
)
141 probability
= REG_BR_PROB_BASE
- probability
;
143 predict_insn (last_insn
, predictor
, probability
);
146 /* Predict edge E by given predictor if possible. */
148 predict_edge_def (e
, predictor
, taken
)
150 enum br_predictor predictor
;
151 enum prediction taken
;
153 int probability
= predictor_info
[(int) predictor
].hitrate
;
156 probability
= REG_BR_PROB_BASE
- probability
;
157 predict_edge (e
, predictor
, probability
);
160 /* Invert all branch predictions or probability notes in the INSN. This needs
161 to be done each time we invert the condition used by the jump. */
163 invert_br_probabilities (insn
)
166 rtx note
= REG_NOTES (insn
);
170 if (REG_NOTE_KIND (note
) == REG_BR_PROB
)
171 XEXP (note
, 0) = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (note
, 0)));
172 else if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
173 XEXP (XEXP (note
, 0), 1)
174 = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (XEXP (note
, 0), 1)));
175 note
= XEXP (note
, 1);
179 /* Dump information about the branch prediction to the output file. */
181 dump_prediction (predictor
, probability
, bb
, used
)
182 enum br_predictor predictor
;
192 while (e
->flags
& EDGE_FALLTHRU
)
195 fprintf (rtl_dump_file
, " %s heuristics%s: %.1f%%",
196 predictor_info
[predictor
].name
,
197 used
? "" : " (ignored)",
198 probability
* 100.0 / REG_BR_PROB_BASE
);
202 fprintf (rtl_dump_file
, " exec ");
203 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
204 (HOST_WIDEST_INT
) bb
->count
);
205 fprintf (rtl_dump_file
, " hit ");
206 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
207 (HOST_WIDEST_INT
) e
->count
);
208 fprintf (rtl_dump_file
, " (%.1f%%)",
209 e
->count
* 100.0 / bb
->count
);
211 fprintf (rtl_dump_file
, "\n");
214 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
215 note if not already present. Remove now useless REG_BR_PRED notes. */
217 combine_predictions_for_insn (insn
, bb
)
221 rtx prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
222 rtx
*pnote
= ®_NOTES (insn
);
223 rtx note
= REG_NOTES (insn
);
224 int best_probability
= PROB_EVEN
;
225 int best_predictor
= END_PREDICTORS
;
226 int combined_probability
= REG_BR_PROB_BASE
/ 2;
228 bool first_match
= false;
232 fprintf (rtl_dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
235 /* We implement "first match" heuristics and use probability guessed
236 by predictor with smallest index. In the future we will use better
237 probability combination techniques. */
240 if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
242 int predictor
= INTVAL (XEXP (XEXP (note
, 0), 0));
243 int probability
= INTVAL (XEXP (XEXP (note
, 0), 1));
246 if (best_predictor
> predictor
)
247 best_probability
= probability
, best_predictor
= predictor
;
249 d
= (combined_probability
* probability
250 + (REG_BR_PROB_BASE
- combined_probability
)
251 * (REG_BR_PROB_BASE
- probability
));
252 /* An FP math to avoid overflows of 32bit integers. */
253 combined_probability
= (((double)combined_probability
) * probability
254 * REG_BR_PROB_BASE
/ d
+ 0.5);
256 note
= XEXP (note
, 1);
259 /* Decide heuristic to use. In case we didn't match anything, use
260 no_prediction heuristic, in case we did match, use either
261 first match or Dempster-Shaffer theory depending on the flags. */
263 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
267 dump_prediction (PRED_NO_PREDICTION
, combined_probability
, bb
, true);
270 dump_prediction (PRED_DS_THEORY
, combined_probability
, bb
,
272 dump_prediction (PRED_FIRST_MATCH
, best_probability
, bb
, first_match
);
276 combined_probability
= best_probability
;
277 dump_prediction (PRED_COMBINED
, combined_probability
, bb
, true);
281 if (REG_NOTE_KIND (*pnote
) == REG_BR_PRED
)
283 int predictor
= INTVAL (XEXP (XEXP (*pnote
, 0), 0));
284 int probability
= INTVAL (XEXP (XEXP (*pnote
, 0), 1));
286 dump_prediction (predictor
, probability
, bb
,
287 !first_match
|| best_predictor
== predictor
);
288 *pnote
= XEXP (*pnote
, 1);
291 pnote
= &XEXP (*pnote
, 1);
296 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
297 GEN_INT (combined_probability
), REG_NOTES (insn
));
298 /* Save the prediction into CFG in case we are seeing non-degenerated
300 if (bb
->succ
->succ_next
)
302 BRANCH_EDGE (bb
)->probability
= combined_probability
;
303 FALLTHRU_EDGE (bb
)->probability
= REG_BR_PROB_BASE
- combined_probability
;
308 /* Statically estimate the probability that a branch will be taken.
309 ??? In the next revision there will be a number of other predictors added
310 from the above references. Further, each heuristic will be factored out
311 into its own function for clarity (and to facilitate the combination of
315 estimate_probability (loops_info
)
316 struct loops
*loops_info
;
318 sbitmap
*dominators
, *post_dominators
;
320 int found_noreturn
= 0;
322 dominators
= sbitmap_vector_alloc (n_basic_blocks
, n_basic_blocks
);
323 post_dominators
= sbitmap_vector_alloc (n_basic_blocks
, n_basic_blocks
);
324 calculate_dominance_info (NULL
, dominators
, CDI_DOMINATORS
);
325 calculate_dominance_info (NULL
, post_dominators
, CDI_POST_DOMINATORS
);
327 /* Try to predict out blocks in a loop that are not part of a
329 for (i
= 0; i
< loops_info
->num
; i
++)
333 struct loop
*loop
= &loops_info
->array
[i
];
335 flow_loop_scan (loops_info
, loop
, LOOP_EXIT_EDGES
);
336 exits
= loop
->num_exits
;
338 for (j
= loop
->first
->index
;
339 j
<= loop
->last
->index
;
342 if (TEST_BIT (loop
->nodes
, j
))
344 int header_found
= 0;
347 /* Loop branch heuristics - predict as taken an edge back to
349 for (e
= BASIC_BLOCK(j
)->succ
; e
; e
= e
->succ_next
)
350 if (e
->dest
== loop
->header
351 && e
->src
== loop
->latch
)
354 predict_edge_def (e
, PRED_LOOP_BRANCH
, TAKEN
);
356 /* Loop exit heuristics - predict as not taken an edge
357 exiting the loop if the conditinal has no loop header
360 for (e
= BASIC_BLOCK(j
)->succ
; e
; e
= e
->succ_next
)
361 if (e
->dest
->index
< 0
362 || !TEST_BIT (loop
->nodes
, e
->dest
->index
))
363 predict_edge (e
, PRED_LOOP_EXIT
,
365 - predictor_info
[(int)PRED_LOOP_EXIT
].hitrate
)
371 /* Attempt to predict conditional jumps using a number of heuristics. */
372 for (i
= 0; i
< n_basic_blocks
; i
++)
374 basic_block bb
= BASIC_BLOCK (i
);
375 rtx last_insn
= bb
->end
;
379 /* If block has no successor, predict all possible paths to
380 it as improbable, as the block contains a call to a noreturn
381 function and thus can be executed only once. */
382 if (bb
->succ
== NULL
&& !found_noreturn
)
386 /* ??? Postdominator claims each noreturn block to be postdominated
387 by each, so we need to run only once. This needs to be changed
388 once postdominace algorithm is updated to say something more sane.
391 for (y
= 0; y
< n_basic_blocks
; y
++)
392 if (!TEST_BIT (post_dominators
[y
], i
))
394 for (e
= BASIC_BLOCK (y
)->succ
; e
; e
= e
->succ_next
)
395 if (e
->dest
->index
>= 0
396 && TEST_BIT (post_dominators
[e
->dest
->index
], i
))
397 predict_edge_def (e
, PRED_NORETURN
, NOT_TAKEN
);
401 if (GET_CODE (last_insn
) != JUMP_INSN
402 || ! any_condjump_p (last_insn
))
405 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
407 /* Predict edges to blocks that return immediately to be
408 improbable. These are usually used to signal error states. */
409 if (e
->dest
== EXIT_BLOCK_PTR
410 || (e
->dest
->succ
&& !e
->dest
->succ
->succ_next
411 && e
->dest
->succ
->dest
== EXIT_BLOCK_PTR
))
412 predict_edge_def (e
, PRED_ERROR_RETURN
, NOT_TAKEN
);
414 /* Look for block we are guarding (ie we dominate it,
415 but it doesn't postdominate us). */
416 if (e
->dest
!= EXIT_BLOCK_PTR
418 && TEST_BIT (dominators
[e
->dest
->index
], e
->src
->index
)
419 && !TEST_BIT (post_dominators
[e
->src
->index
], e
->dest
->index
))
422 /* The call heuristic claims that a guarded function call
423 is improbable. This is because such calls are often used
424 to signal exceptional situations such as printing error
426 for (insn
= e
->dest
->head
; insn
!= NEXT_INSN (e
->dest
->end
);
427 insn
= NEXT_INSN (insn
))
428 if (GET_CODE (insn
) == CALL_INSN
429 /* Constant and pure calls are hardly used to signalize
430 something exceptional. */
431 && ! CONST_OR_PURE_CALL_P (insn
))
433 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
439 cond
= get_condition (last_insn
, &earliest
);
443 /* Try "pointer heuristic."
444 A comparison ptr == 0 is predicted as false.
445 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
446 if (GET_RTX_CLASS (GET_CODE (cond
)) == '<'
447 && ((REG_P (XEXP (cond
, 0)) && REG_POINTER (XEXP (cond
, 0)))
448 || (REG_P (XEXP (cond
, 1)) && REG_POINTER (XEXP (cond
, 1)))))
449 switch (GET_CODE (cond
))
452 predict_insn_def (last_insn
, PRED_POINTER
, NOT_TAKEN
);
455 predict_insn_def (last_insn
, PRED_POINTER
, TAKEN
);
461 /* Try "opcode heuristic."
462 EQ tests are usually false and NE tests are usually true. Also,
463 most quantities are positive, so we can make the appropriate guesses
464 about signed comparisons against zero. */
465 switch (GET_CODE (cond
))
468 /* Unconditional branch. */
469 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
470 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
475 /* Floating point comparisons appears to behave in a very
476 inpredictable way because of special role of = tests in
478 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
480 /* Comparisons with 0 are often used for booleans and there is
481 nothing usefull to predict about them. */
482 else if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 0) == const0_rtx
)
485 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
489 /* Floating point comparisons appears to behave in a very
490 inpredictable way because of special role of = tests in
492 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
494 /* Comparisons with 0 are often used for booleans and there is
495 nothing usefull to predict about them. */
496 else if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 0) == const0_rtx
)
499 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
502 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
505 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
509 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
510 || XEXP (cond
, 1) == constm1_rtx
)
511 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, NOT_TAKEN
);
515 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
516 || XEXP (cond
, 1) == constm1_rtx
)
517 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, TAKEN
);
525 /* Attach the combined probability to each conditional jump. */
526 for (i
= 0; i
< n_basic_blocks
; i
++)
528 rtx last_insn
= BLOCK_END (i
);
530 if (GET_CODE (last_insn
) != JUMP_INSN
531 || ! any_condjump_p (last_insn
))
533 combine_predictions_for_insn (last_insn
, BASIC_BLOCK (i
));
535 sbitmap_vector_free (post_dominators
);
536 sbitmap_vector_free (dominators
);
538 estimate_bb_frequencies (loops_info
);
541 /* __builtin_expect dropped tokens into the insn stream describing
542 expected values of registers. Generate branch probabilities
543 based off these values. */
546 expected_value_to_br_prob ()
548 rtx insn
, cond
, ev
= NULL_RTX
, ev_reg
= NULL_RTX
;
550 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
552 switch (GET_CODE (insn
))
555 /* Look for expected value notes. */
556 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EXPECTED_VALUE
)
558 ev
= NOTE_EXPECTED_VALUE (insn
);
559 ev_reg
= XEXP (ev
, 0);
565 /* Never propagate across labels. */
570 /* Look for insns that clobber the EV register. */
571 if (ev
&& reg_set_p (ev_reg
, insn
))
576 /* Look for simple conditional branches. If we haven't got an
577 expected value yet, no point going further. */
578 if (GET_CODE (insn
) != JUMP_INSN
|| ev
== NULL_RTX
)
580 if (! any_condjump_p (insn
))
585 /* Collect the branch condition, hopefully relative to EV_REG. */
586 /* ??? At present we'll miss things like
587 (expected_value (eq r70 0))
589 (set r80 (lt r70 r71))
590 (set pc (if_then_else (ne r80 0) ...))
591 as canonicalize_condition will render this to us as
593 Could use cselib to try and reduce this further. */
594 cond
= XEXP (SET_SRC (pc_set (insn
)), 0);
595 cond
= canonicalize_condition (insn
, cond
, 0, NULL
, ev_reg
);
597 || XEXP (cond
, 0) != ev_reg
598 || GET_CODE (XEXP (cond
, 1)) != CONST_INT
)
601 /* Substitute and simplify. Given that the expression we're
602 building involves two constants, we should wind up with either
604 cond
= gen_rtx_fmt_ee (GET_CODE (cond
), VOIDmode
,
605 XEXP (ev
, 1), XEXP (cond
, 1));
606 cond
= simplify_rtx (cond
);
608 /* Turn the condition into a scaled branch probability. */
609 if (cond
!= const_true_rtx
&& cond
!= const0_rtx
)
611 predict_insn_def (insn
, PRED_BUILTIN_EXPECT
,
612 cond
== const_true_rtx
? TAKEN
: NOT_TAKEN
);
616 /* This is used to carry information about basic blocks. It is
617 attached to the AUX field of the standard CFG block. */
619 typedef struct block_info_def
621 /* Estimated frequency of execution of basic_block. */
622 volatile double frequency
;
624 /* To keep queue of basic blocks to process. */
627 /* True if block needs to be visited in prop_freqency. */
630 /* Number of predecessors we need to visit first. */
634 /* Similar information for edges. */
635 typedef struct edge_info_def
637 /* In case edge is an loopback edge, the probability edge will be reached
638 in case header is. Estimated number of iterations of the loop can be
639 then computed as 1 / (1 - back_edge_prob).
641 Volatile is needed to avoid differences in the optimized and unoptimized
642 builds on machines where FP registers are wider than double. */
643 volatile double back_edge_prob
;
644 /* True if the edge is an loopback edge in the natural loop. */
648 #define BLOCK_INFO(B) ((block_info) (B)->aux)
649 #define EDGE_INFO(E) ((edge_info) (E)->aux)
651 /* Helper function for estimate_bb_frequencies.
652 Propagate the frequencies for loops headed by HEAD. */
654 propagate_freq (head
)
657 basic_block bb
= head
;
658 basic_block last
= bb
;
663 /* For each basic block we need to visit count number of his predecessors
664 we need to visit first. */
665 for (n
= 0; n
< n_basic_blocks
; n
++)
667 basic_block bb
= BASIC_BLOCK (n
);
668 if (BLOCK_INFO (bb
)->tovisit
)
671 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
672 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
674 else if (BLOCK_INFO (e
->src
)->tovisit
675 && rtl_dump_file
&& !EDGE_INFO (e
)->back_edge
)
676 fprintf (rtl_dump_file
,
677 "Irreducible region hit, ignoring edge to %i->%i\n",
678 e
->src
->index
, bb
->index
);
679 BLOCK_INFO (bb
)->npredecessors
= count
;
683 BLOCK_INFO (head
)->frequency
= 1;
684 for (; bb
; bb
= nextbb
)
686 volatile double cyclic_probability
= 0, frequency
= 0;
688 nextbb
= BLOCK_INFO (bb
)->next
;
689 BLOCK_INFO (bb
)->next
= NULL
;
691 /* Compute frequency of basic block. */
694 #ifdef ENABLE_CHECKING
695 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
696 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
700 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
701 if (EDGE_INFO (e
)->back_edge
)
702 cyclic_probability
+= EDGE_INFO (e
)->back_edge_prob
;
703 else if (!(e
->flags
& EDGE_DFS_BACK
))
704 frequency
+= (e
->probability
705 * BLOCK_INFO (e
->src
)->frequency
/
708 if (cyclic_probability
> 1.0 - 1.0 / REG_BR_PROB_BASE
)
709 cyclic_probability
= 1.0 - 1.0 / REG_BR_PROB_BASE
;
711 BLOCK_INFO (bb
)->frequency
= frequency
/ (1 - cyclic_probability
);
714 BLOCK_INFO (bb
)->tovisit
= 0;
716 /* Compute back edge frequencies. */
717 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
719 EDGE_INFO (e
)->back_edge_prob
= (e
->probability
720 * BLOCK_INFO (bb
)->frequency
723 /* Propagate to successor blocks. */
724 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
725 if (!(e
->flags
& EDGE_DFS_BACK
)
726 && BLOCK_INFO (e
->dest
)->npredecessors
)
728 BLOCK_INFO (e
->dest
)->npredecessors
--;
729 if (!BLOCK_INFO (e
->dest
)->npredecessors
)
734 BLOCK_INFO (last
)->next
= e
->dest
;
741 /* Estimate probabilities of loopback edges in loops at same nest level. */
743 estimate_loops_at_level (first_loop
)
744 struct loop
*first_loop
;
746 struct loop
*l
, *loop
= first_loop
;
748 for (loop
= first_loop
; loop
; loop
= loop
->next
)
753 estimate_loops_at_level (loop
->inner
);
755 /* Find current loop back edge and mark it. */
756 for (e
= loop
->latch
->succ
; e
->dest
!= loop
->header
; e
= e
->succ_next
);
758 EDGE_INFO (e
)->back_edge
= 1;
760 /* In case the loop header is shared, ensure that it is the last
761 one sharing the same header, so we avoid redundant work. */
764 for (l
= loop
->next
; l
; l
= l
->next
)
765 if (l
->header
== loop
->header
)
771 /* Now merge all nodes of all loops with given header as not visited. */
772 for (l
= loop
->shared
? first_loop
: loop
; l
!= loop
->next
; l
= l
->next
)
773 if (loop
->header
== l
->header
)
774 EXECUTE_IF_SET_IN_SBITMAP (l
->nodes
, 0, n
,
775 BLOCK_INFO (BASIC_BLOCK (n
))->tovisit
= 1
777 propagate_freq (loop
->header
);
781 /* Convert counts measured by profile driven feedback to frequencies. */
785 HOST_WIDEST_INT count_max
= 1;
788 for (i
= 0; i
< n_basic_blocks
; i
++)
789 if (BASIC_BLOCK (i
)->count
> count_max
)
790 count_max
= BASIC_BLOCK (i
)->count
;
792 for (i
= -2; i
< n_basic_blocks
; i
++)
796 bb
= ENTRY_BLOCK_PTR
;
800 bb
= BASIC_BLOCK (i
);
801 bb
->frequency
= ((bb
->count
* BB_FREQ_MAX
+ count_max
/ 2)
806 /* Return true if function is likely to be expensive, so there is no point
807 to optimizer performance of prologue, epilogue or do inlining at the
808 expense of code size growth. THRESHOLD is the limit of number
809 of isntructions function can execute at average to be still considered
812 expensive_function_p (threshold
)
815 unsigned int sum
= 0;
819 /* We can not compute accurately for large thresholds due to scaled
821 if (threshold
> BB_FREQ_MAX
)
824 /* Frequencies are out of range. This either means that function contains
825 internal loop executing more than BB_FREQ_MAX times or profile feedback
826 is available and function has not been executed at all. */
827 if (ENTRY_BLOCK_PTR
->frequency
== 0)
830 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
831 limit
= ENTRY_BLOCK_PTR
->frequency
* threshold
;
832 for (i
= 0; i
< n_basic_blocks
; i
++)
834 basic_block bb
= BASIC_BLOCK (i
);
837 for (insn
= bb
->head
; insn
!= NEXT_INSN (bb
->end
);
838 insn
= NEXT_INSN (insn
))
840 if (active_insn_p (insn
))
842 sum
+= bb
->frequency
;
851 /* Estimate basic blocks frequency by given branch probabilities. */
853 estimate_bb_frequencies (loops
)
859 mark_dfs_back_edges ();
860 if (flag_branch_probabilities
)
866 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
868 for (i
= 0; i
< n_basic_blocks
; i
++)
870 rtx last_insn
= BLOCK_END (i
);
872 edge fallthru
, branch
;
874 if (GET_CODE (last_insn
) != JUMP_INSN
|| !any_condjump_p (last_insn
)
875 /* Avoid handling of conditional jumps jumping to fallthru edge. */
876 || BASIC_BLOCK (i
)->succ
->succ_next
== NULL
)
878 /* We can predict only conditional jumps at the moment.
879 Expect each edge to be equally probable.
880 ?? In the future we want to make abnormal edges improbable. */
884 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
887 if (e
->probability
!= 0)
891 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
892 e
->probability
= (REG_BR_PROB_BASE
+ nedges
/ 2) / nedges
;
896 probability
= INTVAL (XEXP (find_reg_note (last_insn
,
897 REG_BR_PROB
, 0), 0));
898 fallthru
= BASIC_BLOCK (i
)->succ
;
899 if (!fallthru
->flags
& EDGE_FALLTHRU
)
900 fallthru
= fallthru
->succ_next
;
901 branch
= BASIC_BLOCK (i
)->succ
;
902 if (branch
->flags
& EDGE_FALLTHRU
)
903 branch
= branch
->succ_next
;
905 branch
->probability
= probability
;
906 fallthru
->probability
= REG_BR_PROB_BASE
- probability
;
909 ENTRY_BLOCK_PTR
->succ
->probability
= REG_BR_PROB_BASE
;
911 /* Set up block info for each basic block. */
912 alloc_aux_for_blocks (sizeof (struct block_info_def
));
913 alloc_aux_for_edges (sizeof (struct edge_info_def
));
914 for (i
= -2; i
< n_basic_blocks
; i
++)
920 bb
= ENTRY_BLOCK_PTR
;
924 bb
= BASIC_BLOCK (i
);
925 BLOCK_INFO (bb
)->tovisit
= 0;
926 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
927 EDGE_INFO (e
)->back_edge_prob
= ((double) e
->probability
930 /* First compute probabilities locally for each loop from innermost
931 to outermost to examine probabilities for back edges. */
932 estimate_loops_at_level (loops
->tree_root
);
934 /* Now fake loop around whole function to finalize probabilities. */
935 for (i
= 0; i
< n_basic_blocks
; i
++)
936 BLOCK_INFO (BASIC_BLOCK (i
))->tovisit
= 1;
937 BLOCK_INFO (ENTRY_BLOCK_PTR
)->tovisit
= 1;
938 BLOCK_INFO (EXIT_BLOCK_PTR
)->tovisit
= 1;
939 propagate_freq (ENTRY_BLOCK_PTR
);
941 for (i
= 0; i
< n_basic_blocks
; i
++)
942 if (BLOCK_INFO (BASIC_BLOCK (i
))->frequency
> freq_max
)
943 freq_max
= BLOCK_INFO (BASIC_BLOCK (i
))->frequency
;
944 for (i
= -2; i
< n_basic_blocks
; i
++)
948 bb
= ENTRY_BLOCK_PTR
;
952 bb
= BASIC_BLOCK (i
);
953 bb
->frequency
= (BLOCK_INFO (bb
)->frequency
* BB_FREQ_MAX
/ freq_max
957 free_aux_for_blocks ();
958 free_aux_for_edges ();