source: trunk/src/gcc/gcc/predict.c@ 2

Last change on this file since 2 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 28.0 KB
Line 
1/* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21/* References:
22
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. */
29
30
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"
48
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)
57
58static void combine_predictions_for_insn PARAMS ((rtx, basic_block));
59static void dump_prediction PARAMS ((enum br_predictor, int,
60 basic_block, int));
61static void estimate_loops_at_level PARAMS ((struct loop *loop));
62static void propagate_freq PARAMS ((basic_block));
63static void estimate_bb_frequencies PARAMS ((struct loops *));
64static void counts_to_freqs PARAMS ((void));
65
66/* Information we hold about each branch predictor.
67 Filled using information from predict.def. */
68
69struct predictor_info
70{
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;
75};
76
77/* Use given predictor without Dempster-Shaffer theory if it matches
78 using first_match heuristics. */
79#define PRED_FLAG_FIRST_MATCH 1
80
81/* Recompute hitrate in percent to our representation. */
82
83#define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
84
85#define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
86static const struct predictor_info predictor_info[]= {
87#include "predict.def"
88
89 /* Upper bound on predictors. */
90 {NULL, 0, 0}
91};
92#undef DEF_PREDICTOR
93
94void
95predict_insn (insn, predictor, probability)
96 rtx insn;
97 int probability;
98 enum br_predictor predictor;
99{
100 if (!any_condjump_p (insn))
101 abort ();
102
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));
109}
110
111/* Predict insn by given predictor. */
112
113void
114predict_insn_def (insn, predictor, taken)
115 rtx insn;
116 enum br_predictor predictor;
117 enum prediction taken;
118{
119 int probability = predictor_info[(int) predictor].hitrate;
120
121 if (taken != TAKEN)
122 probability = REG_BR_PROB_BASE - probability;
123
124 predict_insn (insn, predictor, probability);
125}
126
127/* Predict edge E with given probability if possible. */
128
129void
130predict_edge (e, predictor, probability)
131 edge e;
132 int probability;
133 enum br_predictor predictor;
134{
135 rtx last_insn;
136 last_insn = e->src->end;
137
138 /* We can store the branch prediction information only about
139 conditional jumps. */
140 if (!any_condjump_p (last_insn))
141 return;
142
143 /* We always store probability of branching. */
144 if (e->flags & EDGE_FALLTHRU)
145 probability = REG_BR_PROB_BASE - probability;
146
147 predict_insn (last_insn, predictor, probability);
148}
149
150/* Predict edge E by given predictor if possible. */
151
152void
153predict_edge_def (e, predictor, taken)
154 edge e;
155 enum br_predictor predictor;
156 enum prediction taken;
157{
158 int probability = predictor_info[(int) predictor].hitrate;
159
160 if (taken != TAKEN)
161 probability = REG_BR_PROB_BASE - probability;
162
163 predict_edge (e, predictor, probability);
164}
165
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. */
168
169void
170invert_br_probabilities (insn)
171 rtx insn;
172{
173 rtx note;
174
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)));
181}
182
183/* Dump information about the branch prediction to the output file. */
184
185static void
186dump_prediction (predictor, probability, bb, used)
187 enum br_predictor predictor;
188 int probability;
189 basic_block bb;
190 int used;
191{
192 edge e = bb->succ;
193
194 if (!rtl_dump_file)
195 return;
196
197 while (e->flags & EDGE_FALLTHRU)
198 e = e->succ_next;
199
200 fprintf (rtl_dump_file, " %s heuristics%s: %.1f%%",
201 predictor_info[predictor].name,
202 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
203
204 if (bb->count)
205 {
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);
211 }
212
213 fprintf (rtl_dump_file, "\n");
214}
215
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. */
218
219static void
220combine_predictions_for_insn (insn, bb)
221 rtx insn;
222 basic_block bb;
223{
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;
233
234 if (rtl_dump_file)
235 fprintf (rtl_dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
236 bb->index);
237
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)
243 {
244 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
245 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
246
247 found = true;
248 if (best_predictor > predictor)
249 best_probability = probability, best_predictor = predictor;
250
251 d = (combined_probability * probability
252 + (REG_BR_PROB_BASE - combined_probability)
253 * (REG_BR_PROB_BASE - probability));
254
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);
262 }
263
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. */
267
268 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
269 first_match = true;
270
271 if (!found)
272 dump_prediction (PRED_NO_PREDICTION, combined_probability, bb, true);
273 else
274 {
275 dump_prediction (PRED_DS_THEORY, combined_probability, bb, !first_match);
276 dump_prediction (PRED_FIRST_MATCH, best_probability, bb, first_match);
277 }
278
279 if (first_match)
280 combined_probability = best_probability;
281 dump_prediction (PRED_COMBINED, combined_probability, bb, true);
282
283 while (*pnote)
284 {
285 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
286 {
287 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
288 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
289
290 dump_prediction (predictor, probability, bb,
291 !first_match || best_predictor == predictor);
292 *pnote = XEXP (*pnote, 1);
293 }
294 else
295 pnote = &XEXP (*pnote, 1);
296 }
297
298 if (!prob_note)
299 {
300 REG_NOTES (insn)
301 = gen_rtx_EXPR_LIST (REG_BR_PROB,
302 GEN_INT (combined_probability), REG_NOTES (insn));
303
304 /* Save the prediction into CFG in case we are seeing non-degenerated
305 conditional jump. */
306 if (bb->succ->succ_next)
307 {
308 BRANCH_EDGE (bb)->probability = combined_probability;
309 FALLTHRU_EDGE (bb)->probability
310 = REG_BR_PROB_BASE - combined_probability;
311 }
312 }
313}
314
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). */
320
321void
322estimate_probability (loops_info)
323 struct loops *loops_info;
324{
325 sbitmap *dominators, *post_dominators;
326 int i;
327 int found_noreturn = 0;
328
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);
333
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++)
337 {
338 int j;
339 int exits;
340 struct loop *loop = &loops_info->array[i];
341
342 flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
343 exits = loop->num_exits;
344
345 for (j = loop->first->index; j <= loop->last->index; ++j)
346 if (TEST_BIT (loop->nodes, j))
347 {
348 int header_found = 0;
349 edge e;
350
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)
356 {
357 header_found = 1;
358 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
359 }
360
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);
372 }
373 }
374
375 /* Attempt to predict conditional jumps using a number of heuristics. */
376 for (i = 0; i < n_basic_blocks; i++)
377 {
378 basic_block bb = BASIC_BLOCK (i);
379 rtx last_insn = bb->end;
380 rtx cond, earliest;
381 edge e;
382
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)
387 {
388 int y;
389
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);
401 }
402
403 if (GET_CODE (last_insn) != JUMP_INSN || ! any_condjump_p (last_insn))
404 continue;
405
406 for (e = bb->succ; e; e = e->succ_next)
407 {
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);
414
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))
420 {
421 rtx insn;
422
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))
433 {
434 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
435 break;
436 }
437 }
438 }
439
440 cond = get_condition (last_insn, &earliest);
441 if (! cond)
442 continue;
443
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)))))
450 {
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);
455 }
456 else
457
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))
463 {
464 case CONST_INT:
465 /* Unconditional branch. */
466 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
467 cond == const0_rtx ? NOT_TAKEN : TAKEN);
468 break;
469
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))))
476 ;
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)
481 ;
482 else
483 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
484 break;
485
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))))
492 ;
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)
497 ;
498 else
499 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
500 break;
501
502 case ORDERED:
503 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
504 break;
505
506 case UNORDERED:
507 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
508 break;
509
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;
516
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;
523
524 default:
525 break;
526 }
527 }
528
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));
534
535 sbitmap_vector_free (post_dominators);
536 sbitmap_vector_free (dominators);
537
538 estimate_bb_frequencies (loops_info);
539}
540
541
542/* __builtin_expect dropped tokens into the insn stream describing expected
543 values of registers. Generate branch probabilities based off these
544 values. */
545
546void
547expected_value_to_br_prob ()
548{
549 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
550
551 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
552 {
553 switch (GET_CODE (insn))
554 {
555 case NOTE:
556 /* Look for expected value notes. */
557 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
558 {
559 ev = NOTE_EXPECTED_VALUE (insn);
560 ev_reg = XEXP (ev, 0);
561 delete_insn (insn);
562 }
563 continue;
564
565 case CODE_LABEL:
566 /* Never propagate across labels. */
567 ev = NULL_RTX;
568 continue;
569
570 case JUMP_INSN:
571 /* Look for simple conditional branches. If we haven't got an
572 expected value yet, no point going further. */
573 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX
574 || ! any_condjump_p (insn))
575 continue;
576 break;
577
578 default:
579 /* Look for insns that clobber the EV register. */
580 if (ev && reg_set_p (ev_reg, insn))
581 ev = NULL_RTX;
582 continue;
583 }
584
585 /* Collect the branch condition, hopefully relative to EV_REG. */
586 /* ??? At present we'll miss things like
587 (expected_value (eq r70 0))
588 (set r71 -1)
589 (set r80 (lt r70 r71))
590 (set pc (if_then_else (ne r80 0) ...))
591 as canonicalize_condition will render this to us as
592 (lt r70, r71)
593 Could use cselib to try and reduce this further. */
594 cond = XEXP (SET_SRC (pc_set (insn)), 0);
595 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg);
596 if (! cond || XEXP (cond, 0) != ev_reg
597 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
598 continue;
599
600 /* Substitute and simplify. Given that the expression we're
601 building involves two constants, we should wind up with either
602 true or false. */
603 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
604 XEXP (ev, 1), XEXP (cond, 1));
605 cond = simplify_rtx (cond);
606
607 /* Turn the condition into a scaled branch probability. */
608 if (cond != const_true_rtx && cond != const0_rtx)
609 abort ();
610 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
611 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
612 }
613}
614
615
616/* This is used to carry information about basic blocks. It is
617 attached to the AUX field of the standard CFG block. */
618
619typedef struct block_info_def
620{
621 /* Estimated frequency of execution of basic_block. */
622 volatile double frequency;
623
624 /* To keep queue of basic blocks to process. */
625 basic_block next;
626
627 /* True if block needs to be visited in prop_freqency. */
628 int tovisit:1;
629
630 /* Number of predecessors we need to visit first. */
631 int npredecessors;
632} *block_info;
633
634/* Similar information for edges. */
635typedef struct edge_info_def
636{
637 /* In case edge is an loopback edge, the probability edge will be reached
638 in case header is. Estimated number of iterations of the loop can be
639 then computed as 1 / (1 - back_edge_prob).
640
641 Volatile is needed to avoid differences in the optimized and unoptimized
642 builds on machines where FP registers are wider than double. */
643 volatile double back_edge_prob;
644 /* True if the edge is an loopback edge in the natural loop. */
645 int back_edge:1;
646} *edge_info;
647
648#define BLOCK_INFO(B) ((block_info) (B)->aux)
649#define EDGE_INFO(E) ((edge_info) (E)->aux)
650
651/* Helper function for estimate_bb_frequencies.
652 Propagate the frequencies for loops headed by HEAD. */
653
654static void
655propagate_freq (head)
656 basic_block head;
657{
658 basic_block bb = head;
659 basic_block last = bb;
660 edge e;
661 basic_block nextbb;
662 int n;
663
664 /* For each basic block we need to visit count number of his predecessors
665 we need to visit first. */
666 for (n = 0; n < n_basic_blocks; n++)
667 {
668 basic_block bb = BASIC_BLOCK (n);
669 if (BLOCK_INFO (bb)->tovisit)
670 {
671 int count = 0;
672
673 for (e = bb->pred; e; e = e->pred_next)
674 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
675 count++;
676 else if (BLOCK_INFO (e->src)->tovisit
677 && rtl_dump_file && !EDGE_INFO (e)->back_edge)
678 fprintf (rtl_dump_file,
679 "Irreducible region hit, ignoring edge to %i->%i\n",
680 e->src->index, bb->index);
681 BLOCK_INFO (bb)->npredecessors = count;
682 }
683 }
684
685 BLOCK_INFO (head)->frequency = 1;
686 for (; bb; bb = nextbb)
687 {
688 double cyclic_probability = 0, frequency = 0;
689
690 nextbb = BLOCK_INFO (bb)->next;
691 BLOCK_INFO (bb)->next = NULL;
692
693 /* Compute frequency of basic block. */
694 if (bb != head)
695 {
696#ifdef ENABLE_CHECKING
697 for (e = bb->pred; e; e = e->pred_next)
698 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
699 abort ();
700#endif
701
702 for (e = bb->pred; e; e = e->pred_next)
703 if (EDGE_INFO (e)->back_edge)
704 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
705 else if (!(e->flags & EDGE_DFS_BACK))
706 frequency += (e->probability
707 * BLOCK_INFO (e->src)->frequency /
708 REG_BR_PROB_BASE);
709
710 if (cyclic_probability > 1.0 - 1.0 / REG_BR_PROB_BASE)
711 cyclic_probability = 1.0 - 1.0 / REG_BR_PROB_BASE;
712
713 BLOCK_INFO (bb)->frequency = frequency / (1 - cyclic_probability);
714 }
715
716 BLOCK_INFO (bb)->tovisit = 0;
717
718 /* Compute back edge frequencies. */
719 for (e = bb->succ; e; e = e->succ_next)
720 if (e->dest == head)
721 EDGE_INFO (e)->back_edge_prob
722 = ((e->probability * BLOCK_INFO (bb)->frequency)
723 / REG_BR_PROB_BASE);
724
725 /* Propagate to successor blocks. */
726 for (e = bb->succ; e; e = e->succ_next)
727 if (!(e->flags & EDGE_DFS_BACK)
728 && BLOCK_INFO (e->dest)->npredecessors)
729 {
730 BLOCK_INFO (e->dest)->npredecessors--;
731 if (!BLOCK_INFO (e->dest)->npredecessors)
732 {
733 if (!nextbb)
734 nextbb = e->dest;
735 else
736 BLOCK_INFO (last)->next = e->dest;
737
738 last = e->dest;
739 }
740 }
741 }
742}
743
744/* Estimate probabilities of loopback edges in loops at same nest level. */
745
746static void
747estimate_loops_at_level (first_loop)
748 struct loop *first_loop;
749{
750 struct loop *l, *loop = first_loop;
751
752 for (loop = first_loop; loop; loop = loop->next)
753 {
754 int n;
755 edge e;
756
757 estimate_loops_at_level (loop->inner);
758
759 /* Find current loop back edge and mark it. */
760 for (e = loop->latch->succ; e->dest != loop->header; e = e->succ_next)
761 ;
762
763 EDGE_INFO (e)->back_edge = 1;
764
765 /* In case the loop header is shared, ensure that it is the last
766 one sharing the same header, so we avoid redundant work. */
767 if (loop->shared)
768 {
769 for (l = loop->next; l; l = l->next)
770 if (l->header == loop->header)
771 break;
772
773 if (l)
774 continue;
775 }
776
777 /* Now merge all nodes of all loops with given header as not visited. */
778 for (l = loop->shared ? first_loop : loop; l != loop->next; l = l->next)
779 if (loop->header == l->header)
780 EXECUTE_IF_SET_IN_SBITMAP (l->nodes, 0, n,
781 BLOCK_INFO (BASIC_BLOCK (n))->tovisit = 1
782 );
783
784 propagate_freq (loop->header);
785 }
786}
787
788/* Convert counts measured by profile driven feedback to frequencies. */
789
790static void
791counts_to_freqs ()
792{
793 HOST_WIDEST_INT count_max = 1;
794 int i;
795
796 for (i = 0; i < n_basic_blocks; i++)
797 count_max = MAX (BASIC_BLOCK (i)->count, count_max);
798
799 for (i = -2; i < n_basic_blocks; i++)
800 {
801 basic_block bb;
802
803 if (i == -2)
804 bb = ENTRY_BLOCK_PTR;
805 else if (i == -1)
806 bb = EXIT_BLOCK_PTR;
807 else
808 bb = BASIC_BLOCK (i);
809
810 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
811 }
812}
813
814/* Return true if function is likely to be expensive, so there is no point to
815 optimize performance of prologue, epilogue or do inlining at the expense
816 of code size growth. THRESHOLD is the limit of number of isntructions
817 function can execute at average to be still considered not expensive. */
818
819bool
820expensive_function_p (threshold)
821 int threshold;
822{
823 unsigned int sum = 0;
824 int i;
825 unsigned int limit;
826
827 /* We can not compute accurately for large thresholds due to scaled
828 frequencies. */
829 if (threshold > BB_FREQ_MAX)
830 abort ();
831
832 /* Frequencies are out of range. This either means that function contains
833 internal loop executing more than BB_FREQ_MAX times or profile feedback
834 is available and function has not been executed at all. */
835 if (ENTRY_BLOCK_PTR->frequency == 0)
836 return true;
837
838 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
839 limit = ENTRY_BLOCK_PTR->frequency * threshold;
840 for (i = 0; i < n_basic_blocks; i++)
841 {
842 basic_block bb = BASIC_BLOCK (i);
843 rtx insn;
844
845 for (insn = bb->head; insn != NEXT_INSN (bb->end);
846 insn = NEXT_INSN (insn))
847 if (active_insn_p (insn))
848 {
849 sum += bb->frequency;
850 if (sum > limit)
851 return true;
852 }
853 }
854
855 return false;
856}
857
858/* Estimate basic blocks frequency by given branch probabilities. */
859
860static void
861estimate_bb_frequencies (loops)
862 struct loops *loops;
863{
864 int i;
865 double freq_max = 0;
866
867 mark_dfs_back_edges ();
868 if (flag_branch_probabilities)
869 {
870 counts_to_freqs ();
871 return;
872 }
873
874 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
875 notes. */
876 for (i = 0; i < n_basic_blocks; i++)
877 {
878 rtx last_insn = BLOCK_END (i);
879 int probability;
880 edge fallthru, branch;
881
882 if (GET_CODE (last_insn) != JUMP_INSN || !any_condjump_p (last_insn)
883 /* Avoid handling of conditional jumps jumping to fallthru edge. */
884 || BASIC_BLOCK (i)->succ->succ_next == NULL)
885 {
886 /* We can predict only conditional jumps at the moment.
887 Expect each edge to be equally probable.
888 ?? In the future we want to make abnormal edges improbable. */
889 int nedges = 0;
890 edge e;
891
892 for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
893 {
894 nedges++;
895 if (e->probability != 0)
896 break;
897 }
898 if (!e)
899 for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
900 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
901 }
902 else
903 {
904 probability = INTVAL (XEXP (find_reg_note (last_insn,
905 REG_BR_PROB, 0), 0));
906 fallthru = BASIC_BLOCK (i)->succ;
907 if (!fallthru->flags & EDGE_FALLTHRU)
908 fallthru = fallthru->succ_next;
909 branch = BASIC_BLOCK (i)->succ;
910 if (branch->flags & EDGE_FALLTHRU)
911 branch = branch->succ_next;
912
913 branch->probability = probability;
914 fallthru->probability = REG_BR_PROB_BASE - probability;
915 }
916 }
917
918 ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
919
920 /* Set up block info for each basic block. */
921 alloc_aux_for_blocks (sizeof (struct block_info_def));
922 alloc_aux_for_edges (sizeof (struct edge_info_def));
923 for (i = -2; i < n_basic_blocks; i++)
924 {
925 edge e;
926 basic_block bb;
927
928 if (i == -2)
929 bb = ENTRY_BLOCK_PTR;
930 else if (i == -1)
931 bb = EXIT_BLOCK_PTR;
932 else
933 bb = BASIC_BLOCK (i);
934
935 BLOCK_INFO (bb)->tovisit = 0;
936 for (e = bb->succ; e; e = e->succ_next)
937 EDGE_INFO (e)->back_edge_prob = ((double) e->probability
938 / REG_BR_PROB_BASE);
939 }
940
941 /* First compute probabilities locally for each loop from innermost
942 to outermost to examine probabilities for back edges. */
943 estimate_loops_at_level (loops->tree_root);
944
945 /* Now fake loop around whole function to finalize probabilities. */
946 for (i = 0; i < n_basic_blocks; i++)
947 BLOCK_INFO (BASIC_BLOCK (i))->tovisit = 1;
948
949 BLOCK_INFO (ENTRY_BLOCK_PTR)->tovisit = 1;
950 BLOCK_INFO (EXIT_BLOCK_PTR)->tovisit = 1;
951 propagate_freq (ENTRY_BLOCK_PTR);
952
953 for (i = 0; i < n_basic_blocks; i++)
954 if (BLOCK_INFO (BASIC_BLOCK (i))->frequency > freq_max)
955 freq_max = BLOCK_INFO (BASIC_BLOCK (i))->frequency;
956
957 for (i = -2; i < n_basic_blocks; i++)
958 {
959 basic_block bb;
960 volatile double tmp;
961
962 if (i == -2)
963 bb = ENTRY_BLOCK_PTR;
964 else if (i == -1)
965 bb = EXIT_BLOCK_PTR;
966 else
967 bb = BASIC_BLOCK (i);
968
969 /* ??? Prevent rounding differences due to optimization on x86. */
970 tmp = BLOCK_INFO (bb)->frequency * BB_FREQ_MAX;
971 tmp /= freq_max;
972 tmp += 0.5;
973 bb->frequency = tmp;
974 }
975
976 free_aux_for_blocks ();
977 free_aux_for_edges ();
978}
Note: See TracBrowser for help on using the repository browser.