Current state of work.
[official-gcc.git] / gcc / predict.c
blob7168621aee165aa4b9f72123b3a7557c1dee2136
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 (JUMP_P (insn)
251 && any_condjump_p (insn)
252 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succ) >= 2);
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;
292 unsigned ix;
294 if (!file)
295 return;
297 FOR_EACH_EDGE (e, bb->succ, ix)
298 if (! (e->flags & EDGE_FALLTHRU))
299 break;
301 fprintf (file, " %s heuristics%s: %.1f%%",
302 predictor_info[predictor].name,
303 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
305 if (bb->count)
307 fprintf (file, " exec ");
308 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
309 if (e)
311 fprintf (file, " hit ");
312 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
313 fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count);
317 fprintf (file, "\n");
320 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
321 note if not already present. Remove now useless REG_BR_PRED notes. */
323 static void
324 combine_predictions_for_insn (rtx insn, basic_block bb)
326 rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
327 rtx *pnote = &REG_NOTES (insn);
328 rtx note;
329 int best_probability = PROB_EVEN;
330 int best_predictor = END_PREDICTORS;
331 int combined_probability = REG_BR_PROB_BASE / 2;
332 int d;
333 bool first_match = false;
334 bool found = false;
336 if (dump_file)
337 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
338 bb->index);
340 /* We implement "first match" heuristics and use probability guessed
341 by predictor with smallest index. */
342 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
343 if (REG_NOTE_KIND (note) == REG_BR_PRED)
345 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
346 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
348 found = true;
349 if (best_predictor > predictor)
350 best_probability = probability, best_predictor = predictor;
352 d = (combined_probability * probability
353 + (REG_BR_PROB_BASE - combined_probability)
354 * (REG_BR_PROB_BASE - probability));
356 /* Use FP math to avoid overflows of 32bit integers. */
357 if (d == 0)
358 /* If one probability is 0% and one 100%, avoid division by zero. */
359 combined_probability = REG_BR_PROB_BASE / 2;
360 else
361 combined_probability = (((double) combined_probability) * probability
362 * REG_BR_PROB_BASE / d + 0.5);
365 /* Decide which heuristic to use. In case we didn't match anything,
366 use no_prediction heuristic, in case we did match, use either
367 first match or Dempster-Shaffer theory depending on the flags. */
369 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
370 first_match = true;
372 if (!found)
373 dump_prediction (dump_file, PRED_NO_PREDICTION,
374 combined_probability, bb, true);
375 else
377 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
378 bb, !first_match);
379 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
380 bb, first_match);
383 if (first_match)
384 combined_probability = best_probability;
385 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
387 while (*pnote)
389 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
391 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
392 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
394 dump_prediction (dump_file, predictor, probability, bb,
395 !first_match || best_predictor == predictor);
396 *pnote = XEXP (*pnote, 1);
398 else
399 pnote = &XEXP (*pnote, 1);
402 if (!prob_note)
404 REG_NOTES (insn)
405 = gen_rtx_EXPR_LIST (REG_BR_PROB,
406 GEN_INT (combined_probability), REG_NOTES (insn));
408 /* Save the prediction into CFG in case we are seeing non-degenerated
409 conditional jump. */
410 if (EDGE_COUNT (bb->succ) > 1)
412 BRANCH_EDGE (bb)->probability = combined_probability;
413 FALLTHRU_EDGE (bb)->probability
414 = REG_BR_PROB_BASE - combined_probability;
419 /* Combine predictions into single probability and store them into CFG.
420 Remove now useless prediction entries. */
422 static void
423 combine_predictions_for_bb (FILE *file, basic_block bb)
425 int best_probability = PROB_EVEN;
426 int best_predictor = END_PREDICTORS;
427 int combined_probability = REG_BR_PROB_BASE / 2;
428 int d;
429 bool first_match = false;
430 bool found = false;
431 struct edge_prediction *pred;
432 int nedges = 0;
433 edge e, first = NULL, second = NULL;
434 unsigned ix;
436 FOR_EACH_EDGE (e, bb->succ, ix)
437 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
439 nedges ++;
440 if (first && !second)
441 second = e;
442 if (!first)
443 first = e;
446 /* When there is no successor or only one choice, prediction is easy.
448 We are lazy for now and predict only basic blocks with two outgoing
449 edges. It is possible to predict generic case too, but we have to
450 ignore first match heuristics and do more involved combining. Implement
451 this later. */
452 if (nedges != 2)
454 FOR_EACH_EDGE (e, bb->succ, ix)
455 if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
456 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
457 else
458 e->probability = 0;
459 bb_ann (bb)->predictions = NULL;
460 if (file)
461 fprintf (file, "%i edges in bb %i predicted to even probabilities\n",
462 nedges, bb->index);
463 return;
466 if (file)
467 fprintf (file, "Predictions for bb %i\n", bb->index);
469 /* We implement "first match" heuristics and use probability guessed
470 by predictor with smallest index. */
471 for (pred = bb_ann (bb)->predictions; pred; pred = pred->next)
473 int predictor = pred->predictor;
474 int probability = pred->probability;
476 if (pred->edge != first)
477 probability = REG_BR_PROB_BASE - probability;
479 found = true;
480 if (best_predictor > predictor)
481 best_probability = probability, best_predictor = predictor;
483 d = (combined_probability * probability
484 + (REG_BR_PROB_BASE - combined_probability)
485 * (REG_BR_PROB_BASE - probability));
487 /* Use FP math to avoid overflows of 32bit integers. */
488 if (d == 0)
489 /* If one probability is 0% and one 100%, avoid division by zero. */
490 combined_probability = REG_BR_PROB_BASE / 2;
491 else
492 combined_probability = (((double) combined_probability) * probability
493 * REG_BR_PROB_BASE / d + 0.5);
496 /* Decide which heuristic to use. In case we didn't match anything,
497 use no_prediction heuristic, in case we did match, use either
498 first match or Dempster-Shaffer theory depending on the flags. */
500 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
501 first_match = true;
503 if (!found)
504 dump_prediction (file, PRED_NO_PREDICTION, combined_probability, bb, true);
505 else
507 dump_prediction (file, PRED_DS_THEORY, combined_probability, bb,
508 !first_match);
509 dump_prediction (file, PRED_FIRST_MATCH, best_probability, bb,
510 first_match);
513 if (first_match)
514 combined_probability = best_probability;
515 dump_prediction (file, PRED_COMBINED, combined_probability, bb, true);
517 for (pred = bb_ann (bb)->predictions; pred; pred = pred->next)
519 int predictor = pred->predictor;
520 int probability = pred->probability;
522 if (pred->edge != EDGE_0 (bb->succ))
523 probability = REG_BR_PROB_BASE - probability;
524 dump_prediction (file, predictor, probability, bb,
525 !first_match || best_predictor == predictor);
527 bb_ann (bb)->predictions = NULL;
529 first->probability = combined_probability;
530 second->probability = REG_BR_PROB_BASE - combined_probability;
533 /* Predict edge probabilities by exploiting loop structure.
534 When SIMPLELOOPS is set, attempt to count number of iterations by analyzing
535 RTL. */
536 static void
537 predict_loops (struct loops *loops_info, bool simpleloops)
539 unsigned i;
541 /* Try to predict out blocks in a loop that are not part of a
542 natural loop. */
543 for (i = 1; i < loops_info->num; i++)
545 basic_block bb, *bbs;
546 unsigned j;
547 int exits;
548 struct loop *loop = loops_info->parray[i];
549 struct niter_desc desc;
550 unsigned HOST_WIDE_INT niter;
552 flow_loop_scan (loop, LOOP_EXIT_EDGES);
553 exits = loop->num_exits;
555 if (simpleloops)
557 iv_analysis_loop_init (loop);
558 find_simple_exit (loop, &desc);
560 if (desc.simple_p && desc.const_iter)
562 int prob;
563 niter = desc.niter + 1;
564 if (niter == 0) /* We might overflow here. */
565 niter = desc.niter;
567 prob = (REG_BR_PROB_BASE
568 - (REG_BR_PROB_BASE + niter /2) / niter);
569 /* Branch prediction algorithm gives 0 frequency for everything
570 after the end of loop for loop having 0 probability to finish. */
571 if (prob == REG_BR_PROB_BASE)
572 prob = REG_BR_PROB_BASE - 1;
573 predict_edge (desc.in_edge, PRED_LOOP_ITERATIONS,
574 prob);
578 bbs = get_loop_body (loop);
580 for (j = 0; j < loop->num_nodes; j++)
582 int header_found = 0;
583 edge e;
584 unsigned ix;
586 bb = bbs[j];
588 /* Bypass loop heuristics on continue statement. These
589 statements construct loops via "non-loop" constructs
590 in the source language and are better to be handled
591 separately. */
592 if ((simpleloops && !can_predict_insn_p (BB_END (bb)))
593 || predicted_by_p (bb, PRED_CONTINUE))
594 continue;
596 /* Loop branch heuristics - predict an edge back to a
597 loop's head as taken. */
598 FOR_EACH_EDGE (e, bb->succ, ix)
599 if (e->dest == loop->header
600 && e->src == loop->latch)
602 header_found = 1;
603 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
606 /* Loop exit heuristics - predict an edge exiting the loop if the
607 conditional has no loop header successors as not taken. */
608 if (!header_found)
609 FOR_EACH_EDGE (e, bb->succ, ix)
610 if (e->dest->index < 0
611 || !flow_bb_inside_loop_p (loop, e->dest))
612 predict_edge
613 (e, PRED_LOOP_EXIT,
614 (REG_BR_PROB_BASE
615 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
616 / exits);
619 /* Free basic blocks from get_loop_body. */
620 free (bbs);
624 /* Statically estimate the probability that a branch will be taken and produce
625 estimated profile. When profile feedback is present never executed portions
626 of function gets estimated. */
628 void
629 estimate_probability (struct loops *loops_info)
631 basic_block bb;
633 connect_infinite_loops_to_exit ();
634 calculate_dominance_info (CDI_DOMINATORS);
635 calculate_dominance_info (CDI_POST_DOMINATORS);
637 predict_loops (loops_info, true);
639 iv_analysis_done ();
641 /* Attempt to predict conditional jumps using a number of heuristics. */
642 FOR_EACH_BB (bb)
644 rtx last_insn = BB_END (bb);
645 rtx cond, earliest;
646 edge e;
647 unsigned ix;
649 if (! can_predict_insn_p (last_insn))
650 continue;
652 FOR_EACH_EDGE (e, bb->succ, ix)
654 /* Predict early returns to be probable, as we've already taken
655 care for error returns and other are often used for fast paths
656 trought function. */
657 if ((e->dest == EXIT_BLOCK_PTR
658 || (EDGE_COUNT (e->dest->succ) == 1
659 && EDGE_0 (e->dest->succ)->dest == EXIT_BLOCK_PTR))
660 && !predicted_by_p (bb, PRED_NULL_RETURN)
661 && !predicted_by_p (bb, PRED_CONST_RETURN)
662 && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
663 && !last_basic_block_p (e->dest))
664 predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
666 /* Look for block we are guarding (ie we dominate it,
667 but it doesn't postdominate us). */
668 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
669 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
670 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
672 rtx insn;
674 /* The call heuristic claims that a guarded function call
675 is improbable. This is because such calls are often used
676 to signal exceptional situations such as printing error
677 messages. */
678 for (insn = BB_HEAD (e->dest); insn != NEXT_INSN (BB_END (e->dest));
679 insn = NEXT_INSN (insn))
680 if (CALL_P (insn)
681 /* Constant and pure calls are hardly used to signalize
682 something exceptional. */
683 && ! CONST_OR_PURE_CALL_P (insn))
685 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
686 break;
691 cond = get_condition (last_insn, &earliest, false);
692 if (! cond)
693 continue;
695 /* Try "pointer heuristic."
696 A comparison ptr == 0 is predicted as false.
697 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
698 if (COMPARISON_P (cond)
699 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
700 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
702 if (GET_CODE (cond) == EQ)
703 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
704 else if (GET_CODE (cond) == NE)
705 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
707 else
709 /* Try "opcode heuristic."
710 EQ tests are usually false and NE tests are usually true. Also,
711 most quantities are positive, so we can make the appropriate guesses
712 about signed comparisons against zero. */
713 switch (GET_CODE (cond))
715 case CONST_INT:
716 /* Unconditional branch. */
717 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
718 cond == const0_rtx ? NOT_TAKEN : TAKEN);
719 break;
721 case EQ:
722 case UNEQ:
723 /* Floating point comparisons appears to behave in a very
724 unpredictable way because of special role of = tests in
725 FP code. */
726 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
728 /* Comparisons with 0 are often used for booleans and there is
729 nothing useful to predict about them. */
730 else if (XEXP (cond, 1) == const0_rtx
731 || XEXP (cond, 0) == const0_rtx)
733 else
734 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
735 break;
737 case NE:
738 case LTGT:
739 /* Floating point comparisons appears to behave in a very
740 unpredictable way because of special role of = tests in
741 FP code. */
742 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
744 /* Comparisons with 0 are often used for booleans and there is
745 nothing useful to predict about them. */
746 else if (XEXP (cond, 1) == const0_rtx
747 || XEXP (cond, 0) == const0_rtx)
749 else
750 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
751 break;
753 case ORDERED:
754 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
755 break;
757 case UNORDERED:
758 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
759 break;
761 case LE:
762 case LT:
763 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
764 || XEXP (cond, 1) == constm1_rtx)
765 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
766 break;
768 case GE:
769 case GT:
770 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
771 || XEXP (cond, 1) == constm1_rtx)
772 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
773 break;
775 default:
776 break;
780 /* Attach the combined probability to each conditional jump. */
781 FOR_EACH_BB (bb)
782 if (JUMP_P (BB_END (bb))
783 && any_condjump_p (BB_END (bb))
784 && EDGE_COUNT (bb->succ) >= 2)
785 combine_predictions_for_insn (BB_END (bb), bb);
787 remove_fake_exit_edges ();
788 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
789 notes. */
790 FOR_EACH_BB (bb)
792 rtx last_insn = BB_END (bb);
794 if (!can_predict_insn_p (last_insn))
796 /* We can predict only conditional jumps at the moment.
797 Expect each edge to be equally probable.
798 ?? In the future we want to make abnormal edges improbable. */
799 int nedges = 0;
800 edge e;
801 unsigned ix;
803 FOR_EACH_EDGE (e, bb->succ, ix)
805 nedges++;
806 if (e->probability != 0)
807 break;
809 if (!e)
810 FOR_EACH_EDGE (e, bb->succ, ix)
811 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
814 estimate_bb_frequencies (loops_info);
815 free_dominance_info (CDI_POST_DOMINATORS);
819 /* Predict using opcode of the last statement in basic block. */
820 static void
821 tree_predict_by_opcode (basic_block bb)
823 tree stmt = last_stmt (bb);
824 edge then_edge;
825 tree cond;
826 tree op0;
827 tree type;
828 unsigned ix;
830 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
831 return;
832 FOR_EACH_EDGE (then_edge, bb->succ, ix)
833 if (then_edge->flags & EDGE_TRUE_VALUE)
834 break;
835 cond = TREE_OPERAND (stmt, 0);
836 if (TREE_CODE_CLASS (TREE_CODE (cond)) != '<')
837 return;
838 op0 = TREE_OPERAND (cond, 0);
839 type = TREE_TYPE (op0);
840 /* Try "pointer heuristic."
841 A comparison ptr == 0 is predicted as false.
842 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
843 if (POINTER_TYPE_P (type))
845 if (TREE_CODE (cond) == EQ_EXPR)
846 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
847 else if (TREE_CODE (cond) == NE_EXPR)
848 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
850 else
852 /* Try "opcode heuristic."
853 EQ tests are usually false and NE tests are usually true. Also,
854 most quantities are positive, so we can make the appropriate guesses
855 about signed comparisons against zero. */
856 switch (TREE_CODE (cond))
858 case EQ_EXPR:
859 case UNEQ_EXPR:
860 /* Floating point comparisons appears to behave in a very
861 unpredictable way because of special role of = tests in
862 FP code. */
863 if (FLOAT_TYPE_P (type))
865 /* Comparisons with 0 are often used for booleans and there is
866 nothing useful to predict about them. */
867 else if (integer_zerop (op0)
868 || integer_zerop (TREE_OPERAND (cond, 1)))
870 else
871 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
872 break;
874 case NE_EXPR:
875 case LTGT_EXPR:
876 /* Floating point comparisons appears to behave in a very
877 unpredictable way because of special role of = tests in
878 FP code. */
879 if (FLOAT_TYPE_P (type))
881 /* Comparisons with 0 are often used for booleans and there is
882 nothing useful to predict about them. */
883 else if (integer_zerop (op0)
884 || integer_zerop (TREE_OPERAND (cond, 1)))
886 else
887 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
888 break;
890 case ORDERED_EXPR:
891 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
892 break;
894 case UNORDERED_EXPR:
895 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
896 break;
898 case LE_EXPR:
899 case LT_EXPR:
900 if (integer_zerop (TREE_OPERAND (cond, 1))
901 || integer_onep (TREE_OPERAND (cond, 1))
902 || integer_all_onesp (TREE_OPERAND (cond, 1))
903 || real_zerop (TREE_OPERAND (cond, 1))
904 || real_onep (TREE_OPERAND (cond, 1))
905 || real_minus_onep (TREE_OPERAND (cond, 1)))
906 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
907 break;
909 case GE_EXPR:
910 case GT_EXPR:
911 if (integer_zerop (TREE_OPERAND (cond, 1))
912 || integer_onep (TREE_OPERAND (cond, 1))
913 || integer_all_onesp (TREE_OPERAND (cond, 1))
914 || real_zerop (TREE_OPERAND (cond, 1))
915 || real_onep (TREE_OPERAND (cond, 1))
916 || real_minus_onep (TREE_OPERAND (cond, 1)))
917 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
918 break;
920 default:
921 break;
925 /* Predict branch probabilities and estimate profile of the tree CFG. */
926 static void
927 tree_estimate_probability (void)
929 basic_block bb;
930 struct loops loops_info;
932 flow_loops_find (&loops_info, LOOP_TREE);
933 if (dump_file && (dump_flags & TDF_DETAILS))
934 flow_loops_dump (&loops_info, dump_file, NULL, 0);
936 connect_infinite_loops_to_exit ();
937 calculate_dominance_info (CDI_DOMINATORS);
938 calculate_dominance_info (CDI_POST_DOMINATORS);
940 predict_loops (&loops_info, false);
942 FOR_EACH_BB (bb)
944 edge e;
945 unsigned ix;
947 FOR_EACH_EDGE (e, bb->succ, ix)
949 /* Predict early returns to be probable, as we've already taken
950 care for error returns and other are often used for fast paths
951 trought function. */
952 if ((e->dest == EXIT_BLOCK_PTR
953 || (EDGE_COUNT (e->dest->succ) == 1
954 && EDGE_0 (e->dest->succ)->dest == EXIT_BLOCK_PTR))
955 && !predicted_by_p (bb, PRED_NULL_RETURN)
956 && !predicted_by_p (bb, PRED_CONST_RETURN)
957 && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
958 && !last_basic_block_p (e->dest))
959 predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
961 /* Look for block we are guarding (ie we dominate it,
962 but it doesn't postdominate us). */
963 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
964 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
965 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
967 block_stmt_iterator bi;
969 /* The call heuristic claims that a guarded function call
970 is improbable. This is because such calls are often used
971 to signal exceptional situations such as printing error
972 messages. */
973 for (bi = bsi_start (e->dest); !bsi_end_p (bi);
974 bsi_next (&bi))
976 tree stmt = bsi_stmt (bi);
977 if ((TREE_CODE (stmt) == CALL_EXPR
978 || (TREE_CODE (stmt) == MODIFY_EXPR
979 && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR))
980 /* Constant and pure calls are hardly used to signalize
981 something exceptional. */
982 && TREE_SIDE_EFFECTS (stmt))
984 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
985 break;
990 tree_predict_by_opcode (bb);
992 FOR_EACH_BB (bb)
993 combine_predictions_for_bb (dump_file, bb);
995 estimate_bb_frequencies (&loops_info);
996 free_dominance_info (CDI_POST_DOMINATORS);
997 remove_fake_exit_edges ();
998 flow_loops_free (&loops_info);
999 if (dump_file && (dump_flags & TDF_DETAILS))
1000 dump_tree_cfg (dump_file, dump_flags);
1003 /* __builtin_expect dropped tokens into the insn stream describing expected
1004 values of registers. Generate branch probabilities based off these
1005 values. */
1007 void
1008 expected_value_to_br_prob (void)
1010 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
1012 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1014 switch (GET_CODE (insn))
1016 case NOTE:
1017 /* Look for expected value notes. */
1018 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
1020 ev = NOTE_EXPECTED_VALUE (insn);
1021 ev_reg = XEXP (ev, 0);
1022 delete_insn (insn);
1024 continue;
1026 case CODE_LABEL:
1027 /* Never propagate across labels. */
1028 ev = NULL_RTX;
1029 continue;
1031 case JUMP_INSN:
1032 /* Look for simple conditional branches. If we haven't got an
1033 expected value yet, no point going further. */
1034 if (!JUMP_P (insn) || ev == NULL_RTX
1035 || ! any_condjump_p (insn))
1036 continue;
1037 break;
1039 default:
1040 /* Look for insns that clobber the EV register. */
1041 if (ev && reg_set_p (ev_reg, insn))
1042 ev = NULL_RTX;
1043 continue;
1046 /* Collect the branch condition, hopefully relative to EV_REG. */
1047 /* ??? At present we'll miss things like
1048 (expected_value (eq r70 0))
1049 (set r71 -1)
1050 (set r80 (lt r70 r71))
1051 (set pc (if_then_else (ne r80 0) ...))
1052 as canonicalize_condition will render this to us as
1053 (lt r70, r71)
1054 Could use cselib to try and reduce this further. */
1055 cond = XEXP (SET_SRC (pc_set (insn)), 0);
1056 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg, false);
1057 if (! cond || XEXP (cond, 0) != ev_reg
1058 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
1059 continue;
1061 /* Substitute and simplify. Given that the expression we're
1062 building involves two constants, we should wind up with either
1063 true or false. */
1064 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
1065 XEXP (ev, 1), XEXP (cond, 1));
1066 cond = simplify_rtx (cond);
1068 /* Turn the condition into a scaled branch probability. */
1069 if (cond != const_true_rtx && cond != const0_rtx)
1070 abort ();
1071 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
1072 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
1076 /* Check whether this is the last basic block of function. Commonly
1077 there is one extra common cleanup block. */
1078 static bool
1079 last_basic_block_p (basic_block bb)
1081 if (bb == EXIT_BLOCK_PTR)
1082 return false;
1084 return (bb->next_bb == EXIT_BLOCK_PTR
1085 || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
1086 && EDGE_COUNT (bb->succ) == 1
1087 && EDGE_0 (bb->succ)->dest->next_bb == EXIT_BLOCK_PTR));
1090 /* Sets branch probabilities according to PREDiction and
1091 FLAGS. HEADS[bb->index] should be index of basic block in that we
1092 need to alter branch predictions (i.e. the first of our dominators
1093 such that we do not post-dominate it) (but we fill this information
1094 on demand, so -1 may be there in case this was not needed yet). */
1096 static void
1097 process_note_prediction (basic_block bb, int *heads, int pred, int flags)
1099 edge e;
1100 int y;
1101 bool taken;
1102 unsigned ix;
1104 taken = flags & IS_TAKEN;
1106 if (heads[bb->index] < 0)
1108 /* This is first time we need this field in heads array; so
1109 find first dominator that we do not post-dominate (we are
1110 using already known members of heads array). */
1111 basic_block ai = bb;
1112 basic_block next_ai = get_immediate_dominator (CDI_DOMINATORS, bb);
1113 int head;
1115 while (heads[next_ai->index] < 0)
1117 if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb))
1118 break;
1119 heads[next_ai->index] = ai->index;
1120 ai = next_ai;
1121 next_ai = get_immediate_dominator (CDI_DOMINATORS, next_ai);
1123 if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb))
1124 head = next_ai->index;
1125 else
1126 head = heads[next_ai->index];
1127 while (next_ai != bb)
1129 next_ai = ai;
1130 if (heads[ai->index] == ENTRY_BLOCK)
1131 ai = ENTRY_BLOCK_PTR;
1132 else
1133 ai = BASIC_BLOCK (heads[ai->index]);
1134 heads[next_ai->index] = head;
1137 y = heads[bb->index];
1139 /* Now find the edge that leads to our branch and aply the prediction. */
1141 if (y == last_basic_block || !can_predict_insn_p (BB_END (BASIC_BLOCK (y))))
1142 return;
1144 FOR_EACH_EDGE (e, BASIC_BLOCK (y)->succ, ix)
1145 if (e->dest->index >= 0
1146 && dominated_by_p (CDI_POST_DOMINATORS, e->dest, bb))
1147 predict_edge_def (e, pred, taken);
1150 /* Gathers NOTE_INSN_PREDICTIONs in given basic block and turns them
1151 into branch probabilities. For description of heads array, see
1152 process_note_prediction. */
1154 static void
1155 process_note_predictions (basic_block bb, int *heads)
1157 rtx insn;
1158 edge e;
1159 unsigned ix;
1161 /* Additionally, we check here for blocks with no successors. */
1162 int contained_noreturn_call = 0;
1163 int was_bb_head = 0;
1164 int noreturn_block = 1;
1166 for (insn = BB_END (bb); insn;
1167 was_bb_head |= (insn == BB_HEAD (bb)), insn = PREV_INSN (insn))
1169 if (!NOTE_P (insn))
1171 if (was_bb_head)
1172 break;
1173 else
1175 /* Noreturn calls cause program to exit, therefore they are
1176 always predicted as not taken. */
1177 if (CALL_P (insn)
1178 && find_reg_note (insn, REG_NORETURN, NULL))
1179 contained_noreturn_call = 1;
1180 continue;
1183 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION)
1185 int alg = (int) NOTE_PREDICTION_ALG (insn);
1186 /* Process single prediction note. */
1187 process_note_prediction (bb,
1188 heads,
1189 alg, (int) NOTE_PREDICTION_FLAGS (insn));
1190 delete_insn (insn);
1193 FOR_EACH_EDGE (e, bb->succ, ix)
1194 if (!(e->flags & EDGE_FAKE))
1195 noreturn_block = 0;
1196 if (contained_noreturn_call)
1198 /* This block ended from other reasons than because of return.
1199 If it is because of noreturn call, this should certainly not
1200 be taken. Otherwise it is probably some error recovery. */
1201 process_note_prediction (bb, heads, PRED_NORETURN, NOT_TAKEN);
1205 /* Gathers NOTE_INSN_PREDICTIONs and turns them into
1206 branch probabilities. */
1208 void
1209 note_prediction_to_br_prob (void)
1211 basic_block bb;
1212 int *heads;
1214 /* To enable handling of noreturn blocks. */
1215 add_noreturn_fake_exit_edges ();
1216 connect_infinite_loops_to_exit ();
1218 calculate_dominance_info (CDI_POST_DOMINATORS);
1219 calculate_dominance_info (CDI_DOMINATORS);
1221 heads = xmalloc (sizeof (int) * last_basic_block);
1222 memset (heads, -1, sizeof (int) * last_basic_block);
1223 heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block;
1225 /* Process all prediction notes. */
1227 FOR_EACH_BB (bb)
1228 process_note_predictions (bb, heads);
1230 free_dominance_info (CDI_POST_DOMINATORS);
1231 free_dominance_info (CDI_DOMINATORS);
1232 free (heads);
1234 remove_fake_exit_edges ();
1237 /* This is used to carry information about basic blocks. It is
1238 attached to the AUX field of the standard CFG block. */
1240 typedef struct block_info_def
1242 /* Estimated frequency of execution of basic_block. */
1243 sreal frequency;
1245 /* To keep queue of basic blocks to process. */
1246 basic_block next;
1248 /* True if block needs to be visited in propagate_freq. */
1249 unsigned int tovisit:1;
1251 /* Number of predecessors we need to visit first. */
1252 int npredecessors;
1253 } *block_info;
1255 /* Similar information for edges. */
1256 typedef struct edge_info_def
1258 /* In case edge is an loopback edge, the probability edge will be reached
1259 in case header is. Estimated number of iterations of the loop can be
1260 then computed as 1 / (1 - back_edge_prob). */
1261 sreal back_edge_prob;
1262 /* True if the edge is an loopback edge in the natural loop. */
1263 unsigned int back_edge:1;
1264 } *edge_info;
1266 #define BLOCK_INFO(B) ((block_info) (B)->aux)
1267 #define EDGE_INFO(E) ((edge_info) (E)->aux)
1269 /* Helper function for estimate_bb_frequencies.
1270 Propagate the frequencies for LOOP. */
1272 static void
1273 propagate_freq (struct loop *loop)
1275 basic_block head = loop->header;
1276 basic_block bb;
1277 basic_block last;
1278 basic_block nextbb;
1279 edge e;
1280 unsigned ix;
1282 /* For each basic block we need to visit count number of his predecessors
1283 we need to visit first. */
1284 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1286 if (BLOCK_INFO (bb)->tovisit)
1288 int count = 0;
1290 FOR_EACH_EDGE (e, bb->pred, ix)
1291 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
1292 count++;
1293 else if (BLOCK_INFO (e->src)->tovisit
1294 && dump_file && !EDGE_INFO (e)->back_edge)
1295 fprintf (dump_file,
1296 "Irreducible region hit, ignoring edge to %i->%i\n",
1297 e->src->index, bb->index);
1298 BLOCK_INFO (bb)->npredecessors = count;
1302 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
1303 last = head;
1304 for (bb = head; bb; bb = nextbb)
1306 sreal cyclic_probability, frequency;
1308 memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
1309 memcpy (&frequency, &real_zero, sizeof (real_zero));
1311 nextbb = BLOCK_INFO (bb)->next;
1312 BLOCK_INFO (bb)->next = NULL;
1314 /* Compute frequency of basic block. */
1315 if (bb != head)
1317 #ifdef ENABLE_CHECKING
1318 FOR_EACH_EDGE (e, bb->pred, ix)
1319 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
1320 abort ();
1321 #endif
1323 FOR_EACH_EDGE (e, bb->pred, ix)
1324 if (EDGE_INFO (e)->back_edge)
1326 sreal_add (&cyclic_probability, &cyclic_probability,
1327 &EDGE_INFO (e)->back_edge_prob);
1329 else if (!(e->flags & EDGE_DFS_BACK))
1331 sreal tmp;
1333 /* frequency += (e->probability
1334 * BLOCK_INFO (e->src)->frequency /
1335 REG_BR_PROB_BASE); */
1337 sreal_init (&tmp, e->probability, 0);
1338 sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency);
1339 sreal_mul (&tmp, &tmp, &real_inv_br_prob_base);
1340 sreal_add (&frequency, &frequency, &tmp);
1343 if (sreal_compare (&cyclic_probability, &real_zero) == 0)
1345 memcpy (&BLOCK_INFO (bb)->frequency, &frequency,
1346 sizeof (frequency));
1348 else
1350 if (sreal_compare (&cyclic_probability, &real_almost_one) > 0)
1352 memcpy (&cyclic_probability, &real_almost_one,
1353 sizeof (real_almost_one));
1356 /* BLOCK_INFO (bb)->frequency = frequency
1357 / (1 - cyclic_probability) */
1359 sreal_sub (&cyclic_probability, &real_one, &cyclic_probability);
1360 sreal_div (&BLOCK_INFO (bb)->frequency,
1361 &frequency, &cyclic_probability);
1365 BLOCK_INFO (bb)->tovisit = 0;
1367 /* Compute back edge frequencies. */
1368 FOR_EACH_EDGE (e, bb->succ, ix)
1369 if (e->dest == head)
1371 sreal tmp;
1373 /* EDGE_INFO (e)->back_edge_prob
1374 = ((e->probability * BLOCK_INFO (bb)->frequency)
1375 / REG_BR_PROB_BASE); */
1377 sreal_init (&tmp, e->probability, 0);
1378 sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency);
1379 sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1380 &tmp, &real_inv_br_prob_base);
1383 /* Propagate to successor blocks. */
1384 FOR_EACH_EDGE (e, bb->succ, ix)
1385 if (!(e->flags & EDGE_DFS_BACK)
1386 && BLOCK_INFO (e->dest)->npredecessors)
1388 BLOCK_INFO (e->dest)->npredecessors--;
1389 if (!BLOCK_INFO (e->dest)->npredecessors)
1391 if (!nextbb)
1392 nextbb = e->dest;
1393 else
1394 BLOCK_INFO (last)->next = e->dest;
1396 last = e->dest;
1402 /* Estimate probabilities of loopback edges in loops at same nest level. */
1404 static void
1405 estimate_loops_at_level (struct loop *first_loop)
1407 struct loop *loop;
1409 for (loop = first_loop; loop; loop = loop->next)
1411 edge e;
1412 basic_block *bbs;
1413 unsigned i;
1415 estimate_loops_at_level (loop->inner);
1417 if (loop->latch->succ) /* Do not do this for dummy function loop. */
1419 /* Find current loop back edge and mark it. */
1420 e = loop_latch_edge (loop);
1421 EDGE_INFO (e)->back_edge = 1;
1424 bbs = get_loop_body (loop);
1425 for (i = 0; i < loop->num_nodes; i++)
1426 BLOCK_INFO (bbs[i])->tovisit = 1;
1427 free (bbs);
1428 propagate_freq (loop);
1432 /* Convert counts measured by profile driven feedback to frequencies.
1433 Return nonzero iff there was any nonzero execution count. */
1435 static int
1436 counts_to_freqs (void)
1438 gcov_type count_max, true_count_max = 0;
1439 basic_block bb;
1441 FOR_EACH_BB (bb)
1442 true_count_max = MAX (bb->count, true_count_max);
1444 count_max = MAX (true_count_max, 1);
1445 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1446 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
1447 return true_count_max;
1450 /* Return true if function is likely to be expensive, so there is no point to
1451 optimize performance of prologue, epilogue or do inlining at the expense
1452 of code size growth. THRESHOLD is the limit of number of instructions
1453 function can execute at average to be still considered not expensive. */
1455 bool
1456 expensive_function_p (int threshold)
1458 unsigned int sum = 0;
1459 basic_block bb;
1460 unsigned int limit;
1462 /* We can not compute accurately for large thresholds due to scaled
1463 frequencies. */
1464 if (threshold > BB_FREQ_MAX)
1465 abort ();
1467 /* Frequencies are out of range. This either means that function contains
1468 internal loop executing more than BB_FREQ_MAX times or profile feedback
1469 is available and function has not been executed at all. */
1470 if (ENTRY_BLOCK_PTR->frequency == 0)
1471 return true;
1473 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
1474 limit = ENTRY_BLOCK_PTR->frequency * threshold;
1475 FOR_EACH_BB (bb)
1477 rtx insn;
1479 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
1480 insn = NEXT_INSN (insn))
1481 if (active_insn_p (insn))
1483 sum += bb->frequency;
1484 if (sum > limit)
1485 return true;
1489 return false;
1492 /* Estimate basic blocks frequency by given branch probabilities. */
1494 static void
1495 estimate_bb_frequencies (struct loops *loops)
1497 basic_block bb;
1498 sreal freq_max;
1500 if (!flag_branch_probabilities || !counts_to_freqs ())
1502 static int real_values_initialized = 0;
1504 if (!real_values_initialized)
1506 real_values_initialized = 1;
1507 sreal_init (&real_zero, 0, 0);
1508 sreal_init (&real_one, 1, 0);
1509 sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0);
1510 sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0);
1511 sreal_init (&real_one_half, 1, -1);
1512 sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base);
1513 sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base);
1516 mark_dfs_back_edges ();
1518 EDGE_0 (ENTRY_BLOCK_PTR->succ)->probability = REG_BR_PROB_BASE;
1520 /* Set up block info for each basic block. */
1521 alloc_aux_for_blocks (sizeof (struct block_info_def));
1522 alloc_aux_for_edges (sizeof (struct edge_info_def));
1523 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1525 edge e;
1526 unsigned ix;
1528 BLOCK_INFO (bb)->tovisit = 0;
1529 FOR_EACH_EDGE (e, bb->succ, ix)
1531 sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0);
1532 sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1533 &EDGE_INFO (e)->back_edge_prob,
1534 &real_inv_br_prob_base);
1538 /* First compute probabilities locally for each loop from innermost
1539 to outermost to examine probabilities for back edges. */
1540 estimate_loops_at_level (loops->tree_root);
1542 memcpy (&freq_max, &real_zero, sizeof (real_zero));
1543 FOR_EACH_BB (bb)
1544 if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0)
1545 memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max));
1547 sreal_div (&freq_max, &real_bb_freq_max, &freq_max);
1548 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1550 sreal tmp;
1552 sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max);
1553 sreal_add (&tmp, &tmp, &real_one_half);
1554 bb->frequency = sreal_to_int (&tmp);
1557 free_aux_for_blocks ();
1558 free_aux_for_edges ();
1560 compute_function_frequency ();
1561 if (flag_reorder_functions)
1562 choose_function_section ();
1565 /* Decide whether function is hot, cold or unlikely executed. */
1566 static void
1567 compute_function_frequency (void)
1569 basic_block bb;
1571 if (!profile_info || !flag_branch_probabilities)
1572 return;
1573 cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
1574 FOR_EACH_BB (bb)
1576 if (maybe_hot_bb_p (bb))
1578 cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
1579 return;
1581 if (!probably_never_executed_bb_p (bb))
1582 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
1586 /* Choose appropriate section for the function. */
1587 static void
1588 choose_function_section (void)
1590 if (DECL_SECTION_NAME (current_function_decl)
1591 || !targetm.have_named_sections
1592 /* Theoretically we can split the gnu.linkonce text section too,
1593 but this requires more work as the frequency needs to match
1594 for all generated objects so we need to merge the frequency
1595 of all instances. For now just never set frequency for these. */
1596 || DECL_ONE_ONLY (current_function_decl))
1597 return;
1598 if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
1599 DECL_SECTION_NAME (current_function_decl) =
1600 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
1601 if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1602 DECL_SECTION_NAME (current_function_decl) =
1603 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
1604 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
1608 struct tree_opt_pass pass_profile =
1610 "profile", /* name */
1611 NULL, /* gate */
1612 tree_estimate_probability, /* execute */
1613 NULL, /* sub */
1614 NULL, /* next */
1615 0, /* static_pass_number */
1616 TV_BRANCH_PROB, /* tv_id */
1617 PROP_cfg, /* properties_required */
1618 0, /* properties_provided */
1619 0, /* properties_destroyed */
1620 0, /* todo_flags_start */
1621 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */