PR target/5828
[official-gcc.git] / gcc / predict.c
blobf496b38dce0ad046766fad222f9163bdaee65a4f
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
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 "tree.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "insn-config.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "function.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "expr.h"
47 #include "predict.h"
49 /* Random guesstimation given names. */
50 #define PROB_NEVER (0)
51 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
52 #define PROB_UNLIKELY (REG_BR_PROB_BASE * 4 / 10 - 1)
53 #define PROB_EVEN (REG_BR_PROB_BASE / 2)
54 #define PROB_LIKELY (REG_BR_PROB_BASE - PROB_UNLIKELY)
55 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
56 #define PROB_ALWAYS (REG_BR_PROB_BASE)
58 static void combine_predictions_for_insn PARAMS ((rtx, basic_block));
59 static void dump_prediction PARAMS ((enum br_predictor, int,
60 basic_block, int));
61 static void estimate_loops_at_level PARAMS ((struct loop *loop));
62 static void propagate_freq PARAMS ((basic_block));
63 static void estimate_bb_frequencies PARAMS ((struct loops *));
64 static void counts_to_freqs PARAMS ((void));
66 /* Information we hold about each branch predictor.
67 Filled using information from predict.def. */
69 struct predictor_info
71 const char *const name; /* Name used in the debugging dumps. */
72 const int hitrate; /* Expected hitrate used by
73 predict_insn_def call. */
74 const int flags;
77 /* Use given predictor without Dempster-Shaffer theory if it matches
78 using first_match heuristics. */
79 #define PRED_FLAG_FIRST_MATCH 1
81 /* Recompute hitrate in percent to our representation. */
83 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
85 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
86 static const struct predictor_info predictor_info[]= {
87 #include "predict.def"
89 /* Upper bound on predictors. */
90 {NULL, 0, 0}
92 #undef DEF_PREDICTOR
94 void
95 predict_insn (insn, predictor, probability)
96 rtx insn;
97 int probability;
98 enum br_predictor predictor;
100 if (!any_condjump_p (insn))
101 abort ();
103 REG_NOTES (insn)
104 = gen_rtx_EXPR_LIST (REG_BR_PRED,
105 gen_rtx_CONCAT (VOIDmode,
106 GEN_INT ((int) predictor),
107 GEN_INT ((int) probability)),
108 REG_NOTES (insn));
111 /* Predict insn by given predictor. */
113 void
114 predict_insn_def (insn, predictor, taken)
115 rtx insn;
116 enum br_predictor predictor;
117 enum prediction taken;
119 int probability = predictor_info[(int) predictor].hitrate;
121 if (taken != TAKEN)
122 probability = REG_BR_PROB_BASE - probability;
124 predict_insn (insn, predictor, probability);
127 /* Predict edge E with given probability if possible. */
129 void
130 predict_edge (e, predictor, probability)
131 edge e;
132 int probability;
133 enum br_predictor predictor;
135 rtx last_insn;
136 last_insn = e->src->end;
138 /* We can store the branch prediction information only about
139 conditional jumps. */
140 if (!any_condjump_p (last_insn))
141 return;
143 /* We always store probability of branching. */
144 if (e->flags & EDGE_FALLTHRU)
145 probability = REG_BR_PROB_BASE - probability;
147 predict_insn (last_insn, predictor, probability);
150 /* Predict edge E by given predictor if possible. */
152 void
153 predict_edge_def (e, predictor, taken)
154 edge e;
155 enum br_predictor predictor;
156 enum prediction taken;
158 int probability = predictor_info[(int) predictor].hitrate;
160 if (taken != TAKEN)
161 probability = REG_BR_PROB_BASE - probability;
163 predict_edge (e, predictor, probability);
166 /* Invert all branch predictions or probability notes in the INSN. This needs
167 to be done each time we invert the condition used by the jump. */
169 void
170 invert_br_probabilities (insn)
171 rtx insn;
173 rtx note;
175 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
176 if (REG_NOTE_KIND (note) == REG_BR_PROB)
177 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
178 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
179 XEXP (XEXP (note, 0), 1)
180 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
183 /* Dump information about the branch prediction to the output file. */
185 static void
186 dump_prediction (predictor, probability, bb, used)
187 enum br_predictor predictor;
188 int probability;
189 basic_block bb;
190 int used;
192 edge e = bb->succ;
194 if (!rtl_dump_file)
195 return;
197 while (e->flags & EDGE_FALLTHRU)
198 e = e->succ_next;
200 fprintf (rtl_dump_file, " %s heuristics%s: %.1f%%",
201 predictor_info[predictor].name,
202 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
204 if (bb->count)
206 fprintf (rtl_dump_file, " exec ");
207 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
208 fprintf (rtl_dump_file, " hit ");
209 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, e->count);
210 fprintf (rtl_dump_file, " (%.1f%%)", e->count * 100.0 / bb->count);
213 fprintf (rtl_dump_file, "\n");
216 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
217 note if not already present. Remove now useless REG_BR_PRED notes. */
219 static void
220 combine_predictions_for_insn (insn, bb)
221 rtx insn;
222 basic_block bb;
224 rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
225 rtx *pnote = &REG_NOTES (insn);
226 rtx note;
227 int best_probability = PROB_EVEN;
228 int best_predictor = END_PREDICTORS;
229 int combined_probability = REG_BR_PROB_BASE / 2;
230 int d;
231 bool first_match = false;
232 bool found = false;
234 if (rtl_dump_file)
235 fprintf (rtl_dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
236 bb->index);
238 /* We implement "first match" heuristics and use probability guessed
239 by predictor with smallest index. In the future we will use better
240 probability combination techniques. */
241 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
242 if (REG_NOTE_KIND (note) == REG_BR_PRED)
244 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
245 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
247 found = true;
248 if (best_predictor > predictor)
249 best_probability = probability, best_predictor = predictor;
251 d = (combined_probability * probability
252 + (REG_BR_PROB_BASE - combined_probability)
253 * (REG_BR_PROB_BASE - probability));
255 /* Use FP math to avoid overflows of 32bit integers. */
256 if (d == 0)
257 /* If one probability is 0% and one 100%, avoid division by zero. */
258 combined_probability = REG_BR_PROB_BASE / 2;
259 else
260 combined_probability = (((double) combined_probability) * probability
261 * REG_BR_PROB_BASE / d + 0.5);
264 /* Decide which heuristic to use. In case we didn't match anything,
265 use no_prediction heuristic, in case we did match, use either
266 first match or Dempster-Shaffer theory depending on the flags. */
268 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
269 first_match = true;
271 if (!found)
272 dump_prediction (PRED_NO_PREDICTION, combined_probability, bb, true);
273 else
275 dump_prediction (PRED_DS_THEORY, combined_probability, bb, !first_match);
276 dump_prediction (PRED_FIRST_MATCH, best_probability, bb, first_match);
279 if (first_match)
280 combined_probability = best_probability;
281 dump_prediction (PRED_COMBINED, combined_probability, bb, true);
283 while (*pnote)
285 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
287 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
288 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
290 dump_prediction (predictor, probability, bb,
291 !first_match || best_predictor == predictor);
292 *pnote = XEXP (*pnote, 1);
294 else
295 pnote = &XEXP (*pnote, 1);
298 if (!prob_note)
300 REG_NOTES (insn)
301 = gen_rtx_EXPR_LIST (REG_BR_PROB,
302 GEN_INT (combined_probability), REG_NOTES (insn));
304 /* Save the prediction into CFG in case we are seeing non-degenerated
305 conditional jump. */
306 if (bb->succ->succ_next)
308 BRANCH_EDGE (bb)->probability = combined_probability;
309 FALLTHRU_EDGE (bb)->probability
310 = REG_BR_PROB_BASE - combined_probability;
315 /* Statically estimate the probability that a branch will be taken.
316 ??? In the next revision there will be a number of other predictors added
317 from the above references. Further, each heuristic will be factored out
318 into its own function for clarity (and to facilitate the combination of
319 predictions). */
321 void
322 estimate_probability (loops_info)
323 struct loops *loops_info;
325 sbitmap *dominators, *post_dominators;
326 int i;
327 int found_noreturn = 0;
329 dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
330 post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
331 calculate_dominance_info (NULL, dominators, CDI_DOMINATORS);
332 calculate_dominance_info (NULL, post_dominators, CDI_POST_DOMINATORS);
334 /* Try to predict out blocks in a loop that are not part of a
335 natural loop. */
336 for (i = 0; i < loops_info->num; i++)
338 int j;
339 int exits;
340 struct loop *loop = &loops_info->array[i];
342 flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
343 exits = loop->num_exits;
345 for (j = loop->first->index; j <= loop->last->index; ++j)
346 if (TEST_BIT (loop->nodes, j))
348 int header_found = 0;
349 edge e;
351 /* Loop branch heuristics - predict an edge back to a
352 loop's head as taken. */
353 for (e = BASIC_BLOCK(j)->succ; e; e = e->succ_next)
354 if (e->dest == loop->header
355 && e->src == loop->latch)
357 header_found = 1;
358 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
361 /* Loop exit heuristics - predict an edge exiting the loop if the
362 conditinal has no loop header successors as not taken. */
363 if (!header_found)
364 for (e = BASIC_BLOCK(j)->succ; e; e = e->succ_next)
365 if (e->dest->index < 0
366 || !TEST_BIT (loop->nodes, e->dest->index))
367 predict_edge
368 (e, PRED_LOOP_EXIT,
369 (REG_BR_PROB_BASE
370 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
371 / exits);
375 /* Attempt to predict conditional jumps using a number of heuristics. */
376 for (i = 0; i < n_basic_blocks; i++)
378 basic_block bb = BASIC_BLOCK (i);
379 rtx last_insn = bb->end;
380 rtx cond, earliest;
381 edge e;
383 /* If block has no successor, predict all possible paths to it as
384 improbable, as the block contains a call to a noreturn function and
385 thus can be executed only once. */
386 if (bb->succ == NULL && !found_noreturn)
388 int y;
390 /* ??? Postdominator claims each noreturn block to be postdominated
391 by each, so we need to run only once. This needs to be changed
392 once postdominace algorithm is updated to say something more
393 sane. */
394 found_noreturn = 1;
395 for (y = 0; y < n_basic_blocks; y++)
396 if (!TEST_BIT (post_dominators[y], i))
397 for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next)
398 if (e->dest->index >= 0
399 && TEST_BIT (post_dominators[e->dest->index], i))
400 predict_edge_def (e, PRED_NORETURN, NOT_TAKEN);
403 if (GET_CODE (last_insn) != JUMP_INSN || ! any_condjump_p (last_insn))
404 continue;
406 for (e = bb->succ; e; e = e->succ_next)
408 /* Predict edges to blocks that return immediately to be
409 improbable. These are usually used to signal error states. */
410 if (e->dest == EXIT_BLOCK_PTR
411 || (e->dest->succ && !e->dest->succ->succ_next
412 && e->dest->succ->dest == EXIT_BLOCK_PTR))
413 predict_edge_def (e, PRED_ERROR_RETURN, NOT_TAKEN);
415 /* Look for block we are guarding (ie we dominate it,
416 but it doesn't postdominate us). */
417 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
418 && TEST_BIT (dominators[e->dest->index], e->src->index)
419 && !TEST_BIT (post_dominators[e->src->index], e->dest->index))
421 rtx insn;
423 /* The call heuristic claims that a guarded function call
424 is improbable. This is because such calls are often used
425 to signal exceptional situations such as printing error
426 messages. */
427 for (insn = e->dest->head; insn != NEXT_INSN (e->dest->end);
428 insn = NEXT_INSN (insn))
429 if (GET_CODE (insn) == CALL_INSN
430 /* Constant and pure calls are hardly used to signalize
431 something exceptional. */
432 && ! CONST_OR_PURE_CALL_P (insn))
434 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
435 break;
440 cond = get_condition (last_insn, &earliest);
441 if (! cond)
442 continue;
444 /* Try "pointer heuristic."
445 A comparison ptr == 0 is predicted as false.
446 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
447 if (GET_RTX_CLASS (GET_CODE (cond)) == '<'
448 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
449 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
451 if (GET_CODE (cond) == EQ)
452 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
453 else if (GET_CODE (cond) == NE)
454 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
456 else
458 /* Try "opcode heuristic."
459 EQ tests are usually false and NE tests are usually true. Also,
460 most quantities are positive, so we can make the appropriate guesses
461 about signed comparisons against zero. */
462 switch (GET_CODE (cond))
464 case CONST_INT:
465 /* Unconditional branch. */
466 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
467 cond == const0_rtx ? NOT_TAKEN : TAKEN);
468 break;
470 case EQ:
471 case UNEQ:
472 /* Floating point comparisons appears to behave in a very
473 inpredictable way because of special role of = tests in
474 FP code. */
475 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
477 /* Comparisons with 0 are often used for booleans and there is
478 nothing usefull to predict about them. */
479 else if (XEXP (cond, 1) == const0_rtx
480 || XEXP (cond, 0) == const0_rtx)
482 else
483 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
484 break;
486 case NE:
487 case LTGT:
488 /* Floating point comparisons appears to behave in a very
489 inpredictable way because of special role of = tests in
490 FP code. */
491 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
493 /* Comparisons with 0 are often used for booleans and there is
494 nothing usefull to predict about them. */
495 else if (XEXP (cond, 1) == const0_rtx
496 || XEXP (cond, 0) == const0_rtx)
498 else
499 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
500 break;
502 case ORDERED:
503 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
504 break;
506 case UNORDERED:
507 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
508 break;
510 case LE:
511 case LT:
512 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
513 || XEXP (cond, 1) == constm1_rtx)
514 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
515 break;
517 case GE:
518 case GT:
519 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
520 || XEXP (cond, 1) == constm1_rtx)
521 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
522 break;
524 default:
525 break;
529 /* Attach the combined probability to each conditional jump. */
530 for (i = 0; i < n_basic_blocks; i++)
531 if (GET_CODE (BLOCK_END (i)) == JUMP_INSN
532 && any_condjump_p (BLOCK_END (i)))
533 combine_predictions_for_insn (BLOCK_END (i), BASIC_BLOCK (i));
535 sbitmap_vector_free (post_dominators);
536 sbitmap_vector_free (dominators);
538 estimate_bb_frequencies (loops_info);
541 /* __builtin_expect dropped tokens into the insn stream describing expected
542 values of registers. Generate branch probabilities based off these
543 values. */
545 void
546 expected_value_to_br_prob ()
548 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
550 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
552 switch (GET_CODE (insn))
554 case NOTE:
555 /* Look for expected value notes. */
556 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
558 ev = NOTE_EXPECTED_VALUE (insn);
559 ev_reg = XEXP (ev, 0);
560 delete_insn (insn);
562 continue;
564 case CODE_LABEL:
565 /* Never propagate across labels. */
566 ev = NULL_RTX;
567 continue;
569 case JUMP_INSN:
570 /* Look for simple conditional branches. If we haven't got an
571 expected value yet, no point going further. */
572 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX
573 || ! any_condjump_p (insn))
574 continue;
575 break;
577 default:
578 /* Look for insns that clobber the EV register. */
579 if (ev && reg_set_p (ev_reg, insn))
580 ev = NULL_RTX;
581 continue;
584 /* Collect the branch condition, hopefully relative to EV_REG. */
585 /* ??? At present we'll miss things like
586 (expected_value (eq r70 0))
587 (set r71 -1)
588 (set r80 (lt r70 r71))
589 (set pc (if_then_else (ne r80 0) ...))
590 as canonicalize_condition will render this to us as
591 (lt r70, r71)
592 Could use cselib to try and reduce this further. */
593 cond = XEXP (SET_SRC (pc_set (insn)), 0);
594 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg);
595 if (! cond || XEXP (cond, 0) != ev_reg
596 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
597 continue;
599 /* Substitute and simplify. Given that the expression we're
600 building involves two constants, we should wind up with either
601 true or false. */
602 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
603 XEXP (ev, 1), XEXP (cond, 1));
604 cond = simplify_rtx (cond);
606 /* Turn the condition into a scaled branch probability. */
607 if (cond != const_true_rtx && cond != const0_rtx)
608 abort ();
609 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
610 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
614 /* This is used to carry information about basic blocks. It is
615 attached to the AUX field of the standard CFG block. */
617 typedef struct block_info_def
619 /* Estimated frequency of execution of basic_block. */
620 volatile double frequency;
622 /* To keep queue of basic blocks to process. */
623 basic_block next;
625 /* True if block needs to be visited in prop_freqency. */
626 int tovisit:1;
628 /* Number of predecessors we need to visit first. */
629 int npredecessors;
630 } *block_info;
632 /* Similar information for edges. */
633 typedef struct edge_info_def
635 /* In case edge is an loopback edge, the probability edge will be reached
636 in case header is. Estimated number of iterations of the loop can be
637 then computed as 1 / (1 - back_edge_prob).
639 Volatile is needed to avoid differences in the optimized and unoptimized
640 builds on machines where FP registers are wider than double. */
641 volatile double back_edge_prob;
642 /* True if the edge is an loopback edge in the natural loop. */
643 int back_edge:1;
644 } *edge_info;
646 #define BLOCK_INFO(B) ((block_info) (B)->aux)
647 #define EDGE_INFO(E) ((edge_info) (E)->aux)
649 /* Helper function for estimate_bb_frequencies.
650 Propagate the frequencies for loops headed by HEAD. */
652 static void
653 propagate_freq (head)
654 basic_block head;
656 basic_block bb = head;
657 basic_block last = bb;
658 edge e;
659 basic_block nextbb;
660 int n;
662 /* For each basic block we need to visit count number of his predecessors
663 we need to visit first. */
664 for (n = 0; n < n_basic_blocks; n++)
666 basic_block bb = BASIC_BLOCK (n);
667 if (BLOCK_INFO (bb)->tovisit)
669 int count = 0;
671 for (e = bb->pred; e; e = e->pred_next)
672 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
673 count++;
674 else if (BLOCK_INFO (e->src)->tovisit
675 && rtl_dump_file && !EDGE_INFO (e)->back_edge)
676 fprintf (rtl_dump_file,
677 "Irreducible region hit, ignoring edge to %i->%i\n",
678 e->src->index, bb->index);
679 BLOCK_INFO (bb)->npredecessors = count;
683 BLOCK_INFO (head)->frequency = 1;
684 for (; bb; bb = nextbb)
686 double cyclic_probability = 0, frequency = 0;
688 nextbb = BLOCK_INFO (bb)->next;
689 BLOCK_INFO (bb)->next = NULL;
691 /* Compute frequency of basic block. */
692 if (bb != head)
694 #ifdef ENABLE_CHECKING
695 for (e = bb->pred; e; e = e->pred_next)
696 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
697 abort ();
698 #endif
700 for (e = bb->pred; e; e = e->pred_next)
701 if (EDGE_INFO (e)->back_edge)
702 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
703 else if (!(e->flags & EDGE_DFS_BACK))
704 frequency += (e->probability
705 * BLOCK_INFO (e->src)->frequency /
706 REG_BR_PROB_BASE);
708 if (cyclic_probability > 1.0 - 1.0 / REG_BR_PROB_BASE)
709 cyclic_probability = 1.0 - 1.0 / REG_BR_PROB_BASE;
711 BLOCK_INFO (bb)->frequency = frequency / (1 - cyclic_probability);
714 BLOCK_INFO (bb)->tovisit = 0;
716 /* Compute back edge frequencies. */
717 for (e = bb->succ; e; e = e->succ_next)
718 if (e->dest == head)
719 EDGE_INFO (e)->back_edge_prob
720 = ((e->probability * BLOCK_INFO (bb)->frequency)
721 / REG_BR_PROB_BASE);
723 /* Propagate to successor blocks. */
724 for (e = bb->succ; e; e = e->succ_next)
725 if (!(e->flags & EDGE_DFS_BACK)
726 && BLOCK_INFO (e->dest)->npredecessors)
728 BLOCK_INFO (e->dest)->npredecessors--;
729 if (!BLOCK_INFO (e->dest)->npredecessors)
731 if (!nextbb)
732 nextbb = e->dest;
733 else
734 BLOCK_INFO (last)->next = e->dest;
736 last = e->dest;
742 /* Estimate probabilities of loopback edges in loops at same nest level. */
744 static void
745 estimate_loops_at_level (first_loop)
746 struct loop *first_loop;
748 struct loop *l, *loop = first_loop;
750 for (loop = first_loop; loop; loop = loop->next)
752 int n;
753 edge e;
755 estimate_loops_at_level (loop->inner);
757 /* Find current loop back edge and mark it. */
758 for (e = loop->latch->succ; e->dest != loop->header; e = e->succ_next)
761 EDGE_INFO (e)->back_edge = 1;
763 /* In case the loop header is shared, ensure that it is the last
764 one sharing the same header, so we avoid redundant work. */
765 if (loop->shared)
767 for (l = loop->next; l; l = l->next)
768 if (l->header == loop->header)
769 break;
771 if (l)
772 continue;
775 /* Now merge all nodes of all loops with given header as not visited. */
776 for (l = loop->shared ? first_loop : loop; l != loop->next; l = l->next)
777 if (loop->header == l->header)
778 EXECUTE_IF_SET_IN_SBITMAP (l->nodes, 0, n,
779 BLOCK_INFO (BASIC_BLOCK (n))->tovisit = 1
782 propagate_freq (loop->header);
786 /* Convert counts measured by profile driven feedback to frequencies. */
788 static void
789 counts_to_freqs ()
791 HOST_WIDEST_INT count_max = 1;
792 int i;
794 for (i = 0; i < n_basic_blocks; i++)
795 count_max = MAX (BASIC_BLOCK (i)->count, count_max);
797 for (i = -2; i < n_basic_blocks; i++)
799 basic_block bb;
801 if (i == -2)
802 bb = ENTRY_BLOCK_PTR;
803 else if (i == -1)
804 bb = EXIT_BLOCK_PTR;
805 else
806 bb = BASIC_BLOCK (i);
808 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
812 /* Return true if function is likely to be expensive, so there is no point to
813 optimize performance of prologue, epilogue or do inlining at the expense
814 of code size growth. THRESHOLD is the limit of number of isntructions
815 function can execute at average to be still considered not expensive. */
817 bool
818 expensive_function_p (threshold)
819 int threshold;
821 unsigned int sum = 0;
822 int i;
823 unsigned int limit;
825 /* We can not compute accurately for large thresholds due to scaled
826 frequencies. */
827 if (threshold > BB_FREQ_MAX)
828 abort ();
830 /* Frequencies are out of range. This either means that function contains
831 internal loop executing more than BB_FREQ_MAX times or profile feedback
832 is available and function has not been executed at all. */
833 if (ENTRY_BLOCK_PTR->frequency == 0)
834 return true;
836 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
837 limit = ENTRY_BLOCK_PTR->frequency * threshold;
838 for (i = 0; i < n_basic_blocks; i++)
840 basic_block bb = BASIC_BLOCK (i);
841 rtx insn;
843 for (insn = bb->head; insn != NEXT_INSN (bb->end);
844 insn = NEXT_INSN (insn))
845 if (active_insn_p (insn))
847 sum += bb->frequency;
848 if (sum > limit)
849 return true;
853 return false;
856 /* Estimate basic blocks frequency by given branch probabilities. */
858 static void
859 estimate_bb_frequencies (loops)
860 struct loops *loops;
862 int i;
863 double freq_max = 0;
865 mark_dfs_back_edges ();
866 if (flag_branch_probabilities)
868 counts_to_freqs ();
869 return;
872 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
873 notes. */
874 for (i = 0; i < n_basic_blocks; i++)
876 rtx last_insn = BLOCK_END (i);
877 int probability;
878 edge fallthru, branch;
880 if (GET_CODE (last_insn) != JUMP_INSN || !any_condjump_p (last_insn)
881 /* Avoid handling of conditional jumps jumping to fallthru edge. */
882 || BASIC_BLOCK (i)->succ->succ_next == NULL)
884 /* We can predict only conditional jumps at the moment.
885 Expect each edge to be equally probable.
886 ?? In the future we want to make abnormal edges improbable. */
887 int nedges = 0;
888 edge e;
890 for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
892 nedges++;
893 if (e->probability != 0)
894 break;
896 if (!e)
897 for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
898 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
902 ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
904 /* Set up block info for each basic block. */
905 alloc_aux_for_blocks (sizeof (struct block_info_def));
906 alloc_aux_for_edges (sizeof (struct edge_info_def));
907 for (i = -2; i < n_basic_blocks; i++)
909 edge e;
910 basic_block bb;
912 if (i == -2)
913 bb = ENTRY_BLOCK_PTR;
914 else if (i == -1)
915 bb = EXIT_BLOCK_PTR;
916 else
917 bb = BASIC_BLOCK (i);
919 BLOCK_INFO (bb)->tovisit = 0;
920 for (e = bb->succ; e; e = e->succ_next)
921 EDGE_INFO (e)->back_edge_prob = ((double) e->probability
922 / REG_BR_PROB_BASE);
925 /* First compute probabilities locally for each loop from innermost
926 to outermost to examine probabilities for back edges. */
927 estimate_loops_at_level (loops->tree_root);
929 /* Now fake loop around whole function to finalize probabilities. */
930 for (i = 0; i < n_basic_blocks; i++)
931 BLOCK_INFO (BASIC_BLOCK (i))->tovisit = 1;
933 BLOCK_INFO (ENTRY_BLOCK_PTR)->tovisit = 1;
934 BLOCK_INFO (EXIT_BLOCK_PTR)->tovisit = 1;
935 propagate_freq (ENTRY_BLOCK_PTR);
937 for (i = 0; i < n_basic_blocks; i++)
938 if (BLOCK_INFO (BASIC_BLOCK (i))->frequency > freq_max)
939 freq_max = BLOCK_INFO (BASIC_BLOCK (i))->frequency;
941 for (i = -2; i < n_basic_blocks; i++)
943 basic_block bb;
945 if (i == -2)
946 bb = ENTRY_BLOCK_PTR;
947 else if (i == -1)
948 bb = EXIT_BLOCK_PTR;
949 else
950 bb = BASIC_BLOCK (i);
951 bb->frequency
952 = BLOCK_INFO (bb)->frequency * BB_FREQ_MAX / freq_max + 0.5;
955 free_aux_for_blocks ();
956 free_aux_for_edges ();