* cp-tree.h (enum cp_storage_class): Remove trailing comma.
[official-gcc.git] / gcc / predict.c
blob7d97040e1f88e7cf486280dc6713ae0516681d11
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
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 /* References:
23 [1] "Branch Prediction for Free"
24 Ball and Larus; PLDI '93.
25 [2] "Static Branch Frequency and Program Profile Analysis"
26 Wu and Larus; MICRO-27.
27 [3] "Corpus-based Static Branch Prediction"
28 Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "tm_p.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "insn-config.h"
41 #include "regs.h"
42 #include "flags.h"
43 #include "output.h"
44 #include "function.h"
45 #include "except.h"
46 #include "toplev.h"
47 #include "recog.h"
48 #include "expr.h"
49 #include "predict.h"
50 #include "coverage.h"
51 #include "sreal.h"
52 #include "params.h"
53 #include "target.h"
54 #include "loop.h"
55 #include "cfgloop.h"
56 #include "tree-flow.h"
57 #include "ggc.h"
58 #include "tree-dump.h"
59 #include "tree-pass.h"
60 #include "timevar.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. */
89 struct predictor_info
91 const char *const name; /* Name used in the debugging dumps. */
92 const int hitrate; /* Expected hitrate used by
93 predict_insn_def call. */
94 const int flags;
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. */
110 {NULL, 0, 0}
112 #undef DEF_PREDICTOR
114 /* Return true in case BB can be CPU intensive and should be optimized
115 for maximal performance. */
117 bool
118 maybe_hot_bb_p (basic_block bb)
120 if (profile_info && flag_branch_probabilities
121 && (bb->count
122 < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
123 return false;
124 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
125 return false;
126 return true;
129 /* Return true in case BB is cold and should be optimized for size. */
131 bool
132 probably_cold_bb_p (basic_block bb)
134 if (profile_info && flag_branch_probabilities
135 && (bb->count
136 < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
137 return true;
138 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
139 return true;
140 return false;
143 /* Return true in case BB is probably never executed. */
144 bool
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;
149 return false;
152 /* Return true if the one of outgoing edges is already predicted by
153 PREDICTOR. */
155 bool
156 rtl_predicted_by_p (basic_block bb, enum br_predictor predictor)
158 rtx note;
159 if (!INSN_P (BB_END (bb)))
160 return false;
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)
164 return true;
165 return false;
168 /* Return true if the one of outgoing edges is already predicted by
169 PREDICTOR. */
171 bool
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)
177 return true;
178 return false;
181 void
182 predict_insn (rtx insn, enum br_predictor predictor, int probability)
184 if (!any_condjump_p (insn))
185 abort ();
186 if (!flag_guess_branch_prob)
187 return;
189 REG_NOTES (insn)
190 = gen_rtx_EXPR_LIST (REG_BR_PRED,
191 gen_rtx_CONCAT (VOIDmode,
192 GEN_INT ((int) predictor),
193 GEN_INT ((int) probability)),
194 REG_NOTES (insn));
197 /* Predict insn by given predictor. */
199 void
200 predict_insn_def (rtx insn, enum br_predictor predictor,
201 enum prediction taken)
203 int probability = predictor_info[(int) predictor].hitrate;
205 if (taken != TAKEN)
206 probability = REG_BR_PROB_BASE - probability;
208 predict_insn (insn, predictor, probability);
211 /* Predict edge E with given probability if possible. */
213 void
214 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
216 rtx last_insn;
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))
222 return;
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. */
232 void
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;
241 i->edge = e;
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. */
247 static bool
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. */
257 void
258 predict_edge_def (edge e, enum br_predictor predictor,
259 enum prediction taken)
261 int probability = predictor_info[(int) predictor].hitrate;
263 if (taken != TAKEN)
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. */
272 void
273 invert_br_probabilities (rtx insn)
275 rtx note;
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. */
287 static void
288 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
289 basic_block bb, int used)
291 edge e = bb->succ;
293 if (!file)
294 return;
296 while (e && (e->flags & EDGE_FALLTHRU))
297 e = e->succ_next;
299 fprintf (file, " %s heuristics%s: %.1f%%",
300 predictor_info[predictor].name,
301 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
303 if (bb->count)
305 fprintf (file, " exec ");
306 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
307 if (e)
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. */
321 static void
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 = &REG_NOTES (insn);
326 rtx note;
327 int best_probability = PROB_EVEN;
328 int best_predictor = END_PREDICTORS;
329 int combined_probability = REG_BR_PROB_BASE / 2;
330 int d;
331 bool first_match = false;
332 bool found = false;
334 if (dump_file)
335 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
336 bb->index);
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));
346 found = true;
347 if (best_predictor > predictor)
348 best_probability = probability, best_predictor = predictor;
350 d = (combined_probability * probability
351 + (REG_BR_PROB_BASE - combined_probability)
352 * (REG_BR_PROB_BASE - probability));
354 /* Use FP math to avoid overflows of 32bit integers. */
355 if (d == 0)
356 /* If one probability is 0% and one 100%, avoid division by zero. */
357 combined_probability = REG_BR_PROB_BASE / 2;
358 else
359 combined_probability = (((double) combined_probability) * probability
360 * REG_BR_PROB_BASE / d + 0.5);
363 /* Decide which heuristic to use. In case we didn't match anything,
364 use no_prediction heuristic, in case we did match, use either
365 first match or Dempster-Shaffer theory depending on the flags. */
367 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
368 first_match = true;
370 if (!found)
371 dump_prediction (dump_file, PRED_NO_PREDICTION,
372 combined_probability, bb, true);
373 else
375 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
376 bb, !first_match);
377 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
378 bb, first_match);
381 if (first_match)
382 combined_probability = best_probability;
383 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
385 while (*pnote)
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);
396 else
397 pnote = &XEXP (*pnote, 1);
400 if (!prob_note)
402 REG_NOTES (insn)
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
407 conditional jump. */
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. */
420 static void
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;
426 int d;
427 bool first_match = false;
428 bool found = false;
429 struct edge_prediction *pred;
430 int nedges = 0;
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)))
436 nedges ++;
437 if (first && !second)
438 second = e;
439 if (!first)
440 first = e;
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
448 this later. */
449 if (nedges != 2)
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;
454 else
455 e->probability = 0;
456 bb_ann (bb)->predictions = NULL;
457 if (file)
458 fprintf (file, "%i edges in bb %i predicted to even probabilities\n",
459 nedges, bb->index);
460 return;
463 if (file)
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;
476 found = true;
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. */
485 if (d == 0)
486 /* If one probability is 0% and one 100%, avoid division by zero. */
487 combined_probability = REG_BR_PROB_BASE / 2;
488 else
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)
498 first_match = true;
500 if (!found)
501 dump_prediction (file, PRED_NO_PREDICTION, combined_probability, bb, true);
502 else
504 dump_prediction (file, PRED_DS_THEORY, combined_probability, bb,
505 !first_match);
506 dump_prediction (file, PRED_FIRST_MATCH, best_probability, bb,
507 first_match);
510 if (first_match)
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
532 RTL. */
533 static void
534 predict_loops (struct loops *loops_info, bool simpleloops)
536 unsigned i;
538 /* Try to predict out blocks in a loop that are not part of a
539 natural loop. */
540 for (i = 1; i < loops_info->num; i++)
542 basic_block bb, *bbs;
543 unsigned j;
544 int exits;
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;
552 if (simpleloops)
554 iv_analysis_loop_init (loop);
555 find_simple_exit (loop, &desc);
557 if (desc.simple_p && desc.const_iter)
559 int prob;
560 niter = desc.niter + 1;
561 if (niter == 0) /* We might overflow here. */
562 niter = desc.niter;
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,
571 prob);
575 bbs = get_loop_body (loop);
577 for (j = 0; j < loop->num_nodes; j++)
579 int header_found = 0;
580 edge e;
582 bb = bbs[j];
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
587 separately. */
588 if ((simpleloops && !can_predict_insn_p (BB_END (bb)))
589 || predicted_by_p (bb, PRED_CONTINUE))
590 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)
598 header_found = 1;
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. */
604 if (!header_found)
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))
608 predict_edge
609 (e, PRED_LOOP_EXIT,
610 (REG_BR_PROB_BASE
611 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
612 / exits);
615 /* Free basic blocks from get_loop_body. */
616 free (bbs);
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. */
624 void
625 estimate_probability (struct loops *loops_info)
627 basic_block bb;
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);
635 iv_analysis_done ();
637 /* Attempt to predict conditional jumps using a number of heuristics. */
638 FOR_EACH_BB (bb)
640 rtx last_insn = BB_END (bb);
641 rtx cond, earliest;
642 edge e;
644 if (! can_predict_insn_p (last_insn))
645 continue;
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
651 trought function. */
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))
667 rtx insn;
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
672 messages. */
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);
681 break;
686 cond = get_condition (last_insn, &earliest, false);
687 if (! cond)
688 continue;
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);
702 else
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))
710 case CONST_INT:
711 /* Unconditional branch. */
712 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
713 cond == const0_rtx ? NOT_TAKEN : TAKEN);
714 break;
716 case EQ:
717 case UNEQ:
718 /* Floating point comparisons appears to behave in a very
719 unpredictable way because of special role of = tests in
720 FP code. */
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)
728 else
729 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
730 break;
732 case NE:
733 case LTGT:
734 /* Floating point comparisons appears to behave in a very
735 unpredictable way because of special role of = tests in
736 FP code. */
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)
744 else
745 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
746 break;
748 case ORDERED:
749 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
750 break;
752 case UNORDERED:
753 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
754 break;
756 case LE:
757 case LT:
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);
761 break;
763 case GE:
764 case GT:
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);
768 break;
770 default:
771 break;
775 /* Attach the combined probability to each conditional jump. */
776 FOR_EACH_BB (bb)
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
784 notes. */
785 FOR_EACH_BB (bb)
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. */
794 int nedges = 0;
795 edge e;
797 for (e = bb->succ; e; e = e->succ_next)
799 nedges++;
800 if (e->probability != 0)
801 break;
803 if (!e)
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. */
814 static void
815 tree_predict_by_opcode (basic_block bb)
817 tree stmt = last_stmt (bb);
818 edge then_edge;
819 tree cond;
820 tree op0;
821 tree type;
823 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
824 return;
825 for (then_edge = bb->succ; then_edge; then_edge = then_edge->succ_next)
826 if (then_edge->flags & EDGE_TRUE_VALUE)
827 break;
828 cond = TREE_OPERAND (stmt, 0);
829 if (TREE_CODE_CLASS (TREE_CODE (cond)) != '<')
830 return;
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);
843 else
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))
851 case EQ_EXPR:
852 case UNEQ_EXPR:
853 /* Floating point comparisons appears to behave in a very
854 unpredictable way because of special role of = tests in
855 FP code. */
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)))
863 else
864 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
865 break;
867 case NE_EXPR:
868 case LTGT_EXPR:
869 /* Floating point comparisons appears to behave in a very
870 unpredictable way because of special role of = tests in
871 FP code. */
872 if (FLOAT_TYPE_P (type))
874 /* Comparisons with 0 are often used for booleans and there is
875 nothing useful to predict about them. */
876 else if (integer_zerop (op0)
877 || integer_zerop (TREE_OPERAND (cond, 1)))
879 else
880 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
881 break;
883 case ORDERED_EXPR:
884 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
885 break;
887 case UNORDERED_EXPR:
888 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
889 break;
891 case LE_EXPR:
892 case LT_EXPR:
893 if (integer_zerop (TREE_OPERAND (cond, 1))
894 || integer_onep (TREE_OPERAND (cond, 1))
895 || integer_all_onesp (TREE_OPERAND (cond, 1))
896 || real_zerop (TREE_OPERAND (cond, 1))
897 || real_onep (TREE_OPERAND (cond, 1))
898 || real_minus_onep (TREE_OPERAND (cond, 1)))
899 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
900 break;
902 case GE_EXPR:
903 case GT_EXPR:
904 if (integer_zerop (TREE_OPERAND (cond, 1))
905 || integer_onep (TREE_OPERAND (cond, 1))
906 || integer_all_onesp (TREE_OPERAND (cond, 1))
907 || real_zerop (TREE_OPERAND (cond, 1))
908 || real_onep (TREE_OPERAND (cond, 1))
909 || real_minus_onep (TREE_OPERAND (cond, 1)))
910 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
911 break;
913 default:
914 break;
918 /* Predict branch probabilities and estimate profile of the tree CFG. */
919 static void
920 tree_estimate_probability (void)
922 basic_block bb;
923 struct loops loops_info;
925 flow_loops_find (&loops_info, LOOP_TREE);
926 if (dump_file && (dump_flags & TDF_DETAILS))
927 flow_loops_dump (&loops_info, dump_file, NULL, 0);
929 connect_infinite_loops_to_exit ();
930 calculate_dominance_info (CDI_DOMINATORS);
931 calculate_dominance_info (CDI_POST_DOMINATORS);
933 predict_loops (&loops_info, false);
935 FOR_EACH_BB (bb)
937 edge e;
939 for (e = bb->succ; e; e = e->succ_next)
941 /* Predict early returns to be probable, as we've already taken
942 care for error returns and other are often used for fast paths
943 trought function. */
944 if ((e->dest == EXIT_BLOCK_PTR
945 || (e->dest->succ && !e->dest->succ->succ_next
946 && e->dest->succ->dest == EXIT_BLOCK_PTR))
947 && !predicted_by_p (bb, PRED_NULL_RETURN)
948 && !predicted_by_p (bb, PRED_CONST_RETURN)
949 && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
950 && !last_basic_block_p (e->dest))
951 predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
953 /* Look for block we are guarding (ie we dominate it,
954 but it doesn't postdominate us). */
955 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
956 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
957 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
959 block_stmt_iterator bi;
961 /* The call heuristic claims that a guarded function call
962 is improbable. This is because such calls are often used
963 to signal exceptional situations such as printing error
964 messages. */
965 for (bi = bsi_start (e->dest); !bsi_end_p (bi);
966 bsi_next (&bi))
968 tree stmt = bsi_stmt (bi);
969 if ((TREE_CODE (stmt) == CALL_EXPR
970 || (TREE_CODE (stmt) == MODIFY_EXPR
971 && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR))
972 /* Constant and pure calls are hardly used to signalize
973 something exceptional. */
974 && TREE_SIDE_EFFECTS (stmt))
976 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
977 break;
982 tree_predict_by_opcode (bb);
984 FOR_EACH_BB (bb)
985 combine_predictions_for_bb (dump_file, bb);
987 estimate_bb_frequencies (&loops_info);
988 free_dominance_info (CDI_POST_DOMINATORS);
989 remove_fake_edges ();
990 flow_loops_free (&loops_info);
991 if (dump_file && (dump_flags & TDF_DETAILS))
992 dump_tree_cfg (dump_file, dump_flags);
995 /* __builtin_expect dropped tokens into the insn stream describing expected
996 values of registers. Generate branch probabilities based off these
997 values. */
999 void
1000 expected_value_to_br_prob (void)
1002 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
1004 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1006 switch (GET_CODE (insn))
1008 case NOTE:
1009 /* Look for expected value notes. */
1010 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
1012 ev = NOTE_EXPECTED_VALUE (insn);
1013 ev_reg = XEXP (ev, 0);
1014 delete_insn (insn);
1016 continue;
1018 case CODE_LABEL:
1019 /* Never propagate across labels. */
1020 ev = NULL_RTX;
1021 continue;
1023 case JUMP_INSN:
1024 /* Look for simple conditional branches. If we haven't got an
1025 expected value yet, no point going further. */
1026 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX
1027 || ! any_condjump_p (insn))
1028 continue;
1029 break;
1031 default:
1032 /* Look for insns that clobber the EV register. */
1033 if (ev && reg_set_p (ev_reg, insn))
1034 ev = NULL_RTX;
1035 continue;
1038 /* Collect the branch condition, hopefully relative to EV_REG. */
1039 /* ??? At present we'll miss things like
1040 (expected_value (eq r70 0))
1041 (set r71 -1)
1042 (set r80 (lt r70 r71))
1043 (set pc (if_then_else (ne r80 0) ...))
1044 as canonicalize_condition will render this to us as
1045 (lt r70, r71)
1046 Could use cselib to try and reduce this further. */
1047 cond = XEXP (SET_SRC (pc_set (insn)), 0);
1048 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg, false);
1049 if (! cond || XEXP (cond, 0) != ev_reg
1050 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
1051 continue;
1053 /* Substitute and simplify. Given that the expression we're
1054 building involves two constants, we should wind up with either
1055 true or false. */
1056 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
1057 XEXP (ev, 1), XEXP (cond, 1));
1058 cond = simplify_rtx (cond);
1060 /* Turn the condition into a scaled branch probability. */
1061 if (cond != const_true_rtx && cond != const0_rtx)
1062 abort ();
1063 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
1064 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
1068 /* Check whether this is the last basic block of function. Commonly
1069 there is one extra common cleanup block. */
1070 static bool
1071 last_basic_block_p (basic_block bb)
1073 if (bb == EXIT_BLOCK_PTR)
1074 return false;
1076 return (bb->next_bb == EXIT_BLOCK_PTR
1077 || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
1078 && bb->succ && !bb->succ->succ_next
1079 && bb->succ->dest->next_bb == EXIT_BLOCK_PTR));
1082 /* Sets branch probabilities according to PREDiction and
1083 FLAGS. HEADS[bb->index] should be index of basic block in that we
1084 need to alter branch predictions (i.e. the first of our dominators
1085 such that we do not post-dominate it) (but we fill this information
1086 on demand, so -1 may be there in case this was not needed yet). */
1088 static void
1089 process_note_prediction (basic_block bb, int *heads, int pred, int flags)
1091 edge e;
1092 int y;
1093 bool taken;
1095 taken = flags & IS_TAKEN;
1097 if (heads[bb->index] < 0)
1099 /* This is first time we need this field in heads array; so
1100 find first dominator that we do not post-dominate (we are
1101 using already known members of heads array). */
1102 basic_block ai = bb;
1103 basic_block next_ai = get_immediate_dominator (CDI_DOMINATORS, bb);
1104 int head;
1106 while (heads[next_ai->index] < 0)
1108 if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb))
1109 break;
1110 heads[next_ai->index] = ai->index;
1111 ai = next_ai;
1112 next_ai = get_immediate_dominator (CDI_DOMINATORS, next_ai);
1114 if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb))
1115 head = next_ai->index;
1116 else
1117 head = heads[next_ai->index];
1118 while (next_ai != bb)
1120 next_ai = ai;
1121 if (heads[ai->index] == ENTRY_BLOCK)
1122 ai = ENTRY_BLOCK_PTR;
1123 else
1124 ai = BASIC_BLOCK (heads[ai->index]);
1125 heads[next_ai->index] = head;
1128 y = heads[bb->index];
1130 /* Now find the edge that leads to our branch and aply the prediction. */
1132 if (y == last_basic_block || !can_predict_insn_p (BB_END (BASIC_BLOCK (y))))
1133 return;
1134 for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next)
1135 if (e->dest->index >= 0
1136 && dominated_by_p (CDI_POST_DOMINATORS, e->dest, bb))
1137 predict_edge_def (e, pred, taken);
1140 /* Gathers NOTE_INSN_PREDICTIONs in given basic block and turns them
1141 into branch probabilities. For description of heads array, see
1142 process_note_prediction. */
1144 static void
1145 process_note_predictions (basic_block bb, int *heads)
1147 rtx insn;
1148 edge e;
1150 /* Additionally, we check here for blocks with no successors. */
1151 int contained_noreturn_call = 0;
1152 int was_bb_head = 0;
1153 int noreturn_block = 1;
1155 for (insn = BB_END (bb); insn;
1156 was_bb_head |= (insn == BB_HEAD (bb)), insn = PREV_INSN (insn))
1158 if (GET_CODE (insn) != NOTE)
1160 if (was_bb_head)
1161 break;
1162 else
1164 /* Noreturn calls cause program to exit, therefore they are
1165 always predicted as not taken. */
1166 if (GET_CODE (insn) == CALL_INSN
1167 && find_reg_note (insn, REG_NORETURN, NULL))
1168 contained_noreturn_call = 1;
1169 continue;
1172 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION)
1174 int alg = (int) NOTE_PREDICTION_ALG (insn);
1175 /* Process single prediction note. */
1176 process_note_prediction (bb,
1177 heads,
1178 alg, (int) NOTE_PREDICTION_FLAGS (insn));
1179 delete_insn (insn);
1182 for (e = bb->succ; e; e = e->succ_next)
1183 if (!(e->flags & EDGE_FAKE))
1184 noreturn_block = 0;
1185 if (contained_noreturn_call)
1187 /* This block ended from other reasons than because of return.
1188 If it is because of noreturn call, this should certainly not
1189 be taken. Otherwise it is probably some error recovery. */
1190 process_note_prediction (bb, heads, PRED_NORETURN, NOT_TAKEN);
1194 /* Gathers NOTE_INSN_PREDICTIONs and turns them into
1195 branch probabilities. */
1197 void
1198 note_prediction_to_br_prob (void)
1200 basic_block bb;
1201 int *heads;
1203 /* To enable handling of noreturn blocks. */
1204 add_noreturn_fake_exit_edges ();
1205 connect_infinite_loops_to_exit ();
1207 calculate_dominance_info (CDI_POST_DOMINATORS);
1208 calculate_dominance_info (CDI_DOMINATORS);
1210 heads = xmalloc (sizeof (int) * last_basic_block);
1211 memset (heads, -1, sizeof (int) * last_basic_block);
1212 heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block;
1214 /* Process all prediction notes. */
1216 FOR_EACH_BB (bb)
1217 process_note_predictions (bb, heads);
1219 free_dominance_info (CDI_POST_DOMINATORS);
1220 free_dominance_info (CDI_DOMINATORS);
1221 free (heads);
1223 remove_fake_edges ();
1226 /* This is used to carry information about basic blocks. It is
1227 attached to the AUX field of the standard CFG block. */
1229 typedef struct block_info_def
1231 /* Estimated frequency of execution of basic_block. */
1232 sreal frequency;
1234 /* To keep queue of basic blocks to process. */
1235 basic_block next;
1237 /* True if block needs to be visited in propagate_freq. */
1238 unsigned int tovisit:1;
1240 /* Number of predecessors we need to visit first. */
1241 int npredecessors;
1242 } *block_info;
1244 /* Similar information for edges. */
1245 typedef struct edge_info_def
1247 /* In case edge is an loopback edge, the probability edge will be reached
1248 in case header is. Estimated number of iterations of the loop can be
1249 then computed as 1 / (1 - back_edge_prob). */
1250 sreal back_edge_prob;
1251 /* True if the edge is an loopback edge in the natural loop. */
1252 unsigned int back_edge:1;
1253 } *edge_info;
1255 #define BLOCK_INFO(B) ((block_info) (B)->aux)
1256 #define EDGE_INFO(E) ((edge_info) (E)->aux)
1258 /* Helper function for estimate_bb_frequencies.
1259 Propagate the frequencies for LOOP. */
1261 static void
1262 propagate_freq (struct loop *loop)
1264 basic_block head = loop->header;
1265 basic_block bb;
1266 basic_block last;
1267 edge e;
1268 basic_block nextbb;
1270 /* For each basic block we need to visit count number of his predecessors
1271 we need to visit first. */
1272 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1274 if (BLOCK_INFO (bb)->tovisit)
1276 int count = 0;
1278 for (e = bb->pred; e; e = e->pred_next)
1279 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
1280 count++;
1281 else if (BLOCK_INFO (e->src)->tovisit
1282 && dump_file && !EDGE_INFO (e)->back_edge)
1283 fprintf (dump_file,
1284 "Irreducible region hit, ignoring edge to %i->%i\n",
1285 e->src->index, bb->index);
1286 BLOCK_INFO (bb)->npredecessors = count;
1290 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
1291 last = head;
1292 for (bb = head; bb; bb = nextbb)
1294 sreal cyclic_probability, frequency;
1296 memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
1297 memcpy (&frequency, &real_zero, sizeof (real_zero));
1299 nextbb = BLOCK_INFO (bb)->next;
1300 BLOCK_INFO (bb)->next = NULL;
1302 /* Compute frequency of basic block. */
1303 if (bb != head)
1305 #ifdef ENABLE_CHECKING
1306 for (e = bb->pred; e; e = e->pred_next)
1307 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
1308 abort ();
1309 #endif
1311 for (e = bb->pred; e; e = e->pred_next)
1312 if (EDGE_INFO (e)->back_edge)
1314 sreal_add (&cyclic_probability, &cyclic_probability,
1315 &EDGE_INFO (e)->back_edge_prob);
1317 else if (!(e->flags & EDGE_DFS_BACK))
1319 sreal tmp;
1321 /* frequency += (e->probability
1322 * BLOCK_INFO (e->src)->frequency /
1323 REG_BR_PROB_BASE); */
1325 sreal_init (&tmp, e->probability, 0);
1326 sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency);
1327 sreal_mul (&tmp, &tmp, &real_inv_br_prob_base);
1328 sreal_add (&frequency, &frequency, &tmp);
1331 if (sreal_compare (&cyclic_probability, &real_zero) == 0)
1333 memcpy (&BLOCK_INFO (bb)->frequency, &frequency,
1334 sizeof (frequency));
1336 else
1338 if (sreal_compare (&cyclic_probability, &real_almost_one) > 0)
1340 memcpy (&cyclic_probability, &real_almost_one,
1341 sizeof (real_almost_one));
1344 /* BLOCK_INFO (bb)->frequency = frequency
1345 / (1 - cyclic_probability) */
1347 sreal_sub (&cyclic_probability, &real_one, &cyclic_probability);
1348 sreal_div (&BLOCK_INFO (bb)->frequency,
1349 &frequency, &cyclic_probability);
1353 BLOCK_INFO (bb)->tovisit = 0;
1355 /* Compute back edge frequencies. */
1356 for (e = bb->succ; e; e = e->succ_next)
1357 if (e->dest == head)
1359 sreal tmp;
1361 /* EDGE_INFO (e)->back_edge_prob
1362 = ((e->probability * BLOCK_INFO (bb)->frequency)
1363 / REG_BR_PROB_BASE); */
1365 sreal_init (&tmp, e->probability, 0);
1366 sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency);
1367 sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1368 &tmp, &real_inv_br_prob_base);
1371 /* Propagate to successor blocks. */
1372 for (e = bb->succ; e; e = e->succ_next)
1373 if (!(e->flags & EDGE_DFS_BACK)
1374 && BLOCK_INFO (e->dest)->npredecessors)
1376 BLOCK_INFO (e->dest)->npredecessors--;
1377 if (!BLOCK_INFO (e->dest)->npredecessors)
1379 if (!nextbb)
1380 nextbb = e->dest;
1381 else
1382 BLOCK_INFO (last)->next = e->dest;
1384 last = e->dest;
1390 /* Estimate probabilities of loopback edges in loops at same nest level. */
1392 static void
1393 estimate_loops_at_level (struct loop *first_loop)
1395 struct loop *loop;
1397 for (loop = first_loop; loop; loop = loop->next)
1399 edge e;
1400 basic_block *bbs;
1401 unsigned i;
1403 estimate_loops_at_level (loop->inner);
1405 if (loop->latch->succ) /* Do not do this for dummy function loop. */
1407 /* Find current loop back edge and mark it. */
1408 e = loop_latch_edge (loop);
1409 EDGE_INFO (e)->back_edge = 1;
1412 bbs = get_loop_body (loop);
1413 for (i = 0; i < loop->num_nodes; i++)
1414 BLOCK_INFO (bbs[i])->tovisit = 1;
1415 free (bbs);
1416 propagate_freq (loop);
1420 /* Convert counts measured by profile driven feedback to frequencies.
1421 Return nonzero iff there was any nonzero execution count. */
1423 static int
1424 counts_to_freqs (void)
1426 gcov_type count_max, true_count_max = 0;
1427 basic_block bb;
1429 FOR_EACH_BB (bb)
1430 true_count_max = MAX (bb->count, true_count_max);
1432 count_max = MAX (true_count_max, 1);
1433 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1434 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
1435 return true_count_max;
1438 /* Return true if function is likely to be expensive, so there is no point to
1439 optimize performance of prologue, epilogue or do inlining at the expense
1440 of code size growth. THRESHOLD is the limit of number of instructions
1441 function can execute at average to be still considered not expensive. */
1443 bool
1444 expensive_function_p (int threshold)
1446 unsigned int sum = 0;
1447 basic_block bb;
1448 unsigned int limit;
1450 /* We can not compute accurately for large thresholds due to scaled
1451 frequencies. */
1452 if (threshold > BB_FREQ_MAX)
1453 abort ();
1455 /* Frequencies are out of range. This either means that function contains
1456 internal loop executing more than BB_FREQ_MAX times or profile feedback
1457 is available and function has not been executed at all. */
1458 if (ENTRY_BLOCK_PTR->frequency == 0)
1459 return true;
1461 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
1462 limit = ENTRY_BLOCK_PTR->frequency * threshold;
1463 FOR_EACH_BB (bb)
1465 rtx insn;
1467 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
1468 insn = NEXT_INSN (insn))
1469 if (active_insn_p (insn))
1471 sum += bb->frequency;
1472 if (sum > limit)
1473 return true;
1477 return false;
1480 /* Estimate basic blocks frequency by given branch probabilities. */
1482 static void
1483 estimate_bb_frequencies (struct loops *loops)
1485 basic_block bb;
1486 sreal freq_max;
1488 if (!flag_branch_probabilities || !counts_to_freqs ())
1490 static int real_values_initialized = 0;
1492 if (!real_values_initialized)
1494 real_values_initialized = 1;
1495 sreal_init (&real_zero, 0, 0);
1496 sreal_init (&real_one, 1, 0);
1497 sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0);
1498 sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0);
1499 sreal_init (&real_one_half, 1, -1);
1500 sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base);
1501 sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base);
1504 mark_dfs_back_edges ();
1506 ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
1508 /* Set up block info for each basic block. */
1509 alloc_aux_for_blocks (sizeof (struct block_info_def));
1510 alloc_aux_for_edges (sizeof (struct edge_info_def));
1511 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1513 edge e;
1515 BLOCK_INFO (bb)->tovisit = 0;
1516 for (e = bb->succ; e; e = e->succ_next)
1518 sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0);
1519 sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1520 &EDGE_INFO (e)->back_edge_prob,
1521 &real_inv_br_prob_base);
1525 /* First compute probabilities locally for each loop from innermost
1526 to outermost to examine probabilities for back edges. */
1527 estimate_loops_at_level (loops->tree_root);
1529 memcpy (&freq_max, &real_zero, sizeof (real_zero));
1530 FOR_EACH_BB (bb)
1531 if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0)
1532 memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max));
1534 sreal_div (&freq_max, &real_bb_freq_max, &freq_max);
1535 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1537 sreal tmp;
1539 sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max);
1540 sreal_add (&tmp, &tmp, &real_one_half);
1541 bb->frequency = sreal_to_int (&tmp);
1544 free_aux_for_blocks ();
1545 free_aux_for_edges ();
1547 compute_function_frequency ();
1548 if (flag_reorder_functions)
1549 choose_function_section ();
1552 /* Decide whether function is hot, cold or unlikely executed. */
1553 static void
1554 compute_function_frequency (void)
1556 basic_block bb;
1558 if (!profile_info || !flag_branch_probabilities)
1559 return;
1560 cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
1561 FOR_EACH_BB (bb)
1563 if (maybe_hot_bb_p (bb))
1565 cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
1566 return;
1568 if (!probably_never_executed_bb_p (bb))
1569 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
1573 /* Choose appropriate section for the function. */
1574 static void
1575 choose_function_section (void)
1577 if (DECL_SECTION_NAME (current_function_decl)
1578 || !targetm.have_named_sections
1579 /* Theoretically we can split the gnu.linkonce text section too,
1580 but this requires more work as the frequency needs to match
1581 for all generated objects so we need to merge the frequency
1582 of all instances. For now just never set frequency for these. */
1583 || DECL_ONE_ONLY (current_function_decl))
1584 return;
1585 if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
1586 DECL_SECTION_NAME (current_function_decl) =
1587 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
1588 if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1589 DECL_SECTION_NAME (current_function_decl) =
1590 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
1591 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
1595 struct tree_opt_pass pass_profile =
1597 "profile", /* name */
1598 NULL, /* gate */
1599 tree_estimate_probability, /* execute */
1600 NULL, /* sub */
1601 NULL, /* next */
1602 0, /* static_pass_number */
1603 TV_BRANCH_PROB, /* tv_id */
1604 PROP_cfg, /* properties_required */
1605 0, /* properties_provided */
1606 0, /* properties_destroyed */
1607 0, /* todo_flags_start */
1608 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */