1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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. */
33 #include "coretypes.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "insn-config.h"
56 #include "tree-flow.h"
58 #include "tree-dump.h"
59 #include "tree-pass.h"
62 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
63 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
64 static sreal real_zero
, real_one
, real_almost_one
, real_br_prob_base
,
65 real_inv_br_prob_base
, real_one_half
, real_bb_freq_max
;
67 /* Random guesstimation given names. */
68 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
69 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
70 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
71 #define PROB_ALWAYS (REG_BR_PROB_BASE)
73 static void combine_predictions_for_insn (rtx
, basic_block
);
74 static void dump_prediction (FILE *, enum br_predictor
, int, basic_block
, int);
75 static void estimate_loops_at_level (struct loop
*loop
);
76 static void propagate_freq (struct loop
*);
77 static void estimate_bb_frequencies (struct loops
*);
78 static int counts_to_freqs (void);
79 static void process_note_predictions (basic_block
, int *);
80 static void process_note_prediction (basic_block
, int *, int, int);
81 static bool last_basic_block_p (basic_block
);
82 static void compute_function_frequency (void);
83 static void choose_function_section (void);
84 static bool can_predict_insn_p (rtx
);
86 /* Information we hold about each branch predictor.
87 Filled using information from predict.def. */
91 const char *const name
; /* Name used in the debugging dumps. */
92 const int hitrate
; /* Expected hitrate used by
93 predict_insn_def call. */
97 /* Use given predictor without Dempster-Shaffer theory if it matches
98 using first_match heuristics. */
99 #define PRED_FLAG_FIRST_MATCH 1
101 /* Recompute hitrate in percent to our representation. */
103 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
105 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
106 static const struct predictor_info predictor_info
[]= {
107 #include "predict.def"
109 /* Upper bound on predictors. */
114 /* Return true in case BB can be CPU intensive and should be optimized
115 for maximal performance. */
118 maybe_hot_bb_p (basic_block bb
)
120 if (profile_info
&& flag_branch_probabilities
122 < profile_info
->sum_max
/ PARAM_VALUE (HOT_BB_COUNT_FRACTION
)))
124 if (bb
->frequency
< BB_FREQ_MAX
/ PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
))
129 /* Return true in case BB is cold and should be optimized for size. */
132 probably_cold_bb_p (basic_block bb
)
134 if (profile_info
&& flag_branch_probabilities
136 < profile_info
->sum_max
/ PARAM_VALUE (HOT_BB_COUNT_FRACTION
)))
138 if (bb
->frequency
< BB_FREQ_MAX
/ PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION
))
143 /* Return true in case BB is probably never executed. */
145 probably_never_executed_bb_p (basic_block bb
)
147 if (profile_info
&& flag_branch_probabilities
)
148 return ((bb
->count
+ profile_info
->runs
/ 2) / profile_info
->runs
) == 0;
152 /* Return true if the one of outgoing edges is already predicted by
156 rtl_predicted_by_p (basic_block bb
, enum br_predictor predictor
)
159 if (!INSN_P (BB_END (bb
)))
161 for (note
= REG_NOTES (BB_END (bb
)); note
; note
= XEXP (note
, 1))
162 if (REG_NOTE_KIND (note
) == REG_BR_PRED
163 && INTVAL (XEXP (XEXP (note
, 0), 0)) == (int)predictor
)
168 /* Return true if the one of outgoing edges is already predicted by
172 tree_predicted_by_p (basic_block bb
, enum br_predictor predictor
)
174 struct edge_prediction
*i
= bb_ann (bb
)->predictions
;
175 for (i
= bb_ann (bb
)->predictions
; i
; i
= i
->next
)
176 if (i
->predictor
== predictor
)
182 predict_insn (rtx insn
, enum br_predictor predictor
, int probability
)
184 if (!any_condjump_p (insn
))
186 if (!flag_guess_branch_prob
)
190 = gen_rtx_EXPR_LIST (REG_BR_PRED
,
191 gen_rtx_CONCAT (VOIDmode
,
192 GEN_INT ((int) predictor
),
193 GEN_INT ((int) probability
)),
197 /* Predict insn by given predictor. */
200 predict_insn_def (rtx insn
, 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 rtl_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
217 last_insn
= BB_END (e
->src
);
219 /* We can store the branch prediction information only about
220 conditional jumps. */
221 if (!any_condjump_p (last_insn
))
224 /* We always store probability of branching. */
225 if (e
->flags
& EDGE_FALLTHRU
)
226 probability
= REG_BR_PROB_BASE
- probability
;
228 predict_insn (last_insn
, predictor
, probability
);
231 /* Predict edge E with the given PROBABILITY. */
233 tree_predict_edge (edge e
, enum br_predictor predictor
, int probability
)
235 struct edge_prediction
*i
= ggc_alloc (sizeof (struct edge_prediction
));
237 i
->next
= bb_ann (e
->src
)->predictions
;
238 bb_ann (e
->src
)->predictions
= i
;
239 i
->probability
= probability
;
240 i
->predictor
= predictor
;
244 /* Return true when we can store prediction on insn INSN.
245 At the moment we represent predictions only on conditional
246 jumps, not at computed jump or other complicated cases. */
248 can_predict_insn_p (rtx insn
)
250 return (GET_CODE (insn
) == JUMP_INSN
251 && any_condjump_p (insn
)
252 && BLOCK_FOR_INSN (insn
)->succ
->succ_next
);
255 /* Predict edge E by given predictor if possible. */
258 predict_edge_def (edge e
, enum br_predictor predictor
,
259 enum prediction taken
)
261 int probability
= predictor_info
[(int) predictor
].hitrate
;
264 probability
= REG_BR_PROB_BASE
- probability
;
266 predict_edge (e
, predictor
, probability
);
269 /* Invert all branch predictions or probability notes in the INSN. This needs
270 to be done each time we invert the condition used by the jump. */
273 invert_br_probabilities (rtx insn
)
277 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
278 if (REG_NOTE_KIND (note
) == REG_BR_PROB
)
279 XEXP (note
, 0) = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (note
, 0)));
280 else if (REG_NOTE_KIND (note
) == REG_BR_PRED
)
281 XEXP (XEXP (note
, 0), 1)
282 = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (XEXP (note
, 0), 1)));
285 /* Dump information about the branch prediction to the output file. */
288 dump_prediction (FILE *file
, enum br_predictor predictor
, int probability
,
289 basic_block bb
, int used
)
296 while (e
&& (e
->flags
& EDGE_FALLTHRU
))
299 fprintf (file
, " %s heuristics%s: %.1f%%",
300 predictor_info
[predictor
].name
,
301 used
? "" : " (ignored)", probability
* 100.0 / REG_BR_PROB_BASE
);
305 fprintf (file
, " exec ");
306 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
309 fprintf (file
, " hit ");
310 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
311 fprintf (file
, " (%.1f%%)", e
->count
* 100.0 / bb
->count
);
315 fprintf (file
, "\n");
318 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
319 note if not already present. Remove now useless REG_BR_PRED notes. */
322 combine_predictions_for_insn (rtx insn
, basic_block bb
)
324 rtx prob_note
= find_reg_note (insn
, REG_BR_PROB
, 0);
325 rtx
*pnote
= ®_NOTES (insn
);
327 int best_probability
= PROB_EVEN
;
328 int best_predictor
= END_PREDICTORS
;
329 int combined_probability
= REG_BR_PROB_BASE
/ 2;
331 bool first_match
= false;
335 fprintf (dump_file
, "Predictions for insn %i bb %i\n", INSN_UID (insn
),
338 /* We implement "first match" heuristics and use probability guessed
339 by predictor with smallest index. */
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 (dump_file
, PRED_NO_PREDICTION
,
372 combined_probability
, bb
, true);
375 dump_prediction (dump_file
, PRED_DS_THEORY
, combined_probability
,
377 dump_prediction (dump_file
, PRED_FIRST_MATCH
, best_probability
,
382 combined_probability
= best_probability
;
383 dump_prediction (dump_file
, PRED_COMBINED
, combined_probability
, bb
, true);
387 if (REG_NOTE_KIND (*pnote
) == REG_BR_PRED
)
389 int predictor
= INTVAL (XEXP (XEXP (*pnote
, 0), 0));
390 int probability
= INTVAL (XEXP (XEXP (*pnote
, 0), 1));
392 dump_prediction (dump_file
, predictor
, probability
, bb
,
393 !first_match
|| best_predictor
== predictor
);
394 *pnote
= XEXP (*pnote
, 1);
397 pnote
= &XEXP (*pnote
, 1);
403 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
404 GEN_INT (combined_probability
), REG_NOTES (insn
));
406 /* Save the prediction into CFG in case we are seeing non-degenerated
408 if (bb
->succ
->succ_next
)
410 BRANCH_EDGE (bb
)->probability
= combined_probability
;
411 FALLTHRU_EDGE (bb
)->probability
412 = REG_BR_PROB_BASE
- combined_probability
;
417 /* Combine predictions into single probability and store them into CFG.
418 Remove now useless prediction entries. */
421 combine_predictions_for_bb (FILE *file
, basic_block bb
)
423 int best_probability
= PROB_EVEN
;
424 int best_predictor
= END_PREDICTORS
;
425 int combined_probability
= REG_BR_PROB_BASE
/ 2;
427 bool first_match
= false;
429 struct edge_prediction
*pred
;
431 edge e
, first
= NULL
, second
= NULL
;
433 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
434 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
437 if (first
&& !second
)
443 /* When there is no successor or only one choice, prediction is easy.
445 We are lazy for now and predict only basic blocks with two outgoing
446 edges. It is possible to predict generic case too, but we have to
447 ignore first match heuristics and do more involved combining. Implement
451 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
452 if (!(e
->flags
& (EDGE_EH
| EDGE_FAKE
)))
453 e
->probability
= (REG_BR_PROB_BASE
+ nedges
/ 2) / nedges
;
456 bb_ann (bb
)->predictions
= NULL
;
458 fprintf (file
, "%i edges in bb %i predicted to even probabilities\n",
464 fprintf (file
, "Predictions for bb %i\n", bb
->index
);
466 /* We implement "first match" heuristics and use probability guessed
467 by predictor with smallest index. */
468 for (pred
= bb_ann (bb
)->predictions
; pred
; pred
= pred
->next
)
470 int predictor
= pred
->predictor
;
471 int probability
= pred
->probability
;
473 if (pred
->edge
!= first
)
474 probability
= REG_BR_PROB_BASE
- probability
;
477 if (best_predictor
> predictor
)
478 best_probability
= probability
, best_predictor
= predictor
;
480 d
= (combined_probability
* probability
481 + (REG_BR_PROB_BASE
- combined_probability
)
482 * (REG_BR_PROB_BASE
- probability
));
484 /* Use FP math to avoid overflows of 32bit integers. */
486 /* If one probability is 0% and one 100%, avoid division by zero. */
487 combined_probability
= REG_BR_PROB_BASE
/ 2;
489 combined_probability
= (((double) combined_probability
) * probability
490 * REG_BR_PROB_BASE
/ d
+ 0.5);
493 /* Decide which heuristic to use. In case we didn't match anything,
494 use no_prediction heuristic, in case we did match, use either
495 first match or Dempster-Shaffer theory depending on the flags. */
497 if (predictor_info
[best_predictor
].flags
& PRED_FLAG_FIRST_MATCH
)
501 dump_prediction (file
, PRED_NO_PREDICTION
, combined_probability
, bb
, true);
504 dump_prediction (file
, PRED_DS_THEORY
, combined_probability
, bb
,
506 dump_prediction (file
, PRED_FIRST_MATCH
, best_probability
, bb
,
511 combined_probability
= best_probability
;
512 dump_prediction (file
, PRED_COMBINED
, combined_probability
, bb
, true);
514 for (pred
= bb_ann (bb
)->predictions
; pred
; pred
= pred
->next
)
516 int predictor
= pred
->predictor
;
517 int probability
= pred
->probability
;
519 if (pred
->edge
!= bb
->succ
)
520 probability
= REG_BR_PROB_BASE
- probability
;
521 dump_prediction (file
, predictor
, probability
, bb
,
522 !first_match
|| best_predictor
== predictor
);
524 bb_ann (bb
)->predictions
= NULL
;
526 first
->probability
= combined_probability
;
527 second
->probability
= REG_BR_PROB_BASE
- combined_probability
;
530 /* Predict edge probabilities by exploiting loop structure.
531 When SIMPLELOOPS is set, attempt to count number of iterations by analyzing
534 predict_loops (struct loops
*loops_info
, bool simpleloops
)
538 /* Try to predict out blocks in a loop that are not part of a
540 for (i
= 1; i
< loops_info
->num
; i
++)
542 basic_block bb
, *bbs
;
545 struct loop
*loop
= loops_info
->parray
[i
];
546 struct niter_desc desc
;
547 unsigned HOST_WIDE_INT niter
;
549 flow_loop_scan (loop
, LOOP_EXIT_EDGES
);
550 exits
= loop
->num_exits
;
554 iv_analysis_loop_init (loop
);
555 find_simple_exit (loop
, &desc
);
557 if (desc
.simple_p
&& desc
.const_iter
)
560 niter
= desc
.niter
+ 1;
561 if (niter
== 0) /* We might overflow here. */
564 prob
= (REG_BR_PROB_BASE
565 - (REG_BR_PROB_BASE
+ niter
/2) / niter
);
566 /* Branch prediction algorithm gives 0 frequency for everything
567 after the end of loop for loop having 0 probability to finish. */
568 if (prob
== REG_BR_PROB_BASE
)
569 prob
= REG_BR_PROB_BASE
- 1;
570 predict_edge (desc
.in_edge
, PRED_LOOP_ITERATIONS
,
575 bbs
= get_loop_body (loop
);
577 for (j
= 0; j
< loop
->num_nodes
; j
++)
579 int header_found
= 0;
584 /* Bypass loop heuristics on continue statement. These
585 statements construct loops via "non-loop" constructs
586 in the source language and are better to be handled
588 if ((simpleloops
&& !can_predict_insn_p (BB_END (bb
)))
589 || predicted_by_p (bb
, PRED_CONTINUE
))
592 /* Loop branch heuristics - predict an edge back to a
593 loop's head as taken. */
594 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
595 if (e
->dest
== loop
->header
596 && e
->src
== loop
->latch
)
599 predict_edge_def (e
, PRED_LOOP_BRANCH
, TAKEN
);
602 /* Loop exit heuristics - predict an edge exiting the loop if the
603 conditional has no loop header successors as not taken. */
605 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
606 if (e
->dest
->index
< 0
607 || !flow_bb_inside_loop_p (loop
, e
->dest
))
611 - predictor_info
[(int) PRED_LOOP_EXIT
].hitrate
)
615 /* Free basic blocks from get_loop_body. */
620 /* Statically estimate the probability that a branch will be taken and produce
621 estimated profile. When profile feedback is present never executed portions
622 of function gets estimated. */
625 estimate_probability (struct loops
*loops_info
)
629 connect_infinite_loops_to_exit ();
630 calculate_dominance_info (CDI_DOMINATORS
);
631 calculate_dominance_info (CDI_POST_DOMINATORS
);
633 predict_loops (loops_info
, true);
637 /* Attempt to predict conditional jumps using a number of heuristics. */
640 rtx last_insn
= BB_END (bb
);
644 if (! can_predict_insn_p (last_insn
))
647 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
649 /* Predict early returns to be probable, as we've already taken
650 care for error returns and other are often used for fast paths
652 if ((e
->dest
== EXIT_BLOCK_PTR
653 || (e
->dest
->succ
&& !e
->dest
->succ
->succ_next
654 && e
->dest
->succ
->dest
== EXIT_BLOCK_PTR
))
655 && !predicted_by_p (bb
, PRED_NULL_RETURN
)
656 && !predicted_by_p (bb
, PRED_CONST_RETURN
)
657 && !predicted_by_p (bb
, PRED_NEGATIVE_RETURN
)
658 && !last_basic_block_p (e
->dest
))
659 predict_edge_def (e
, PRED_EARLY_RETURN
, TAKEN
);
661 /* Look for block we are guarding (ie we dominate it,
662 but it doesn't postdominate us). */
663 if (e
->dest
!= EXIT_BLOCK_PTR
&& e
->dest
!= bb
664 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
)
665 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e
->dest
))
669 /* The call heuristic claims that a guarded function call
670 is improbable. This is because such calls are often used
671 to signal exceptional situations such as printing error
673 for (insn
= BB_HEAD (e
->dest
); insn
!= NEXT_INSN (BB_END (e
->dest
));
674 insn
= NEXT_INSN (insn
))
675 if (GET_CODE (insn
) == CALL_INSN
676 /* Constant and pure calls are hardly used to signalize
677 something exceptional. */
678 && ! CONST_OR_PURE_CALL_P (insn
))
680 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
686 cond
= get_condition (last_insn
, &earliest
, false);
690 /* Try "pointer heuristic."
691 A comparison ptr == 0 is predicted as false.
692 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
693 if (COMPARISON_P (cond
)
694 && ((REG_P (XEXP (cond
, 0)) && REG_POINTER (XEXP (cond
, 0)))
695 || (REG_P (XEXP (cond
, 1)) && REG_POINTER (XEXP (cond
, 1)))))
697 if (GET_CODE (cond
) == EQ
)
698 predict_insn_def (last_insn
, PRED_POINTER
, NOT_TAKEN
);
699 else if (GET_CODE (cond
) == NE
)
700 predict_insn_def (last_insn
, PRED_POINTER
, TAKEN
);
704 /* Try "opcode heuristic."
705 EQ tests are usually false and NE tests are usually true. Also,
706 most quantities are positive, so we can make the appropriate guesses
707 about signed comparisons against zero. */
708 switch (GET_CODE (cond
))
711 /* Unconditional branch. */
712 predict_insn_def (last_insn
, PRED_UNCONDITIONAL
,
713 cond
== const0_rtx
? NOT_TAKEN
: TAKEN
);
718 /* Floating point comparisons appears to behave in a very
719 unpredictable way because of special role of = tests in
721 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
723 /* Comparisons with 0 are often used for booleans and there is
724 nothing useful to predict about them. */
725 else if (XEXP (cond
, 1) == const0_rtx
726 || XEXP (cond
, 0) == const0_rtx
)
729 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, NOT_TAKEN
);
734 /* Floating point comparisons appears to behave in a very
735 unpredictable way because of special role of = tests in
737 if (FLOAT_MODE_P (GET_MODE (XEXP (cond
, 0))))
739 /* Comparisons with 0 are often used for booleans and there is
740 nothing useful to predict about them. */
741 else if (XEXP (cond
, 1) == const0_rtx
742 || XEXP (cond
, 0) == const0_rtx
)
745 predict_insn_def (last_insn
, PRED_OPCODE_NONEQUAL
, TAKEN
);
749 predict_insn_def (last_insn
, PRED_FPOPCODE
, TAKEN
);
753 predict_insn_def (last_insn
, PRED_FPOPCODE
, NOT_TAKEN
);
758 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
759 || XEXP (cond
, 1) == constm1_rtx
)
760 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, NOT_TAKEN
);
765 if (XEXP (cond
, 1) == const0_rtx
|| XEXP (cond
, 1) == const1_rtx
766 || XEXP (cond
, 1) == constm1_rtx
)
767 predict_insn_def (last_insn
, PRED_OPCODE_POSITIVE
, TAKEN
);
775 /* Attach the combined probability to each conditional jump. */
777 if (GET_CODE (BB_END (bb
)) == JUMP_INSN
778 && any_condjump_p (BB_END (bb
))
779 && bb
->succ
->succ_next
!= NULL
)
780 combine_predictions_for_insn (BB_END (bb
), bb
);
782 remove_fake_edges ();
783 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
787 rtx last_insn
= BB_END (bb
);
789 if (!can_predict_insn_p (last_insn
))
791 /* We can predict only conditional jumps at the moment.
792 Expect each edge to be equally probable.
793 ?? In the future we want to make abnormal edges improbable. */
797 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
800 if (e
->probability
!= 0)
804 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
805 e
->probability
= (REG_BR_PROB_BASE
+ nedges
/ 2) / nedges
;
808 estimate_bb_frequencies (loops_info
);
809 free_dominance_info (CDI_POST_DOMINATORS
);
813 /* Predict using opcode of the last statement in basic block. */
815 tree_predict_by_opcode (basic_block bb
)
817 tree stmt
= last_stmt (bb
);
823 if (!stmt
|| TREE_CODE (stmt
) != COND_EXPR
)
825 for (then_edge
= bb
->succ
; then_edge
; then_edge
= then_edge
->succ_next
)
826 if (then_edge
->flags
& EDGE_TRUE_VALUE
)
828 cond
= TREE_OPERAND (stmt
, 0);
829 if (TREE_CODE_CLASS (TREE_CODE (cond
)) != '<')
831 op0
= TREE_OPERAND (cond
, 0);
832 type
= TREE_TYPE (op0
);
833 /* Try "pointer heuristic."
834 A comparison ptr == 0 is predicted as false.
835 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
836 if (POINTER_TYPE_P (type
))
838 if (TREE_CODE (cond
) == EQ_EXPR
)
839 predict_edge_def (then_edge
, PRED_TREE_POINTER
, NOT_TAKEN
);
840 else if (TREE_CODE (cond
) == NE_EXPR
)
841 predict_edge_def (then_edge
, PRED_TREE_POINTER
, TAKEN
);
845 /* Try "opcode heuristic."
846 EQ tests are usually false and NE tests are usually true. Also,
847 most quantities are positive, so we can make the appropriate guesses
848 about signed comparisons against zero. */
849 switch (TREE_CODE (cond
))
853 /* Floating point comparisons appears to behave in a very
854 unpredictable way because of special role of = tests in
856 if (FLOAT_TYPE_P (type
))
858 /* Comparisons with 0 are often used for booleans and there is
859 nothing useful to predict about them. */
860 else if (integer_zerop (op0
)
861 || integer_zerop (TREE_OPERAND (cond
, 1)))
864 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, NOT_TAKEN
);
868 /* Floating point comparisons appears to behave in a very
869 unpredictable way because of special role of = tests in
871 if (FLOAT_TYPE_P (type
))
873 /* Comparisons with 0 are often used for booleans and there is
874 nothing useful to predict about them. */
875 else if (integer_zerop (op0
)
876 || integer_zerop (TREE_OPERAND (cond
, 1)))
879 predict_edge_def (then_edge
, PRED_TREE_OPCODE_NONEQUAL
, TAKEN
);
883 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, TAKEN
);
887 predict_edge_def (then_edge
, PRED_TREE_FPOPCODE
, NOT_TAKEN
);
892 if (integer_zerop (TREE_OPERAND (cond
, 1))
893 || integer_onep (TREE_OPERAND (cond
, 1))
894 || integer_all_onesp (TREE_OPERAND (cond
, 1))
895 || real_zerop (TREE_OPERAND (cond
, 1))
896 || real_onep (TREE_OPERAND (cond
, 1))
897 || real_minus_onep (TREE_OPERAND (cond
, 1)))
898 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, NOT_TAKEN
);
903 if (integer_zerop (TREE_OPERAND (cond
, 1))
904 || integer_onep (TREE_OPERAND (cond
, 1))
905 || integer_all_onesp (TREE_OPERAND (cond
, 1))
906 || real_zerop (TREE_OPERAND (cond
, 1))
907 || real_onep (TREE_OPERAND (cond
, 1))
908 || real_minus_onep (TREE_OPERAND (cond
, 1)))
909 predict_edge_def (then_edge
, PRED_TREE_OPCODE_POSITIVE
, TAKEN
);
917 /* Predict branch probabilities and estimate profile of the tree CFG. */
919 tree_estimate_probability (void)
922 struct loops loops_info
;
924 flow_loops_find (&loops_info
, LOOP_TREE
);
925 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
926 flow_loops_dump (&loops_info
, dump_file
, NULL
, 0);
928 connect_infinite_loops_to_exit ();
929 calculate_dominance_info (CDI_DOMINATORS
);
930 calculate_dominance_info (CDI_POST_DOMINATORS
);
932 predict_loops (&loops_info
, false);
938 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
940 /* Predict early returns to be probable, as we've already taken
941 care for error returns and other are often used for fast paths
943 if ((e
->dest
== EXIT_BLOCK_PTR
944 || (e
->dest
->succ
&& !e
->dest
->succ
->succ_next
945 && e
->dest
->succ
->dest
== EXIT_BLOCK_PTR
))
946 && !predicted_by_p (bb
, PRED_NULL_RETURN
)
947 && !predicted_by_p (bb
, PRED_CONST_RETURN
)
948 && !predicted_by_p (bb
, PRED_NEGATIVE_RETURN
)
949 && !last_basic_block_p (e
->dest
))
950 predict_edge_def (e
, PRED_EARLY_RETURN
, TAKEN
);
952 /* Look for block we are guarding (ie we dominate it,
953 but it doesn't postdominate us). */
954 if (e
->dest
!= EXIT_BLOCK_PTR
&& e
->dest
!= bb
955 && dominated_by_p (CDI_DOMINATORS
, e
->dest
, e
->src
)
956 && !dominated_by_p (CDI_POST_DOMINATORS
, e
->src
, e
->dest
))
958 block_stmt_iterator bi
;
960 /* The call heuristic claims that a guarded function call
961 is improbable. This is because such calls are often used
962 to signal exceptional situations such as printing error
964 for (bi
= bsi_start (e
->dest
); !bsi_end_p (bi
);
967 tree stmt
= bsi_stmt (bi
);
968 if ((TREE_CODE (stmt
) == CALL_EXPR
969 || (TREE_CODE (stmt
) == MODIFY_EXPR
970 && TREE_CODE (TREE_OPERAND (stmt
, 1)) == CALL_EXPR
))
971 /* Constant and pure calls are hardly used to signalize
972 something exceptional. */
973 && TREE_SIDE_EFFECTS (stmt
))
975 predict_edge_def (e
, PRED_CALL
, NOT_TAKEN
);
981 tree_predict_by_opcode (bb
);
984 combine_predictions_for_bb (dump_file
, bb
);
986 estimate_bb_frequencies (&loops_info
);
987 free_dominance_info (CDI_POST_DOMINATORS
);
988 remove_fake_edges ();
989 flow_loops_free (&loops_info
);
990 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
991 dump_tree_cfg (dump_file
, dump_flags
);
994 /* __builtin_expect dropped tokens into the insn stream describing expected
995 values of registers. Generate branch probabilities based off these
999 expected_value_to_br_prob (void)
1001 rtx insn
, cond
, ev
= NULL_RTX
, ev_reg
= NULL_RTX
;
1003 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1005 switch (GET_CODE (insn
))
1008 /* Look for expected value notes. */
1009 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_EXPECTED_VALUE
)
1011 ev
= NOTE_EXPECTED_VALUE (insn
);
1012 ev_reg
= XEXP (ev
, 0);
1018 /* Never propagate across labels. */
1023 /* Look for simple conditional branches. If we haven't got an
1024 expected value yet, no point going further. */
1025 if (GET_CODE (insn
) != JUMP_INSN
|| ev
== NULL_RTX
1026 || ! any_condjump_p (insn
))
1031 /* Look for insns that clobber the EV register. */
1032 if (ev
&& reg_set_p (ev_reg
, insn
))
1037 /* Collect the branch condition, hopefully relative to EV_REG. */
1038 /* ??? At present we'll miss things like
1039 (expected_value (eq r70 0))
1041 (set r80 (lt r70 r71))
1042 (set pc (if_then_else (ne r80 0) ...))
1043 as canonicalize_condition will render this to us as
1045 Could use cselib to try and reduce this further. */
1046 cond
= XEXP (SET_SRC (pc_set (insn
)), 0);
1047 cond
= canonicalize_condition (insn
, cond
, 0, NULL
, ev_reg
, false);
1048 if (! cond
|| XEXP (cond
, 0) != ev_reg
1049 || GET_CODE (XEXP (cond
, 1)) != CONST_INT
)
1052 /* Substitute and simplify. Given that the expression we're
1053 building involves two constants, we should wind up with either
1055 cond
= gen_rtx_fmt_ee (GET_CODE (cond
), VOIDmode
,
1056 XEXP (ev
, 1), XEXP (cond
, 1));
1057 cond
= simplify_rtx (cond
);
1059 /* Turn the condition into a scaled branch probability. */
1060 if (cond
!= const_true_rtx
&& cond
!= const0_rtx
)
1062 predict_insn_def (insn
, PRED_BUILTIN_EXPECT
,
1063 cond
== const_true_rtx
? TAKEN
: NOT_TAKEN
);
1067 /* Check whether this is the last basic block of function. Commonly
1068 there is one extra common cleanup block. */
1070 last_basic_block_p (basic_block bb
)
1072 if (bb
== EXIT_BLOCK_PTR
)
1075 return (bb
->next_bb
== EXIT_BLOCK_PTR
1076 || (bb
->next_bb
->next_bb
== EXIT_BLOCK_PTR
1077 && bb
->succ
&& !bb
->succ
->succ_next
1078 && bb
->succ
->dest
->next_bb
== EXIT_BLOCK_PTR
));
1081 /* Sets branch probabilities according to PREDiction and
1082 FLAGS. HEADS[bb->index] should be index of basic block in that we
1083 need to alter branch predictions (i.e. the first of our dominators
1084 such that we do not post-dominate it) (but we fill this information
1085 on demand, so -1 may be there in case this was not needed yet). */
1088 process_note_prediction (basic_block bb
, int *heads
, int pred
, int flags
)
1094 taken
= flags
& IS_TAKEN
;
1096 if (heads
[bb
->index
] < 0)
1098 /* This is first time we need this field in heads array; so
1099 find first dominator that we do not post-dominate (we are
1100 using already known members of heads array). */
1101 basic_block ai
= bb
;
1102 basic_block next_ai
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1105 while (heads
[next_ai
->index
] < 0)
1107 if (!dominated_by_p (CDI_POST_DOMINATORS
, next_ai
, bb
))
1109 heads
[next_ai
->index
] = ai
->index
;
1111 next_ai
= get_immediate_dominator (CDI_DOMINATORS
, next_ai
);
1113 if (!dominated_by_p (CDI_POST_DOMINATORS
, next_ai
, bb
))
1114 head
= next_ai
->index
;
1116 head
= heads
[next_ai
->index
];
1117 while (next_ai
!= bb
)
1120 if (heads
[ai
->index
] == ENTRY_BLOCK
)
1121 ai
= ENTRY_BLOCK_PTR
;
1123 ai
= BASIC_BLOCK (heads
[ai
->index
]);
1124 heads
[next_ai
->index
] = head
;
1127 y
= heads
[bb
->index
];
1129 /* Now find the edge that leads to our branch and aply the prediction. */
1131 if (y
== last_basic_block
|| !can_predict_insn_p (BB_END (BASIC_BLOCK (y
))))
1133 for (e
= BASIC_BLOCK (y
)->succ
; e
; e
= e
->succ_next
)
1134 if (e
->dest
->index
>= 0
1135 && dominated_by_p (CDI_POST_DOMINATORS
, e
->dest
, bb
))
1136 predict_edge_def (e
, pred
, taken
);
1139 /* Gathers NOTE_INSN_PREDICTIONs in given basic block and turns them
1140 into branch probabilities. For description of heads array, see
1141 process_note_prediction. */
1144 process_note_predictions (basic_block bb
, int *heads
)
1149 /* Additionally, we check here for blocks with no successors. */
1150 int contained_noreturn_call
= 0;
1151 int was_bb_head
= 0;
1152 int noreturn_block
= 1;
1154 for (insn
= BB_END (bb
); insn
;
1155 was_bb_head
|= (insn
== BB_HEAD (bb
)), insn
= PREV_INSN (insn
))
1157 if (GET_CODE (insn
) != NOTE
)
1163 /* Noreturn calls cause program to exit, therefore they are
1164 always predicted as not taken. */
1165 if (GET_CODE (insn
) == CALL_INSN
1166 && find_reg_note (insn
, REG_NORETURN
, NULL
))
1167 contained_noreturn_call
= 1;
1171 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_PREDICTION
)
1173 int alg
= (int) NOTE_PREDICTION_ALG (insn
);
1174 /* Process single prediction note. */
1175 process_note_prediction (bb
,
1177 alg
, (int) NOTE_PREDICTION_FLAGS (insn
));
1181 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1182 if (!(e
->flags
& EDGE_FAKE
))
1184 if (contained_noreturn_call
)
1186 /* This block ended from other reasons than because of return.
1187 If it is because of noreturn call, this should certainly not
1188 be taken. Otherwise it is probably some error recovery. */
1189 process_note_prediction (bb
, heads
, PRED_NORETURN
, NOT_TAKEN
);
1193 /* Gathers NOTE_INSN_PREDICTIONs and turns them into
1194 branch probabilities. */
1197 note_prediction_to_br_prob (void)
1202 /* To enable handling of noreturn blocks. */
1203 add_noreturn_fake_exit_edges ();
1204 connect_infinite_loops_to_exit ();
1206 calculate_dominance_info (CDI_POST_DOMINATORS
);
1207 calculate_dominance_info (CDI_DOMINATORS
);
1209 heads
= xmalloc (sizeof (int) * last_basic_block
);
1210 memset (heads
, -1, sizeof (int) * last_basic_block
);
1211 heads
[ENTRY_BLOCK_PTR
->next_bb
->index
] = last_basic_block
;
1213 /* Process all prediction notes. */
1216 process_note_predictions (bb
, heads
);
1218 free_dominance_info (CDI_POST_DOMINATORS
);
1219 free_dominance_info (CDI_DOMINATORS
);
1222 remove_fake_edges ();
1225 /* This is used to carry information about basic blocks. It is
1226 attached to the AUX field of the standard CFG block. */
1228 typedef struct block_info_def
1230 /* Estimated frequency of execution of basic_block. */
1233 /* To keep queue of basic blocks to process. */
1236 /* True if block needs to be visited in propagate_freq. */
1237 unsigned int tovisit
:1;
1239 /* Number of predecessors we need to visit first. */
1243 /* Similar information for edges. */
1244 typedef struct edge_info_def
1246 /* In case edge is an loopback edge, the probability edge will be reached
1247 in case header is. Estimated number of iterations of the loop can be
1248 then computed as 1 / (1 - back_edge_prob). */
1249 sreal back_edge_prob
;
1250 /* True if the edge is an loopback edge in the natural loop. */
1251 unsigned int back_edge
:1;
1254 #define BLOCK_INFO(B) ((block_info) (B)->aux)
1255 #define EDGE_INFO(E) ((edge_info) (E)->aux)
1257 /* Helper function for estimate_bb_frequencies.
1258 Propagate the frequencies for LOOP. */
1261 propagate_freq (struct loop
*loop
)
1263 basic_block head
= loop
->header
;
1269 /* For each basic block we need to visit count number of his predecessors
1270 we need to visit first. */
1271 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1273 if (BLOCK_INFO (bb
)->tovisit
)
1277 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1278 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
1280 else if (BLOCK_INFO (e
->src
)->tovisit
1281 && dump_file
&& !EDGE_INFO (e
)->back_edge
)
1283 "Irreducible region hit, ignoring edge to %i->%i\n",
1284 e
->src
->index
, bb
->index
);
1285 BLOCK_INFO (bb
)->npredecessors
= count
;
1289 memcpy (&BLOCK_INFO (head
)->frequency
, &real_one
, sizeof (real_one
));
1291 for (bb
= head
; bb
; bb
= nextbb
)
1293 sreal cyclic_probability
, frequency
;
1295 memcpy (&cyclic_probability
, &real_zero
, sizeof (real_zero
));
1296 memcpy (&frequency
, &real_zero
, sizeof (real_zero
));
1298 nextbb
= BLOCK_INFO (bb
)->next
;
1299 BLOCK_INFO (bb
)->next
= NULL
;
1301 /* Compute frequency of basic block. */
1304 #ifdef ENABLE_CHECKING
1305 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1306 if (BLOCK_INFO (e
->src
)->tovisit
&& !(e
->flags
& EDGE_DFS_BACK
))
1310 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1311 if (EDGE_INFO (e
)->back_edge
)
1313 sreal_add (&cyclic_probability
, &cyclic_probability
,
1314 &EDGE_INFO (e
)->back_edge_prob
);
1316 else if (!(e
->flags
& EDGE_DFS_BACK
))
1320 /* frequency += (e->probability
1321 * BLOCK_INFO (e->src)->frequency /
1322 REG_BR_PROB_BASE); */
1324 sreal_init (&tmp
, e
->probability
, 0);
1325 sreal_mul (&tmp
, &tmp
, &BLOCK_INFO (e
->src
)->frequency
);
1326 sreal_mul (&tmp
, &tmp
, &real_inv_br_prob_base
);
1327 sreal_add (&frequency
, &frequency
, &tmp
);
1330 if (sreal_compare (&cyclic_probability
, &real_zero
) == 0)
1332 memcpy (&BLOCK_INFO (bb
)->frequency
, &frequency
,
1333 sizeof (frequency
));
1337 if (sreal_compare (&cyclic_probability
, &real_almost_one
) > 0)
1339 memcpy (&cyclic_probability
, &real_almost_one
,
1340 sizeof (real_almost_one
));
1343 /* BLOCK_INFO (bb)->frequency = frequency
1344 / (1 - cyclic_probability) */
1346 sreal_sub (&cyclic_probability
, &real_one
, &cyclic_probability
);
1347 sreal_div (&BLOCK_INFO (bb
)->frequency
,
1348 &frequency
, &cyclic_probability
);
1352 BLOCK_INFO (bb
)->tovisit
= 0;
1354 /* Compute back edge frequencies. */
1355 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1356 if (e
->dest
== head
)
1360 /* EDGE_INFO (e)->back_edge_prob
1361 = ((e->probability * BLOCK_INFO (bb)->frequency)
1362 / REG_BR_PROB_BASE); */
1364 sreal_init (&tmp
, e
->probability
, 0);
1365 sreal_mul (&tmp
, &tmp
, &BLOCK_INFO (bb
)->frequency
);
1366 sreal_mul (&EDGE_INFO (e
)->back_edge_prob
,
1367 &tmp
, &real_inv_br_prob_base
);
1370 /* Propagate to successor blocks. */
1371 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1372 if (!(e
->flags
& EDGE_DFS_BACK
)
1373 && BLOCK_INFO (e
->dest
)->npredecessors
)
1375 BLOCK_INFO (e
->dest
)->npredecessors
--;
1376 if (!BLOCK_INFO (e
->dest
)->npredecessors
)
1381 BLOCK_INFO (last
)->next
= e
->dest
;
1389 /* Estimate probabilities of loopback edges in loops at same nest level. */
1392 estimate_loops_at_level (struct loop
*first_loop
)
1396 for (loop
= first_loop
; loop
; loop
= loop
->next
)
1402 estimate_loops_at_level (loop
->inner
);
1404 if (loop
->latch
->succ
) /* Do not do this for dummy function loop. */
1406 /* Find current loop back edge and mark it. */
1407 e
= loop_latch_edge (loop
);
1408 EDGE_INFO (e
)->back_edge
= 1;
1411 bbs
= get_loop_body (loop
);
1412 for (i
= 0; i
< loop
->num_nodes
; i
++)
1413 BLOCK_INFO (bbs
[i
])->tovisit
= 1;
1415 propagate_freq (loop
);
1419 /* Convert counts measured by profile driven feedback to frequencies.
1420 Return nonzero iff there was any nonzero execution count. */
1423 counts_to_freqs (void)
1425 gcov_type count_max
, true_count_max
= 0;
1429 true_count_max
= MAX (bb
->count
, true_count_max
);
1431 count_max
= MAX (true_count_max
, 1);
1432 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1433 bb
->frequency
= (bb
->count
* BB_FREQ_MAX
+ count_max
/ 2) / count_max
;
1434 return true_count_max
;
1437 /* Return true if function is likely to be expensive, so there is no point to
1438 optimize performance of prologue, epilogue or do inlining at the expense
1439 of code size growth. THRESHOLD is the limit of number of instructions
1440 function can execute at average to be still considered not expensive. */
1443 expensive_function_p (int threshold
)
1445 unsigned int sum
= 0;
1449 /* We can not compute accurately for large thresholds due to scaled
1451 if (threshold
> BB_FREQ_MAX
)
1454 /* Frequencies are out of range. This either means that function contains
1455 internal loop executing more than BB_FREQ_MAX times or profile feedback
1456 is available and function has not been executed at all. */
1457 if (ENTRY_BLOCK_PTR
->frequency
== 0)
1460 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
1461 limit
= ENTRY_BLOCK_PTR
->frequency
* threshold
;
1466 for (insn
= BB_HEAD (bb
); insn
!= NEXT_INSN (BB_END (bb
));
1467 insn
= NEXT_INSN (insn
))
1468 if (active_insn_p (insn
))
1470 sum
+= bb
->frequency
;
1479 /* Estimate basic blocks frequency by given branch probabilities. */
1482 estimate_bb_frequencies (struct loops
*loops
)
1487 if (!flag_branch_probabilities
|| !counts_to_freqs ())
1489 static int real_values_initialized
= 0;
1491 if (!real_values_initialized
)
1493 real_values_initialized
= 1;
1494 sreal_init (&real_zero
, 0, 0);
1495 sreal_init (&real_one
, 1, 0);
1496 sreal_init (&real_br_prob_base
, REG_BR_PROB_BASE
, 0);
1497 sreal_init (&real_bb_freq_max
, BB_FREQ_MAX
, 0);
1498 sreal_init (&real_one_half
, 1, -1);
1499 sreal_div (&real_inv_br_prob_base
, &real_one
, &real_br_prob_base
);
1500 sreal_sub (&real_almost_one
, &real_one
, &real_inv_br_prob_base
);
1503 mark_dfs_back_edges ();
1505 ENTRY_BLOCK_PTR
->succ
->probability
= REG_BR_PROB_BASE
;
1507 /* Set up block info for each basic block. */
1508 alloc_aux_for_blocks (sizeof (struct block_info_def
));
1509 alloc_aux_for_edges (sizeof (struct edge_info_def
));
1510 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1514 BLOCK_INFO (bb
)->tovisit
= 0;
1515 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1517 sreal_init (&EDGE_INFO (e
)->back_edge_prob
, e
->probability
, 0);
1518 sreal_mul (&EDGE_INFO (e
)->back_edge_prob
,
1519 &EDGE_INFO (e
)->back_edge_prob
,
1520 &real_inv_br_prob_base
);
1524 /* First compute probabilities locally for each loop from innermost
1525 to outermost to examine probabilities for back edges. */
1526 estimate_loops_at_level (loops
->tree_root
);
1528 memcpy (&freq_max
, &real_zero
, sizeof (real_zero
));
1530 if (sreal_compare (&freq_max
, &BLOCK_INFO (bb
)->frequency
) < 0)
1531 memcpy (&freq_max
, &BLOCK_INFO (bb
)->frequency
, sizeof (freq_max
));
1533 sreal_div (&freq_max
, &real_bb_freq_max
, &freq_max
);
1534 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1538 sreal_mul (&tmp
, &BLOCK_INFO (bb
)->frequency
, &freq_max
);
1539 sreal_add (&tmp
, &tmp
, &real_one_half
);
1540 bb
->frequency
= sreal_to_int (&tmp
);
1543 free_aux_for_blocks ();
1544 free_aux_for_edges ();
1546 compute_function_frequency ();
1547 if (flag_reorder_functions
)
1548 choose_function_section ();
1551 /* Decide whether function is hot, cold or unlikely executed. */
1553 compute_function_frequency (void)
1557 if (!profile_info
|| !flag_branch_probabilities
)
1559 cfun
->function_frequency
= FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
;
1562 if (maybe_hot_bb_p (bb
))
1564 cfun
->function_frequency
= FUNCTION_FREQUENCY_HOT
;
1567 if (!probably_never_executed_bb_p (bb
))
1568 cfun
->function_frequency
= FUNCTION_FREQUENCY_NORMAL
;
1572 /* Choose appropriate section for the function. */
1574 choose_function_section (void)
1576 if (DECL_SECTION_NAME (current_function_decl
)
1577 || !targetm
.have_named_sections
1578 /* Theoretically we can split the gnu.linkonce text section too,
1579 but this requires more work as the frequency needs to match
1580 for all generated objects so we need to merge the frequency
1581 of all instances. For now just never set frequency for these. */
1582 || DECL_ONE_ONLY (current_function_decl
))
1584 if (cfun
->function_frequency
== FUNCTION_FREQUENCY_HOT
)
1585 DECL_SECTION_NAME (current_function_decl
) =
1586 build_string (strlen (HOT_TEXT_SECTION_NAME
), HOT_TEXT_SECTION_NAME
);
1587 if (cfun
->function_frequency
== FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
)
1588 DECL_SECTION_NAME (current_function_decl
) =
1589 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME
),
1590 UNLIKELY_EXECUTED_TEXT_SECTION_NAME
);
1594 struct tree_opt_pass pass_profile
=
1596 "profile", /* name */
1598 tree_estimate_probability
, /* execute */
1601 0, /* static_pass_number */
1602 TV_BRANCH_PROB
, /* tv_id */
1603 PROP_cfg
, /* properties_required */
1604 0, /* properties_provided */
1605 0, /* properties_destroyed */
1606 0, /* todo_flags_start */
1607 TODO_ggc_collect
| TODO_verify_ssa
/* todo_flags_finish */